author | wenzelm |
Sat, 07 Aug 2021 22:23:37 +0200 | |
changeset 74142 | 0f051404f487 |
parent 73775 | 6bd747b71bd3 |
child 74147 | d030b988d470 |
permissions | -rw-r--r-- |
40743 | 1 |
(* Title: Pure/System/isabelle_system.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Isabelle system support. |
|
5 |
*) |
|
6 |
||
7 |
signature ISABELLE_SYSTEM = |
|
8 |
sig |
|
74142
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
9 |
val bash_process: |
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
10 |
{script: string, input: string, redirect: bool, timeout: Time.time} -> Process_Result.T |
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
11 |
val bash_process_script: string -> Process_Result.T |
71902
1529336eaedc
check bash functions against Isabelle settings environment;
wenzelm
parents:
70050
diff
changeset
|
12 |
val bash_output: string -> string * int |
1529336eaedc
check bash functions against Isabelle settings environment;
wenzelm
parents:
70050
diff
changeset
|
13 |
val bash: string -> int |
1529336eaedc
check bash functions against Isabelle settings environment;
wenzelm
parents:
70050
diff
changeset
|
14 |
val bash_functions: unit -> string list |
1529336eaedc
check bash functions against Isabelle settings environment;
wenzelm
parents:
70050
diff
changeset
|
15 |
val check_bash_function: Proof.context -> string * Position.T -> string |
73434
00b77365552e
clarified signature: refer to file name instead of file content;
wenzelm
parents:
73419
diff
changeset
|
16 |
val absolute_path: Path.T -> string |
72376 | 17 |
val make_directory: Path.T -> Path.T |
40743 | 18 |
val copy_dir: Path.T -> Path.T -> unit |
56533
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56498
diff
changeset
|
19 |
val copy_file: Path.T -> Path.T -> unit |
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56498
diff
changeset
|
20 |
val copy_file_base: Path.T * Path.T -> Path.T -> unit |
42127
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
21 |
val create_tmp_path: string -> string -> Path.T |
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
22 |
val with_tmp_file: string -> string -> (Path.T -> 'a) -> 'a |
73324 | 23 |
val rm_tree: Path.T -> unit |
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
24 |
val with_tmp_dir: string -> (Path.T -> 'a) -> 'a |
73566 | 25 |
val download: string -> string |
26 |
val download_file: string -> Path.T -> unit |
|
73775 | 27 |
val decode_base64: string -> string |
28 |
val encode_base64: string -> string |
|
73523
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
29 |
val isabelle_id: unit -> string |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
30 |
val isabelle_identifier: unit -> string option |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
31 |
val isabelle_heading: unit -> string |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
32 |
val isabelle_name: unit -> string |
73547 | 33 |
val identification: unit -> string |
40743 | 34 |
end; |
35 |
||
36 |
structure Isabelle_System: ISABELLE_SYSTEM = |
|
37 |
struct |
|
38 |
||
43850
7f2cbc713344
moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents:
43849
diff
changeset
|
39 |
(* bash *) |
7f2cbc713344
moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents:
43849
diff
changeset
|
40 |
|
74142
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
41 |
fun bash_process {script, input, redirect, timeout} = |
73419
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73336
diff
changeset
|
42 |
Scala.function "bash_process" |
74142
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
43 |
["export ISABELLE_TMP=" ^ Bash.string (getenv "ISABELLE_TMP") ^ "\n" ^ script, |
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
44 |
input, Value.print_bool redirect, Value.print_int (Time.toMilliseconds timeout)] |
73268
c8abfe393c16
more accurate process_result in ML, corresponding to Process_Result in Scala;
wenzelm
parents:
73264
diff
changeset
|
45 |
|> (fn [] => raise Exn.Interrupt |
c8abfe393c16
more accurate process_result in ML, corresponding to Process_Result in Scala;
wenzelm
parents:
73264
diff
changeset
|
46 |
| [err] => error err |
73273
17c28251fff0
clarified signature: process_result timing from Isabelle/Scala;
wenzelm
parents:
73268
diff
changeset
|
47 |
| a :: b :: c :: d :: lines => |
73264 | 48 |
let |
73268
c8abfe393c16
more accurate process_result in ML, corresponding to Process_Result in Scala;
wenzelm
parents:
73264
diff
changeset
|
49 |
val rc = Value.parse_int a; |
73273
17c28251fff0
clarified signature: process_result timing from Isabelle/Scala;
wenzelm
parents:
73268
diff
changeset
|
50 |
val (elapsed, cpu) = apply2 (Time.fromMilliseconds o Value.parse_int) (b, c); |
73275 | 51 |
val (out_lines, err_lines) = chop (Value.parse_int d) lines; |
73273
17c28251fff0
clarified signature: process_result timing from Isabelle/Scala;
wenzelm
parents:
73268
diff
changeset
|
52 |
in |
74142
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
53 |
if rc = Process_Result.timeout_rc then raise Timeout.TIMEOUT elapsed else (); |
73275 | 54 |
Process_Result.make |
55 |
{rc = rc, |
|
56 |
out_lines = out_lines, |
|
57 |
err_lines = err_lines, |
|
58 |
timing = {elapsed = elapsed, cpu = cpu, gc = Time.zeroTime}} |
|
73273
17c28251fff0
clarified signature: process_result timing from Isabelle/Scala;
wenzelm
parents:
73268
diff
changeset
|
59 |
end |
17c28251fff0
clarified signature: process_result timing from Isabelle/Scala;
wenzelm
parents:
73268
diff
changeset
|
60 |
| _ => raise Fail "Malformed Isabelle/Scala result"); |
73264 | 61 |
|
74142
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
62 |
fun bash_process_script script = |
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
63 |
bash_process {script = script, input = "", redirect = false, timeout = Time.zeroTime}; |
73604 | 64 |
|
74142
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
65 |
fun bash script = |
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
66 |
bash_process_script script |
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
67 |
|> Process_Result.print |
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
68 |
|> Process_Result.rc; |
73281 | 69 |
|
43850
7f2cbc713344
moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents:
43849
diff
changeset
|
70 |
fun bash_output s = |
47499
4b0daca2bf88
redirect bash stderr to Isabelle warning as appropriate -- avoid raw process error output which may either get ignored or overload PIDE syslog in extreme cases;
wenzelm
parents:
46502
diff
changeset
|
71 |
let |
74142
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
72 |
val res = bash_process_script s; |
73277
0110e2e2964c
clarified signature: always trim_line of Process_Result.out/err, uniformly in ML and Scala;
wenzelm
parents:
73275
diff
changeset
|
73 |
val _ = warning (Process_Result.err res); |
73275 | 74 |
in (Process_Result.out res, Process_Result.rc res) end; |
43850
7f2cbc713344
moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents:
43849
diff
changeset
|
75 |
|
7f2cbc713344
moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents:
43849
diff
changeset
|
76 |
|
71902
1529336eaedc
check bash functions against Isabelle settings environment;
wenzelm
parents:
70050
diff
changeset
|
77 |
(* bash functions *) |
1529336eaedc
check bash functions against Isabelle settings environment;
wenzelm
parents:
70050
diff
changeset
|
78 |
|
1529336eaedc
check bash functions against Isabelle settings environment;
wenzelm
parents:
70050
diff
changeset
|
79 |
fun bash_functions () = |
74142
0f051404f487
clarified signature: more options for bash_process;
wenzelm
parents:
73775
diff
changeset
|
80 |
bash_process_script "declare -Fx" |
73279 | 81 |
|> Process_Result.check |
82 |
|> Process_Result.out_lines |
|
83 |
|> map_filter (space_explode " " #> try List.last); |
|
71902
1529336eaedc
check bash functions against Isabelle settings environment;
wenzelm
parents:
70050
diff
changeset
|
84 |
|
71911 | 85 |
fun check_bash_function ctxt arg = |
71912 | 86 |
Completion.check_entity Markup.bash_functionN |
87 |
(bash_functions () |> map (rpair Position.none)) ctxt arg; |
|
71902
1529336eaedc
check bash functions against Isabelle settings environment;
wenzelm
parents:
70050
diff
changeset
|
88 |
|
1529336eaedc
check bash functions against Isabelle settings environment;
wenzelm
parents:
70050
diff
changeset
|
89 |
|
73322 | 90 |
(* directory and file operations *) |
91 |
||
73330 | 92 |
val absolute_path = Path.implode o File.absolute_path; |
73567 | 93 |
fun scala_function name = ignore o Scala.function name o map absolute_path; |
40743 | 94 |
|
73322 | 95 |
fun make_directory path = (scala_function "make_directory" [path]; path); |
40743 | 96 |
|
73322 | 97 |
fun copy_dir src dst = scala_function "copy_dir" [src, dst]; |
40743 | 98 |
|
73322 | 99 |
fun copy_file src dst = scala_function "copy_file" [src, dst]; |
56533
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56498
diff
changeset
|
100 |
|
73322 | 101 |
fun copy_file_base (base_dir, src) target_dir = |
73567 | 102 |
ignore (Scala.function "copy_file_base" |
103 |
[absolute_path base_dir, Path.implode src, absolute_path target_dir]); |
|
56533
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56498
diff
changeset
|
104 |
|
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
105 |
|
56498 | 106 |
(* tmp files *) |
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
107 |
|
42127
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
108 |
fun create_tmp_path name ext = |
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
109 |
let |
42127
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
110 |
val path = File.tmp_path (Path.basic (name ^ serial_string ()) |> Path.ext ext); |
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
111 |
val _ = File.exists path andalso |
41944
b97091ae583a
Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
wenzelm
parents:
41352
diff
changeset
|
112 |
raise Fail ("Temporary file already exists: " ^ Path.print path); |
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
113 |
in path end; |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
114 |
|
42127
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
115 |
fun with_tmp_file name ext f = |
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
116 |
let val path = create_tmp_path name ext |
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
117 |
in Exn.release (Exn.capture f path before ignore (try File.rm path)) end; |
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
118 |
|
56498 | 119 |
|
120 |
(* tmp dirs *) |
|
121 |
||
73324 | 122 |
fun rm_tree path = scala_function "rm_tree" [path]; |
123 |
||
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
124 |
fun with_tmp_dir name f = |
72376 | 125 |
let val path = create_tmp_path name "" |
126 |
in Exn.release (Exn.capture f (make_directory path) before ignore (try rm_tree path)) end; |
|
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
127 |
|
72948 | 128 |
|
129 |
(* download file *) |
|
130 |
||
73566 | 131 |
val download = Scala.function1 "download"; |
132 |
||
133 |
fun download_file url path = File.write path (download url); |
|
72948 | 134 |
|
73523
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
135 |
|
73576 | 136 |
(* base64 *) |
137 |
||
138 |
val decode_base64 = Scala.function1 "decode_base64"; |
|
139 |
val encode_base64 = Scala.function1 "encode_base64"; |
|
140 |
||
141 |
||
73523
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
142 |
(* Isabelle distribution identification *) |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
143 |
|
73565 | 144 |
fun isabelle_id () = Scala.function1 "isabelle_id" ""; |
73523
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
145 |
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
146 |
fun isabelle_identifier () = try getenv_strict "ISABELLE_IDENTIFIER"; |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
147 |
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
148 |
fun isabelle_heading () = |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
149 |
(case isabelle_identifier () of |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
150 |
NONE => "" |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
151 |
| SOME version => " (" ^ version ^ ")"); |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
152 |
|
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
153 |
fun isabelle_name () = getenv_strict "ISABELLE_NAME"; |
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73434
diff
changeset
|
154 |
|
73547 | 155 |
fun identification () = "Isabelle/" ^ isabelle_id () ^ isabelle_heading (); |
156 |
||
40743 | 157 |
end; |