#!/usr/bin/env python2 """ param_runner - Run an application multiple times on the BioHPC cluster, exploring a parameter space and summarizing results. Usage: param_runner check [--verbose] param_runner submit [--verbose] param_runner srun [--verbose] param_runner run [--verbose] param_runner -h | --help | --version Options: --verbose Show debug messages """ import logging import os import sys import subprocess import colorlog from docopt import docopt from runner import __version__ from runner import executors from runner import param def main(): arguments = docopt(__doc__, version='param_runner %s' % __version__) handler = colorlog.StreamHandler() handler.setFormatter(colorlog.ColoredFormatter( '%(log_color)s%(levelname)-8s %(message)s')) logger = colorlog.getLogger() logger.addHandler(handler) if arguments['--verbose']: logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO) if arguments['']: param_file = arguments[''] print "param_runner - version %s" % __version__ print "-------------------------------" print "Parameter exploration runner for the BioHPC Cluster" print "D. C. Trudgian, UT Southwestern BioHPC" print "biohpc-help@utsouthwestern.edu" print try: p = param.ParamFile(param_file) p.load() if arguments['run']: runner = executors.LocalExecutor(p.commands, os.path.dirname( (os.path.abspath(param_file))), p) runner.run() if arguments['srun']: runner = executors.SrunExecutor(p.commands, os.path.dirname( (os.path.abspath(param_file))), p) runner.run() if arguments['submit']: # We submit using sbatch python_exe = sys.executable this_script = os.path.abspath(__file__) logger.debug("Python is %s" % python_exe) logger.debug("Script location is %s" % this_script) nodes = str(p.vals['nodes']) partition = str(p.vals['partition']) time_limit = str(p.vals['time_limit']) sbatch_cmd = [ 'sbatch', '-N', nodes, '-p', partition, '-t', time_limit, '-oparam_runner_%j.out' ] batch_script = "#!/bin/bash\n" batch_script += python_exe batch_script += ' -u ' batch_script += this_script batch_script += ' srun ' batch_script += os.path.abspath(param_file) batch_script += ' 2>&1\n' logger.debug(sbatch_cmd) sbatch_proc = subprocess.Popen( sbatch_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = sbatch_proc.communicate(batch_script) if stdout: logger.info(stdout) if stderr: logger.error(stderr) logger.info("Done.") except Exception as e: logger.error("[%s] %s" % (e.__class__.__name__, e)) print "\n" if __name__ == '__main__': main()