11162: Add tests for http_to_keep.
[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         if "cwlVersion" not in self.metadata:
16             raise Exception("missing")
17         self.arvrunner = arvrunner
18         self.work_api = kwargs["work_api"]
19
20     def makeJobRunner(self, **kwargs):
21         if self.work_api == "containers":
22             return ArvadosContainer(self.arvrunner)
23         elif self.work_api == "jobs":
24             return ArvadosJob(self.arvrunner)
25
26     def makePathMapper(self, reffiles, stagedir, **kwargs):
27         # type: (List[Any], unicode, **Any) -> PathMapper
28         if self.work_api == "containers":
29             return ArvPathMapper(self.arvrunner, reffiles+kwargs.get("extra_reffiles", []), kwargs["basedir"],
30                                  "/keep/%s",
31                                  "/keep/%s/%s",
32                                  **kwargs)
33         elif self.work_api == "jobs":
34             return ArvPathMapper(self.arvrunner, reffiles, kwargs["basedir"],
35                                  "$(task.keep)/%s",
36                                  "$(task.keep)/%s/%s",
37                                  **kwargs)
38
39     def job(self, joborder, output_callback, **kwargs):
40
41         # Workaround for #13365
42         builderargs = kwargs.copy()
43         builderargs["toplevel"] = True
44         builderargs["tmp_outdir_prefix"] = ""
45         builder = self._init_job(joborder, **builderargs)
46         joborder = builder.job
47
48         if self.work_api == "containers":
49             dockerReq, is_req = self.get_requirement("DockerRequirement")
50             if dockerReq and dockerReq.get("dockerOutputDirectory"):
51                 kwargs["outdir"] = dockerReq.get("dockerOutputDirectory")
52                 kwargs["docker_outdir"] = dockerReq.get("dockerOutputDirectory")
53             else:
54                 kwargs["outdir"] = "/var/spool/cwl"
55                 kwargs["docker_outdir"] = "/var/spool/cwl"
56         elif self.work_api == "jobs":
57             kwargs["outdir"] = "$(task.outdir)"
58             kwargs["docker_outdir"] = "$(task.outdir)"
59             kwargs["tmpdir"] = "$(task.tmpdir)"
60             kwargs["docker_tmpdir"] = "$(task.tmpdir)"
61         return super(ArvadosCommandTool, self).job(joborder, output_callback, **kwargs)