[libvirt] [jenkins-ci PATCH] lcitool: check for virt-install existing

This improves: $ ./lcitool install libvirt-fedora-29 ./lcitool: Failed to install 'libvirt-fedora-29': [Errno 2] No such file or directory To $ ./lcitool install libvirt-fedora-29 ./lcitool: Cannot find virt-install in $PATH Signed-off-by: Daniel P. Berrangé <berrange@redhat.com> --- guests/lcitool | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/guests/lcitool b/guests/lcitool index 0f60704..3be16c8 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -37,6 +37,23 @@ except ImportError: class Util: + @staticmethod + def which(program): + def is_exe(fpath): + return os.path.isfile(fpath) and os.access(fpath, os.X_OK) + + fpath, fname = os.path.split(program) + if fpath: + if is_exe(program): + return program + else: + for path in os.environ["PATH"].split(os.pathsep): + exe_file = os.path.join(path, program) + if is_exe(exe_file): + return exe_file + + return None + @staticmethod def get_base(): return os.path.dirname(os.path.abspath(__file__)) @@ -534,8 +551,12 @@ class Application: # a kernel argument extra_arg = "console=ttyS0 ks=file:/{}".format(install_config) + vipath = Util.which("virt-install") + if vipath is None: + raise Exception("Cannot find virt-install in $PATH") + cmd = [ - "virt-install", + vipath, "--name", host, "--location", facts["install_url"], "--virt-type", facts["install_virt_type"], -- 2.21.0

On Thursday, 2 May 2019 15:44:33 CEST Daniel P. Berrangé wrote:
This improves:
$ ./lcitool install libvirt-fedora-29 ./lcitool: Failed to install 'libvirt-fedora-29': [Errno 2] No such file or directory
To
$ ./lcitool install libvirt-fedora-29 ./lcitool: Cannot find virt-install in $PATH
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com> --- guests/lcitool | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/guests/lcitool b/guests/lcitool index 0f60704..3be16c8 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -37,6 +37,23 @@ except ImportError:
class Util:
+ @staticmethod + def which(program): + def is_exe(fpath): + return os.path.isfile(fpath) and os.access(fpath, os.X_OK) + + fpath, fname = os.path.split(program) + if fpath: + if is_exe(program): + return program + else: + for path in os.environ["PATH"].split(os.pathsep): + exe_file = os.path.join(path, program) + if is_exe(exe_file): + return exe_file + + return None
There is already shutil.which which does this, although it is only Python 3.3+. As fallback for older versions: - instead of splitting the specified program, I'd just check whether it is an absolute path (os.path.isabs()) - it seems like distutils.spawn.find_executable() can be used for this, which IMHO is better than reimplementing it -- Pino Toscano
participants (2)
-
Daniel P. Berrangé
-
Pino Toscano