2009-02-28 23:28:56 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
import os
|
2009-09-15 07:30:13 +08:00
|
|
|
import sys
|
2009-05-27 06:23:24 +08:00
|
|
|
import subprocess
|
|
|
|
|
import re
|
|
|
|
|
|
2009-02-28 23:28:56 +08:00
|
|
|
#justPrintCommands = False
|
|
|
|
|
|
2009-09-15 07:30:13 +08:00
|
|
|
def cmd(cmd_str_from_user, farm_queue=False, output_head=None, just_print_commands=False, outputFile = None, waitID = None, jobName = None, die_on_fail = False):
|
|
|
|
|
"""if farm_queue != False, submits to queue, other
|
|
|
|
|
die_on_fail_msg: if != None, die on command failure (non-zero return) and show die_on_fail_msg"""
|
2009-02-28 23:28:56 +08:00
|
|
|
|
|
|
|
|
if farm_queue:
|
2009-04-16 05:41:30 +08:00
|
|
|
if outputFile <> None:
|
|
|
|
|
farm_stdout = outputFile
|
2009-05-20 20:54:41 +08:00
|
|
|
elif output_head <> None:
|
2009-04-16 05:41:30 +08:00
|
|
|
farm_stdout = output_head+".stdout"
|
2009-05-20 20:54:41 +08:00
|
|
|
else:
|
2009-07-28 21:18:58 +08:00
|
|
|
#farm_stdout = None
|
|
|
|
|
farm_stdout = "%J.lsf.output"
|
2009-05-20 20:54:41 +08:00
|
|
|
|
2009-05-22 06:26:19 +08:00
|
|
|
cmd_str = "bsub -q "+farm_queue
|
2009-05-20 20:54:41 +08:00
|
|
|
if farm_stdout <> None:
|
|
|
|
|
cmd_str += " -o " + farm_stdout
|
2009-05-27 06:23:24 +08:00
|
|
|
|
|
|
|
|
if waitID <> None:
|
2009-07-07 22:15:39 +08:00
|
|
|
cmd_str += " -w \"ended(%s)\"" % (str(waitID))
|
2009-05-27 06:23:24 +08:00
|
|
|
|
2009-09-11 21:08:53 +08:00
|
|
|
if jobName <> None:
|
|
|
|
|
cmd_str += " -J %s" % (jobName)
|
|
|
|
|
|
2010-01-06 21:15:40 +08:00
|
|
|
cmd_str += " '"+cmd_str_from_user + "'"
|
2009-05-20 20:54:41 +08:00
|
|
|
|
2009-02-28 23:28:56 +08:00
|
|
|
print ">>> Farming via "+cmd_str
|
|
|
|
|
else:
|
2009-05-22 06:26:19 +08:00
|
|
|
cmd_str = cmd_str_from_user
|
2009-02-28 23:28:56 +08:00
|
|
|
print ">>> Executing "+cmd_str
|
|
|
|
|
|
|
|
|
|
if just_print_commands or (globals().has_key("justPrintCommands") and globals().justPrintCommands):
|
|
|
|
|
return -1
|
2009-06-20 01:45:47 +08:00
|
|
|
elif farm_queue:
|
|
|
|
|
result = subprocess.Popen([cmd_str, ""], shell=True, stdout=subprocess.PIPE).communicate()[0]
|
|
|
|
|
p = re.compile('Job <(\d+)> is submitted to queue')
|
|
|
|
|
jobid = p.match(result).group(1)
|
|
|
|
|
return jobid
|
2009-02-28 23:28:56 +08:00
|
|
|
else:
|
|
|
|
|
# Actually execute the command if we're not just in debugging output mode
|
|
|
|
|
status = os.system(cmd_str)
|
|
|
|
|
if not farm_queue:
|
|
|
|
|
print "<<< Exit code:", status,"\n"
|
2009-09-15 07:30:13 +08:00
|
|
|
if die_on_fail != None and status != 0:
|
|
|
|
|
print "### Failed with exit code "+str(status)+" while executing command "+cmd_str_from_user
|
|
|
|
|
sys.exit()
|
2009-06-03 04:37:19 +08:00
|
|
|
return int(status)
|