view mzmatch_wrapper.py @ 0:201a15633354 draft default tip

Initial commit.
author galaxyp
date Fri, 10 May 2013 17:28:02 -0400
parents
children
line wrap: on
line source

from os.path import dirname, join, abspath
import sys
from optparse import OptionParser
from ConfigParser import SafeConfigParser
import subprocess

DEBUG = False


def main():
    (options, args) = _parse_args()
    for executable, config_path in zip(options.executables, options.configs):
        command_handler = COMMAND_HANDLERS.get(executable, _run_mzmatch)
        command_handler(executable, config_path)


def _run_shell(executable, config_path):
    command = open(config_path, "r").read().strip()
    if DEBUG:
        print "Running shell command %s" % command
    _exec(command)


def _run_mzmatch(executable, config_path):
    command_prefix = "java -cp %s" % _jar_path()
    java_class = "mzmatch.ipeak.%s" % executable
    args = open(config_path, "r").read().strip()
    _exec("%s %s %s" % (command_prefix, java_class, args))


def _jar_path():
    py_path = __file__
    jar_path = join(dirname(py_path), "mzmatch_2.0.jar")
    return jar_path

COMMAND_HANDLERS = {
    "__SHELL__": _run_shell,
}


def _exec(command):
    proc = subprocess.Popen(args=command, shell=True)
    return_code = proc.wait()
    if return_code != 0:
        print "Error executing command [%s], return code is %d" % (command, return_code)
        sys.exit(return_code)


def _parse_args():
    parser = OptionParser()
    parser.add_option("-e", "--executable", dest="executables", default=[], action="append")
    parser.add_option("-c", "--config", dest="configs", default=[], action="append")
    return parser.parse_args()


def _load_options(config_path):
    config_parser = SafeConfigParser()
    config_parser.optionxform = str
    config_parser.read(config_path)
    return config_parser

if __name__ == "__main__":
    main()