v2v: add optional tmpdir parameter for Python_script

Add an optional parameter for Python_script.create, to specific the
temporary directory to use instead of creating a new one.
This commit is contained in:
Pino Toscano
2019-09-19 14:26:59 +02:00
parent 977fac2012
commit b659d044f7
2 changed files with 12 additions and 6 deletions

View File

@@ -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 }