comparison xena_restore.py @ 0:8bb037f88ed2

Uploaded
author melissacline
date Tue, 13 Jan 2015 23:37:23 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8bb037f88ed2
1 #!/usr/bin/env python
2
3 """
4 xena_restore.py: delete a dataset from Xena
5
6 Back up the Xena data to a user-specified external directory.
7 """
8
9 import argparse
10 import os
11 import re
12 import shutil
13 import subprocess
14 import sys
15 import traceback
16 import xena_utils as xena
17
18 def writeException(outFp, msg = None):
19 exc_type, exc_value, exc_traceback = sys.exc_info()
20 lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
21 allLines = ''.join('!! ' + line for line in lines)
22 if msg is None:
23 outFp.write("Unsuccessful: error %s\n" % allLines)
24 else:
25 outFp.write("%s\n%s" % (msg, allLines))
26
27
28
29 def main():
30 parser = argparse.ArgumentParser()
31 parser.add_argument("backupDir", type=str)
32 parser.add_argument("outfile", type=str)
33 args = parser.parse_args()
34
35 outFp = open(args.outfile, "w")
36 xenaFileDir = xena.fileDir()
37
38 for thisFile in os.listdir(args.backupDir):
39 # The files in the xena directory should be pairs of
40 # data and metadata files, with the metadata having the same
41 # name as the data file plus the .json suffix. When you find
42 # a json file in the backup directory, then copy the json and data
43 # file into the xena directory, starting with the json file, and
44 # execute the xena load command.
45 if re.search(".json$", thisFile):
46 jsonFile = thisFile
47 dataFile = re.sub(".json$", "", jsonFile)
48 assert(os.path.exists(args.backupDir + "/" + dataFile))
49 try:
50 shutil.copy(args.backupDir + "/" + jsonFile, xenaFileDir)
51 except:
52 writeException(outFp,
53 msg="Error: cannot restore %s from %s" \
54 % (jsonFile, args.backupDir))
55 outFp.close()
56 sys.exit(-1)
57 try:
58 shutil.copy(args.backupDir + "/" + dataFile, xenaFileDir)
59 except:
60 writeException(outFp,
61 msg="Error: cannot restore %s from %s" \
62 % (dataFile, args.backupDir))
63 outFp.close()
64 sys.exit(-1)
65 # Now set up the xena load command and try to execute it.
66 xenaLoadCmd = "java -jar %s -l --force %s/%s -p %s" % (xena.jarPath(),
67 xenaFileDir,
68 dataFile,
69 xena.port())
70 try:
71 subprocess.call(xenaLoadCmd, shell=True)
72 except:
73 writeException(outFp,
74 msg="Could not reload %s into Xena" % dataFile)
75 outFp.close()
76 sys.exit(-1)
77 # At this point, the restore should've been successful
78 outFp.write("Restore complete\n")
79 outFp.close()
80 sys.exit(0)
81
82
83 if __name__ == "__main__":
84 main()