annotate xena_restore.py @ 46:cdba6c63dce5

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