]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
ci: helper: Add a required_deps higher order helper/decorator
authorErik Skultety <eskultet@redhat.com>
Thu, 24 Aug 2023 08:28:55 +0000 (10:28 +0200)
committerErik Skultety <eskultet@redhat.com>
Tue, 12 Sep 2023 09:36:03 +0000 (11:36 +0200)
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>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
ci/helper

index 75552774f684d8c26d76e050cce2381c7fa8b302..4727145b2894db6f77f1db16b077d5336e21937a 100755 (executable)
--- 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