-
-
Notifications
You must be signed in to change notification settings - Fork 232
Implement targeted "overrides" of requirements on specific tools #440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
36032fa
Apply requirement overrides at load time instead of run time.
83f2235
type annotations.
e7b3bde
Add testing for "overrides".
810e60e
Remove debug print
25f94b9
Fix using $namespaces on file formats.
19fb8d1
Fix mypy, change to use diff-quality --violations=pycodestyle,
46eb65e
More mypy fixups.
552af58
Add documentation section about overrides.
5e49fd9
Fix README
ae19a05
README tweaks.
920f322
README tweaks 2
f404c3c
More tests for overrides. Fix bugs.
a7dc5c5
Tests
c55cdb0
Use kwargs.get()
0815a40
Make override identifiers are relative to the toplevel workflow uri.
d6e5545
Override identifiers are relative to workflow, other identifiers are …
d246862
Fix mypy
53dd06f
Make mypy happy.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,8 @@ | |
import uuid | ||
import hashlib | ||
import json | ||
from typing import Any, Callable, Dict, List, Text, Tuple, Union, cast | ||
import copy | ||
from typing import Any, Callable, Dict, List, Text, Tuple, Union, cast, Iterable | ||
|
||
import requests.sessions | ||
from six import itervalues, string_types | ||
|
@@ -23,19 +24,65 @@ | |
|
||
from . import process, update | ||
from .errors import WorkflowException | ||
from .process import Process, shortname | ||
from .process import Process, shortname, get_schema | ||
from .update import ALLUPDATES | ||
|
||
_logger = logging.getLogger("cwltool") | ||
|
||
jobloaderctx = { | ||
u"cwl": "https://w3id.org/cwl/cwl#", | ||
u"cwltool": "http://commonwl.org/cwltool#", | ||
u"path": {u"@type": u"@id"}, | ||
u"location": {u"@type": u"@id"}, | ||
u"format": {u"@type": u"@id"}, | ||
u"id": u"@id" | ||
} | ||
|
||
|
||
overrides_ctx = { | ||
u"overrideTarget": {u"@type": u"@id"}, | ||
u"cwltool": "http://commonwl.org/cwltool#", | ||
u"overrides": { | ||
"@id": "cwltool:overrides", | ||
"mapSubject": "overrideTarget", | ||
"mapPredicate": "override" | ||
}, | ||
u"override": { | ||
"@id": "cwltool:override", | ||
"mapSubject": "class" | ||
} | ||
} # type: Dict[Text, Union[Dict[Any, Any], Text, Iterable[Text]]] | ||
|
||
def resolve_tool_uri(argsworkflow, # type: Text | ||
resolver=None, # type: Callable[[Loader, Union[Text, Dict[Text, Any]]], Text] | ||
fetcher_constructor=None, | ||
# type: Callable[[Dict[Text, Text], requests.sessions.Session], Fetcher] | ||
document_loader=None # type: Loader | ||
): | ||
# type: (...) -> Tuple[Text, Text] | ||
|
||
uri = None # type: Text | ||
split = urllib.parse.urlsplit(argsworkflow) | ||
# In case of Windows path, urlsplit misjudge Drive letters as scheme, here we are skipping that | ||
if split.scheme and split.scheme in [u'http',u'https',u'file']: | ||
uri = argsworkflow | ||
elif os.path.exists(os.path.abspath(argsworkflow)): | ||
uri = file_uri(str(os.path.abspath(argsworkflow))) | ||
elif resolver: | ||
if document_loader is None: | ||
document_loader = Loader(jobloaderctx, fetcher_constructor=fetcher_constructor) # type: ignore | ||
uri = resolver(document_loader, argsworkflow) | ||
|
||
if uri is None: | ||
raise ValidationException("Not found: '%s'" % argsworkflow) | ||
|
||
if argsworkflow != uri: | ||
_logger.info("Resolved '%s' to '%s'", argsworkflow, uri) | ||
|
||
fileuri = urllib.parse.urldefrag(uri)[0] | ||
return uri, fileuri | ||
|
||
|
||
def fetch_document(argsworkflow, # type: Union[Text, Dict[Text, Any]] | ||
resolver=None, # type: Callable[[Loader, Union[Text, Dict[Text, Any]]], Text] | ||
fetcher_constructor=None | ||
|
@@ -49,22 +96,7 @@ def fetch_document(argsworkflow, # type: Union[Text, Dict[Text, Any]] | |
uri = None # type: Text | ||
workflowobj = None # type: CommentedMap | ||
if isinstance(argsworkflow, string_types): | ||
split = urllib.parse.urlsplit(argsworkflow) | ||
# In case of Windows path, urlsplit misjudge Drive letters as scheme, here we are skipping that | ||
if split.scheme and split.scheme in [u'http',u'https',u'file']: | ||
uri = argsworkflow | ||
elif os.path.exists(os.path.abspath(argsworkflow)): | ||
uri = file_uri(str(os.path.abspath(argsworkflow))) | ||
elif resolver: | ||
uri = resolver(document_loader, argsworkflow) | ||
|
||
if uri is None: | ||
raise ValidationException("Not found: '%s'" % argsworkflow) | ||
|
||
if argsworkflow != uri: | ||
_logger.info("Resolved '%s' to '%s'", argsworkflow, uri) | ||
|
||
fileuri = urllib.parse.urldefrag(uri)[0] | ||
uri, fileuri = resolve_tool_uri(argsworkflow, resolver=resolver, document_loader=document_loader) | ||
workflowobj = document_loader.fetch(fileuri) | ||
elif isinstance(argsworkflow, dict): | ||
uri = "#" + Text(id(argsworkflow)) | ||
|
@@ -139,8 +171,9 @@ def validate_document(document_loader, # type: Loader | |
strict=True, # type: bool | ||
preprocess_only=False, # type: bool | ||
fetcher_constructor=None, | ||
skip_schemas=None | ||
skip_schemas=None, | ||
# type: Callable[[Dict[Text, Text], requests.sessions.Session], Fetcher] | ||
overrides=None # type: List[Dict] | ||
): | ||
# type: (...) -> Tuple[Loader, Names, Union[Dict[Text, Any], List[Dict[Text, Any]]], Dict[Text, Any], Text] | ||
"""Validate a CWL document.""" | ||
|
@@ -155,9 +188,15 @@ def validate_document(document_loader, # type: Loader | |
|
||
jobobj = None | ||
if "cwl:tool" in workflowobj: | ||
jobobj, _ = document_loader.resolve_all(workflowobj, uri) | ||
job_loader = Loader(jobloaderctx, fetcher_constructor=fetcher_constructor) # type: ignore | ||
jobobj, _ = job_loader.resolve_all(workflowobj, uri) | ||
uri = urllib.parse.urljoin(uri, workflowobj["https://w3id.org/cwl/cwl#tool"]) | ||
del cast(dict, jobobj)["https://w3id.org/cwl/cwl#tool"] | ||
|
||
if "http://commonwl.org/cwltool#overrides" in jobobj: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is over-indented |
||
overrides.extend(resolve_overrides(jobobj, uri, uri)) | ||
del jobobj["http://commonwl.org/cwltool#overrides"] | ||
|
||
workflowobj = fetch_document(uri, fetcher_constructor=fetcher_constructor)[1] | ||
|
||
fileuri = urllib.parse.urldefrag(uri)[0] | ||
|
@@ -225,6 +264,9 @@ def validate_document(document_loader, # type: Loader | |
if jobobj: | ||
metadata[u"cwl:defaults"] = jobobj | ||
|
||
if overrides: | ||
metadata[u"cwltool:overrides"] = overrides | ||
|
||
return document_loader, avsc_names, processobj, metadata, uri | ||
|
||
|
||
|
@@ -277,14 +319,29 @@ def load_tool(argsworkflow, # type: Union[Text, Dict[Text, Any]] | |
enable_dev=False, # type: bool | ||
strict=True, # type: bool | ||
resolver=None, # type: Callable[[Loader, Union[Text, Dict[Text, Any]]], Text] | ||
fetcher_constructor=None # type: Callable[[Dict[Text, Text], requests.sessions.Session], Fetcher] | ||
fetcher_constructor=None, # type: Callable[[Dict[Text, Text], requests.sessions.Session], Fetcher] | ||
overrides=None | ||
): | ||
# type: (...) -> Process | ||
|
||
document_loader, workflowobj, uri = fetch_document(argsworkflow, resolver=resolver, | ||
fetcher_constructor=fetcher_constructor) | ||
document_loader, avsc_names, processobj, metadata, uri = validate_document( | ||
document_loader, workflowobj, uri, enable_dev=enable_dev, | ||
strict=strict, fetcher_constructor=fetcher_constructor) | ||
strict=strict, fetcher_constructor=fetcher_constructor, | ||
overrides=overrides) | ||
return make_tool(document_loader, avsc_names, metadata, uri, | ||
makeTool, kwargs if kwargs else {}) | ||
|
||
def resolve_overrides(ov, ov_uri, baseurl): # type: (CommentedMap, Text, Text) -> List[Dict[Text, Any]] | ||
ovloader = Loader(overrides_ctx) | ||
ret, _ = ovloader.resolve_all(ov, baseurl) | ||
if not isinstance(ret, CommentedMap): | ||
raise Exception("Expected CommentedMap, got %s" % type(ret)) | ||
cwl_docloader = get_schema("v1.0")[0] | ||
cwl_docloader.resolve_all(ret, ov_uri) | ||
return ret["overrides"] | ||
|
||
def load_overrides(ov, base_url): # type: (Text, Text) -> List[Dict[Text, Any]] | ||
ovloader = Loader(overrides_ctx) | ||
return resolve_overrides(ovloader.fetch(ov), ov, base_url) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add an example of an identifier for a workflow step?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some discussion. Also made this more complex (but hopefully more useful) by resolving workflow ids relative to the workflow, and everything else relative to the job/--overrides document.