|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +from os.path import join, dirname, exists |
| 5 | +import argparse |
| 6 | + |
| 7 | +sys.path.append(join(dirname(__file__), '.', 'o2dpg_workflow_utils')) |
| 8 | + |
| 9 | +from o2dpg_workflow_utils import createTask, read_workflow, dump_workflow, check_workflow, update_workflow_resource_requirements, make_workflow_filename |
| 10 | + |
| 11 | +def extend(args): |
| 12 | + """extend a workflow by another one |
| 13 | +
|
| 14 | + The overall configuration from the original workflow |
| 15 | + is kept |
| 16 | + """ |
| 17 | + # load workflows |
| 18 | + workflow_orig = read_workflow(args.orig_wf) |
| 19 | + workflow_extend = read_workflow(args.extend_wf) |
| 20 | + |
| 21 | + # extend |
| 22 | + workflow_orig.extend(workflow_extend) |
| 23 | + |
| 24 | + # dump in new file |
| 25 | + filename = args.output if args.output else args.orig_wf |
| 26 | + dump_workflow(workflow_orig, filename) |
| 27 | + |
| 28 | + |
| 29 | +def create(args): |
| 30 | + """create an empty workflow skeleton or add task skeletons to existing workflow |
| 31 | + """ |
| 32 | + filename = make_workflow_filename(args.file) |
| 33 | + if not args.add_task and exists(filename): |
| 34 | + print(f"Workflow file {filename} does already exist. Delete it and try again") |
| 35 | + return |
| 36 | + if not args.add_task or not exists(filename): |
| 37 | + # just create an empty workflow |
| 38 | + dump_workflow([], filename) |
| 39 | + if args.add_task: |
| 40 | + # add another task skeleton with name |
| 41 | + workflow = read_workflow(filename) |
| 42 | + for name in args.add_task: |
| 43 | + workflow.append(createTask(name=name)) |
| 44 | + dump_workflow(workflow, filename) |
| 45 | + |
| 46 | + |
| 47 | +def find_task(workflow, task_name): |
| 48 | + for s in workflow: |
| 49 | + if s["name"] == task_name: |
| 50 | + return s |
| 51 | + return None |
| 52 | + |
| 53 | + |
| 54 | +def modify(args): |
| 55 | + |
| 56 | + if args.task: |
| 57 | + workflow = read_workflow(args.file) |
| 58 | + # try to find the requested task |
| 59 | + task = find_task(workflow, args.task) |
| 60 | + if not task: |
| 61 | + print(f"Task with name {args.task} does not exist") |
| 62 | + exit(1) |
| 63 | + for attr in ("name", "needs", "timeframe", "cwd", "labels", "cmd"): |
| 64 | + if hasattr(args, attr) and getattr(args, attr) is not None: |
| 65 | + task[attr] = getattr(args, attr) |
| 66 | + for attr in ("cpu", "relative_cpu", "mem"): |
| 67 | + if hasattr(args, attr) and getattr(args, attr) is not None: |
| 68 | + task["resources"][attr] = getattr(args, attr) |
| 69 | + |
| 70 | + dump_workflow(workflow, args.file) |
| 71 | + |
| 72 | + |
| 73 | +def nworkers(args): |
| 74 | + workflow = read_workflow(args.file) |
| 75 | + update_workflow_resource_requirements(workflow, args.jobs) |
| 76 | + dump_workflow(workflow, args.file) |
| 77 | + |
| 78 | + |
| 79 | +def inspect(args): |
| 80 | + """Inspecting a workflow |
| 81 | +
|
| 82 | + This is at the moment more show-casing what one could do |
| 83 | + """ |
| 84 | + workflow = read_workflow(args.file) |
| 85 | + if args.check: |
| 86 | + check_workflow(workflow) |
| 87 | + if args.summary: |
| 88 | + summary_workflow(workflow) |
| 89 | + if args.task: |
| 90 | + task = find_task(workflow, args.task) |
| 91 | + if not task: |
| 92 | + print(f"Task with name {args.task}") |
| 93 | + exit(1) |
| 94 | + print("Here are the requested task information") |
| 95 | + print(task) |
| 96 | + |
| 97 | + |
| 98 | +def main(): |
| 99 | + |
| 100 | + parser = argparse.ArgumentParser(description='Create an ALICE (Run3) MC simulation workflow') |
| 101 | + |
| 102 | + sub_parsers = parser.add_subparsers(dest="command") |
| 103 | + |
| 104 | + create_parser = sub_parsers.add_parser("create", help="manage a workflow") |
| 105 | + create_parser.set_defaults(func=create) |
| 106 | + create_parser.add_argument("file", help="workflow file to be created or modifed") |
| 107 | + create_parser.add_argument("--add-task", dest="add_task", nargs="+", help="add named tasks to workflow file") |
| 108 | + |
| 109 | + # Append to (sim) workflow |
| 110 | + merge_parser = sub_parsers.add_parser("merge", help="append stages") |
| 111 | + merge_parser.set_defaults(func=extend) |
| 112 | + merge_parser.add_argument("orig_wf", help="original workflow") |
| 113 | + merge_parser.add_argument("extend_wf", help="workflow JSON to be merged to orig") |
| 114 | + merge_parser.add_argument("--output", "-o", help="extended workflow output file name", default="workflow_merged.json") |
| 115 | + |
| 116 | + nworker_parser = sub_parsers.add_parser("nworkers", help="update number of workers") |
| 117 | + nworker_parser.set_defaults(func=nworkers) |
| 118 | + nworker_parser.add_argument("file", help="the workflow file to be modified") |
| 119 | + nworker_parser.add_argument("jobs", type=int, help="number of workers to recompute relative cpu") |
| 120 | + |
| 121 | + modify_parser = sub_parsers.add_parser("modify", help="modify a task") |
| 122 | + modify_parser.set_defaults(func=modify) |
| 123 | + modify_parser.add_argument("file", help="the workflow file to be modified") |
| 124 | + modify_parser.add_argument("task", help="name of task to be modified") |
| 125 | + # not allowing for changing the name at the moment as this also goes into the log-file name |
| 126 | + #modify_parser.add_argument("--name", help="new name of this task") |
| 127 | + modify_parser.add_argument("--needs", nargs="+", help="required tasks to be executed before this one") |
| 128 | + modify_parser.add_argument("--timeframe", type=int, help="timeframe") |
| 129 | + modify_parser.add_argument("--cwd", help="current working directory of this task") |
| 130 | + modify_parser.add_argument("--labels", nargs="+", help="attached labels") |
| 131 | + modify_parser.add_argument("--cpu", type=int, help="absolute number of workers to be used for this task") |
| 132 | + modify_parser.add_argument("--relative-cpu", dest="relative_cpu", type=float, help="realtive fraction of maximum number of available workers") |
| 133 | + modify_parser.add_argument("--mem", type=int, help="estimated memory") |
| 134 | + modify_parser.add_argument("--cmd", help="command line to be executed") |
| 135 | + |
| 136 | + inspect_parser = sub_parsers.add_parser("inspect", help="inspect a workflow") |
| 137 | + inspect_parser.set_defaults(func=inspect) |
| 138 | + inspect_parser.add_argument("file", help="Workflow file to inspect") |
| 139 | + inspect_parser.add_argument("--summary", action="store_true", help="print summary of workflow") |
| 140 | + inspect_parser.add_argument("--check", action="store_true", help="Check sanity of workflow") |
| 141 | + |
| 142 | + args = parser.parse_args() |
| 143 | + |
| 144 | + if not hasattr(args, "func"): |
| 145 | + parser.parse_args(["--help"]) |
| 146 | + exit(0) |
| 147 | + |
| 148 | + args.func(args) |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + # provide this also stand-alone if called directly from the interpreter |
| 153 | + main() |
0 commit comments