Skip to content

Commit c4ed587

Browse files
author
Anton Khodak
committed
Generate the same document for a previously packed workflow
1 parent 6d986bc commit c4ed587

File tree

5 files changed

+77
-6
lines changed

5 files changed

+77
-6
lines changed

cwltool/pack.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import absolute_import
22
import copy
3+
import re
34
from typing import Any, Callable, Dict, List, Set, Text, Union, cast
45

56
from schema_salad.ref_resolver import Loader
@@ -111,12 +112,16 @@ def loadref(b, u):
111112

112113
mainpath, _ = urllib.parse.urldefrag(uri)
113114

114-
def rewrite_id(r, mainuri):
115-
# type: (Text, Text) -> None
115+
def rewrite_id(r, mainuri, document_packed=False):
116+
# type: (Text, Text, bool) -> None
116117
if r == mainuri:
117118
rewrite[r] = "#main"
118119
elif r.startswith(mainuri) and r[len(mainuri)] in ("#", "/"):
119-
pass
120+
if document_packed:
121+
# rewrite tool and mainuri ids in a packed document
122+
tool_id = re.search("#[^/]*$", r)
123+
if tool_id:
124+
rewrite[r] = tool_id.group()
120125
else:
121126
path, frag = urllib.parse.urldefrag(r)
122127
if path == mainpath:
@@ -127,9 +132,10 @@ def rewrite_id(r, mainuri):
127132

128133
sortedids = sorted(ids)
129134

135+
is_document_packed = all(id.startswith(uri) for id in sortedids)
130136
for r in sortedids:
131137
if r in document_loader.idx:
132-
rewrite_id(r, uri)
138+
rewrite_id(r, uri, is_document_packed)
133139

134140
packed = {"$graph": [], "cwlVersion": metadata["cwlVersion"]
135141
} # type: Dict[Text, Any]
@@ -138,15 +144,15 @@ def rewrite_id(r, mainuri):
138144
for r in sorted(runs):
139145
dcr, metadata = document_loader.resolve_ref(r)
140146
if not isinstance(dcr, dict):
141-
continue
147+
dcr = dcr[0]
142148
for doc in (dcr, metadata):
143149
if "$schemas" in doc:
144150
for s in doc["$schemas"]:
145151
schemas.add(s)
146152
if dcr.get("class") not in ("Workflow", "CommandLineTool", "ExpressionTool"):
147153
continue
148154
dc = cast(Dict[Text, Any], copy.deepcopy(dcr))
149-
v = rewrite[r]
155+
v = rewrite.get(r, r + "#main")
150156
dc["id"] = v
151157
for n in ("name", "cwlVersion", "$namespaces", "$schemas"):
152158
if n in dc:

tests/test_pack.py

+7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ def test_pack_idempotence_tool(self):
6868
# Testing single tool
6969
self._pack_idempotently("tests/wf/hello_single_tool.cwl")
7070

71+
def test_pack_idempotence_workflow(self):
72+
"""Test to ensure that pack produces exactly the same document for
73+
an already packed document"""
74+
75+
# Testing workflow
76+
self._pack_idempotently("tests/wf/count-lines1-wf.cwl")
77+
7178
def _pack_idempotently(self, document):
7279
self.maxDiff = None
7380
document_loader, workflowobj, uri = fetch_document(

tests/wf/count-lines1-wf.cwl

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env cwl-runner
2+
class: Workflow
3+
cwlVersion: v1.0
4+
5+
inputs:
6+
file1:
7+
type: File
8+
9+
outputs:
10+
count_output:
11+
type: int
12+
outputSource: step2/output
13+
14+
steps:
15+
step1:
16+
run: wc-tool.cwl
17+
in:
18+
file1: file1
19+
out: [output]
20+
21+
step2:
22+
run: parseInt-tool.cwl
23+
in:
24+
file1: step1/output
25+
out: [output]

tests/wf/parseInt-tool.cwl

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env cwl-runner
2+
3+
class: ExpressionTool
4+
requirements:
5+
- class: InlineJavascriptRequirement
6+
cwlVersion: v1.0
7+
8+
inputs:
9+
file1:
10+
type: File
11+
inputBinding: { loadContents: true }
12+
13+
outputs:
14+
output: int
15+
16+
expression: "$({'output': parseInt(inputs.file1.contents)})"

tests/wf/wc-tool.cwl

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env cwl-runner
2+
3+
class: CommandLineTool
4+
cwlVersion: v1.0
5+
6+
inputs:
7+
file1: File
8+
9+
outputs:
10+
output:
11+
type: File
12+
outputBinding: { glob: output }
13+
14+
baseCommand: [wc, -l]
15+
16+
stdin: $(inputs.file1.path)
17+
stdout: output

0 commit comments

Comments
 (0)