Merge branch '12248-crunch2-reversemap' closes #12248
[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.draft2tool 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["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         if self.work_api == "containers":
39             kwargs["outdir"] = "/var/spool/cwl"
40             kwargs["docker_outdir"] = "/var/spool/cwl"
41         elif self.work_api == "jobs":
42             kwargs["outdir"] = "$(task.outdir)"
43             kwargs["docker_outdir"] = "$(task.outdir)"
44             kwargs["tmpdir"] = "$(task.tmpdir)"
45             kwargs["docker_tmpdir"] = "$(task.tmpdir)"
46         return super(ArvadosCommandTool, self).job(joborder, output_callback, **kwargs)