[Libguestfs] [PATCH v2 2/3] v2v: utils: Add utility functions for running external Python code.
Tomáš Golembiovský
tgolembi at redhat.com
Wed Feb 28 11:21:07 UTC 2018
On Tue, 27 Feb 2018 21:33:23 +0000
"Richard W.M. Jones" <rjones at redhat.com> wrote:
> ---
> v2v/utils.ml | 23 +++++++++++++++++++++++
> v2v/utils.mli | 11 +++++++++++
> 2 files changed, 34 insertions(+)
>
> diff --git a/v2v/utils.ml b/v2v/utils.ml
> index 1ceba94cd..bf4106620 100644
> --- a/v2v/utils.ml
> +++ b/v2v/utils.ml
> @@ -196,3 +196,26 @@ let find_file_in_tar tar filename =
> )
> in
> loop lines
> +
> +let run_python ?(python = "python") code =
> + (* In debug mode output the python code into the log. *)
> + debug "running python code using ‘%s’:\n%s" python code;
> +
> + (* Write the Python code to a temporary file so we don't have to
> + * quote things.
> + *)
> + let filename, chan = Filename.open_temp_file "py" ".py" in
> + output_string chan code;
> + close_out chan;
> +
> + let cmd = sprintf "%s %s" (quote python) filename in
quote filename
Tomas
> + let lines = external_command cmd in
> + Unix.unlink filename;
> + lines
> +
> +let py_quote str =
> + let str = String.replace str "\\" "\\\\" in
> + let str = String.replace str "'" "\\'" in
> + "'''" ^ str ^ "'''"
> +
> +let py_bool = function true -> "True" | false -> "False"
> diff --git a/v2v/utils.mli b/v2v/utils.mli
> index 34402e28f..3a8dfd437 100644
> --- a/v2v/utils.mli
> +++ b/v2v/utils.mli
> @@ -61,3 +61,14 @@ val find_file_in_tar : string -> string -> int64 * int64
>
> Function raises [Not_found] if there is no such file inside [tar] and
> [Failure] if there is any error parsing the tar output. *)
> +
> +val run_python : ?python:string -> string -> string list
> +(** Run a snippet of Python code, and collect the output.
> +
> + On failure this raises an error. *)
> +
> +val py_quote : string -> string
> +(** Return a fully quoted Python string literal. *)
> +
> +val py_bool : bool -> string
> +(** Return "True" or "False". *)
> --
> 2.13.2
>
--
Tomáš Golembiovský <tgolembi at redhat.com>
More information about the Libguestfs
mailing list