Skip to content

Commit defd882

Browse files
authored
Packing handles mixed document versions in a workflow (#1320)
* Support packing mixed versions. Now automatically updates to greatest version used by the workflow. * scandeps finds file literals under IWD 'listing'
1 parent fb29e35 commit defd882

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

cwltool/load_tool.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,15 @@ def resolve_and_validate_document(
377377

378378
document_loader.resolve_all(workflowobj, fileuri)
379379
processobj, metadata = document_loader.resolve_ref(uri)
380-
if loadingContext.metadata:
381-
metadata = loadingContext.metadata
382380
if not isinstance(processobj, (CommentedMap, CommentedSeq)):
383381
raise ValidationException("Workflow must be a CommentedMap or CommentedSeq.")
382+
383+
if not hasattr(processobj.lc, "filename"):
384+
processobj.lc.filename = fileuri
385+
386+
if loadingContext.metadata:
387+
metadata = loadingContext.metadata
388+
384389
if not isinstance(metadata, CommentedMap):
385390
raise ValidationException(
386391
"metadata must be a CommentedMap, was %s" % type(metadata)

cwltool/pack.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from .load_tool import fetch_document, resolve_and_validate_document
2323
from .process import shortname, uniquename
2424
from .utils import CWLObjectType, CWLOutputType
25+
from .loghandler import _logger
26+
from .update import update, ORDERED_VERSIONS
2527

2628
LoadRefType = Callable[[Optional[str], str], ResolveType]
2729

@@ -151,15 +153,37 @@ def pack(
151153
document_loader.idx[po["id"]] = CommentedMap(po.items())
152154
document_loader.idx[metadata["id"]] = CommentedMap(metadata.items())
153155

154-
def loadref(base: Optional[str], uri: str) -> ResolveType:
155-
return document_loader.resolve_ref(uri, base_url=base)[0]
156+
found_versions = {
157+
cast(str, loadingContext.metadata["cwlVersion"])
158+
} # type: Set[str]
159+
160+
def loadref(base: Optional[str], lr_uri: str) -> ResolveType:
161+
lr_loadingContext = loadingContext.copy()
162+
lr_loadingContext.metadata = {}
163+
lr_loadingContext, lr_workflowobj, lr_uri = fetch_document(
164+
lr_uri, lr_loadingContext
165+
)
166+
lr_loadingContext, lr_uri = resolve_and_validate_document(
167+
lr_loadingContext, lr_workflowobj, lr_uri
168+
)
169+
found_versions.add(cast(str, lr_loadingContext.metadata["cwlVersion"]))
170+
if lr_loadingContext.loader is None:
171+
raise Exception("loader should not be None")
172+
return lr_loadingContext.loader.resolve_ref(lr_uri, base_url=base)[0]
156173

157174
ids = set() # type: Set[str]
158175
find_ids(processobj, ids)
159176

160177
runs = {uri}
161178
find_run(processobj, loadref, runs)
162179

180+
# Figure out the highest version, everything needs to be updated
181+
# to it.
182+
m = 0
183+
for fv in found_versions:
184+
m = max(m, ORDERED_VERSIONS.index(fv))
185+
update_to_version = ORDERED_VERSIONS[m]
186+
163187
for f in runs:
164188
find_ids(document_loader.resolve_ref(f)[0], ids)
165189

@@ -193,7 +217,7 @@ def rewrite_id(r: str, mainuri: str) -> None:
193217
rewrite_id(r, uri)
194218

195219
packed = CommentedMap(
196-
(("$graph", CommentedSeq()), ("cwlVersion", metadata["cwlVersion"]))
220+
(("$graph", CommentedSeq()), ("cwlVersion", update_to_version))
197221
)
198222
namespaces = metadata.get("$namespaces", None)
199223

@@ -208,6 +232,21 @@ def rewrite_id(r: str, mainuri: str) -> None:
208232
dcr = cast(CommentedMap, dcr)
209233
if not isinstance(dcr, MutableMapping):
210234
continue
235+
236+
dcr = update(
237+
dcr,
238+
document_loader,
239+
r,
240+
loadingContext.enable_dev,
241+
metadata,
242+
update_to_version,
243+
)
244+
245+
if "http://commonwl.org/cwltool#original_cwlVersion" in metadata:
246+
del metadata["http://commonwl.org/cwltool#original_cwlVersion"]
247+
if "http://commonwl.org/cwltool#original_cwlVersion" in dcr:
248+
del dcr["http://commonwl.org/cwltool#original_cwlVersion"]
249+
211250
if "$schemas" in metadata:
212251
for s in metadata["$schemas"]:
213252
schemas.add(s)

cwltool/process.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,10 @@ def scandeps(
12351235
if nestdirs:
12361236
deps = nestdir(base, deps)
12371237
r.append(deps)
1238-
elif k not in ("listing", "secondaryFiles"):
1238+
elif doc.get("class") in ("File", "Directory") and k in ("listing", "secondaryFiles"):
1239+
# should be handled earlier.
1240+
pass
1241+
else:
12391242
r.extend(
12401243
scandeps(
12411244
base,

cwltool/update.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@ def v1_2_0dev3todev4(
181181
return (doc, "v1.2.0-dev4")
182182

183183

184+
ORDERED_VERSIONS = [
185+
"v1.0",
186+
"v1.1.0-dev1",
187+
"v1.1",
188+
"v1.2.0-dev1",
189+
"v1.2.0-dev2",
190+
"v1.2.0-dev3",
191+
"v1.2.0-dev4",
192+
]
193+
184194
UPDATES = {
185195
u"v1.0": v1_0to1_1,
186196
u"v1.1": v1_1to1_2,
@@ -268,16 +278,20 @@ def update(
268278
baseuri: str,
269279
enable_dev: bool,
270280
metadata: CommentedMap,
281+
update_to: Optional[str] = None,
271282
) -> CommentedMap:
272283

284+
if update_to is None:
285+
update_to = INTERNAL_VERSION
286+
273287
(cdoc, version) = checkversion(doc, metadata, enable_dev)
274288
originalversion = copy.copy(version)
275289

276290
nextupdate = (
277291
identity
278292
) # type: Optional[Callable[[CommentedMap, Loader, str], Tuple[CommentedMap, str]]]
279293

280-
while nextupdate:
294+
while version != update_to and nextupdate:
281295
(cdoc, version) = nextupdate(cdoc, loader, baseuri)
282296
nextupdate = ALLUPDATES[version]
283297

0 commit comments

Comments
 (0)