Merge branch '12061-flaky-tests'
[arvados.git] / sdk / cwl / arvados_cwl / arvtool.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 from cwltool.command_line_tool import CommandLineTool
6 from .arvjob import ArvadosJob
7 from .arvcontainer import ArvadosContainer
8 from .pathmapper import ArvPathMapper
9
10 class ArvadosCommandTool(CommandLineTool):
11     """Wrap cwltool CommandLineTool to override selected methods."""
12
13     def __init__(self, arvrunner, toolpath_object, **kwargs):
14         super(ArvadosCommandTool, self).__init__(toolpath_object, **kwargs)
15         self.arvrunner = arvrunner
16         self.work_api = kwargs["work_api"]
17
18     def makeJobRunner(self, **kwargs):
19         if self.work_api == "containers":
20             return ArvadosContainer(self.arvrunner)
21         elif self.work_api == "jobs":
22             return ArvadosJob(self.arvrunner)
23
24     def makePathMapper(self, reffiles, stagedir, **kwargs):
25         # type: (List[Any], unicode, **Any) -> PathMapper
26         if self.work_api == "containers":
27             return ArvPathMapper(self.arvrunner, reffiles+kwargs.get("extra_reffiles", []), kwargs["basedir"],
28                                  "/keep/%s",
29                                  "/keep/%s/%s",
30                                  **kwargs)
31         elif self.work_api == "jobs":
32             return ArvPathMapper(self.arvrunner, reffiles, kwargs["basedir"],
33                                  "$(task.keep)/%s",
34                                  "$(task.keep)/%s/%s",
35                                  **kwargs)
36
37     def job(self, joborder, output_callback, **kwargs):
38
39         # Workaround for #13365
40         builderargs = kwargs.copy()
41         builderargs["toplevel"] = True
42         builderargs["tmp_outdir_prefix"] = ""
43         builder = self._init_job(joborder, **builderargs)
44         joborder = builder.job
45
46         if self.work_api == "containers":
47             dockerReq, is_req = self.get_requirement("DockerRequirement")
48             if dockerReq and dockerReq.get("dockerOutputDirectory"):
49                 kwargs["outdir"] = dockerReq.get("dockerOutputDirectory")
50                 kwargs["docker_outdir"] = dockerReq.get("dockerOutputDirectory")
51             else:
52                 kwargs["outdir"] = "/var/spool/cwl"
53                 kwargs["docker_outdir"] = "/var/spool/cwl"
54         elif self.work_api == "jobs":
55             kwargs["outdir"] = "$(task.outdir)"
56             kwargs["docker_outdir"] = "$(task.outdir)"
57             kwargs["tmpdir"] = "$(task.tmpdir)"
58             kwargs["docker_tmpdir"] = "$(task.tmpdir)"
59         return super(ArvadosCommandTool, self).job(joborder, output_callback, **kwargs)