src/Pure/System/isabelle_system.ML
author blanchet
Wed, 15 Dec 2010 18:10:32 +0100
changeset 41171 043f8dc3b51f
parent 40785 c755df0f7062
child 41307 bb8468ae414e
permissions -rw-r--r--
facilitate debugging
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 rm_tree: Path.T -> unit
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    11
  val mkdirs: Path.T -> unit
40745
1dabcda202c3 more basic Isabelle_System.mkdir;
wenzelm
parents: 40743
diff changeset
    12
  val mkdir: Path.T -> unit
40743
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    13
  val copy_dir: Path.T -> Path.T -> unit
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    14
end;
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    15
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    16
structure Isabelle_System: ISABELLE_SYSTEM =
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    17
struct
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    18
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    19
(* system commands *)
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    20
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    21
fun isabelle_tool name args =
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    22
  (case space_explode ":" (getenv "ISABELLE_TOOLS") |> get_first (fn dir =>
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    23
      let val path = dir ^ "/" ^ name in
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    24
        if can OS.FileSys.modTime path andalso
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    25
          not (OS.FileSys.isDir path) andalso
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    26
          OS.FileSys.access (path, [OS.FileSys.A_READ, OS.FileSys.A_EXEC])
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    27
        then SOME path
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    28
        else NONE
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    29
      end handle OS.SysErr _ => NONE) of
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    30
    SOME path => bash (File.shell_quote path ^ " " ^ args)
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    31
  | NONE => (writeln ("Unknown Isabelle tool: " ^ name); 2));
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    32
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    33
fun system_command cmd =
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    34
  if bash cmd <> 0 then error ("System command failed: " ^ cmd)
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    35
  else ();
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    36
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    37
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    38
(* directory operations *)
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    39
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    40
fun rm_tree path = system_command ("rm -r " ^ File.shell_path path);
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    41
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    42
fun mkdirs path = system_command ("mkdir -p " ^ File.shell_path path);
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    43
40785
c755df0f7062 more permissive Isabelle_System.mkdir;
wenzelm
parents: 40745
diff changeset
    44
fun mkdir path =
c755df0f7062 more permissive Isabelle_System.mkdir;
wenzelm
parents: 40745
diff changeset
    45
  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
    46
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    47
fun copy_dir src dst =
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    48
  if File.eq (src, dst) then ()
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    49
  else (system_command ("cp -r -f " ^ File.shell_path src ^ "/. " ^ File.shell_path dst); ());
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    50
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    51
end;
b07a0dbc8a38 more explicit Isabelle_System operations;
wenzelm
parents:
diff changeset
    52