|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import json |
| 4 | +import getopt |
| 5 | +from pysqa.queueadapter import QueueAdapter |
| 6 | + |
| 7 | + |
| 8 | +def command_line(argv): |
| 9 | + """ |
| 10 | + Parse the command line arguments. |
| 11 | +
|
| 12 | + Args: |
| 13 | + argv: Command line arguments |
| 14 | +
|
| 15 | + """ |
| 16 | + directory = "~/.queues" |
| 17 | + queue = None, |
| 18 | + job_name = None, |
| 19 | + working_directory = None, |
| 20 | + cores = None, |
| 21 | + memory_max = None, |
| 22 | + run_time_max = None, |
| 23 | + command = None, |
| 24 | + job_id = None |
| 25 | + try: |
| 26 | + opts, args = getopt.getopt( |
| 27 | + argv, "f:pq:j:w:n:m:t:c:ri:dslh", ["config_directory=", "submit", "queue=", "job_name=", |
| 28 | + "working_directory=", "cores=", "memory=", "run_time=", |
| 29 | + "command=", "reservation", "id", "delete", "status", "list", "help"] |
| 30 | + ) |
| 31 | + except getopt.GetoptError: |
| 32 | + print("cmd.py help") |
| 33 | + sys.exit() |
| 34 | + else: |
| 35 | + mode_submit = False |
| 36 | + mode_delete = False |
| 37 | + mode_reservation = False |
| 38 | + mode_status = False |
| 39 | + mode_list = False |
| 40 | + for opt, arg in opts: |
| 41 | + if opt in ("-f", "--config_directory"): |
| 42 | + directory = arg |
| 43 | + elif opt in ("-p", "--submit"): |
| 44 | + mode_submit = True |
| 45 | + elif opt in ("-q", "--queue"): |
| 46 | + queue = arg |
| 47 | + elif opt in ("-j", "--job_name"): |
| 48 | + job_name = arg |
| 49 | + elif opt in ("-w", "--working_directory"): |
| 50 | + working_directory = arg |
| 51 | + elif opt in ("-n", "--cores"): |
| 52 | + cores = int(arg) |
| 53 | + elif opt in ("-m", "--memory"): |
| 54 | + memory_max = arg |
| 55 | + elif opt in ("-t", "--run_time"): |
| 56 | + run_time_max = arg |
| 57 | + elif opt in ("-c", "--command"): |
| 58 | + command = arg |
| 59 | + elif opt in ("-r", "--reservation"): |
| 60 | + mode_reservation = arg |
| 61 | + elif opt in ("-i", "--id"): |
| 62 | + job_id = int(arg) |
| 63 | + elif opt in ("-d", "--delete"): |
| 64 | + mode_delete = True |
| 65 | + elif opt in ("-d", "--status"): |
| 66 | + mode_status = True |
| 67 | + elif opt in ("-l", "--list"): |
| 68 | + mode_list = True |
| 69 | + elif opt in ("-h", "--help"): |
| 70 | + print("cmd.py help ... coming soon.") |
| 71 | + sys.exit() |
| 72 | + if mode_submit or mode_delete or mode_reservation or mode_status: |
| 73 | + qa = QueueAdapter(directory=directory) |
| 74 | + if mode_submit: |
| 75 | + print(qa.submit_job( |
| 76 | + queue=queue, |
| 77 | + job_name=job_name, |
| 78 | + working_directory=working_directory, |
| 79 | + cores=cores, |
| 80 | + memory_max=memory_max, |
| 81 | + run_time_max=run_time_max, |
| 82 | + command=command |
| 83 | + )) |
| 84 | + elif mode_delete: |
| 85 | + print(qa.delete_job(process_id=job_id)) |
| 86 | + elif mode_reservation: |
| 87 | + print(qa.enable_reservation(process_id=job_id)) |
| 88 | + elif mode_status: |
| 89 | + print(json.dumps(qa.get_queue_status().to_dict(orient='list'))) |
| 90 | + elif mode_list: |
| 91 | + working_directory = os.path.abspath(os.path.expanduser(working_directory)) |
| 92 | + remote_dirs, remote_files = [], [] |
| 93 | + for p, folder, files in os.walk(working_directory): |
| 94 | + remote_dirs.append(p) |
| 95 | + remote_files += [os.path.join(p, f) for f in files] |
| 96 | + print(json.dumps({'dirs': remote_dirs, 'files': remote_files})) |
| 97 | + sys.exit() |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + command_line(sys.argv[1:]) |
0 commit comments