[libvirt PATCH 26/33] ci: helper: Add a required_deps higher order helper/decorator

Erik Skultety posted 33 patches 2 years, 5 months ago
There is a newer version of this series
[libvirt PATCH 26/33] ci: helper: Add a required_deps higher order helper/decorator
Posted by Erik Skultety 2 years, 5 months ago
Since we'll depend on GitPython for repo cloning, we need to make sure
to emit a user friendly error if the module is not installed. This
patch introduces a helper which future patches will use as a decorator.
Inspiration for this helper has been taken out of lcitool where we use
an identical helper for this purpose.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
---
 ci/helper | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/ci/helper b/ci/helper
index 60fdaed5f4..780f4161f5 100755
--- a/ci/helper
+++ b/ci/helper
@@ -14,6 +14,28 @@ import textwrap
 import util
 
 
+def required_deps(*deps):
+    module2pkg = {
+        "git": "GitPython"
+    }
+
+    def inner_decorator(func):
+        def wrapped(*args, **kwargs):
+            cmd = func.__name__[len('_action_'):]
+            for dep in deps:
+                try:
+                    import importlib
+                    importlib.import_module(dep)
+                except ImportError:
+                    pkg = module2pkg[dep]
+                    msg = f"'{pkg}' not found (required by the '{cmd}' command)"
+                    print(msg, file=sys.stderr)
+                    sys.exit(1)
+            func(*args, **kwargs)
+        return wrapped
+    return inner_decorator
+
+
 class Parser:
     def __init__(self):
         # Options that are common to all actions that use containers
-- 
2.41.0
Re: [libvirt PATCH 26/33] ci: helper: Add a required_deps higher order helper/decorator
Posted by Daniel P. Berrangé 2 years, 5 months ago
On Fri, Aug 25, 2023 at 07:55:34PM +0200, Erik Skultety wrote:
> Since we'll depend on GitPython for repo cloning, we need to make sure
> to emit a user friendly error if the module is not installed. This
> patch introduces a helper which future patches will use as a decorator.
> Inspiration for this helper has been taken out of lcitool where we use
> an identical helper for this purpose.
> 
> Signed-off-by: Erik Skultety <eskultet@redhat.com>
> ---
>  ci/helper | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|