Skip to content

Commit cb81b22

Browse files
authored
Merge pull request #1105 from common-workflow-language/do_update_flag
Make do_update optional to override updating behavior.
2 parents 4a31f2a + 537d278 commit cb81b22

File tree

6 files changed

+71
-15
lines changed

6 files changed

+71
-15
lines changed

cwltool/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __init__(self, kwargs=None):
6464
self.host_provenance = False # type: bool
6565
self.user_provenance = False # type: bool
6666
self.prov_obj = None # type: Optional[ProvenanceProfile]
67-
self.do_update = True # type: bool
67+
self.do_update = None # type: Optional[bool]
6868
self.jobdefaults = None # type: Optional[MutableMapping[Text, Any]]
6969

7070
super(LoadingContext, self).__init__(kwargs)

cwltool/load_tool.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,16 @@ def resolve_and_validate_document(loadingContext,
216216

217217
fileuri = urllib.parse.urldefrag(uri)[0]
218218

219-
cwlVersion = workflowobj.get("cwlVersion")
219+
cwlVersion = loadingContext.metadata.get("cwlVersion")
220220
if not cwlVersion:
221-
fileobj = fetch_document(fileuri, loadingContext)[1]
222-
cwlVersion = fileobj.get("cwlVersion")
223-
if not cwlVersion:
224-
raise ValidationException(
225-
"No cwlVersion found. "
226-
"Use the following syntax in your CWL document to declare "
227-
"the version: cwlVersion: <version>.\n"
228-
"Note: if this is a CWL draft-2 (pre v1.0) document then it "
229-
"will need to be upgraded first.")
221+
cwlVersion = workflowobj.get("cwlVersion")
222+
if not cwlVersion:
223+
raise ValidationException(
224+
"No cwlVersion found. "
225+
"Use the following syntax in your CWL document to declare "
226+
"the version: cwlVersion: <version>.\n"
227+
"Note: if this is a CWL draft-2 (pre v1.0) document then it "
228+
"will need to be upgraded first.")
230229

231230
if not isinstance(cwlVersion, string_types):
232231
with SourceLine(workflowobj, "cwlVersion", ValidationException):
@@ -285,6 +284,8 @@ def resolve_and_validate_document(loadingContext,
285284
workflowobj["id"] = fileuri
286285
processobj, metadata = document_loader.resolve_all(
287286
workflowobj, fileuri, checklinks=loadingContext.do_validate)
287+
if loadingContext.metadata:
288+
metadata = loadingContext.metadata
288289
if not isinstance(processobj, (CommentedMap, CommentedSeq)):
289290
raise ValidationException("Workflow must be a CommentedMap or CommentedSeq.")
290291
if not isinstance(metadata, CommentedMap):
@@ -298,7 +299,8 @@ def resolve_and_validate_document(loadingContext,
298299
if loadingContext.do_validate:
299300
schema.validate_doc(avsc_names, processobj, document_loader, loadingContext.strict)
300301

301-
if loadingContext.do_update:
302+
# None means default behavior (do update)
303+
if loadingContext.do_update in (True, None):
302304
processobj = cast(CommentedMap, cmap(update.update(
303305
processobj, document_loader, fileuri, loadingContext.enable_dev, metadata)))
304306
if isinstance(processobj, MutableMapping):

cwltool/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,8 @@ def formatTime(self, record, datefmt=None):
625625
loadingContext.construct_tool_object = getdefault(
626626
loadingContext.construct_tool_object, workflow.default_make_tool)
627627
loadingContext.resolver = getdefault(loadingContext.resolver, tool_resolver)
628-
loadingContext.do_update = not (args.pack or args.print_subgraph)
628+
if loadingContext.do_update is None:
629+
loadingContext.do_update = not (args.pack or args.print_subgraph)
629630

630631
uri, tool_file_uri = resolve_tool_uri(
631632
args.workflow, resolver=loadingContext.resolver,
@@ -654,7 +655,7 @@ def formatTime(self, record, datefmt=None):
654655
= resolve_and_validate_document(loadingContext, workflowobj, uri,
655656
preprocess_only=(args.print_pre or args.pack),
656657
skip_schemas=args.skip_schemas)
657-
658+
658659
if loadingContext.loader is None:
659660
raise Exception("Impossible code path.")
660661
processobj, metadata = loadingContext.loader.resolve_ref(uri)

cwltool/process.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
from .utils import (DEFAULT_TMP_PREFIX, aslist, cmp_like_py2,
4747
copytree_with_merge, onWindows, random_outdir)
4848
from .validate_js import validate_js_expressions
49+
from .update import INTERNAL_VERSION
50+
4951
try:
5052
from os import scandir # type: ignore
5153
except ImportError:
@@ -586,6 +588,10 @@ def __init__(self,
586588
def _init_job(self, joborder, runtime_context):
587589
# type: (Mapping[Text, Text], RuntimeContext) -> Builder
588590

591+
if self.metadata.get("cwlVersion") != INTERNAL_VERSION:
592+
raise WorkflowException("Process object loaded with version '%s', must update to '%s' in order to execute." % (
593+
self.metadata.get("cwlVersion"), INTERNAL_VERSION))
594+
589595
job = cast(Dict[Text, Union[Dict[Text, Any], List[Any], Text, None]],
590596
copy.deepcopy(joborder))
591597

cwltool/update.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ def fix_inputBinding(t):
8989
ALLUPDATES = UPDATES.copy()
9090
ALLUPDATES.update(DEVUPDATES)
9191

92+
INTERNAL_VERSION = u"v1.1.0-dev1"
93+
9294
def identity(doc, loader, baseuri): # pylint: disable=unused-argument
9395
# type: (Any, Loader, Text) -> Tuple[Any, Union[Text, Text]]
9496
"""The default, do-nothing, CWL document upgrade function."""
@@ -120,7 +122,8 @@ def checkversion(doc, # type: Union[CommentedSeq, CommentedMap]
120122
else:
121123
raise Exception("Expected CommentedMap or CommentedSeq")
122124

123-
version = cdoc[u"cwlVersion"]
125+
version = metadata[u"cwlVersion"]
126+
cdoc["cwlVersion"] = version
124127

125128
if version not in UPDATES:
126129
if version in DEVUPDATES:

tests/test_load_tool.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from cwltool.load_tool import load_tool
2+
from cwltool.context import LoadingContext, RuntimeContext
3+
from cwltool.errors import WorkflowException
4+
import pytest
5+
from .util import (get_data, get_main_output,
6+
get_windows_safe_factory,
7+
needs_docker, working_directory,
8+
needs_singularity, temp_dir,
9+
windows_needs_docker)
10+
11+
@windows_needs_docker
12+
def test_check_version():
13+
"""Test that it is permitted to load without updating, but not
14+
execute. Attempting to execute without updating to the internal
15+
version should raise an error.
16+
17+
"""
18+
19+
joborder = {"inp": "abc"}
20+
loadingContext = LoadingContext({"do_update": True})
21+
tool = load_tool(get_data("tests/echo.cwl"), loadingContext)
22+
for j in tool.job(joborder, None, RuntimeContext()):
23+
pass
24+
25+
loadingContext = LoadingContext({"do_update": False})
26+
tool = load_tool(get_data("tests/echo.cwl"), loadingContext)
27+
with pytest.raises(WorkflowException):
28+
for j in tool.job(joborder, None, RuntimeContext()):
29+
pass
30+
31+
def test_use_metadata():
32+
"""Test that it will use the version from loadingContext.metadata if
33+
cwlVersion isn't present in the document.
34+
35+
"""
36+
37+
loadingContext = LoadingContext({"do_update": False})
38+
tool = load_tool(get_data("tests/echo.cwl"), loadingContext)
39+
40+
loadingContext = LoadingContext()
41+
loadingContext.metadata = tool.metadata
42+
tooldata = tool.tool.copy()
43+
del tooldata["cwlVersion"]
44+
tool2 = load_tool(tooldata, loadingContext)

0 commit comments

Comments
 (0)