Skip to content
Snippets Groups Projects
Select Git revision
  • b44989bc6003fb8c7cf433bb1193de48703b4807
  • master default protected
  • debugging
3 results

oargen.py

Blame
  • user avatar
    Jeremy Auguste authored
    b44989bc
    History
    oargen.py 4.06 KiB
    #!/usr/bin/env python
    # coding: utf-8
    
    from __future__ import print_function
    from __future__ import unicode_literals
    
    import argparse
    import subprocess
    
    
    def argparser():
        parser = argparse.ArgumentParser()
        # 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('-n', '--name',
                            help="Name to give to the job")
        parser.add_argument('-b', '--besteffort', action="store_true",
                            help="Launch job in besteffort mode")
        parser.add_argument('-t', '--time', default="10",
                            help="Estimated maximum duration of the job (format: h[:m[:s]])")
        parser.add_argument('-g', '--gpu', action="store_true",
                            help="If True, reserves only cores with GPUs")
        parser.add_argument('-c', '--core', default=1, type=int,
                            help="Number of cores to reserve")
        parser.add_argument('-H', '--host',
                            help="Name of the host (SQL LIKE syntax accepted)")
        parser.add_argument('-i', '--interactive', action="store_true",
                            help="Launch job in interactive mode")
        parser.add_argument('-C', '--checkpoint', type=int, metavar="SECONDS",
                            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('-a', '--anterior',
                            help="Anterior job id that must be terminated to start this new one")
        args = parser.parse_args()
    
        return args
    
    
    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 gpu:
            properties += "(gpu IS NOT NULL)"
        else:
            properties += "(gpu IS NULL)"
        if host is not None:
            properties += " AND host LIKE '{}'".format(host)
        properties += ""
        oar_cmd.append(properties)
    
        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(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 besteffort:
            oar_cmd.extend(["-t", "besteffort", "-t", "idempotent"])
    
        if checkpoint is not None:
            oar_cmd.extend(["--checkpoint", checkpoint])
    
        if anterior is not None:
            oar_cmd.extend(["-a", anterior])
    
        if interactive:
            oar_cmd.append('-I')
        else:
            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 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__':
        main()