[Libguestfs] [PATCH 4/5] dib: require a Python interpreter

Pino Toscano ptoscano at redhat.com
Wed Mar 22 10:19:49 UTC 2017


d-i-b 2.0 is written in Python, and thus it passes the information about
the interpreter used for it (sys.executable) to the scripts that need
it; this mechanism replaces the old discovery of what is the default
Python interpreter in the system.

Since we are not Python-based, look for 'python' and use it as default
interpreter, with the --python command line option to set a different
one.

The only place where it is used so far are extra-data.d scripts; in case
it will be used also in other out-of-chroot phases, a different solution
will be needed.
---
 dib/cmdline.ml   | 21 ++++++++++++++++++++-
 dib/cmdline.mli  |  1 +
 dib/dib.ml       | 10 +++++++++-
 dib/virt-dib.pod | 11 +++++++++++
 4 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/dib/cmdline.ml b/dib/cmdline.ml
index 875f617..2cc1722 100644
--- a/dib/cmdline.ml
+++ b/dib/cmdline.ml
@@ -54,6 +54,7 @@ type cmdline = {
   arch : string;
   envvars : string list;
   checksum : bool;
+  python : string option;
 }
 
 let parse_cmdline () =
@@ -151,6 +152,9 @@ read the man page virt-dib(1).
 
   let checksum = ref false in
 
+  let python = ref None in
+  let set_python arg = python := Some arg in
+
   let argspec = [
     [ S 'p'; L"element-path" ],           Getopt.String ("path", append_element_path),  s_"Add new a elements location";
     [ L"exclude-element" ], Getopt.String ("element", append_excluded_element),
@@ -167,6 +171,7 @@ read the man page virt-dib(1).
     [ L"extra-packages" ], Getopt.String ("pkg,...", append_extra_packages),
       s_"Add extra packages to install";
     [ L"checksum" ],   Getopt.Set checksum,          s_"Generate MD5 and SHA256 checksum files";
+    [ L"python" ],     Getopt.String ("python", set_python),         s_"Set Python interpreter";
 
     [ L"ramdisk" ],    Getopt.Set is_ramdisk,        "Switch to a ramdisk build";
     [ L"ramdisk-element" ], Getopt.Set_string ("name", ramdisk_element), s_"Main element for building ramdisks";
@@ -223,6 +228,7 @@ read the man page virt-dib(1).
   let machine_readable = !machine_readable in
   let extra_packages = List.rev !extra_packages in
   let checksum = !checksum in
+  let python = !python in
 
   (* No elements and machine-readable mode?  Print some facts. *)
   if elements = [] && machine_readable then (
@@ -246,6 +252,19 @@ read the man page virt-dib(1).
   if elements = [] then
     error (f_"at least one distribution root element must be specified");
 
+  let python =
+    match python with
+    | Some exe ->
+      let p =
+        if Filename.is_relative exe then
+          get_required_tool exe
+        else (
+          Unix.access exe [Unix.X_OK];
+          exe
+        ) in
+      Some p
+    | None -> None in
+
   { debug = debug; basepath = basepath; elements = elements;
     excluded_elements = excluded_elements; element_paths = element_paths;
     excluded_scripts = excluded_scripts; use_base = use_base; drive = drive;
@@ -256,5 +275,5 @@ read the man page virt-dib(1).
     extra_packages = extra_packages; memsize = memsize; network = network;
     smp = smp; delete_on_failure = delete_on_failure;
     formats = formats; arch = arch; envvars = envvars;
-    checksum = checksum;
+    checksum = checksum; python = python;
   }
diff --git a/dib/cmdline.mli b/dib/cmdline.mli
index acfce5a..5c82efd 100644
--- a/dib/cmdline.mli
+++ b/dib/cmdline.mli
@@ -46,6 +46,7 @@ type cmdline = {
   arch : string;
   envvars : string list;
   checksum : bool;
+  python : string option;
 }
 
 val parse_cmdline : unit -> cmdline
diff --git a/dib/dib.ml b/dib/dib.ml
index 1df9aff..ab5481a 100644
--- a/dib/dib.ml
+++ b/dib/dib.ml
@@ -74,6 +74,7 @@ let envvars_string l =
 
 let prepare_external ~envvars ~dib_args ~dib_vars ~out_name ~root_label
   ~rootfs_uuid ~image_cache ~arch ~network ~debug ~fs_type ~checksum
+  ~python
   destdir libdir fakebindir loaded_elements all_elements element_paths =
   let network_string = if network then "" else "1" in
   let checksum_string = if checksum then "1" else "" in
@@ -123,6 +124,7 @@ export TMP_DIR=\"${TMPDIR}\"
 export DIB_DEBUG_TRACE=%d
 export FS_TYPE=%s
 export DIB_CHECKSUM=%s
+export DIB_PYTHON_EXEC=%s
 
 elinfo_out=$(<${VIRT_DIB_OURPATH}/elinfo_out)
 eval \"$elinfo_out\"
@@ -158,7 +160,8 @@ $target_dir/$script
     (quote dib_vars)
     debug
     fs_type
-    checksum_string in
+    checksum_string
+    python in
   write_script (destdir // "run-part-extra.sh") run_extra;
   let elinfo_out = sprintf "\
 function get_image_element_array {
@@ -524,6 +527,10 @@ let main () =
     error (f_"the specified base path is not the diskimage-builder library");
 
   (* Check for required tools. *)
+  let python =
+    match cmdline.python with
+    | None -> get_required_tool "python"
+    | Some exe -> exe in
   require_tool "uuidgen";
   Output_format.check_formats_prerequisites cmdline.formats;
   if cmdline.checksum then
@@ -654,6 +661,7 @@ let main () =
                    ~network:cmdline.network ~debug
                    ~fs_type:cmdline.fs_type
                    ~checksum:cmdline.checksum
+                   ~python
                    tmpdir cmdline.basepath
                    (auxtmpdir // "fake-bin")
                    loaded_elements all_elements cmdline.element_paths;
diff --git a/dib/virt-dib.pod b/dib/virt-dib.pod
index be7550a..5c1423e 100644
--- a/dib/virt-dib.pod
+++ b/dib/virt-dib.pod
@@ -354,6 +354,17 @@ to debug failures to run scripts.
 The default is to delete the output files if virt-dib fails (or,
 for example, some script that it runs fails).
 
+=item B<--python> PYTHON
+
+Specify a different Python interpreter to use.  Parts of
+C<diskimage-builder> are implemented in Python, and thus an
+interpreter is needed.
+
+C<PYTHON> can either be an executable filename (e.g. F<python2>,
+which is then searched in C<$PATH>), or a full path (e.g.
+F</usr/bin/python2>).  If not specified, the default value is
+F<python>.
+
 =item B<-q>
 
 =item B<--quiet>
-- 
2.9.3




More information about the Libguestfs mailing list