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 adjustFiles(job_order_object, keeppath)
40 runner = arvados_cwl.ArvCwlRunner(api_client=arvados.api('v1', model=OrderedJsonModel()))
42 t = cwltool.main.load_tool(job_order_object, False, True, runner.arvMakeTool, True)
44 args = argparse.Namespace()
45 args.project_uuid = arvados.current_job()["owner_uuid"]
46 args.enable_reuse = True
50 args.ignore_docker_for_reuse = False
51 outputObj = runner.arvExecutor(t, job_order_object, "", args, cwl_runner_job={"uuid": arvados.current_job()["uuid"], "state": arvados.current_job()["state"]})
59 files[col].add("/".join(sp[1:]))
62 adjustFiles(outputObj, capture)
64 final = arvados.collection.Collection()
66 for k,v in files.iteritems():
67 with arvados.collection.Collection(k) as c:
69 final.copy(f, f, c, True)
71 def makeRelative(path):
72 return "/".join(path.split("/")[1:])
74 adjustFiles(outputObj, makeRelative)
76 with final.open("cwl.output.json", "w") as f:
77 json.dump(outputObj, f, indent=4)
79 api.job_tasks().update(uuid=arvados.current_task()['uuid'],
81 'output': final.save_new(create_collection_record=False),
85 except Exception as e:
86 logging.exception("Unhandled exception")
87 api.job_tasks().update(uuid=arvados.current_task()['uuid'],