@ -586,30 +586,42 @@ let read_optional_json_file path =
Error msg
Error msg
else Ok ( ` Assoc [] )
else Ok ( ` Assoc [] )
let do_finally f g =
let res = try f () with exc -> g () | > ignore ; raise exc in
let res' = g () in
( res , res' )
let with_file file ~ f =
let with_file file ~ f =
let oc = open_out file in
let oc = open_out file in
try
let f () = f oc in
let res = f oc in
let g () = close_out oc in
close_out oc ;
do_finally f g
res
with exc ->
close_out oc ;
raise exc
let write_json_to_file destfile json =
let write_json_to_file destfile json =
with_file destfile ~ f : ( fun oc -> Yojson . Basic . pretty_to_channel oc json )
with_file destfile ~ f : ( fun oc -> Yojson . Basic . pretty_to_channel oc json )
| > fst
let consume_in chan_in =
try
while true do input_line chan_in | > ignore done
with End_of_file -> ()
let with_process_in command read =
let with_process_in command read =
let chan = Unix . open_process_in command in
let chan = Unix . open_process_in command in
let res =
let f () = read chan in
try
let g () =
read chan
consume_in chan ;
with exc ->
Unix . close_process_in chan in
Unix . close_process_in chan | > ignore ;
do_finally f g
raise exc
in
let with_process_full command read_out read_err =
Unix . close_process_in chan | > ignore ;
let ( out_chan , _ , err_chan ) as chans = Unix . open_process_full command ( Unix . environment () ) in
res
let f () = ( read_out out_chan , read_err err_chan ) in
let g () =
consume_in out_chan ;
consume_in err_chan ;
Unix . close_process_full chans in
do_finally f g
let failwithf fmt =
let failwithf fmt =
Format . kfprintf ( fun _ -> failwith ( Format . flush_str_formatter () ) )
Format . kfprintf ( fun _ -> failwith ( Format . flush_str_formatter () ) )