Skip to content

Commit

Permalink
Print verbose runProgram output as it happens
Browse files Browse the repository at this point in the history
stdout may still not print until the end if it isn't flushed.

Also, use printerr for "running" message and display exit status on
error.
  • Loading branch information
d-torrance authored and mahrud committed Aug 7, 2024
1 parent 50eb8e2 commit bf9ac3e
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions M2/Macaulay2/m2/programs.m2
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,16 @@ runProgram(Program, String, String) := opts -> (program, name, args) -> (
makeDirectory opts.RunDirectory;
"cd " | opts.RunDirectory | " && " ) else "";
cmd = cmd | program#"path" | addPrefix(name, program#"prefix") | " " | args;
if opts.Verbose then printerr("running: ", cmd);
if match("\\|", cmd) then cmd = "{ " | cmd | ";}";
returnValue := run (cmd | " > " | outFile | " 2> " | errFile);
message := "running: " | cmd | "\n";
returnValue := (
if opts.Verbose
then run ("(((((" | cmd | "; echo $? >&4) | tee " | outFile |
") 3>&1 1>&2 2>&3 | tee " | errFile |
" >&5) 4>&1) | (read r; exit $r)) 5>&1")
else run (cmd | " > " | outFile | " 2> " | errFile));
output := get outFile;
if output != "" then message = message | output;
err := get errFile;
if err != "" then message = message | err;
if opts.Verbose then print(message);
result := {
"command" => cmd,
"output" => output,
Expand All @@ -167,8 +169,8 @@ runProgram(Program, String, String) := opts -> (program, name, args) -> (
removeFile errFile;
);
if opts.RaiseError and returnValue != 0 then error(
program#"name" | " returned an error" |
if opts.Verbose then "" else "\n" | message);
program#"name" | " returned an error (" | toString returnValue |")" |
if opts.Verbose then "" else ":\n" | err);
new ProgramRun from result
)

Expand Down

0 comments on commit bf9ac3e

Please sign in to comment.