view xena_backup.py @ 13:be73a33f333b

Tweaking of ucsc data pages tool
author melissacline
date Thu, 19 Mar 2015 14:08:11 -0700
parents 8bb037f88ed2
children
line wrap: on
line source

#!/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()