Skip to content
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
17 changes: 7 additions & 10 deletions cwl_utils/cwl_v1_0_expression_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import hashlib
import uuid
from collections.abc import Mapping, MutableSequence, Sequence
from contextlib import suppress
from typing import Any, Optional, cast

from ruamel import yaml
Expand All @@ -30,11 +31,9 @@ def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool
result = copy.deepcopy(process)
stdout_path = process.stdout
if not stdout_path:
stdout_path = str(
hashlib.sha1( # nosec
json_dumps(cwl.save(process)).encode("utf-8")
).hexdigest()
)
stdout_path = hashlib.sha1( # nosec
json_dumps(cwl.save(process)).encode("utf-8")
).hexdigest()
result.stdout = stdout_path
result.outputs[index].type_ = "File"
output.outputBinding = cwl.CommandOutputBinding(stdout_path, None, None)
Expand Down Expand Up @@ -534,12 +533,10 @@ def empty_inputs(
elif param.source is None and param.default:
result[param_id] = param.default
else:
try:
with suppress(WorkflowException):
result[param_id] = example_input(
utils.type_for_source(process_or_step.run, param.source, parent)
)
except WorkflowException:
pass
return result


Expand Down Expand Up @@ -797,8 +794,8 @@ def process_workflow_reqs_and_hints(
isinstance(expr_result, Mapping)
and "class" in expr_result
and (
expr_result["class"] == "File"
or expr_result["class"] == "Directory"
expr_result["class"]
in ("File", "Directory")
)
):
target = cwl.InputParameter(
Expand Down
17 changes: 7 additions & 10 deletions cwl_utils/cwl_v1_1_expression_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import hashlib
import uuid
from collections.abc import Mapping, MutableSequence, Sequence
from contextlib import suppress
from typing import Any, Optional, cast

from ruamel import yaml
Expand All @@ -30,11 +31,9 @@ def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool
result = copy.deepcopy(process)
stdout_path = process.stdout
if not stdout_path:
stdout_path = str(
hashlib.sha1( # nosec
json_dumps(cwl.save(process)).encode("utf-8")
).hexdigest()
)
stdout_path = hashlib.sha1( # nosec
json_dumps(cwl.save(process)).encode("utf-8")
).hexdigest()
result.stdout = stdout_path
result.outputs[index].type_ = "File"
output.outputBinding = cwl.CommandOutputBinding(stdout_path, None, None)
Expand Down Expand Up @@ -532,12 +531,10 @@ def empty_inputs(
elif param.source is None and param.default:
result[param_id] = param.default
else:
try:
with suppress(WorkflowException):
result[param_id] = example_input(
utils.type_for_source(process_or_step.run, param.source, parent)
)
except WorkflowException:
pass
return result


Expand Down Expand Up @@ -799,8 +796,8 @@ def process_workflow_reqs_and_hints(
isinstance(expr_result, Mapping)
and "class" in expr_result
and (
expr_result["class"] == "File"
or expr_result["class"] == "Directory"
expr_result["class"]
in ("File", "Directory")
)
):
target = cwl.WorkflowInputParameter(
Expand Down
17 changes: 7 additions & 10 deletions cwl_utils/cwl_v1_2_expression_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import hashlib
import uuid
from collections.abc import Mapping, MutableSequence, Sequence
from contextlib import suppress
from typing import Any, Optional, cast

from ruamel import yaml
Expand All @@ -30,11 +31,9 @@ def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool
result = copy.deepcopy(process)
stdout_path = process.stdout
if not stdout_path:
stdout_path = str(
hashlib.sha1( # nosec
json_dumps(cwl.save(process)).encode("utf-8")
).hexdigest()
)
stdout_path = hashlib.sha1( # nosec
json_dumps(cwl.save(process)).encode("utf-8")
).hexdigest()
result.stdout = stdout_path
result.outputs[index].type_ = "File"
output.outputBinding = cwl.CommandOutputBinding(stdout_path, None, None)
Expand Down Expand Up @@ -532,12 +531,10 @@ def empty_inputs(
elif param.source is None and param.default:
result[param_id] = param.default
else:
try:
with suppress(WorkflowException):
result[param_id] = example_input(
utils.type_for_source(process_or_step.run, param.source, parent)
)
except WorkflowException:
pass
return result


Expand Down Expand Up @@ -894,8 +891,8 @@ def process_workflow_reqs_and_hints(
isinstance(expr_result, Mapping)
and "class" in expr_result
and (
expr_result["class"] == "File"
or expr_result["class"] == "Directory"
expr_result["class"]
in ("File", "Directory")
)
):
target = cwl.WorkflowInputParameter(
Expand Down
4 changes: 2 additions & 2 deletions cwl_utils/docker_extract.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
import argparse
import os
import sys
from collections.abc import Iterator
from pathlib import Path
from typing import cast

import ruamel.yaml
Expand Down Expand Up @@ -51,7 +51,7 @@ def run(args: argparse.Namespace) -> list[cwl.DockerRequirement]:
sys.exit(1)

if args.dir:
os.makedirs(args.dir, exist_ok=True)
Path(args.dir).mkdir(parents=True, exist_ok=True)

top = cwl.load_document_by_uri(args.input)
reqs: list[cwl.DockerRequirement] = []
Expand Down
7 changes: 3 additions & 4 deletions cwl_utils/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@ def dump(string: str) -> str:
scan = scan[w[1] :]
w = scanner(scan)
if convert_to_expression:
parts.append(f'"{scan}"') # noqa: B907
parts.append(";}")
parts.extend((f'"{scan}"', ";}")) # noqa: B907
else:
parts.append(scan)
return "".join(parts)
Expand Down Expand Up @@ -290,8 +289,8 @@ def do_eval(
:param timeout: The maximum number of seconds to wait while executing.
"""
runtime = cast(MutableMapping[str, Union[int, str, None]], copy.deepcopy(resources))
runtime["tmpdir"] = tmpdir if tmpdir else None
runtime["outdir"] = outdir if outdir else None
runtime["tmpdir"] = tmpdir or None
runtime["outdir"] = outdir or None

rootvars = cast(
CWLObjectType,
Expand Down
8 changes: 2 additions & 6 deletions cwl_utils/expression_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,7 @@ def refactor(args: argparse.Namespace) -> int:
if not isinstance(result, MutableSequence):
result_json = save(
result,
base_url=(
result.loadingOptions.fileuri
if result.loadingOptions.fileuri
else ""
),
base_url=(result.loadingOptions.fileuri or ""),
)
# ^^ Setting the base_url and keeping the default value
# for relative_uris=True means that the IDs in the generated
Expand All @@ -155,7 +151,7 @@ def refactor(args: argparse.Namespace) -> int:
]
walk_tree(result_json)
# ^ converts multiline strings to nice multiline YAML
with open(output, "w", encoding="utf-8") as output_filehandle:
with output.open("w", encoding="utf-8") as output_filehandle:
output_filehandle.write(
"#!/usr/bin/env cwl-runner\n"
) # TODO: teach the codegen to do this?
Expand Down
4 changes: 2 additions & 2 deletions cwl_utils/graph_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def arg_parser() -> argparse.ArgumentParser:
"-C",
"--outdir",
type=str,
default=os.getcwd(),
default=Path.cwd(),
help="Output folder for the unpacked CWL files.",
)
return parser
Expand Down Expand Up @@ -194,7 +194,7 @@ def rewrite(
else:
document[key] = f"{referrant_file}#{sub}"
elif isinstance(value, list):
new_sources = list()
new_sources = []
for entry in value:
if entry.startswith("#" + doc_id):
new_sources.append(entry[len(doc_id) + 2 :])
Expand Down
19 changes: 7 additions & 12 deletions cwl_utils/image_puller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
"""Classes for docker-extract."""
import logging
import os
import subprocess # nosec
from abc import ABC, abstractmethod
from pathlib import Path
Expand Down Expand Up @@ -53,11 +52,9 @@ class DockerImagePuller(ImagePuller):

def get_image_name(self) -> str:
"""Get the name of the tarball."""
name = "".join(self.req.split("/")) + ".tar"
# Replace colons with underscores in the name.
# See https://github.com/containers/podman/issues/489
name = name.replace(":", "_")
return name
return ("".join(self.req.split("/")) + ".tar").replace(":", "_")

def generate_udocker_loading_command(self) -> str:
"""Generate the udocker loading command."""
Expand All @@ -70,14 +67,14 @@ def save_docker_image(self) -> None:
ImagePuller._run_command_pull(cmd_pull)
_LOGGER.info(f"Image successfully pulled: {self.req}")
if self.save_directory:
dest = os.path.join(self.save_directory, self.get_image_name())
dest = Path(self.save_directory, self.get_image_name())
if self.save_directory and self.force_pull:
os.remove(dest)
dest.unlink()
cmd_save = [
self.cmd,
"save",
"-o",
dest,
str(dest),
self.req,
]
subprocess.run(cmd_save, check=True) # nosec
Expand Down Expand Up @@ -111,10 +108,8 @@ def save_docker_image(self) -> None:
save_directory: str | Path
if self.save_directory:
save_directory = self.save_directory
if (
os.path.exists(os.path.join(save_directory, self.get_image_name()))
and not self.force_pull
):
target = Path(save_directory, self.get_image_name())
if target.exists() and not self.force_pull:
_LOGGER.info(f"Already cached {self.req} with Singularity.")
return
_LOGGER.info(f"Pulling {self.req} with Singularity...")
Expand All @@ -127,7 +122,7 @@ def save_docker_image(self) -> None:
cmd_pull.extend(
[
"--name",
os.path.join(save_directory, self.get_image_name()),
str(target),
f"docker://{self.req}",
]
)
Expand Down
Loading
Loading