[Libguestfs] [PATCH 1/2] v2v: add optional tmpdir parameter for Python_script

Pino Toscano ptoscano at redhat.com
Thu Sep 19 12:30:04 UTC 2019


Add an optional parameter for Python_script.create, to specific the
temporary directory to use instead of creating a new one.
---
 v2v/python_script.ml  | 13 ++++++++-----
 v2v/python_script.mli |  5 ++++-
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/v2v/python_script.ml b/v2v/python_script.ml
index 3159373a1..6bb14ec1f 100644
--- a/v2v/python_script.ml
+++ b/v2v/python_script.ml
@@ -31,12 +31,15 @@ type script = {
   path : string;                (* Path to script. *)
 }
 
-let create ?(name = "script.py") code =
+let create ?(name = "script.py") ?tmpdir code =
   let tmpdir =
-    let base_dir = (open_guestfs ())#get_cachedir () in
-    let t = Mkdtemp.temp_dir ~base_dir "v2v." in
-    rmdir_on_exit t;
-    t in
+    match tmpdir with
+    | None ->
+      let base_dir = (open_guestfs ())#get_cachedir () in
+      let t = Mkdtemp.temp_dir ~base_dir "v2v." in
+      rmdir_on_exit t;
+      t
+    | Some dir -> dir in
   let path = tmpdir // name in
   with_open_out path (fun chan -> output_string chan code);
   { tmpdir; path }
diff --git a/v2v/python_script.mli b/v2v/python_script.mli
index c008eec1a..496ac60d0 100644
--- a/v2v/python_script.mli
+++ b/v2v/python_script.mli
@@ -20,11 +20,14 @@
 
 type script
 
-val create : ?name:string -> string -> script
+val create : ?name:string -> ?tmpdir:string -> string -> script
 (** Create a Python script object.
 
     The optional parameter [?name] is a hint for the name of the script.
 
+    The optional parameter [?tmpdir] is the temporary directory to use
+    (instead of creating a new one).
+
     The parameter is the Python code.  Usually this is
     [Some_source.code] where [some_source.ml] is generated from
     the Python file by [v2v/embed.sh] (see also [v2v/Makefile.am]). *)
-- 
2.21.0




More information about the Libguestfs mailing list