author | wenzelm |
Sat, 16 Jul 2011 20:14:58 +0200 | |
changeset 43849 | 00f4b305687d |
parent 43606 | e1a09c2a6248 |
child 43850 | 7f2cbc713344 |
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 |
|
9 |
val isabelle_tool: string -> string -> int |
|
10 |
val mkdirs: Path.T -> unit |
|
40745 | 11 |
val mkdir: Path.T -> unit |
40743 | 12 |
val copy_dir: Path.T -> Path.T -> unit |
42127
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
13 |
val create_tmp_path: string -> string -> Path.T |
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
14 |
val with_tmp_file: string -> string -> (Path.T -> 'a) -> 'a |
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
15 |
val with_tmp_dir: string -> (Path.T -> 'a) -> 'a |
43849
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
16 |
val with_tmp_fifo: (Path.T -> 'a) -> 'a |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
17 |
val bash_output_stream: string -> (TextIO.instream -> 'a) -> 'a |
40743 | 18 |
end; |
19 |
||
20 |
structure Isabelle_System: ISABELLE_SYSTEM = |
|
21 |
struct |
|
22 |
||
23 |
(* system commands *) |
|
24 |
||
25 |
fun isabelle_tool name args = |
|
26 |
(case space_explode ":" (getenv "ISABELLE_TOOLS") |> get_first (fn dir => |
|
43606 | 27 |
let val path = File.platform_path (Path.append (Path.explode dir) (Path.basic name)) in |
40743 | 28 |
if can OS.FileSys.modTime path andalso |
29 |
not (OS.FileSys.isDir path) andalso |
|
30 |
OS.FileSys.access (path, [OS.FileSys.A_READ, OS.FileSys.A_EXEC]) |
|
31 |
then SOME path |
|
32 |
else NONE |
|
33 |
end handle OS.SysErr _ => NONE) of |
|
34 |
SOME path => bash (File.shell_quote path ^ " " ^ args) |
|
35 |
| NONE => (writeln ("Unknown Isabelle tool: " ^ name); 2)); |
|
36 |
||
37 |
fun system_command cmd = |
|
38 |
if bash cmd <> 0 then error ("System command failed: " ^ cmd) |
|
39 |
else (); |
|
40 |
||
41 |
||
42 |
(* directory operations *) |
|
43 |
||
44 |
fun mkdirs path = system_command ("mkdir -p " ^ File.shell_path path); |
|
45 |
||
40785 | 46 |
fun mkdir path = |
47 |
if File.is_dir path then () else OS.FileSys.mkDir (File.platform_path path); |
|
40743 | 48 |
|
49 |
fun copy_dir src dst = |
|
50 |
if File.eq (src, dst) then () |
|
51 |
else (system_command ("cp -r -f " ^ File.shell_path src ^ "/. " ^ File.shell_path dst); ()); |
|
52 |
||
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
53 |
|
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
54 |
(* unique tmp files *) |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
55 |
|
42127
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
56 |
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
|
57 |
let |
42127
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
in path end; |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
62 |
|
42127
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
63 |
fun with_tmp_file name ext f = |
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
64 |
let val path = create_tmp_path name ext |
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
65 |
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
|
66 |
|
42127
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
67 |
fun rm_tree path = system_command ("rm -r -f " ^ File.shell_path path); |
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
68 |
|
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
69 |
fun with_tmp_dir name f = |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
70 |
let |
42127
8223e7f4b0da
Isabelle_System.create_tmp_path/with_tmp_file: optional extension;
wenzelm
parents:
41944
diff
changeset
|
71 |
val path = create_tmp_path name ""; |
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
72 |
val _ = mkdirs path; |
41352
87adb55fb0fb
make SML/NJ and Poly/ML agree on the type of "before";
wenzelm
parents:
41307
diff
changeset
|
73 |
in Exn.release (Exn.capture f 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
|
74 |
|
43849
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
75 |
fun with_tmp_fifo f = |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
76 |
with_tmp_file "isabelle-fifo-" "" |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
77 |
(fn path => |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
78 |
(case bash_output ("mkfifo -m 600 " ^ File.shell_path path) of |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
79 |
(_, 0) => f path |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
80 |
| (out, _) => error (perhaps (try (unsuffix "\n")) out))); |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
81 |
|
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
82 |
|
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
83 |
(* process output stream *) |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
84 |
|
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
85 |
fun bash_output_stream script f = |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
86 |
with_tmp_fifo (fn fifo => |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
87 |
uninterruptible (fn restore_attributes => fn () => |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
88 |
(case bash_process (script ^ " > " ^ File.shell_path fifo ^ " &") of |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
89 |
{rc = 0, terminate, ...} => |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
90 |
Exn.release |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
91 |
(Exn.capture (restore_attributes (fn () => File.open_input f fifo)) () |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
92 |
before terminate ()) |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
93 |
| {output, ...} => error (perhaps (try (unsuffix "\n")) output))) ()); |
00f4b305687d
access to process output stream via auxiliary fifo;
wenzelm
parents:
43606
diff
changeset
|
94 |
|
40743 | 95 |
end; |
96 |