Skip to content

Generate the same document for a previously packed CWL tool/workflow #570

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 6 commits into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions cwltool/pack.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import absolute_import
import copy
import re
from typing import Any, Callable, Dict, List, Set, Text, Union, cast

from schema_salad.ref_resolver import Loader
from six.moves import urllib
from ruamel.yaml.comments import CommentedSeq, CommentedMap

from .process import shortname, uniquename
import six
Expand Down Expand Up @@ -64,7 +66,12 @@ def replace_refs(d, rewrite, stem, newstem):
if v in rewrite:
d[s] = rewrite[v]
elif v.startswith(stem):
d[s] = newstem + v[len(stem):]
id_ = v[len(stem):]
# prevent appending newstems if tool is already packed
if id_.startswith(newstem.strip("#")):
d[s] = "#" + id_
else:
d[s] = newstem + id_
replace_refs(v, rewrite, stem, newstem)

def import_embed(d, seen):
Expand Down Expand Up @@ -106,12 +113,16 @@ def loadref(b, u):

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

def rewrite_id(r, mainuri):
# type: (Text, Text) -> None
def rewrite_id(r, mainuri, document_packed=False):
# type: (Text, Text, bool) -> None
if r == mainuri:
rewrite[r] = "#main"
elif r.startswith(mainuri) and r[len(mainuri)] in ("#", "/"):
pass
if document_packed:
# rewrite tool and mainuri ids in a packed document
tool_id = re.search("#[^/]*$", r)
if tool_id:
rewrite[r] = tool_id.group()
else:
path, frag = urllib.parse.urldefrag(r)
if path == mainpath:
Expand All @@ -122,16 +133,20 @@ def rewrite_id(r, mainuri):

sortedids = sorted(ids)

is_document_packed = all(id.startswith(uri) for id in sortedids)
for r in sortedids:
if r in document_loader.idx:
rewrite_id(r, uri)
rewrite_id(r, uri, is_document_packed)

packed = {"$graph": [], "cwlVersion": metadata["cwlVersion"]
} # type: Dict[Text, Any]

schemas = set() # type: Set[Text]
for r in sorted(runs):
dcr, metadata = document_loader.resolve_ref(r)
if isinstance(dcr, CommentedSeq):
dcr = dcr[0]
dcr = cast(CommentedMap, dcr)
if not isinstance(dcr, dict):
continue
for doc in (dcr, metadata):
Expand All @@ -141,7 +156,7 @@ def rewrite_id(r, mainuri):
if dcr.get("class") not in ("Workflow", "CommandLineTool", "ExpressionTool"):
continue
dc = cast(Dict[Text, Any], copy.deepcopy(dcr))
v = rewrite[r]
v = rewrite.get(r, r + "#main")
dc["id"] = v
for n in ("name", "cwlVersion", "$namespaces", "$schemas"):
if n in dc:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,32 @@ def test_pack_missing_cwlVersion(self):
packed = json.loads(print_pack(document_loader, processobj, uri, metadata))

self.assertEqual('v1.0', packed["cwlVersion"])

def test_pack_idempotence_tool(self):
"""Test to ensure that pack produces exactly the same document for
an already packed document"""

# Testing single tool
self._pack_idempotently("tests/wf/hello_single_tool.cwl")

def test_pack_idempotence_workflow(self):
"""Test to ensure that pack produces exactly the same document for
an already packed document"""

# Testing workflow
self._pack_idempotently("tests/wf/count-lines1-wf.cwl")

def _pack_idempotently(self, document):
self.maxDiff = None
document_loader, workflowobj, uri = fetch_document(
get_data(document))
document_loader, avsc_names, processobj, metadata, uri = validate_document(
document_loader, workflowobj, uri)
# generate pack output dict
packed = json.loads(print_pack(document_loader, processobj, uri, metadata))

document_loader, workflowobj, uri2 = fetch_document(packed)
document_loader, avsc_names, processobj, metadata, uri2 = validate_document(
document_loader, workflowobj, uri)
double_packed = json.loads(print_pack(document_loader, processobj, uri2, metadata))
self.assertEqual(packed, double_packed)
25 changes: 25 additions & 0 deletions tests/wf/count-lines1-wf.cwl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env cwl-runner
class: Workflow
cwlVersion: v1.0

inputs:
file1:
type: File

outputs:
count_output:
type: int
outputSource: step2/output

steps:
step1:
run: wc-tool.cwl
in:
file1: file1
out: [output]

step2:
run: parseInt-tool.cwl
in:
file1: step1/output
out: [output]
16 changes: 16 additions & 0 deletions tests/wf/parseInt-tool.cwl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env cwl-runner

class: ExpressionTool
requirements:
- class: InlineJavascriptRequirement
cwlVersion: v1.0

inputs:
file1:
type: File
inputBinding: { loadContents: true }

outputs:
output: int

expression: "$({'output': parseInt(inputs.file1.contents)})"
17 changes: 17 additions & 0 deletions tests/wf/wc-tool.cwl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env cwl-runner

class: CommandLineTool
cwlVersion: v1.0

inputs:
file1: File

outputs:
output:
type: File
outputBinding: { glob: output }

baseCommand: [wc, -l]

stdin: $(inputs.file1.path)
stdout: output