diff xena_restore.py @ 0:8bb037f88ed2

Uploaded
author melissacline
date Tue, 13 Jan 2015 23:37:23 -0500
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xena_restore.py	Tue Jan 13 23:37:23 2015 -0500
@@ -0,0 +1,84 @@
+#!/usr/bin/env python
+
+"""
+  xena_restore.py: delete a dataset from Xena
+
+  Back up the Xena data to a user-specified external directory.
+"""
+
+import argparse
+import os
+import re
+import shutil
+import subprocess
+import sys
+import traceback
+import xena_utils as xena
+
+def writeException(outFp, msg = None):
+    exc_type, exc_value, exc_traceback = sys.exc_info()
+    lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
+    allLines = ''.join('!! ' + line for line in lines)
+    if msg is None:
+        outFp.write("Unsuccessful: error %s\n" % allLines)
+    else:
+        outFp.write("%s\n%s" % (msg, allLines))
+    
+    
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument("backupDir", type=str)
+    parser.add_argument("outfile", type=str)
+    args = parser.parse_args()
+
+    outFp = open(args.outfile, "w")
+    xenaFileDir = xena.fileDir()
+
+    for thisFile in os.listdir(args.backupDir):
+        # The files in the xena directory should be pairs of 
+        # data and metadata files, with the metadata having the same
+        # name as the data file plus the .json suffix.  When you find
+        # a json file in the backup directory, then copy the json and data
+        # file into the xena directory, starting with the json file, and 
+        # execute the xena load command.
+        if re.search(".json$", thisFile):
+            jsonFile = thisFile
+            dataFile = re.sub(".json$", "", jsonFile)
+            assert(os.path.exists(args.backupDir + "/" + dataFile))
+            try:
+                shutil.copy(args.backupDir + "/" + jsonFile, xenaFileDir)
+            except:
+                writeException(outFp,
+                               msg="Error: cannot restore %s from %s" \
+                                   % (jsonFile, args.backupDir))
+                outFp.close()
+                sys.exit(-1)
+            try:
+                shutil.copy(args.backupDir + "/" + dataFile, xenaFileDir)
+            except:
+                writeException(outFp,
+                               msg="Error: cannot restore %s from %s" \
+                                   % (dataFile, args.backupDir))
+                outFp.close()
+                sys.exit(-1)
+            # Now set up the xena load command and try to execute it.
+            xenaLoadCmd = "java -jar %s -l --force %s/%s -p %s" % (xena.jarPath(),
+                                                                   xenaFileDir,
+                                                                   dataFile, 
+                                                                   xena.port())
+            try:
+                subprocess.call(xenaLoadCmd, shell=True)
+            except:
+                writeException(outFp,
+                               msg="Could not reload %s into Xena" % dataFile)
+                outFp.close()
+                sys.exit(-1)
+    # At this point, the restore should've been successful
+    outFp.write("Restore complete\n")
+    outFp.close()
+    sys.exit(0)
+
+
+if __name__ == "__main__":
+    main()