src/Pure/General/file.ML
changeset 40743 b07a0dbc8a38
parent 40741 17d6293a1e26
child 40744 0e7c2957fc1d
equal deleted inserted replaced
40742:dc6439c0b8b1 40743:b07a0dbc8a38
    11   val shell_path: Path.T -> string
    11   val shell_path: Path.T -> string
    12   val cd: Path.T -> unit
    12   val cd: Path.T -> unit
    13   val pwd: unit -> Path.T
    13   val pwd: unit -> Path.T
    14   val full_path: Path.T -> Path.T
    14   val full_path: Path.T -> Path.T
    15   val tmp_path: Path.T -> Path.T
    15   val tmp_path: Path.T -> Path.T
    16   val isabelle_tool: string -> string -> int
       
    17   val exists: Path.T -> bool
    16   val exists: Path.T -> bool
    18   val check: Path.T -> unit
    17   val check: Path.T -> unit
    19   val rm: Path.T -> unit
    18   val rm: Path.T -> unit
    20   val rm_tree: Path.T -> unit
       
    21   val mkdir: Path.T -> unit
       
    22   val mkdir_leaf: Path.T -> unit
       
    23   val open_input: (TextIO.instream -> 'a) -> Path.T -> 'a
    19   val open_input: (TextIO.instream -> 'a) -> Path.T -> 'a
    24   val open_output: (TextIO.outstream -> 'a) -> Path.T -> 'a
    20   val open_output: (TextIO.outstream -> 'a) -> Path.T -> 'a
    25   val open_append: (TextIO.outstream -> 'a) -> Path.T -> 'a
    21   val open_append: (TextIO.outstream -> 'a) -> Path.T -> 'a
    26   val fold_lines: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a
    22   val fold_lines: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a
    27   val read: Path.T -> string
    23   val read: Path.T -> string
    30   val write_list: Path.T -> string list -> unit
    26   val write_list: Path.T -> string list -> unit
    31   val append_list: Path.T -> string list -> unit
    27   val append_list: Path.T -> string list -> unit
    32   val write_buffer: Path.T -> Buffer.T -> unit
    28   val write_buffer: Path.T -> Buffer.T -> unit
    33   val eq: Path.T * Path.T -> bool
    29   val eq: Path.T * Path.T -> bool
    34   val copy: Path.T -> Path.T -> unit
    30   val copy: Path.T -> Path.T -> unit
    35   val copy_dir: Path.T -> Path.T -> unit
       
    36 end;
    31 end;
    37 
    32 
    38 structure File: FILE =
    33 structure File: FILE =
    39 struct
    34 struct
    40 
    35 
    60 
    55 
    61 fun tmp_path path =
    56 fun tmp_path path =
    62   Path.append (Path.variable "ISABELLE_TMP") (Path.base path);
    57   Path.append (Path.variable "ISABELLE_TMP") (Path.base path);
    63 
    58 
    64 
    59 
    65 (* system commands *)
       
    66 
       
    67 fun isabelle_tool name args =
       
    68   (case space_explode ":" (getenv "ISABELLE_TOOLS") |> get_first (fn dir =>
       
    69       let val path = dir ^ "/" ^ name in
       
    70         if can OS.FileSys.modTime path andalso
       
    71           not (OS.FileSys.isDir path) andalso
       
    72           OS.FileSys.access (path, [OS.FileSys.A_READ, OS.FileSys.A_EXEC])
       
    73         then SOME path
       
    74         else NONE
       
    75       end handle OS.SysErr _ => NONE) of
       
    76     SOME path => bash (shell_quote path ^ " " ^ args)
       
    77   | NONE => (writeln ("Unknown Isabelle tool: " ^ name); 2));
       
    78 
       
    79 fun system_command cmd =
       
    80   if bash cmd <> 0 then error ("System command failed: " ^ cmd)
       
    81   else ();
       
    82 
       
    83 
       
    84 (* directory entries *)
    60 (* directory entries *)
    85 
    61 
    86 val exists = can OS.FileSys.modTime o platform_path;
    62 val exists = can OS.FileSys.modTime o platform_path;
    87 
    63 
    88 fun check path =
    64 fun check path =
    89   if exists path then ()
    65   if exists path then ()
    90   else error ("No such file or directory: " ^ quote (Path.implode path));
    66   else error ("No such file or directory: " ^ quote (Path.implode path));
    91 
    67 
    92 val rm = OS.FileSys.remove o platform_path;
    68 val rm = OS.FileSys.remove o platform_path;
    93 
       
    94 fun rm_tree path = system_command ("rm -r " ^ shell_path path);
       
    95 
       
    96 fun mkdir path = system_command ("mkdir -p " ^ shell_path path);
       
    97 
       
    98 fun mkdir_leaf path = (check (Path.dir path); mkdir path);
       
    99 
    69 
   100 fun is_dir path =
    70 fun is_dir path =
   101   the_default false (try OS.FileSys.isDir (platform_path path));
    71   the_default false (try OS.FileSys.isDir (platform_path path));
   102 
    72 
   103 
    73 
   161   if eq (src, dst) then ()
   131   if eq (src, dst) then ()
   162   else
   132   else
   163     let val target = if is_dir dst then Path.append dst (Path.base src) else dst
   133     let val target = if is_dir dst then Path.append dst (Path.base src) else dst
   164     in write target (read src) end;
   134     in write target (read src) end;
   165 
   135 
   166 fun copy_dir src dst =
       
   167   if eq (src, dst) then ()
       
   168   else (system_command ("cp -r -f " ^ shell_path src ^ "/. " ^ shell_path dst); ());
       
   169 
       
   170 end;
   136 end;