3 # Crunch script integration for running arvados-cwl-runner (importing
4 # arvados_cwl module) inside a crunch job.
6 # This gets the job record, transforms the script parameters into a valid CWL
7 # input object, then executes the CWL runner to run the underlying workflow or
8 # tool. When the workflow completes, record the output object in an output
9 # collection for this runner job.
13 import arvados.collection
15 from cwltool.process import shortname
22 from arvados.api import OrderedJsonModel
23 from cwltool.process import adjustFileObjs, adjustDirObjs
24 from cwltool.load_tool import load_tool
26 # Print package versions
27 logging.info(cwltool.main.versionstring())
29 api = arvados.api("v1")
32 job_order_object = arvados.current_job()['script_parameters']
34 pdh_path = re.compile(r'^[0-9a-f]{32}\+\d+(/.+)?$')
43 v["location"] = keeppath(v["location"])
45 job_order_object["cwl:tool"] = "file://%s/%s" % (os.environ['TASK_KEEPMOUNT'], job_order_object["cwl:tool"])
47 for k,v in job_order_object.items():
48 if isinstance(v, basestring) and arvados.util.keep_locator_pattern.match(v):
49 job_order_object[k] = {
51 "location": "keep:%s" % v
54 adjustFileObjs(job_order_object, keeppathObj)
55 adjustDirObjs(job_order_object, keeppathObj)
57 runner = arvados_cwl.ArvCwlRunner(api_client=arvados.api('v1', model=OrderedJsonModel()))
59 t = load_tool(job_order_object, runner.arv_make_tool)
61 args = argparse.Namespace()
62 args.project_uuid = arvados.current_job()["owner_uuid"]
63 args.enable_reuse = True
67 args.ignore_docker_for_reuse = False
68 args.basedir = os.getcwd()
69 args.cwl_runner_job={"uuid": arvados.current_job()["uuid"], "state": arvados.current_job()["state"]}
70 outputObj = runner.arv_executor(t, job_order_object, **vars(args))
74 path = fileobj["location"]
79 files[col].add("/".join(sp[1:]))
80 fileobj["location"] = path
82 adjustFileObjs(outputObj, capture)
84 final = arvados.collection.Collection()
86 for k,v in files.iteritems():
87 with arvados.collection.Collection(k) as c:
89 final.copy(f, f, c, True)
91 def makeRelative(fileobj):
92 fileobj["location"] = "/".join(fileobj["location"].split("/")[1:])
94 adjustFileObjs(outputObj, makeRelative)
96 with final.open("cwl.output.json", "w") as f:
97 json.dump(outputObj, f, indent=4)
99 api.job_tasks().update(uuid=arvados.current_task()['uuid'],
101 'output': final.save_new(create_collection_record=False),
105 except Exception as e:
106 logging.exception("Unhandled exception")
107 api.job_tasks().update(uuid=arvados.current_task()['uuid'],