-
-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor 'pack into single graph' functionality into separate module. * Don't modify input step object in WorkflowStep, copy it instead. * Use Text type * Add test for packing feature. * Sort output pack output for deterministic results.
- Loading branch information
Showing
11 changed files
with
404 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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,6 +8,7 @@ eggs/ | |
.eggs/ | ||
*.egg-info/ | ||
*.egg | ||
.tox/ | ||
|
||
# Editor Temps | ||
.*.sw? | ||
|
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import copy | ||
import json | ||
|
||
from schema_salad.ref_resolver import Loader | ||
|
||
from .process import scandeps, shortname | ||
|
||
from typing import Union, Any, cast, Callable, Dict, Tuple, Type, IO, Text | ||
|
||
def flatten_deps(d, files): # type: (Any, Set[Text]) -> None | ||
if isinstance(d, list): | ||
for s in d: | ||
flatten_deps(s, files) | ||
elif isinstance(d, dict): | ||
files.add(d["location"]) | ||
if "secondaryFiles" in d: | ||
flatten_deps(d["secondaryFiles"], files) | ||
|
||
def find_run(d, runs): # type: (Any, Set[Text]) -> None | ||
if isinstance(d, list): | ||
for s in d: | ||
find_run(s, runs) | ||
elif isinstance(d, dict): | ||
if "run" in d and isinstance(d["run"], (str, unicode)): | ||
runs.add(d["run"]) | ||
for s in d.values(): | ||
find_run(s, runs) | ||
|
||
def replace_refs(d, rewrite, stem, newstem): | ||
# type: (Any, Dict[Text, Text], Text, Text) -> None | ||
if isinstance(d, list): | ||
for s,v in enumerate(d): | ||
if isinstance(v, (str, unicode)) and v.startswith(stem): | ||
d[s] = newstem + v[len(stem):] | ||
else: | ||
replace_refs(v, rewrite, stem, newstem) | ||
elif isinstance(d, dict): | ||
if "package" in d: | ||
raise Exception("where the fuck did this come from %s" % json.dumps(d, indent=4)) | ||
if "run" in d and isinstance(d["run"], (str, unicode)): | ||
d["run"] = rewrite[d["run"]] | ||
for s,v in d.items(): | ||
if isinstance(v, (str, unicode)) and v.startswith(stem): | ||
d[s] = newstem + v[len(stem):] | ||
replace_refs(v, rewrite, stem, newstem) | ||
|
||
def pack(document_loader, processobj, uri, metadata): | ||
# type: (Loader, Union[Dict[Text, Any], List[Dict[Text, Any]]], Text, Dict[Text, Text]) -> Dict[Text, Any] | ||
def loadref(b, u): | ||
# type: (Text, Text) -> Union[Dict, List, Text] | ||
return document_loader.resolve_ref(u, base_url=b)[0] | ||
deps = scandeps(uri, processobj, set(("run",)), set(), loadref) | ||
|
||
fdeps = set((uri,)) | ||
flatten_deps(deps, fdeps) | ||
|
||
runs = set() # type: Set[Text] | ||
for f in fdeps: | ||
find_run(document_loader.idx[f], runs) | ||
|
||
rewrite = {} | ||
if isinstance(processobj, list): | ||
for p in processobj: | ||
rewrite[p["id"]] = "#" + shortname(p["id"]) | ||
else: | ||
rewrite[uri] = "#main" | ||
|
||
for r in runs: | ||
rewrite[r] = "#" + shortname(r) | ||
|
||
packed = {"$graph": [], "cwlVersion": metadata["cwlVersion"] | ||
} # type: Dict[Text, Any] | ||
|
||
for r in sorted(rewrite.keys()): | ||
v = rewrite[r] | ||
dc = cast(Dict[Text, Any], copy.deepcopy(document_loader.idx[r])) | ||
dc["id"] = v | ||
for n in ("name", "package", "cwlVersion"): | ||
if n in dc: | ||
del dc[n] | ||
replace_refs(dc, rewrite, r+"/" if "#" in r else r+"#", v+"/") | ||
packed["$graph"].append(dc) | ||
|
||
return packed |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import unittest | ||
import json | ||
from cwltool.load_tool import fetch_document, validate_document | ||
import cwltool.pack | ||
import cwltool.workflow | ||
|
||
class TestPack(unittest.TestCase): | ||
def test_pack(self): | ||
self.maxDiff = None | ||
|
||
document_loader, workflowobj, uri = fetch_document("tests/wf/revsort.cwl") | ||
document_loader, avsc_names, processobj, metadata, uri = validate_document( | ||
document_loader, workflowobj, uri) | ||
packed = cwltool.pack.pack(document_loader, processobj, uri, metadata) | ||
with open("tests/wf/expect_packed.cwl") as f: | ||
expect_packed = json.load(f) | ||
self.assertEqual(expect_packed, packed) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
{ | ||
"cwlVersion": "v1.0", | ||
"$graph": [ | ||
{ | ||
"inputs": [ | ||
{ | ||
"doc": "The input file to be processed.", | ||
"type": "File", | ||
"id": "#main/input" | ||
}, | ||
{ | ||
"default": true, | ||
"doc": "If true, reverse (decending) sort", | ||
"type": "boolean", | ||
"id": "#main/reverse_sort" | ||
} | ||
], | ||
"doc": "Reverse the lines in a document, then sort those lines.", | ||
"class": "Workflow", | ||
"steps": [ | ||
{ | ||
"out": [ | ||
"#main/rev/output" | ||
], | ||
"run": "#revtool.cwl", | ||
"id": "#main/rev", | ||
"in": [ | ||
{ | ||
"source": "#main/input", | ||
"id": "#main/rev/input" | ||
} | ||
] | ||
}, | ||
{ | ||
"out": [ | ||
"#main/sorted/output" | ||
], | ||
"run": "#sorttool.cwl", | ||
"id": "#main/sorted", | ||
"in": [ | ||
{ | ||
"source": "#main/rev/output", | ||
"id": "#main/sorted/input" | ||
}, | ||
{ | ||
"source": "#main/reverse_sort", | ||
"id": "#main/sorted/reverse" | ||
} | ||
] | ||
} | ||
], | ||
"outputs": [ | ||
{ | ||
"outputSource": "#main/sorted/output", | ||
"type": "File", | ||
"id": "#main/output", | ||
"doc": "The output with the lines reversed and sorted." | ||
} | ||
], | ||
"id": "#main", | ||
"hints": [ | ||
{ | ||
"dockerPull": "debian:8", | ||
"class": "DockerRequirement" | ||
} | ||
] | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"inputBinding": {}, | ||
"type": "File", | ||
"id": "#revtool.cwl/input" | ||
} | ||
], | ||
"stdout": "output.txt", | ||
"doc": "Reverse each line using the `rev` command", | ||
"baseCommand": "rev", | ||
"class": "CommandLineTool", | ||
"outputs": [ | ||
{ | ||
"outputBinding": { | ||
"glob": "output.txt" | ||
}, | ||
"type": "File", | ||
"id": "#revtool.cwl/output" | ||
} | ||
], | ||
"id": "#revtool.cwl" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"inputBinding": { | ||
"position": 1, | ||
"prefix": "--reverse" | ||
}, | ||
"type": "boolean", | ||
"id": "#sorttool.cwl/reverse" | ||
}, | ||
{ | ||
"inputBinding": { | ||
"position": 2 | ||
}, | ||
"type": "File", | ||
"id": "#sorttool.cwl/input" | ||
} | ||
], | ||
"stdout": "output.txt", | ||
"doc": "Sort lines using the `sort` command", | ||
"baseCommand": "sort", | ||
"class": "CommandLineTool", | ||
"outputs": [ | ||
{ | ||
"outputBinding": { | ||
"glob": "output.txt" | ||
}, | ||
"type": "File", | ||
"id": "#sorttool.cwl/output" | ||
} | ||
], | ||
"id": "#sorttool.cwl" | ||
} | ||
] | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"input": { | ||
"class": "File", | ||
"location": "whale.txt" | ||
} | ||
} |
Oops, something went wrong.