src/Pure/System/isabelle_system.ML
author wenzelm
Tue, 21 Dec 2010 11:54:35 +0100
changeset 41352 87adb55fb0fb
parent 41307 bb8468ae414e
child 41944 b97091ae583a
permissions -rw-r--r--
make SML/NJ and Poly/ML agree on the type of "before";
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40743
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/System/isabelle_system.ML
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
     3
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
     4
Isabelle system support.
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
     5
*)
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
     6
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
     7
signature ISABELLE_SYSTEM =
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
     8
sig
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
     9
  val isabelle_tool: string -> string -> int
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    10
  val mkdirs: Path.T -> unit
40745
1dabcda202c3 more basic Isabelle_System.mkdir;
wenzelm
parents: 40743
diff changeset
    11
  val mkdir: Path.T -> unit
40743
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    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
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    15
end;
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    16
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    17
structure Isabelle_System: ISABELLE_SYSTEM =
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    18
struct
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    19
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    20
(* system commands *)
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    21
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    22
fun isabelle_tool name args =
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    23
  (case space_explode ":" (getenv "ISABELLE_TOOLS") |> get_first (fn dir =>
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    24
      let val path = dir ^ "/" ^ name in
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    25
        if can OS.FileSys.modTime path andalso
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    26
          not (OS.FileSys.isDir path) andalso
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    27
          OS.FileSys.access (path, [OS.FileSys.A_READ, OS.FileSys.A_EXEC])
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    28
        then SOME path
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    29
        else NONE
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    30
      end handle OS.SysErr _ => NONE) of
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    31
    SOME path => bash (File.shell_quote path ^ " " ^ args)
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    32
  | NONE => (writeln ("Unknown Isabelle tool: " ^ name); 2));
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    33
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    34
fun system_command cmd =
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    35
  if bash cmd <> 0 then error ("System command failed: " ^ cmd)
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    36
  else ();
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    37
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    38
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    39
(* directory operations *)
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    40
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    41
fun mkdirs path = system_command ("mkdir -p " ^ File.shell_path path);
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    42
40785
c755df0f7062 more permissive Isabelle_System.mkdir;
wenzelm
parents: 40745
diff changeset
    43
fun mkdir path =
c755df0f7062 more permissive Isabelle_System.mkdir;
wenzelm
parents: 40745
diff changeset
    44
  if File.is_dir path then () else OS.FileSys.mkDir (File.platform_path path);
40743
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    45
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    46
fun copy_dir src dst =
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    47
  if File.eq (src, dst) then ()
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    48
  else (system_command ("cp -r -f " ^ File.shell_path src ^ "/. " ^ File.shell_path dst); ());
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    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
bb8468ae414e slightly more standard Isabelle_System.with_tmp_file/with_tmp_dir (cf. Scala version);
wenzelm
parents: 40785
diff changeset
    59
      raise Fail ("Temporary file already exists: " ^ quote (Path.implode path));
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
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    76
end;
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    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