diff --git a/oargen.py b/oargen.py index 5d501f995312f0da3646777ed432d1a27602d71b..368f46d0d201684e8ebb89c788eccd3190ee7e6a 100755 --- a/oargen.py +++ b/oargen.py @@ -5,16 +5,15 @@ from __future__ import print_function from __future__ import unicode_literals import argparse -# import logging import subprocess def argparser(): parser = argparse.ArgumentParser() - parser.add_argument('command', nargs='?', + # parser.add_argument('command', nargs='?', + # help="Command to use for the job (in passive mode)") + parser.add_argument('command', nargs=argparse.REMAINDER, help="Command to use for the job (in passive mode)") - parser.add_argument('argument', nargs=argparse.REMAINDER, - help="Arguments of the command (in passive mode)") parser.add_argument('-n', '--name', help="Name to give to the job") parser.add_argument('-b', '--besteffort', action="store_true", @@ -33,65 +32,83 @@ def argparser(): help="Enable checkpoint signals with the given delay (in seconds)") parser.add_argument('-r', '--run', action="store_true", help="Run the command") - # parser.add_argument('-l', '--logger', default='INFO', - # choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], - # help="Logging level: DEBUG, INFO (default), WARNING, ERROR") + parser.add_argument('-a', '--anterior', + help="Anterior job id that must be terminated to start this new one") args = parser.parse_args() - # numeric_level = getattr(logging, args.logger.upper(), None) - # if not isinstance(numeric_level, int): - # raise ValueError("Invalid log level: {}".format(args.logger)) - # logging.basicConfig(level=numeric_level) - return args -def main(): - args = argparser() - - command = ["oarsub"] - command.append("-p") +def prepare_oarsub(gpu, host, core, time, + command=None, argument=None, + interactive=False, + name=None, besteffort=False, + checkpoint=None, anterior=None, + command_is_string=False): + oar_cmd = ["oarsub"] + oar_cmd.append("-p") properties = "" - if args.gpu: + if gpu: properties += "(gpu IS NOT NULL)" else: properties += "(gpu IS NULL)" - if args.host is not None: - properties += " AND host LIKE '{}'".format(args.host) + if host is not None: + properties += " AND host LIKE '{}'".format(host) properties += "" - command.append(properties) + oar_cmd.append(properties) - command.append("-l") - time = args.time.split(':') + oar_cmd.append("-l") + time = time.split(':') hour = time[0] minutes = time[1] if len(time) >= 2 else "00" seconds = time[2] if len(time) >= 3 else "00" - ressources = "core={},walltime={}:{}:{}".format(args.core, hour, minutes, seconds) - command.append(ressources) + ressources = "core={},walltime={}:{}:{}".format(core, hour, minutes, seconds) + oar_cmd.append(ressources) + + if name is not None: + oar_cmd.append("-n") + oar_cmd.append(name) + oar_cmd.append("-O") + oar_cmd.append("{}.%jobid%.stdout".format(name)) + oar_cmd.append("-E") + oar_cmd.append("{}.%jobid%.stderr".format(name)) - if args.name is not None: - command.append("-n") - command.append(args.name) - command.append("-O") - command.append("{}.%jobid%.stdout".format(args.name)) - command.append("-E") - command.append("{}.%jobid%.stderr".format(args.name)) + if besteffort: + oar_cmd.extend(["-t", "besteffort", "-t", "idempotent"]) - if args.besteffort: - command.extend(["-t", "besteffort", "-t", "idempotent"]) + if checkpoint is not None: + oar_cmd.extend(["--checkpoint", checkpoint]) - if args.checkpoint is not None: - command.extend(["--checkpoint", args.checkpoint]) + if anterior is not None: + oar_cmd.extend(["-a", anterior]) - if args.interactive: - command.append('-I') + if interactive: + oar_cmd.append('-I') else: - job_command = [args.command] + args.argument - command.append(" ".join(job_command)) + job_command = command if command_is_string else " ".join(command) + oar_cmd.append(" ".join(job_command)) + +def run_oarsub(command, print_cmd=False, fake_run=False, return_output=False): print(subprocess.list2cmdline(command)) - if args.run: + if fake_run: + return None + if not return_output: subprocess.call(command) + return None + return subprocess.check_output(command) + + +def main(): + args = argparser() + + oar_command = prepare_oarsub(args.gpu, args.host, args.core, args.time, + command=args.command, argument=args.argument, + interactive=args.interactive, name=args.name, + besteffort=args.besteffort, checkpoint=args.checkpoint, + anterior=args.anterior) + + run_oarsub(oar_command, print_cmd=True, fake_run=not args.run) if __name__ == '__main__':