mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-22 07:03:38 +00:00
mltools: Rename run_command std*_chan -> std*_fd.
These are file descriptors, not the high level OCaml in_channel/ out_channel type, so we would normally not refer to them as *_chan. Just renaming, no functional change.
This commit is contained in:
@@ -152,7 +152,7 @@ let compress_to file outdir =
|
||||
let cmd = [ "xz"; "-f"; "--best"; "--block-size=16777216"; "-c"; file ] in
|
||||
let file_flags = [ Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC; ] in
|
||||
let outfd = Unix.openfile outimg file_flags 0o666 in
|
||||
let res = run_command cmd ~stdout_chan:outfd in
|
||||
let res = run_command cmd ~stdout_fd:outfd in
|
||||
if res <> 0 then
|
||||
error (f_"‘xz’ command failed");
|
||||
outimg
|
||||
|
||||
@@ -277,8 +277,8 @@ let rec run_commands ?(echo_cmd = true) cmds =
|
||||
let res = Array.make (List.length cmds) 0 in
|
||||
let pids =
|
||||
List.mapi (
|
||||
fun i (args, stdout_chan, stderr_chan) ->
|
||||
let run_res = do_run args ?stdout_chan ?stderr_chan in
|
||||
fun i (args, stdout_fd, stderr_fd) ->
|
||||
let run_res = do_run args ?stdout_fd ?stderr_fd in
|
||||
match run_res with
|
||||
| Either (pid, app, outfd, errfd) ->
|
||||
Some (i, pid, app, outfd, errfd)
|
||||
@@ -304,8 +304,8 @@ let rec run_commands ?(echo_cmd = true) cmds =
|
||||
done;
|
||||
Array.to_list res
|
||||
|
||||
and run_command ?(echo_cmd = true) ?stdout_chan ?stderr_chan args =
|
||||
let run_res = do_run args ~echo_cmd ?stdout_chan ?stderr_chan in
|
||||
and run_command ?(echo_cmd = true) ?stdout_fd ?stderr_fd args =
|
||||
let run_res = do_run args ~echo_cmd ?stdout_fd ?stderr_fd in
|
||||
match run_res with
|
||||
| Either (pid, app, outfd, errfd) ->
|
||||
let _, stat = Unix.waitpid [] pid in
|
||||
@@ -313,7 +313,7 @@ and run_command ?(echo_cmd = true) ?stdout_chan ?stderr_chan args =
|
||||
| Or code ->
|
||||
code
|
||||
|
||||
and do_run ?(echo_cmd = true) ?stdout_chan ?stderr_chan args =
|
||||
and do_run ?(echo_cmd = true) ?stdout_fd ?stderr_fd args =
|
||||
let app = List.hd args in
|
||||
let get_fd default = function
|
||||
| None ->
|
||||
@@ -326,13 +326,13 @@ and do_run ?(echo_cmd = true) ?stdout_chan ?stderr_chan args =
|
||||
let app =
|
||||
if Filename.is_relative app then which app
|
||||
else (Unix.access app [Unix.X_OK]; app) in
|
||||
let outfd = get_fd Unix.stdout stdout_chan in
|
||||
let errfd = get_fd Unix.stderr stderr_chan in
|
||||
let outfd = get_fd Unix.stdout stdout_fd in
|
||||
let errfd = get_fd Unix.stderr stderr_fd in
|
||||
if echo_cmd then
|
||||
debug "%s" (stringify_args args);
|
||||
let pid = Unix.create_process app (Array.of_list args) Unix.stdin
|
||||
outfd errfd in
|
||||
Either (pid, app, stdout_chan, stderr_chan)
|
||||
Either (pid, app, stdout_fd, stderr_fd)
|
||||
with
|
||||
| Executable_not_found _ ->
|
||||
Or 127
|
||||
|
||||
@@ -98,10 +98,10 @@ val run_commands : ?echo_cmd:bool -> (string list * Unix.file_descr option * Uni
|
||||
[echo_cmd] specifies whether output the full command on verbose
|
||||
mode, and it's on by default. *)
|
||||
|
||||
val run_command : ?echo_cmd:bool -> ?stdout_chan:Unix.file_descr -> ?stderr_chan:Unix.file_descr -> string list -> int
|
||||
val run_command : ?echo_cmd:bool -> ?stdout_fd:Unix.file_descr -> ?stderr_fd:Unix.file_descr -> string list -> int
|
||||
(** Run an external command without using a shell, and return its exit code.
|
||||
|
||||
If [stdout_chan] or [stderr_chan] is specified, the file descriptor
|
||||
If [stdout_fd] or [stderr_fd] is specified, the file descriptor
|
||||
is automatically closed after executing the command.
|
||||
|
||||
[echo_cmd] specifies whether output the full command on verbose
|
||||
|
||||
@@ -94,14 +94,14 @@ let test_run_command ctx =
|
||||
assert_equal_int 0 (run_command ["true"]);
|
||||
begin
|
||||
let tmpfile, chan = bracket_tmpfile ctx in
|
||||
let res = run_command ["echo"; "this is a test"] ~stdout_chan:(Unix.descr_of_out_channel chan) in
|
||||
let res = run_command ["echo"; "this is a test"] ~stdout_fd:(Unix.descr_of_out_channel chan) in
|
||||
assert_equal_int 0 res;
|
||||
let content = read_whole_file tmpfile in
|
||||
assert_equal_string "this is a test\n" content
|
||||
end;
|
||||
begin
|
||||
let tmpfile, chan = bracket_tmpfile ctx in
|
||||
let res = run_command ["ls"; "/this-directory-is-unlikely-to-exist"] ~stderr_chan:(Unix.descr_of_out_channel chan) in
|
||||
let res = run_command ["ls"; "/this-directory-is-unlikely-to-exist"] ~stderr_fd:(Unix.descr_of_out_channel chan) in
|
||||
assert_equal_int 2 res;
|
||||
let content = read_whole_file tmpfile in
|
||||
assert_bool "test_run_commands/not-existing/content" (String.length content > 0)
|
||||
|
||||
@@ -41,13 +41,13 @@ let create ?(name = "script.py") code =
|
||||
with_open_out path (fun chan -> output_string chan code);
|
||||
{ tmpdir; path }
|
||||
|
||||
let run_command ?echo_cmd ?stdout_chan ?stderr_chan
|
||||
let run_command ?echo_cmd ?stdout_fd ?stderr_fd
|
||||
{ tmpdir; path } params args =
|
||||
let param_file = tmpdir // sprintf "params%d.json" (unique ()) in
|
||||
with_open_out
|
||||
param_file
|
||||
(fun chan -> output_string chan (JSON.string_of_doc params));
|
||||
Tools_utils.run_command ?echo_cmd ?stdout_chan ?stderr_chan
|
||||
Tools_utils.run_command ?echo_cmd ?stdout_fd ?stderr_fd
|
||||
(python :: path :: param_file :: args)
|
||||
|
||||
let path { path } = path
|
||||
|
||||
@@ -29,7 +29,7 @@ val create : ?name:string -> string -> script
|
||||
[Some_source.code] where [some_source.ml] is generated from
|
||||
the Python file by [v2v/embed.sh] (see also [v2v/Makefile.am]). *)
|
||||
|
||||
val run_command : ?echo_cmd:bool -> ?stdout_chan:Unix.file_descr -> ?stderr_chan:Unix.file_descr -> script -> JSON.doc -> string list -> int
|
||||
val run_command : ?echo_cmd:bool -> ?stdout_fd:Unix.file_descr -> ?stderr_fd:Unix.file_descr -> script -> JSON.doc -> string list -> int
|
||||
(** [run_command script params args] is a wrapper around
|
||||
{!Tools_utils.run_command} which runs the Python script with the
|
||||
supplied list of JSON parameters and the list of extra arguments.
|
||||
|
||||
Reference in New Issue
Block a user