comparison xena_backup.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_backup.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 shutil
12 import subprocess
13 import sys
14 import traceback
15 import xena_utils as xena
16
17 def writeException(outFp, msg = None):
18 exc_type, exc_value, exc_traceback = sys.exc_info()
19 lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
20 allLines = ''.join('!! ' + line for line in lines)
21 if msg is None:
22 outFp.write("Unsuccessful: error %s\n" % allLines)
23 else:
24 outFp.write("%s\n%s" % (msg, allLines))
25
26
27
28 def main():
29 parser = argparse.ArgumentParser()
30 parser.add_argument("pathname", type=str)
31 parser.add_argument("outfile", type=str)
32 args = parser.parse_args()
33
34 outFp = open(args.outfile, "w")
35 xenaFileDir = xena.fileDir()
36
37 if not os.path.exists(args.pathname):
38 try:
39 os.mkdir(args.pathname)
40 except:
41 writeException(outFp,
42 msg="Error: cannot create %s" % args.pathname)
43 outFp.close()
44 sys.exit(-1)
45 for thisFile in os.listdir(xenaFileDir):
46 try:
47 shutil.copy(xenaFileDir + "/" + thisFile, args.pathname)
48 except:
49 writeException(outFp,
50 msg="Error: cannot back up files from %s to %s" \
51 % (xena.fileDir(), args.pathname))
52 outFp.close()
53 sys.exit(-1)
54 outFp.write("Backup complete\n")
55 outFp.close()
56 sys.exit(0)
57
58
59 if __name__ == "__main__":
60 main()