[utils] return process exit status in with_process_{in,full} helpers

Summary:
It's nice to be able to know how well the process we started did, although this
diff ignores it for now.

Reviewed By: dulmarod

Differential Revision: D3937902

fbshipit-source-id: 80bf20f
master
Jules Villard 8 years ago committed by Facebook Github Bot
parent 3ddc23d7e5
commit f59c18cc44

@ -154,7 +154,7 @@ let align desc_list =
to read the result from `tput`. *)
(try Unix.getenv "TERM" |> ignore
with Not_found -> Unix.putenv "TERM" "ansi");
let cur_term_width = try int_of_string (with_process_in "tput cols" input_line)
let cur_term_width = try with_process_in "tput cols" input_line |> fst |> int_of_string
with
| End_of_file (* tput is unhappy *)
| Failure "int_of_string" (* not sure if possible *) -> min_term_width in

@ -258,6 +258,7 @@ let ncpu =
with_process_in
"getconf _NPROCESSORS_ONLN 2>/dev/null"
(fun chan -> Scanf.fscanf chan "%d" (fun n -> n))
|> fst
with _ ->
1

@ -586,30 +586,42 @@ let read_optional_json_file path =
Error msg
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 oc = open_out file in
try
let res = f oc in
close_out oc;
res
with exc ->
close_out oc;
raise exc
let f () = f oc in
let g () = close_out oc in
do_finally f g
let write_json_to_file destfile 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 chan = Unix.open_process_in command in
let res =
try
read chan
with exc ->
Unix.close_process_in chan |> ignore ;
raise exc
in
Unix.close_process_in chan |> ignore ;
res
let f () = read chan in
let g () =
consume_in chan;
Unix.close_process_in chan in
do_finally f g
let with_process_full command read_out read_err =
let (out_chan, _, err_chan) as chans = Unix.open_process_full command (Unix.environment ()) in
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 =
Format.kfprintf (fun _ -> failwith (Format.flush_str_formatter ()))

@ -279,7 +279,10 @@ val read_optional_json_file : string -> (Yojson.Basic.json, string) result
val write_json_to_file : string -> Yojson.Basic.json -> unit
val with_process_in: string -> (in_channel -> 'a) -> 'a
val with_process_in: string -> (in_channel -> 'a) -> ('a * Unix.process_status)
val with_process_full: string -> (in_channel -> 'a) -> (in_channel -> 'b) ->
(('a * 'b) * Unix.process_status)
val failwithf : ('a, Format.formatter, unit, 'b) format4 -> 'a

@ -168,7 +168,7 @@ let get_compilation_database changed_files =
let buck_targets_list = buck :: "targets" :: "--show-output" :: args_with_flavor in
let buck_targets = String.concat " " buck_targets_list in
try
match Utils.with_process_in buck_targets Std.input_list with
match fst @@ Utils.with_process_in buck_targets Std.input_list with
| [] -> Logging.stdout "There are no files to process, exiting."; exit 0
| lines ->
let scan_output compilation_database_files chan =

Loading…
Cancel
Save