diff xena_backup.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_backup.py	Tue Jan 13 23:37:23 2015 -0500
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+
+"""
+  xena_backup.py: delete a dataset from Xena
+
+  Back up the Xena data to a user-specified external directory.
+"""
+
+import argparse
+import os
+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("pathname", type=str)
+    parser.add_argument("outfile", type=str)
+    args = parser.parse_args()
+
+    outFp = open(args.outfile, "w")
+    xenaFileDir = xena.fileDir()
+
+    if not os.path.exists(args.pathname):
+        try:
+            os.mkdir(args.pathname)
+        except:
+            writeException(outFp, 
+                           msg="Error: cannot create %s" % args.pathname)
+            outFp.close()
+            sys.exit(-1)
+    for thisFile in os.listdir(xenaFileDir):
+        try:
+            shutil.copy(xenaFileDir + "/" + thisFile, args.pathname)
+        except:
+            writeException(outFp,
+                           msg="Error: cannot back up files from %s to %s" \
+                               % (xena.fileDir(), args.pathname))
+            outFp.close()
+            sys.exit(-1)
+    outFp.write("Backup complete\n")
+    outFp.close()
+    sys.exit(0)
+
+
+if __name__ == "__main__":
+    main()