From 915dae6d9114d771ed10a02860c15d7b7836f384 Mon Sep 17 00:00:00 2001 From: Tomoya Tanjo Date: Thu, 11 Nov 2021 01:59:42 +0000 Subject: [PATCH 1/4] Unify `v1*_only` parameter into `target_version` --- cwlupgrader/main.py | 55 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/cwlupgrader/main.py b/cwlupgrader/main.py index ec31cf4..f77c823 100755 --- a/cwlupgrader/main.py +++ b/cwlupgrader/main.py @@ -56,7 +56,6 @@ def main(args: Optional[List[str]] = None) -> int: def run(args: argparse.Namespace) -> int: """Main function.""" - imports: Set[str] = set() for path in args.inputs: _logger.info("Processing %s", path) document = load_cwl_document(path) @@ -71,43 +70,75 @@ def run(args: argparse.Namespace) -> int: if args.v1_1_only: _logger.info("Skipping v1.1 document as requested: %s.", path) continue + + if args.v1_only: + target_version = "v1.0" + elif args.v1_1_only: + target_version = "v1.1" + else: + target_version = "latest" upgraded_document = upgrade_document( - document, args.v1_only, args.v1_1_only, args.dir, imports + document, args.dir, target_version=target_version ) write_cwl_document(upgraded_document, Path(path).name, args.dir) return 0 def upgrade_document( - document: Any, v1_only: bool, v1_1_only: bool, output_dir: str, imports: Set[str] + document: Any, + output_dir: str, + target_version: Optional[str] = "latest", + imports: Optional[Set[str]] = None, ) -> Any: + if imports is None: + imports = set() + supported_versions = ["v1.0", "v1.1", "v1.2", "latest"] + if target_version not in supported_versions: + _logger.error(f"Unsupported target cwlVersion: {target_version}") + return version = document["cwlVersion"] if version == "cwl:draft-3" or version == "draft-3": - if v1_only: + if target_version == "v1.0": main_updater = draft3_to_v1_0 inner_updater = _draft3_to_v1_0 - elif v1_1_only: + elif target_version == "v1.1": main_updater = draft3_to_v1_1 inner_updater = _draft3_to_v1_1 - else: + elif target_version == "v1.2": main_updater = draft3_to_v1_2 inner_updater = _draft3_to_v1_2 + elif target_version == "latest": + main_updater = draft3_to_v1_2 + inner_updater = _draft3_to_v1_2 + else: + pass # does not happen elif version == "v1.0": - if v1_only: + if target_version == "v1.0": _logger.info("Skipping v1.0 document as requested.") return - elif v1_1_only: + elif target_version == "v1.1": main_updater = v1_0_to_v1_1 inner_updater = _v1_0_to_v1_1 - else: + elif target_version == "v1.2": + main_updater = v1_0_to_v1_2 + inner_updater = _v1_0_to_v1_2 + elif target_version == "latest": main_updater = v1_0_to_v1_2 inner_updater = _v1_0_to_v1_2 + else: + pass # does not happen elif version == "v1.1": - if v1_1_only: + if target_version == "v1.1": _logger.info("Skipping v1.1 document as requested.") return - main_updater = v1_1_to_v1_2 - inner_updater = _v1_1_to_v1_2 + elif target_version == "v1.2": + main_updater = v1_1_to_v1_2 + inner_updater = _v1_1_to_v1_2 + elif target_version == "latest": + main_updater = v1_1_to_v1_2 + inner_updater = _v1_1_to_v1_2 + else: + pass # does not happen? How to do the case that base version is v1.0? else: _logger.error(f"Unsupported cwlVersion: {version}") process_imports(document, imports, inner_updater, output_dir) From 2dd93c14e37a595cb471f7720e84cb7460cecb2e Mon Sep 17 00:00:00 2001 From: Tomoya Tanjo Date: Fri, 12 Nov 2021 03:10:58 +0000 Subject: [PATCH 2/4] Provide an import set and reuse it in `run` --- cwlupgrader/main.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cwlupgrader/main.py b/cwlupgrader/main.py index f77c823..45fafbe 100755 --- a/cwlupgrader/main.py +++ b/cwlupgrader/main.py @@ -56,6 +56,7 @@ def main(args: Optional[List[str]] = None) -> int: def run(args: argparse.Namespace) -> int: """Main function.""" + imports: Set[str] = set() for path in args.inputs: _logger.info("Processing %s", path) document = load_cwl_document(path) @@ -78,7 +79,10 @@ def run(args: argparse.Namespace) -> int: else: target_version = "latest" upgraded_document = upgrade_document( - document, args.dir, target_version=target_version + document, + args.dir, + target_version=target_version, + imports=imports, ) write_cwl_document(upgraded_document, Path(path).name, args.dir) return 0 From 603b71929658d62a77d47b3a5b7e23dc42746232 Mon Sep 17 00:00:00 2001 From: Tomoya Tanjo Date: Sun, 14 Nov 2021 11:16:42 +0000 Subject: [PATCH 3/4] Add tests --- testdata/v1.0/listing_deep1.cwl | 13 +++++++++++++ testdata/v1.1/listing_deep1.cwl | 15 +++++++++++++++ testdata/v1.2/listing_deep1.cwl | 15 +++++++++++++++ tests/test_complete.py | 23 ++++++++++++++++++++++- 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100755 testdata/v1.0/listing_deep1.cwl create mode 100755 testdata/v1.1/listing_deep1.cwl create mode 100755 testdata/v1.2/listing_deep1.cwl diff --git a/testdata/v1.0/listing_deep1.cwl b/testdata/v1.0/listing_deep1.cwl new file mode 100755 index 0000000..7877b7c --- /dev/null +++ b/testdata/v1.0/listing_deep1.cwl @@ -0,0 +1,13 @@ +#!/usr/bin/env cwl-runner +class: CommandLineTool +cwlVersion: v1.0 +requirements: + InlineJavascriptRequirement: {} +inputs: + d: Directory +outputs: + out: + type: boolean + outputBinding: + outputEval: '$(inputs.d.listing.length === 1 && inputs.d.listing[0].listing.length === 1)' +baseCommand: "true" diff --git a/testdata/v1.1/listing_deep1.cwl b/testdata/v1.1/listing_deep1.cwl new file mode 100755 index 0000000..09af932 --- /dev/null +++ b/testdata/v1.1/listing_deep1.cwl @@ -0,0 +1,15 @@ +#!/usr/bin/env cwl-runner +class: CommandLineTool +cwlVersion: v1.1 +requirements: + LoadListingRequirement: + loadListing: deep_listing + InlineJavascriptRequirement: {} +inputs: + d: Directory +outputs: + out: + type: boolean + outputBinding: + outputEval: '$(inputs.d.listing.length === 1 && inputs.d.listing[0].listing.length === 1)' +baseCommand: "true" diff --git a/testdata/v1.2/listing_deep1.cwl b/testdata/v1.2/listing_deep1.cwl new file mode 100755 index 0000000..7696466 --- /dev/null +++ b/testdata/v1.2/listing_deep1.cwl @@ -0,0 +1,15 @@ +#!/usr/bin/env cwl-runner +class: CommandLineTool +cwlVersion: v1.2 +requirements: + LoadListingRequirement: + loadListing: deep_listing + InlineJavascriptRequirement: {} +inputs: + d: Directory +outputs: + out: + type: boolean + outputBinding: + outputEval: '$(inputs.d.listing.length === 1 && inputs.d.listing[0].listing.length === 1)' +baseCommand: "true" diff --git a/tests/test_complete.py b/tests/test_complete.py index 19173d1..f5d7555 100644 --- a/tests/test_complete.py +++ b/tests/test_complete.py @@ -1,7 +1,7 @@ import filecmp from pathlib import Path -from cwlupgrader.main import main +from cwlupgrader.main import load_cwl_document, main, upgrade_document from .util import get_data @@ -15,3 +15,24 @@ def test_draft3_workflow(tmp_path: Path) -> None: shallow=False, ) assert result + + +def test_invalid_target(tmp_path: Path) -> None: + """Test for invalid target version""" + doc = load_cwl_document("testdata/v1.0/listing_deep1.cwl") + result = upgrade_document(doc, str(tmp_path), "invalid-version") + assert result is None + + +def test_v1_0_to_v1_1(tmp_path: Path) -> None: + """Basic CWL v1.0 to CWL v1.1 test.""" + doc = load_cwl_document("testdata/v1.0/listing_deep1.cwl") + upgraded = upgrade_document(doc, str(tmp_path), "v1.1") + assert doc == upgraded + + +def test_v1_1_to_v1_2(tmp_path: Path) -> None: + """Basic CWL v1.1 to CWL v1.2 test.""" + doc = load_cwl_document("testdata/v1.1/listing_deep1.cwl") + upgraded = upgrade_document(doc, str(tmp_path), "v1.2") + assert doc == upgraded From a7e29d9ab89cc4f705440c36b9ef460bca0ab498 Mon Sep 17 00:00:00 2001 From: Tomoya Tanjo Date: Sun, 14 Nov 2021 11:28:51 +0000 Subject: [PATCH 4/4] Fix path to the testdata --- tests/test_complete.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_complete.py b/tests/test_complete.py index f5d7555..561f037 100644 --- a/tests/test_complete.py +++ b/tests/test_complete.py @@ -19,20 +19,20 @@ def test_draft3_workflow(tmp_path: Path) -> None: def test_invalid_target(tmp_path: Path) -> None: """Test for invalid target version""" - doc = load_cwl_document("testdata/v1.0/listing_deep1.cwl") + doc = load_cwl_document(get_data("testdata/v1.0/listing_deep1.cwl")) result = upgrade_document(doc, str(tmp_path), "invalid-version") assert result is None def test_v1_0_to_v1_1(tmp_path: Path) -> None: """Basic CWL v1.0 to CWL v1.1 test.""" - doc = load_cwl_document("testdata/v1.0/listing_deep1.cwl") + doc = load_cwl_document(get_data("testdata/v1.0/listing_deep1.cwl")) upgraded = upgrade_document(doc, str(tmp_path), "v1.1") assert doc == upgraded def test_v1_1_to_v1_2(tmp_path: Path) -> None: """Basic CWL v1.1 to CWL v1.2 test.""" - doc = load_cwl_document("testdata/v1.1/listing_deep1.cwl") + doc = load_cwl_document(get_data("testdata/v1.1/listing_deep1.cwl")) upgraded = upgrade_document(doc, str(tmp_path), "v1.2") assert doc == upgraded