view xena_delete.py @ 50:3167c1a26101

fix
author jingchunzhu
date Wed, 12 Aug 2015 16:38:12 -0700
parents 98b498545a52
children
line wrap: on
line source

#!/usr/bin/env python

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

  Given the name of a Xena dataset, delete it from the local Xena database.
"""

import argparse
import json
import os
import re
import shutil
import subprocess
import sys
import traceback
import xena_utils as xena

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("datasetName", type=str)
    parser.add_argument("outfile", type=str)
    args = parser.parse_args()

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

    # Set up the xena delete comamnd and try to execute it.  If an exception
    # is generated, output a traceback and exit with nonzero status.  If
    # no exception was generated, indicate a successful import and exit
    # with zero status.
    xenaDeleteCmd = "java -jar %s --delete %s/%s -p %s" % (xena.jarPath(),
                                                           xenaFileDir,
                                                           args.datasetName,
                                                           xena.port())
    try:
        subprocess.call(xenaDeleteCmd, shell=True)
    except:
        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)
        fp2.write("Unsuccessful: error %s\n" % allLines)
        fp2.close()
        sys.exit(-1)
    else:
        fp2.write( "Dataset %s deleted\n" % args.datasetName)
        dataPathname = xenaFileDir + "/" + args.datasetName
        if os.path.exists(dataPathname):
            os.unlink(dataPathname)
        metadataPathname = dataPathname + ".json"
        if os.path.exists(metadataPathname):
            os.unlink(metadataPathname)
        fp2.close()
        sys.exit(0)


if __name__ == "__main__":
    main()