author | wenzelm |
Mon, 13 Apr 2020 16:16:22 +0200 | |
changeset 71747 | 1dd514c8c1df |
parent 71646 | 86f064893dac |
child 71748 | 34de8369c290 |
permissions | -rw-r--r-- |
62400 | 1 |
/* Title: Pure/System/process_result.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Result of system process. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
71747 | 9 |
object Process_Result |
10 |
{ |
|
11 |
def print_return_code(rc: Int): String = "Return code: " + rc |
|
12 |
def print_rc(rc: Int): String = "return code " + rc |
|
13 |
} |
|
14 |
||
62401 | 15 |
final case class Process_Result( |
62402 | 16 |
rc: Int, |
17 |
out_lines: List[String] = Nil, |
|
18 |
err_lines: List[String] = Nil, |
|
62569 | 19 |
timeout: Boolean = false, |
20 |
timing: Timing = Timing.zero) |
|
62400 | 21 |
{ |
22 |
def out: String = cat_lines(out_lines) |
|
23 |
def err: String = cat_lines(err_lines) |
|
71631 | 24 |
|
71646 | 25 |
def output(outs: List[String]): Process_Result = |
26 |
copy(out_lines = out_lines ::: outs.flatMap(split_lines)) |
|
27 |
def errors(errs: List[String]): Process_Result = |
|
28 |
copy(err_lines = err_lines ::: errs.flatMap(split_lines)) |
|
68091 | 29 |
def error(err: String): Process_Result = errors(List(err)) |
62400 | 30 |
|
62569 | 31 |
def was_timeout: Process_Result = copy(rc = 1, timeout = true) |
62401 | 32 |
|
62400 | 33 |
def ok: Boolean = rc == 0 |
34 |
def interrupted: Boolean = rc == Exn.Interrupt.return_code |
|
35 |
||
68927 | 36 |
def error_rc: Process_Result = if (interrupted) this else copy(rc = rc max 1) |
37 |
||
64408
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
38 |
def check_rc(pred: Int => Boolean): Process_Result = |
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
39 |
if (pred(rc)) this |
62400 | 40 |
else if (interrupted) throw Exn.Interrupt() |
62492 | 41 |
else Exn.error(err) |
62400 | 42 |
|
64408
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
43 |
def check: Process_Result = check_rc(_ == 0) |
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
44 |
|
71747 | 45 |
def print_return_code: String = Process_Result.print_return_code(rc) |
46 |
def print_rc: String = Process_Result.print_rc(rc) |
|
47 |
||
62400 | 48 |
def print: Process_Result = |
49 |
{ |
|
64138
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
50 |
Output.warning(err) |
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
51 |
Output.writeln(out) |
62404
13a0f537e232
retain tail out_lines as printed, but not the whole log content;
wenzelm
parents:
62402
diff
changeset
|
52 |
copy(out_lines = Nil, err_lines = Nil) |
62400 | 53 |
} |
62553 | 54 |
|
55 |
def print_stdout: Process_Result = |
|
56 |
{ |
|
64138
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
57 |
Output.warning(err, stdout = true) |
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
58 |
Output.writeln(out, stdout = true) |
62553 | 59 |
copy(out_lines = Nil, err_lines = Nil) |
60 |
} |
|
64024 | 61 |
|
62 |
def print_if(b: Boolean): Process_Result = if (b) print else this |
|
63 |
def print_stdout_if(b: Boolean): Process_Result = if (b) print_stdout else this |
|
62400 | 64 |
} |