|  | 
| 30 | 30 | from . import draft2tool | 
| 31 | 31 | from .builder import adjustFileObjs, adjustDirObjs | 
| 32 | 32 | from .stdfsaccess import StdFsAccess | 
|  | 33 | +from .pack import pack | 
| 33 | 34 | 
 | 
| 34 | 35 | _logger = logging.getLogger("cwltool") | 
| 35 | 36 | 
 | 
| @@ -495,74 +496,9 @@ def makeRelative(ob): | 
| 495 | 496 | 
 | 
| 496 | 497 |     stdout.write(json.dumps(deps, indent=4)) | 
| 497 | 498 | 
 | 
| 498 |  | -def flatten_deps(d, files):  # type: (Any, Set[Text]) -> None | 
| 499 |  | -    if isinstance(d, list): | 
| 500 |  | -        for s in d: | 
| 501 |  | -            flatten_deps(s, files) | 
| 502 |  | -    elif isinstance(d, dict): | 
| 503 |  | -        files.add(d["location"]) | 
| 504 |  | -        if "secondaryFiles" in d: | 
| 505 |  | -            flatten_deps(d["secondaryFiles"], files) | 
| 506 |  | - | 
| 507 |  | -def find_run(d, runs):  # type: (Any, Set[Text]) -> None | 
| 508 |  | -    if isinstance(d, list): | 
| 509 |  | -        for s in d: | 
| 510 |  | -            find_run(s, runs) | 
| 511 |  | -    elif isinstance(d, dict): | 
| 512 |  | -        if "run" in d and isinstance(d["run"], (Text, Text)): | 
| 513 |  | -            runs.add(d["run"]) | 
| 514 |  | -        for s in d.values(): | 
| 515 |  | -            find_run(s, runs) | 
| 516 |  | - | 
| 517 |  | -def replace_refs(d, rewrite, stem, newstem): | 
| 518 |  | -    # type: (Any, Dict[Text, Text], Text, Text) -> None | 
| 519 |  | -    if isinstance(d, list): | 
| 520 |  | -        for s,v in enumerate(d): | 
| 521 |  | -            if isinstance(v, (str, Text)) and v.startswith(stem): | 
| 522 |  | -                d[s] = newstem + v[len(stem):] | 
| 523 |  | -            else: | 
| 524 |  | -                replace_refs(v, rewrite, stem, newstem) | 
| 525 |  | -    elif isinstance(d, dict): | 
| 526 |  | -        if "run" in d and isinstance(d["run"], (str, Text)): | 
| 527 |  | -            d["run"] = rewrite[d["run"]] | 
| 528 |  | -        for s,v in d.items(): | 
| 529 |  | -            if isinstance(v, (str, Text)) and v.startswith(stem): | 
| 530 |  | -                d[s] = newstem + v[len(stem):] | 
| 531 |  | -            replace_refs(v, rewrite, stem, newstem) | 
| 532 |  | - | 
| 533 | 499 | def print_pack(document_loader, processobj, uri, metadata): | 
| 534 |  | -    # type: (Loader, Any, Text, Dict[Text, Text]) -> Text | 
| 535 |  | -    def loadref(b, u): | 
| 536 |  | -        # type: (Text, Text) -> Union[Dict, List, Text] | 
| 537 |  | -        return document_loader.resolve_ref(u, base_url=b)[0] | 
| 538 |  | -    deps = scandeps(uri, processobj, set(("run",)), set(), loadref) | 
| 539 |  | - | 
| 540 |  | -    fdeps = set((uri,)) | 
| 541 |  | -    flatten_deps(deps, fdeps) | 
| 542 |  | - | 
| 543 |  | -    runs = set()  # type: Set[Text] | 
| 544 |  | -    for f in fdeps: | 
| 545 |  | -        find_run(document_loader.idx[f], runs) | 
| 546 |  | - | 
| 547 |  | -    rewrite = {} | 
| 548 |  | -    if isinstance(processobj, list): | 
| 549 |  | -        for p in processobj: | 
| 550 |  | -            rewrite[p["id"]] = "#" + shortname(p["id"]) | 
| 551 |  | -    else: | 
| 552 |  | -        rewrite[uri] = "#main" | 
| 553 |  | - | 
| 554 |  | -    for r in runs: | 
| 555 |  | -        rewrite[r] = "#" + shortname(r) | 
| 556 |  | - | 
| 557 |  | -    packed = {"$graph": [], "cwlVersion": metadata["cwlVersion"] | 
| 558 |  | -            }  # type: Dict[Text, Any] | 
| 559 |  | -    for r,v in rewrite.items(): | 
| 560 |  | -        dc = cast(Dict[Text, Any], copy.deepcopy(document_loader.idx[r])) | 
| 561 |  | -        dc["id"] = v | 
| 562 |  | -        dc["name"] = v | 
| 563 |  | -        replace_refs(dc, rewrite, r+"/" if "#" in r else r+"#", v+"/") | 
| 564 |  | -        packed["$graph"].append(dc) | 
| 565 |  | - | 
|  | 500 | +    # type: (Loader, Union[Dict[unicode, Any], List[Dict[unicode, Any]]], unicode, Dict[unicode, Any]) -> str | 
|  | 501 | +    packed = pack(document_loader, processobj, uri, metadata) | 
| 566 | 502 |     if len(packed["$graph"]) > 1: | 
| 567 | 503 |         return json.dumps(packed, indent=4) | 
| 568 | 504 |     else: | 
|  | 
0 commit comments