author | wenzelm |
Sun, 13 Mar 2011 16:01:00 +0100 | |
changeset 41944 | b97091ae583a |
parent 41352 | 87adb55fb0fb |
child 42127 | 8223e7f4b0da |
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 |
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
13 |
val with_tmp_file: string -> (Path.T -> 'a) -> 'a |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
14 |
val with_tmp_dir: string -> (Path.T -> 'a) -> 'a |
40743 | 15 |
end; |
16 |
||
17 |
structure Isabelle_System: ISABELLE_SYSTEM = |
|
18 |
struct |
|
19 |
||
20 |
(* system commands *) |
|
21 |
||
22 |
fun isabelle_tool name args = |
|
23 |
(case space_explode ":" (getenv "ISABELLE_TOOLS") |> get_first (fn dir => |
|
24 |
let val path = dir ^ "/" ^ name in |
|
25 |
if can OS.FileSys.modTime path andalso |
|
26 |
not (OS.FileSys.isDir path) andalso |
|
27 |
OS.FileSys.access (path, [OS.FileSys.A_READ, OS.FileSys.A_EXEC]) |
|
28 |
then SOME path |
|
29 |
else NONE |
|
30 |
end handle OS.SysErr _ => NONE) of |
|
31 |
SOME path => bash (File.shell_quote path ^ " " ^ args) |
|
32 |
| NONE => (writeln ("Unknown Isabelle tool: " ^ name); 2)); |
|
33 |
||
34 |
fun system_command cmd = |
|
35 |
if bash cmd <> 0 then error ("System command failed: " ^ cmd) |
|
36 |
else (); |
|
37 |
||
38 |
||
39 |
(* directory operations *) |
|
40 |
||
41 |
fun mkdirs path = system_command ("mkdir -p " ^ File.shell_path path); |
|
42 |
||
40785 | 43 |
fun mkdir path = |
44 |
if File.is_dir path then () else OS.FileSys.mkDir (File.platform_path path); |
|
40743 | 45 |
|
46 |
fun copy_dir src dst = |
|
47 |
if File.eq (src, dst) then () |
|
48 |
else (system_command ("cp -r -f " ^ File.shell_path src ^ "/. " ^ File.shell_path dst); ()); |
|
49 |
||
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
50 |
|
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
51 |
(* unique tmp files *) |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
52 |
|
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
53 |
local |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
54 |
|
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
55 |
fun fresh_path name = |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
56 |
let |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
57 |
val path = File.tmp_path (Path.basic (name ^ serial_string ())); |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
58 |
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
|
59 |
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
|
60 |
in path end; |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
61 |
|
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
62 |
fun rm_tree path = system_command ("rm -r -f " ^ File.shell_path path); |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
63 |
|
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
64 |
in |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
65 |
|
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
66 |
fun with_tmp_file name f = |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
67 |
let val path = fresh_path name |
41352
87adb55fb0fb
make SML/NJ and Poly/ML agree on the type of "before";
wenzelm
parents:
41307
diff
changeset
|
68 |
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
|
69 |
|
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
70 |
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
|
71 |
let |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
72 |
val path = fresh_path name; |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
73 |
val _ = mkdirs path; |
41352
87adb55fb0fb
make SML/NJ and Poly/ML agree on the type of "before";
wenzelm
parents:
41307
diff
changeset
|
74 |
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
|
75 |
|
40743 | 76 |
end; |
77 |
||
41307
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
78 |
end; |
bb8468ae414e
slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents:
40785
diff
changeset
|
79 |