@@ -199,7 +199,12 @@ def write_cwl_document(document: Any, name: str, dirname: str) -> None:
199199 path = Path (dirname ) / name
200200 with open (path , "w" ) as handle :
201201 if "cwlVersion" in document :
202- handle .write ("#!/usr/bin/env cwl-runner\n " )
202+ if not (
203+ document .ca
204+ and document .ca .comment
205+ and "cwl-runner" in document .ca .comment [1 ][0 ].value
206+ ):
207+ handle .write ("#!/usr/bin/env cwl-runner\n " )
203208 yaml .dump (document , stream = handle )
204209 if "cwlVersion" in document :
205210 path .chmod (path .stat ().st_mode | stat .S_IXUSR | stat .S_IXGRP | stat .S_IXOTH )
@@ -249,12 +254,13 @@ def v1_0_to_v1_1(document: CommentedMap, outdir: str) -> CommentedMap:
249254def v1_0_to_v1_2 (document : CommentedMap , outdir : str ) -> CommentedMap :
250255 """CWL v1.0.x to v1.2 transformation."""
251256 document = v1_0_to_v1_1 (document , outdir )
252- document [ "cwlVersion" ] = "v1.2"
257+ document = v1_1_to_v1_2 ( document , outdir )
253258 return document
254259
255260
256261def v1_1_to_v1_2 (document : CommentedMap , outdir : str ) -> CommentedMap :
257262 """CWL v1.1 to v1.2 transformation."""
263+ document = _v1_1_to_v1_2 (document , outdir )
258264 document ["cwlVersion" ] = "v1.2"
259265 return document
260266
@@ -350,6 +356,10 @@ def _v1_0_to_v1_1(document: CommentedMap, outdir: str) -> CommentedMap:
350356 _v1_0_to_v1_1 (process , outdir )
351357 if "cwlVersion" in process :
352358 del process ["cwlVersion" ]
359+ elif isinstance (entry ["run" ], str ) and "#" not in entry ["run" ]:
360+ path = Path (document .lc .filename ).parent / entry ["run" ]
361+ process = v1_0_to_v1_1 (load_cwl_document (str (path )), outdir )
362+ write_cwl_document (process , path .name , outdir )
353363 elif isinstance (steps , MutableMapping ):
354364 for step_name in steps :
355365 with SourceLine (steps , step_name , Exception ):
@@ -413,10 +423,62 @@ def _v1_0_to_v1_1(document: CommentedMap, outdir: str) -> CommentedMap:
413423
414424
415425def _v1_0_to_v1_2 (document : CommentedMap , outdir : str ) -> CommentedMap :
416- return _v1_0_to_v1_1 (document , outdir ) # nothing needs doing for v1.2
426+ document = _v1_0_to_v1_1 (document , outdir )
427+ return _v1_1_to_v1_2 (document , outdir )
417428
418429
419430def _v1_1_to_v1_2 (document : CommentedMap , outdir : str ) -> CommentedMap :
431+ if "class" in document :
432+ if document ["class" ] == "Workflow" :
433+ steps = document ["steps" ]
434+ if isinstance (steps , MutableSequence ):
435+ for index , entry in enumerate (steps ):
436+ with SourceLine (steps , index , Exception ):
437+ if "run" in entry and isinstance (entry ["run" ], CommentedMap ):
438+ process = entry ["run" ]
439+ _v1_1_to_v1_2 (process , outdir )
440+ if "cwlVersion" in process :
441+ del process ["cwlVersion" ]
442+
443+ elif isinstance (entry ["run" ], str ) and "#" not in entry ["run" ]:
444+ if hasattr (document .lc , "filename" ):
445+ dirname = Path (document .lc .filename ).parent
446+ else :
447+ dirname = Path (outdir )
448+ path = dirname / entry ["run" ]
449+ process = v1_1_to_v1_2 (load_cwl_document (str (path )), outdir )
450+ write_cwl_document (process , path .name , outdir )
451+ elif isinstance (steps , MutableMapping ):
452+ for step_name in steps :
453+ with SourceLine (steps , step_name , Exception ):
454+ entry = steps [step_name ]
455+ if "run" in entry :
456+ if isinstance (entry ["run" ], CommentedMap ):
457+ process = entry ["run" ]
458+ _v1_1_to_v1_2 (process , outdir )
459+ if "cwlVersion" in process :
460+ del process ["cwlVersion" ]
461+ elif (
462+ isinstance (entry ["run" ], str )
463+ and "#" not in entry ["run" ]
464+ ):
465+ if hasattr (document .lc , "filename" ):
466+ dirname = Path (document .lc .filename ).parent
467+ else :
468+ dirname = Path (outdir )
469+ path = dirname / entry ["run" ]
470+ process = v1_1_to_v1_2 (
471+ load_cwl_document (str (path )), outdir
472+ )
473+ write_cwl_document (process , path .name , outdir )
474+ elif isinstance (entry ["run" ], str ) and "#" in entry ["run" ]:
475+ pass # reference to $graph entry
476+ else :
477+ raise Exception (
478+ "'run' entry was neither a CWL Process nor "
479+ "a path to one: %s." ,
480+ entry ["run" ],
481+ )
420482 return document
421483
422484
@@ -445,10 +507,10 @@ def cleanup(inp: Dict[str, Any]) -> None:
445507
446508
447509def move_up_loadcontents (document : Dict [str , Any ]) -> None :
448- """'loadContents' is promoted up a level in CWL v1.1."""
510+ """Promote 'loadContents' up a level for CWL v1.1."""
449511
450512 def cleanup (inp : Dict [str , Any ]) -> None :
451- """Move loadContents to the preferred location"""
513+ """Move loadContents to the preferred location. """
452514 if "inputBinding" in inp :
453515 bindings = inp ["inputBinding" ]
454516 for field in list (bindings .keys ()):
0 commit comments