comparison env/lib/python3.7/site-packages/planemo/cwl/run.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 """Module defines a planemo abstraction around running cwltool.
2
3 cwltool is an executable Python script and library mostly maintained by
4 Peter Amstutz and serves the reference implementation for the CWL.
5 It can be found at https://github.com/common-workflow-language/cwltool,
6 """
7 import json
8 import tempfile
9
10 from galaxy.tool_util.cwl.cwltool_deps import (
11 ensure_cwltool_available,
12 main,
13 )
14
15 from planemo.deps import ensure_dependency_resolvers_conf_configured
16 from planemo.io import error, real_io
17 from planemo.runnable import (
18 ErrorRunResponse,
19 SuccessfulRunResponse,
20 )
21
22 JSON_PARSE_ERROR_MESSAGE = ("Failed to parse JSON from cwltool output [%s] "
23 "in file [%s]. cwltool logs [%s].")
24
25
26 class CwlToolRunResponse(SuccessfulRunResponse):
27 """Describe the resut of a cwltool invocation."""
28
29 def __init__(self, log, outputs=None):
30 self._log = log
31 self._outputs = outputs
32
33 @property
34 def log(self):
35 return self._log
36
37 @property
38 def job_info(self):
39 return None
40
41 @property
42 def outputs_dict(self):
43 return self._outputs
44
45
46 def run_cwltool(ctx, path, job_path, **kwds):
47 """Translate planemo kwds to cwltool kwds and run cwltool main function."""
48 ensure_cwltool_available()
49
50 args = []
51 if ctx.verbose:
52 args.append("--verbose")
53 output_directory = kwds.get("output_directory", None)
54 if output_directory:
55 args.append("--outdir")
56 args.append(output_directory)
57 if kwds.get("no_container", False):
58 args.append("--no-container")
59 ensure_dependency_resolvers_conf_configured(ctx, kwds)
60 args.append("--beta-dependency-resolvers-configuration")
61 args.append(kwds["dependency_resolvers_config_file"])
62 if kwds.get("mulled_containers"):
63 args.append("--beta-use-biocontainers")
64
65 if kwds.get("non_strict_cwl", False):
66 args.append("--non-strict")
67
68 args.extend([path, job_path])
69 ctx.vlog("Calling cwltool with arguments %s" % args)
70 with tempfile.NamedTemporaryFile("w") as tmp_stdout, \
71 tempfile.NamedTemporaryFile("w") as tmp_stderr:
72 # cwltool passes sys.stderr to subprocess.Popen - ensure it has
73 # and actual fileno.
74 with real_io():
75 ret_code = main.main(
76 args,
77 stdout=tmp_stdout,
78 stderr=tmp_stderr,
79 )
80 tmp_stdout.flush()
81 tmp_stderr.flush()
82 with open(tmp_stderr.name, "r") as stderr_f:
83 log = stderr_f.read()
84 ctx.vlog("cwltool log output [%s]" % log)
85 with open(tmp_stdout.name, "r") as stdout_f:
86 try:
87 result = json.load(stdout_f)
88 except ValueError:
89 message = JSON_PARSE_ERROR_MESSAGE % (
90 open(tmp_stdout.name, "r").read(),
91 tmp_stdout.name,
92 log
93 )
94 error(message)
95 raise Exception(message)
96
97 if ret_code != 0:
98 return ErrorRunResponse("Error running cwltool", log=log)
99 outputs = result
100 return CwlToolRunResponse(
101 log,
102 outputs=outputs,
103 )
104
105
106 __all__ = (
107 "run_cwltool",
108 )