view xena_restore.py @ 30:77f5d8cbac77

add functionality to check the version of the current running xena server on the galaxy system
author Jing Zhu <jzhu@soe.ucsc.edu>
date Mon, 20 Jul 2015 13:31:33 -0700
parents 8bb037f88ed2
children
line wrap: on
line source

#!/usr/bin/env python

"""
  xena_restore.py: delete a dataset from Xena

  Back up the Xena data to a user-specified external directory.
"""

import argparse
import os
import re
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("backupDir", type=str)
    parser.add_argument("outfile", type=str)
    args = parser.parse_args()

    outFp = open(args.outfile, "w")
    xenaFileDir = xena.fileDir()

    for thisFile in os.listdir(args.backupDir):
        # The files in the xena directory should be pairs of 
        # data and metadata files, with the metadata having the same
        # name as the data file plus the .json suffix.  When you find
        # a json file in the backup directory, then copy the json and data
        # file into the xena directory, starting with the json file, and 
        # execute the xena load command.
        if re.search(".json$", thisFile):
            jsonFile = thisFile
            dataFile = re.sub(".json$", "", jsonFile)
            assert(os.path.exists(args.backupDir + "/" + dataFile))
            try:
                shutil.copy(args.backupDir + "/" + jsonFile, xenaFileDir)
            except:
                writeException(outFp,
                               msg="Error: cannot restore %s from %s" \
                                   % (jsonFile, args.backupDir))
                outFp.close()
                sys.exit(-1)
            try:
                shutil.copy(args.backupDir + "/" + dataFile, xenaFileDir)
            except:
                writeException(outFp,
                               msg="Error: cannot restore %s from %s" \
                                   % (dataFile, args.backupDir))
                outFp.close()
                sys.exit(-1)
            # Now set up the xena load command and try to execute it.
            xenaLoadCmd = "java -jar %s -l --force %s/%s -p %s" % (xena.jarPath(),
                                                                   xenaFileDir,
                                                                   dataFile, 
                                                                   xena.port())
            try:
                subprocess.call(xenaLoadCmd, shell=True)
            except:
                writeException(outFp,
                               msg="Could not reload %s into Xena" % dataFile)
                outFp.close()
                sys.exit(-1)
    # At this point, the restore should've been successful
    outFp.write("Restore complete\n")
    outFp.close()
    sys.exit(0)


if __name__ == "__main__":
    main()