Skip to content

Commit

Permalink
rename to --write-summary, adjust test to not leave file behind.
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-c committed Dec 13, 2022
1 parent 8d46783 commit ef6edf9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 53 deletions.
8 changes: 4 additions & 4 deletions cwltool/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,12 @@ def arg_parser() -> argparse.ArgumentParser:
)

parser.add_argument(
"--output-write",
"-o",
"--write-summary",
"-w",
type=str,
help="Determines whether the final output object should be saved to a file instead of printed to stdout.",
help="Path to write the final output JSON object to. Default is stdout.",
default="",
dest="output_write",
dest="write_summmary",
)

parser.add_argument(
Expand Down
4 changes: 2 additions & 2 deletions cwltool/cwlrdf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import urllib
from codecs import StreamWriter
from typing import Any, Dict, Iterator, Optional, TextIO, Union, cast
from typing import IO, Any, Dict, Iterator, Optional, TextIO, Union, cast

from rdflib import Graph
from rdflib.query import ResultRow
Expand Down Expand Up @@ -217,7 +217,7 @@ def dot_without_parameters(g: Graph, stdout: Union[TextIO, StreamWriter]) -> Non
def printdot(
wf: Process,
ctx: ContextType,
stdout: Union[TextIO, StreamWriter],
stdout: IO[str],
) -> None:
cwl_viewer = CWLViewer(printrdf(wf, ctx, "n3")) # type: CWLViewer
stdout.write(cwl_viewer.dot().replace(f"{wf.metadata['id']}#", ""))
79 changes: 39 additions & 40 deletions cwltool/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@
from schema_salad.exceptions import ValidationException
from schema_salad.ref_resolver import Loader, file_uri, uri_file_path
from schema_salad.sourceline import cmap, strip_dup_lineno
from schema_salad.utils import ContextType, FetcherCallableType, json_dumps, yaml_no_ts
from schema_salad.utils import (
ContextType,
FetcherCallableType,
json_dump,
json_dumps,
yaml_no_ts,
)

import ruamel.yaml
from ruamel.yaml.comments import CommentedMap, CommentedSeq
Expand Down Expand Up @@ -415,7 +421,7 @@ def init_job_order(
args: argparse.Namespace,
process: Process,
loader: Loader,
stdout: Union[TextIO, StreamWriter],
stdout: IO[str],
print_input_deps: bool = False,
relative_deps: str = "primary",
make_fs_access: Callable[[str], StdFsAccess] = StdFsAccess,
Expand All @@ -438,7 +444,7 @@ def init_job_order(
file_uri(os.getcwd()) + "/",
)
if args.tool_help:
toolparser.print_help(cast(IO[str], stdout))
toolparser.print_help(stdout)
exit(0)
cmd_line = vars(toolparser.parse_args(args.job_order))
for record_name in records:
Expand Down Expand Up @@ -564,7 +570,7 @@ def make_relative(base: str, obj: CWLObjectType) -> None:
def printdeps(
obj: CWLObjectType,
document_loader: Loader,
stdout: Union[TextIO, StreamWriter],
stdout: IO[str],
relative_deps: str,
uri: str,
basedir: Optional[str] = None,
Expand All @@ -577,7 +583,7 @@ def printdeps(
elif relative_deps == "cwd":
base = os.getcwd()
visit_class(deps, ("File", "Directory"), functools.partial(make_relative, base))
print(json_dumps(deps, indent=4, default=str), file=stdout)
json_dump(deps, stdout, indent=4, default=str)


def prov_deps(
Expand Down Expand Up @@ -641,10 +647,10 @@ def print_pack(
"""Return a CWL serialization of the CWL document in JSON."""
packed = pack(loadingContext, uri)
if len(cast(Sized, packed["$graph"])) > 1:
return json_dumps(packed, indent=4, default=str)
return json_dumps(
cast(MutableSequence[CWLObjectType], packed["$graph"])[0], indent=4, default=str
)
target = packed
else:
target = cast(MutableSequence[CWLObjectType], packed["$graph"])[0]
return json_dumps(target, indent=4, default=str)


def supported_cwl_versions(enable_dev: bool) -> List[str]:
Expand Down Expand Up @@ -945,7 +951,7 @@ def check_working_directories(

def print_targets(
tool: Process,
stdout: Union[TextIO, StreamWriter],
stdout: IO[str],
loading_context: LoadingContext,
prefix: str = "",
) -> None:
Expand Down Expand Up @@ -982,7 +988,7 @@ def main(
args: Optional[argparse.Namespace] = None,
job_order_object: Optional[CWLObjectType] = None,
stdin: IO[Any] = sys.stdin,
stdout: Optional[Union[TextIO, StreamWriter]] = None,
stdout: Optional[IO[str]] = None,
stderr: IO[Any] = sys.stderr,
versionfunc: Callable[[], str] = versionstring,
logger_handler: Optional[logging.Handler] = None,
Expand All @@ -1003,6 +1009,7 @@ def main(
stdout = getwriter("utf-8")(sys.stdout) # type: ignore
else:
stdout = sys.stdout
assert stdout

_logger.removeHandler(defaultStreamHandler)
stderr_handler = logger_handler
Expand Down Expand Up @@ -1151,15 +1158,13 @@ def main(
)

if args.print_pre:
print(
json_dumps(
processobj,
indent=4,
sort_keys=True,
separators=(",", ": "),
default=str,
),
file=stdout,
json_dump(
processobj,
stdout,
indent=4,
sort_keys=True,
separators=(",", ": "),
default=str,
)
return 0

Expand Down Expand Up @@ -1225,15 +1230,13 @@ def main(
if args.print_subgraph:
if "name" in tool.tool:
del tool.tool["name"]
print(
json_dumps(
tool.tool,
indent=4,
sort_keys=True,
separators=(",", ": "),
default=str,
),
file=stdout,
json_dump(
tool.tool,
stdout,
indent=4,
sort_keys=True,
separators=(",", ": "),
default=str,
)
return 0

Expand Down Expand Up @@ -1398,19 +1401,15 @@ def loc_to_path(obj: CWLObjectType) -> None:
# Unsetting the Generation from final output object
visit_class(out, ("File",), MutationManager().unset_generation)

if args.output_write:
with open(args.output_write, "w") as file:
print(
json_dumps(out, indent=4, ensure_ascii=False, default=str),
file=file,
if args.write_summary:
with open(args.write_summary, "w") as output_file:
json_dump(
out, output_file, indent=4, ensure_ascii=False, default=str
)
else:
print(
json_dumps(out, indent=4, ensure_ascii=False, default=str),
file=stdout,
)
if hasattr(stdout, "flush"):
stdout.flush()
json_dump(out, stdout, indent=4, ensure_ascii=False, default=str)
if hasattr(stdout, "flush"):
stdout.flush()

if status != "success":
_logger.warning("Final process status is %s", status)
Expand Down
11 changes: 6 additions & 5 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -1348,25 +1348,26 @@ def test_cache_relative_paths(tmp_path: Path, factor: str) -> None:
assert (tmp_path / "cwltool_cache" / "27903451fc1ee10c148a0bdeb845b2cf").exists()


def test_output_write() -> None:
"""Test --output-write option works."""
def test_write_summary(tmp_path: Path) -> None:
"""Test --write-summary."""
commands = [
get_data("tests/wf/no-parameters-echo.cwl"),
]
error_code, stdout, stderr = get_main_output(commands)
stderr = re.sub(r"\s\s+", " ", stderr)
assert error_code == 0, stderr

final_output_path = str(tmp_path / "final-output.json")
commands_no = [
"--output-write",
"final-output.json",
"--write-summary",
final_output_path,
get_data("tests/wf/no-parameters-echo.cwl"),
]
error_code, stdout_no, stderr = get_main_output(commands_no)
stderr = re.sub(r"\s\s+", " ", stderr)
assert error_code == 0, stderr

with open("final-output.json") as f:
with open(final_output_path) as f:
final_output_str = f.read()

assert len(stdout_no) + len(final_output_str) == len(stdout)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_iwdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_directory_literal_with_real_inputs_inside(tmp_path: Path) -> None:
"""Cope with unmoveable files in the output directory created by Docker+IWDR."""
err_code, _, _ = get_main_output(
[
"--outdir",
"--out",
str(tmp_path),
get_data("tests/iwdr_dir_literal_real_file.cwl"),
"--example={}".format(get_data("tests/__init__.py")),
Expand All @@ -62,7 +62,7 @@ def test_bad_listing_expression(tmp_path: Path) -> None:
"""Confirm better error message for bad listing expression."""
err_code, _, stderr = get_main_output(
[
"--outdir",
"--out",
str(tmp_path),
get_data("tests/iwdr_bad_expr.cwl"),
"--example={}".format(get_data("tests/__init__.py")),
Expand Down

0 comments on commit ef6edf9

Please sign in to comment.