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

Initial commit.
author galaxyp
date Fri, 10 May 2013 17:28:02 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
1 from os.path import dirname, join, abspath
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
2 import sys
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
3 from optparse import OptionParser
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
4 from ConfigParser import SafeConfigParser
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
5 import subprocess
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
6
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
7 DEBUG = False
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
8
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
9
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
10 def main():
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
11 (options, args) = _parse_args()
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
12 for executable, config_path in zip(options.executables, options.configs):
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
13 command_handler = COMMAND_HANDLERS.get(executable, _run_mzmatch)
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
14 command_handler(executable, config_path)
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
15
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
16
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
17 def _run_shell(executable, config_path):
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
18 command = open(config_path, "r").read().strip()
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
19 if DEBUG:
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
20 print "Running shell command %s" % command
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
21 _exec(command)
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
22
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
23
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
24 def _run_mzmatch(executable, config_path):
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
25 command_prefix = "java -cp %s" % _jar_path()
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
26 java_class = "mzmatch.ipeak.%s" % executable
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
27 args = open(config_path, "r").read().strip()
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
28 _exec("%s %s %s" % (command_prefix, java_class, args))
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
29
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
30
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
31 def _jar_path():
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
32 py_path = __file__
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
33 jar_path = join(dirname(py_path), "mzmatch_2.0.jar")
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
34 return jar_path
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
35
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
36 COMMAND_HANDLERS = {
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
37 "__SHELL__": _run_shell,
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
38 }
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
39
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
40
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
41 def _exec(command):
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
42 proc = subprocess.Popen(args=command, shell=True)
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
43 return_code = proc.wait()
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
44 if return_code != 0:
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
45 print "Error executing command [%s], return code is %d" % (command, return_code)
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
46 sys.exit(return_code)
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
47
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
48
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
49 def _parse_args():
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
50 parser = OptionParser()
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
51 parser.add_option("-e", "--executable", dest="executables", default=[], action="append")
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
52 parser.add_option("-c", "--config", dest="configs", default=[], action="append")
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
53 return parser.parse_args()
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
54
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
55
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
56 def _load_options(config_path):
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
57 config_parser = SafeConfigParser()
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
58 config_parser.optionxform = str
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
59 config_parser.read(config_path)
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
60 return config_parser
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
61
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
62 if __name__ == "__main__":
201a15633354 Initial commit.
galaxyp
parents:
diff changeset
63 main()