Commit d917dc1b authored by David Trudgian's avatar David Trudgian
Browse files

local and srun execution

parent e805fca1
......@@ -22,9 +22,11 @@ Options:
from docopt import docopt
import logging
import colorlog
import os
from runner import __version__
from runner import param
from runner import executors
def main():
......@@ -46,6 +48,7 @@ def main():
if arguments['<paramfile>']:
param_file =arguments['<paramfile>']
print "param_runner - version %s" % __version__
print "-------------------------------"
......@@ -56,10 +59,20 @@ def main():
try:
pfile = param.ParamFile(arguments['<paramfile>'])
pfile.load()
p = param.ParamFile(param_file)
p.load()
if arguments['run']:
runner = executors.LocalExecutor(p.commands, os.path.dirname((os.path.abspath(param_file))))
runner.run()
if arguments['srun']:
runner = executors.SrunExecutor(p.commands, os.path.dirname(
(os.path.abspath(param_file))))
runner.run()
except Exception as e:
raise
logger.error("[%s] %s" % ( e.__class__.__name__, e ))
print "\n"
......
from subprocess import check_output
from subprocess import call
import os
import sys
import datetime
import logging
logger = logging.getLogger(__name__)
def LocalExecutor(object):
class BaseExecutor(object):
commands = []
logdir =""
......@@ -13,16 +13,88 @@ def LocalExecutor(object):
def __init__(self, commands, cwd):
logger.info("Initializing local executor")
self.commands = commands
logdir = os.path.join(cwd, "param_runner_%s" % datetime.datetime.strftime('%Y%m%d-%H%M') )
logdir = os.path.join(cwd, "param_runner_%s" % datetime.datetime.now().strftime('%Y%m%d-%H%M%s') )
try:
os.mkdir(logdir)
logger.info(" - Trace file and output will be at: %s" % os.path.relpath(logdir))
except IOError:
raise IOError("Could not create log directory: %s" % logdir)
self.logdir = logdir
def run(self):
trace_path = os.path.join(self.logdir, 'trace.txt')
trace_file = open(trace_path, 'w', buffering = False)
trace_file.write("Index\t")
for arg in self.commands[0]:
trace_file.write("%s\t" % arg)
trace_file.write("Return Code\tDuration\tStart\tEnd\n")
cmd_idx = 0
for cmd_idx, cmd in enumerate(self.commands):
stdout_path = os.path.join(self.logdir, "%d.out" % cmd_idx)
stderr_path = os.path.join(self.logdir, "%d.err" % cmd_idx)
stderr_file = open(stderr_path, 'w')
stdout_file = open(stdout_path, 'w')
start = datetime.datetime.now()
ret = self.run_cmd(cmd, stderr_file, stdout_file)
end = datetime.datetime.now()
duration = end - start
stderr_file.close()
stdout_file.close()
summary_line = "%d\t" % cmd_idx
for arg in cmd.values():
summary_line += "%s\t" % str(arg['value'])
summary_line += "%d\t%s\t%s\t%s\n" % (
ret,
duration,
start,
end
)
trace_file.write(summary_line)
trace_file.close()
class LocalExecutor(BaseExecutor):
def run_cmd(self, cmd, stderr_file, stdout_file):
ret = call(cmd['__command']['value'], shell=True, stderr=stderr_file,
stdout=stdout_file)
return ret
class SrunExecutor(BaseExecutor):
def run_cmd(self, cmd, stderr_file, stdout_file):
srun_cmd = "srun -n1 '%s'" % cmd['__command']['value']
ret = call(srun_cmd, shell=True, stderr=stderr_file,
stdout=stdout_file)
return ret
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment