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
21 from arvados.api import OrderedJsonModel
22 from cwltool.process import adjustFiles
24 # Print package versions
25 logging.info(cwltool.main.versionstring())
27 api = arvados.api("v1")
30 job_order_object = arvados.current_job()['script_parameters']
33 if arvados.util.keep_locator_pattern.match(v):
34 return "file://%s/%s" % (os.environ['TASK_KEEPMOUNT'], v)
36 job_order_object["cwl:tool"] = keeppath(job_order_object["cwl:tool"])
38 for k,v in job_order_object.items():
39 if arvados.util.keep_locator_pattern.match(v):
40 job_order_object[k] = {
45 adjustFiles(job_order_object, keeppath)
47 runner = arvados_cwl.ArvCwlRunner(api_client=arvados.api('v1', model=OrderedJsonModel()))
49 t = cwltool.main.load_tool(job_order_object, False, True, runner.arvMakeTool, True)
51 args = argparse.Namespace()
52 args.project_uuid = arvados.current_job()["owner_uuid"]
53 args.enable_reuse = True
57 args.ignore_docker_for_reuse = False
58 outputObj = runner.arvExecutor(t, job_order_object, "", args, cwl_runner_job={"uuid": arvados.current_job()["uuid"], "state": arvados.current_job()["state"]})
66 files[col].add("/".join(sp[1:]))
69 adjustFiles(outputObj, capture)
71 final = arvados.collection.Collection()
73 for k,v in files.iteritems():
74 with arvados.collection.Collection(k) as c:
76 final.copy(f, f, c, True)
78 def makeRelative(path):
79 return "/".join(path.split("/")[1:])
81 adjustFiles(outputObj, makeRelative)
83 with final.open("cwl.output.json", "w") as f:
84 json.dump(outputObj, f, indent=4)
86 api.job_tasks().update(uuid=arvados.current_task()['uuid'],
88 'output': final.save_new(create_collection_record=False),
92 except Exception as e:
93 logging.exception("Unhandled exception")
94 api.job_tasks().update(uuid=arvados.current_task()['uuid'],