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
23 from arvados.api import OrderedJsonModel
24 from cwltool.process import shortname, adjustFileObjs, adjustDirObjs, getListing, normalizeFilesDirs
25 from cwltool.load_tool import load_tool
27 # Print package versions
28 logging.info(cwltool.main.versionstring())
30 api = arvados.api("v1")
33 job_order_object = arvados.current_job()['script_parameters']
35 pdh_path = re.compile(r'^[0-9a-f]{32}\+\d+(/.+)?$')
44 v["location"] = keeppath(v["location"])
46 job_order_object["cwl:tool"] = "file://%s/%s" % (os.environ['TASK_KEEPMOUNT'], job_order_object["cwl:tool"])
48 for k,v in job_order_object.items():
49 if isinstance(v, basestring) and arvados.util.keep_locator_pattern.match(v):
50 job_order_object[k] = {
52 "location": "keep:%s" % v
55 adjustFileObjs(job_order_object, keeppathObj)
56 adjustDirObjs(job_order_object, keeppathObj)
57 normalizeFilesDirs(job_order_object)
58 adjustDirObjs(job_order_object, functools.partial(getListing, arvados_cwl.fsaccess.CollectionFsAccess("", api_client=api)))
61 if "arv:output_name" in job_order_object:
62 output_name = job_order_object["arv:output_name"]
63 del job_order_object["arv:output_name"]
65 runner = arvados_cwl.ArvCwlRunner(api_client=arvados.api('v1', model=OrderedJsonModel()),
66 output_name=output_name)
68 t = load_tool(job_order_object, runner.arv_make_tool)
70 args = argparse.Namespace()
71 args.project_uuid = arvados.current_job()["owner_uuid"]
72 args.enable_reuse = True
76 args.ignore_docker_for_reuse = False
77 args.basedir = os.getcwd()
78 args.cwl_runner_job={"uuid": arvados.current_job()["uuid"], "state": arvados.current_job()["state"]}
79 outputObj = runner.arv_executor(t, job_order_object, **vars(args))
81 if runner.final_output_collection:
82 outputCollection = runner.final_output_collection.portable_data_hash()
84 outputCollection = None
86 api.job_tasks().update(uuid=arvados.current_task()['uuid'],
88 'output': outputCollection,
92 except Exception as e:
93 logging.exception("Unhandled exception")
94 api.job_tasks().update(uuid=arvados.current_task()['uuid'],