Skip to content

Commit 214501a

Browse files
author
Peter Amstutz
committed
Reverse map files in cwl.output.json
1 parent f78ee23 commit 214501a

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

cwltool/draft2tool.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ def job(self, joborder, input_basedir, output_callback, **kwargs):
5151

5252
yield j
5353

54+
def revmap_file(builder, outdir, f):
55+
"""Remap a file back to original path. For Docker, this is outside the container.
56+
57+
Uses either files in the pathmapper or remaps internal output directories
58+
to the external directory.
59+
"""
60+
revmap_f = builder.pathmapper.reversemap(f)
61+
if revmap_f:
62+
return revmap_f[1]
63+
elif f.startswith(builder.outdir):
64+
return os.path.join(outdir, f[len(builder.outdir)+1:])
65+
else:
66+
raise WorkflowException("Output file path %s must be within designated output directory or an input file pass through." % f)
67+
5468

5569
class CommandLineTool(Process):
5670
def __init__(self, toolpath_object, **kwargs):
@@ -183,13 +197,15 @@ def job(self, joborder, input_basedir, output_callback, **kwargs):
183197

184198
def collect_output_ports(self, ports, builder, outdir):
185199
try:
200+
ret = {}
186201
custom_output = os.path.join(outdir, "cwl.output.json")
187202
if builder.fs_access.exists(custom_output):
188-
outputdoc = yaml.load(custom_output)
189-
validate.validate_ex(self.names.get_name("outputs_record_schema", ""), outputdoc)
190-
return outputdoc
191-
192-
ret = {}
203+
with builder.fs_access.open(custom_output, "r") as f:
204+
ret = yaml.load(f)
205+
_logger.debug("Raw output from %s: %s", custom_output, json.dumps(ret, indent=4))
206+
adjustFiles(ret, functools.partial(revmap_file, builder, outdir))
207+
validate.validate_ex(self.names.get_name("outputs_record_schema", ""), ret)
208+
return ret
193209

194210
for port in ports:
195211
fragment = shortname(port["id"])
@@ -200,20 +216,6 @@ def collect_output_ports(self, ports, builder, outdir):
200216
raise WorkflowException("Error validating output record, " + str(e) + "\n in " + json.dumps(ret, indent=4))
201217

202218
def collect_output(self, schema, builder, outdir):
203-
def revmap_file(f):
204-
"""Remap a file back to original path. For Docker, this is outside the container.
205-
206-
Uses either files in the pathmapper or remaps internal output directories
207-
to the external directory.
208-
"""
209-
revmap_f = builder.pathmapper.reversemap(f)
210-
if revmap_f:
211-
return revmap_f[-1]
212-
elif f.startswith(builder.outdir):
213-
return f.replace(builder.outdir, outdir)
214-
else:
215-
return f
216-
217219
r = None
218220
if "outputBinding" in schema:
219221
binding = schema["outputBinding"]
@@ -276,9 +278,10 @@ def revmap_file(f):
276278
else:
277279
r = r[0]
278280

281+
# Ensure files point to local references outside of the run environment
282+
adjustFiles(r, functools.partial(revmap_file, builder, outdir))
283+
279284
if "secondaryFiles" in schema:
280-
# remap secondaryFiles since we check if they exist
281-
adjustFiles(r, revmap_file)
282285
for primary in aslist(r):
283286
if isinstance(primary, dict):
284287
primary["secondaryFiles"] = []
@@ -301,6 +304,5 @@ def revmap_file(f):
301304
r = {}
302305
for f in schema["type"]["fields"]:
303306
r[shortname(f["name"])] = self.collect_output(f, builder, outdir)
304-
# Ensure files point to local references outside of the run environment
305-
adjustFiles(r, revmap_file)
307+
306308
return r

cwltool/pathmapper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def reversemap(self, target):
3232
for k,v in self._pathmap.items():
3333
if v[1] == target:
3434
return (k, v[0])
35+
return None
3536

3637
class DockerPathMapper(PathMapper):
3738
def __init__(self, referenced_files, basedir):

0 commit comments

Comments
 (0)