--- 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..66ca269b1 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) (quote filename) in + 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