author | wenzelm |
Wed, 01 Apr 2020 18:19:46 +0200 | |
changeset 71646 | 86f064893dac |
parent 71631 | 3f02bc5a5a03 |
child 71747 | 1dd514c8c1df |
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 |
||
62401 | 9 |
final case class Process_Result( |
62402 | 10 |
rc: Int, |
11 |
out_lines: List[String] = Nil, |
|
12 |
err_lines: List[String] = Nil, |
|
62569 | 13 |
timeout: Boolean = false, |
14 |
timing: Timing = Timing.zero) |
|
62400 | 15 |
{ |
16 |
def out: String = cat_lines(out_lines) |
|
17 |
def err: String = cat_lines(err_lines) |
|
71631 | 18 |
|
71646 | 19 |
def output(outs: List[String]): Process_Result = |
20 |
copy(out_lines = out_lines ::: outs.flatMap(split_lines)) |
|
21 |
def errors(errs: List[String]): Process_Result = |
|
22 |
copy(err_lines = err_lines ::: errs.flatMap(split_lines)) |
|
68091 | 23 |
def error(err: String): Process_Result = errors(List(err)) |
62400 | 24 |
|
62569 | 25 |
def was_timeout: Process_Result = copy(rc = 1, timeout = true) |
62401 | 26 |
|
62400 | 27 |
def ok: Boolean = rc == 0 |
28 |
def interrupted: Boolean = rc == Exn.Interrupt.return_code |
|
29 |
||
68927 | 30 |
def error_rc: Process_Result = if (interrupted) this else copy(rc = rc max 1) |
31 |
||
64408
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
32 |
def check_rc(pred: Int => Boolean): Process_Result = |
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
33 |
if (pred(rc)) this |
62400 | 34 |
else if (interrupted) throw Exn.Interrupt() |
62492 | 35 |
else Exn.error(err) |
62400 | 36 |
|
64408
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
37 |
def check: Process_Result = check_rc(_ == 0) |
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64138
diff
changeset
|
38 |
|
62400 | 39 |
def print: Process_Result = |
40 |
{ |
|
64138
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
41 |
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
|
42 |
Output.writeln(out) |
62404
13a0f537e232
retain tail out_lines as printed, but not the whole log content;
wenzelm
parents:
62402
diff
changeset
|
43 |
copy(out_lines = Nil, err_lines = Nil) |
62400 | 44 |
} |
62553 | 45 |
|
46 |
def print_stdout: Process_Result = |
|
47 |
{ |
|
64138
cf0c8c5782af
eliminated extra trim_line: Process_Result.out/err are based on cat_lines, without trailing newline;
wenzelm
parents:
64024
diff
changeset
|
48 |
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
|
49 |
Output.writeln(out, stdout = true) |
62553 | 50 |
copy(out_lines = Nil, err_lines = Nil) |
51 |
} |
|
64024 | 52 |
|
53 |
def print_if(b: Boolean): Process_Result = if (b) print else this |
|
54 |
def print_stdout_if(b: Boolean): Process_Result = if (b) print_stdout else this |
|
62400 | 55 |
} |