Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dune switch [do not merge] #13

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Pipeline: indent whole file
plus add missing '|' on each first pattern matched
  • Loading branch information
UnixJunkie committed Feb 8, 2021
commit 0eb735214c70d6eb9dcd63c8cf54f2fa5aeba218
126 changes: 63 additions & 63 deletions pipeline.ml
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ let prng = Random.State.make_self_init ();;

let temporary_directory =
match Sys.os_type with
"Unix" | "Cygwin" -> (try Sys.getenv "TMPDIR" with Not_found -> "/tmp")
| "Unix" | "Cygwin" -> (try Sys.getenv "TMPDIR" with Not_found -> "/tmp")
| "Win32" -> (try Sys.getenv "TEMP" with Not_found -> ".")
| _ -> assert false

@@ -60,33 +60,33 @@ let rec remove ?log file =
try
let st = stat file in
match st.st_kind with
S_DIR ->
Array.iter (fun name -> remove (file // name)) (Sys.readdir file);
maybe_log log "remove directory %S\n%!" file;
rmdir file
| S_REG
| S_CHR
| S_BLK
| S_LNK
| S_FIFO
| S_SOCK ->
maybe_log log "remove file %S\n%!" file;
Sys.remove file
| S_DIR ->
Array.iter (fun name -> remove (file // name)) (Sys.readdir file);
maybe_log log "remove directory %S\n%!" file;
rmdir file
| S_REG
| S_CHR
| S_BLK
| S_LNK
| S_FIFO
| S_SOCK ->
maybe_log log "remove file %S\n%!" file;
Sys.remove file
with e -> ()


(* like Sys.command, but without shell interpretation *)
let array_command ?stdin ?stdout prog args =
let real_stdin, close_stdin =
match stdin with
None -> Unix.stdin, false
| Some file -> Unix.openfile file [Unix.O_RDONLY] 0, true in
| None -> Unix.stdin, false
| Some file -> Unix.openfile file [Unix.O_RDONLY] 0, true in
let real_stdout, close_stdout =
match stdout with
None -> Unix.stdout, false
| Some file ->
Unix.openfile file
[Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC] 0o600, true in
| None -> Unix.stdout, false
| Some file ->
Unix.openfile file
[Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC] 0o600, true in
let pid = Unix.create_process prog args real_stdin real_stdout Unix.stderr in
let pid', process_status = Unix.waitpid [] pid in
assert (pid = pid');
@@ -95,26 +95,26 @@ let array_command ?stdin ?stdout prog args =
if close_stdout then
(try Unix.close real_stdout with _ -> ());
match process_status with
Unix.WEXITED n -> n
| Unix.WSIGNALED _ -> 2 (* like OCaml's uncaught exceptions *)
| Unix.WSTOPPED _ ->
(* only possible if the call was done using WUNTRACED
or when the child is being traced *)
assert false
| Unix.WEXITED n -> n
| Unix.WSIGNALED _ -> 2 (* like OCaml's uncaught exceptions *)
| Unix.WSTOPPED _ ->
(* only possible if the call was done using WUNTRACED
or when the child is being traced *)
assert false

let concat_cmd cmd = String.concat " " cmd.args

let run_command ?log cmd =
match cmd.args with
[] ->
maybe_log log "empty command\n%!"; 0
| prog :: _ ->
maybe_log log "%s: %s\n%!" prog (concat_cmd cmd);
let status =
array_command ?stdin:cmd.stdin ?stdout:cmd.stdout
prog (Array.of_list cmd.args) in
maybe_log log "exit status %i\n%!" status;
status
| [] ->
maybe_log log "empty command\n%!"; 0
| prog :: _ ->
maybe_log log "%s: %s\n%!" prog (concat_cmd cmd);
let status =
array_command ?stdin:cmd.stdin ?stdout:cmd.stdout
prog (Array.of_list cmd.args) in
maybe_log log "exit status %i\n%!" status;
status

let exec ?log cmd =
maybe_log log "%s\n%!" (concat_cmd cmd);
@@ -149,7 +149,7 @@ let copy_file ?log ?(head = "") ?(tail = "") ?(force = false) src dst =
let copy_files ?log ?force src dst l =
List.iter
(fun (src_name, dst_name) ->
copy_file ?log ?force (src @@ src_name) (dst @@ dst_name))
copy_file ?log ?force (src @@ src_name) (dst @@ dst_name))
l

let match_files names settings =
@@ -158,43 +158,43 @@ let match_files names settings =
List.iter (fun (id, s) -> Hashtbl.replace tbl id (Some s)) settings;
let pairs =
Hashtbl.fold (fun id opt l ->
let x =
match opt with
None -> (id, id)
| Some s -> (id, s) in
x :: l) tbl [] in
let x =
match opt with
| None -> (id, id)
| Some s -> (id, s) in
x :: l) tbl [] in
pairs

let flip (a, b) = (b, a)

let run ?log
?(before = fun () -> ()) ?(after = fun () -> ())
?(input = []) ?(output = []) p =
?(before = fun () -> ()) ?(after = fun () -> ())
?(input = []) ?(output = []) p =
let rec loop l =
match l with
[] -> 0
| cmd :: rest when cmd.stdin = None && cmd.stdout = None ->
(try
let status = exec ?log cmd in
if status = 0 then loop rest
else status
with _ -> 127)
| _ -> failwith "IO redirections: not implemented" in
| [] -> 0
| cmd :: rest when cmd.stdin = None && cmd.stdout = None ->
(try
let status = exec ?log cmd in
if status = 0 then loop rest
else status
with _ -> 127)
| _ -> failwith "IO redirections: not implemented" in
let dir = temp_dir "ocamlpipeline" "" in
Fun.protect
(fun () ->
let base = Sys.getcwd () in
maybe_log log "change directory %S\n%!" dir;
Sys.chdir dir;
before ();
copy_files ?log base dir (List.map flip (match_files p.input input));
let status = loop p.commands in
maybe_log log "change directory %S\n%!" base;
after ();
Sys.chdir base;
maybe_log log "command pipeline exits with status %i\n%!" status;
if status = 0 then
copy_files ?log ~force:true dir base (match_files p.output output);
status
let base = Sys.getcwd () in
maybe_log log "change directory %S\n%!" dir;
Sys.chdir dir;
before ();
copy_files ?log base dir (List.map flip (match_files p.input input));
let status = loop p.commands in
maybe_log log "change directory %S\n%!" base;
after ();
Sys.chdir base;
maybe_log log "command pipeline exits with status %i\n%!" status;
if status = 0 then
copy_files ?log ~force:true dir base (match_files p.output output);
status
)
~finally:(fun () -> remove ?log dir)