Skip to content

Commit

Permalink
miniwdl run: -o directs output/error JSON to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
mlin committed Mar 17, 2021
1 parent 0661a48 commit 062814e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
31 changes: 24 additions & 7 deletions WDL/CLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,12 @@ def fill_run_subparser(subparsers):
action="store_true",
help="upon failure, print error information JSON to standard output (in addition to standard error logging)",
)
group.add_argument(
"-o",
metavar="OUT.json",
dest="stdout_file",
help="write JSON output/error to specified file instead of standard output (implies --error-json)",
)
group = run_parser.add_argument_group("logging")
group.add_argument(
"-v",
Expand Down Expand Up @@ -551,6 +557,7 @@ def runner(
no_cache=False,
error_json=False,
log_json=False,
stdout_file=None,
**kwargs,
):
# set up logging
Expand Down Expand Up @@ -669,12 +676,10 @@ def runner(
root=eff_root, # if copy_input_files is set, then input files need not reside under the configured root
)
except Error.InputError as exn:
if error_json:
print(json.dumps(runtime.error_json(exn), indent=(None if log_json else 2)))
runner_standard_output(runtime.error_json(exn), stdout_file, error_json, log_json)
die(exn.args[0])
except Exception as exn:
if error_json:
print(json.dumps(runtime.error_json(exn), indent=(None if log_json else 2)))
runner_standard_output(runtime.error_json(exn), stdout_file, error_json, log_json)
raise

if json_only:
Expand Down Expand Up @@ -722,8 +727,7 @@ def runner(
try:
rundir, output_env = runtime.run(cfg, target, input_env, run_dir=run_dir, _cache=cache)
except Exception as exn:
if error_json:
print(json.dumps(runtime.error_json(exn), indent=(None if log_json else 2)))
runner_standard_output(runtime.error_json(exn), stdout_file, error_json, log_json)
exit_status = 2
from_rundir = None
while isinstance(exn, runtime.RunFailed):
Expand Down Expand Up @@ -762,7 +766,7 @@ def runner(

# report
outputs_json = {"outputs": values_to_json(output_env, namespace=target.name), "dir": rundir}
print(json.dumps(outputs_json, indent=(None if log_json else 2)))
runner_standard_output(outputs_json, stdout_file, error_json, log_json)
return outputs_json


Expand Down Expand Up @@ -1158,6 +1162,19 @@ def raiser(exc: OSError):
return path


def runner_standard_output(content, stdout_file, error_json, log_json):
"""
Write the runner output/error JSON in the way requested by the user
"""
if error_json or stdout_file or "error" not in content:
content_json = json.dumps(content, indent=(None if log_json else 2))
if stdout_file:
with open(stdout_file, "w") as outfile:
print(content_json, file=outfile)
else:
print(content_json)


def fill_run_self_test_subparser(subparsers):
run_parser = subparsers.add_parser(
"run_self_test", help="Run a short built-in workflow to test system configuration"
Expand Down
3 changes: 2 additions & 1 deletion WDL/Error.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ def __str__(self) -> str:
elif str(self.actual).replace("?", "") == str(self.expected):
msg += (
" -- to coerce T? X into T, try select_first([X,defaultValue])"
" or select_first([X]) (which might fail at runtime)"
" or select_first([X]) (which might fail at runtime);"
" to coerce Array[T?] X into Array[T], try select_all(X)"
)
return msg

Expand Down
2 changes: 1 addition & 1 deletion tests/runner.t
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ task failer {
}
}
EOF
$miniwdl run --dir failer2000/. --verbose --error-json failer2000.wdl > failer2000.stdout 2> failer2000.log.txt
$miniwdl run --dir failer2000/. --verbose --error-json failer2000.wdl -o failer2000.stdout 2> failer2000.log.txt
is "$?" "42" "failer2000"
is "$(jq '.cause.exit_status' failer2000.stdout)" "42" "workflow error stdout"
is "$(jq '.cause.exit_status' failer2000/error.json)" "42" "workflow error.json"
Expand Down

0 comments on commit 062814e

Please sign in to comment.