annotate xena_backup.py @ 6:a1cad00d45ff

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