From b845afb42f4dfa06f4710a00035a1b07cc6b2a3c Mon Sep 17 00:00:00 2001 From: Jan Pokorny Date: Tue, 30 Apr 2024 16:00:00 +0200 Subject: [PATCH] test: Library checking script for tests Storage role development often relies on blivet library and changes in it. Whether or not is specific feature supported by blivet is usually determined by its version. That is a cumbersome process, especially when the feature has not yet been added into blivet and the version has to be guessed. Added script verifies existence of the feature by using python introspection and asking for existence of specific item in the library (e.g. 'blivet.formats.lvmpv.LVMPhysicalVolume.grow_to_fill'). The script is supposed to be used for the tests only. --- tests/scripts/does_library_support.py | 62 +++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 tests/scripts/does_library_support.py diff --git a/tests/scripts/does_library_support.py b/tests/scripts/does_library_support.py new file mode 100755 index 00000000..2044a87e --- /dev/null +++ b/tests/scripts/does_library_support.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python + +# This script checks for blivet compatibility by trying to access +# blivet parts specified by given parameter +# Returns True if part is found, False otherwise + +# The script is meant to be a supporting tool for the storage role tests +import sys +import importlib + + +def is_supported(var): + + parts = var.split('.') + imports = '' + obj = sys.modules[__name__] + + try: + # create a variable named parts[0] so the subsequent imports work + globals()[parts[0]] = importlib.import_module(parts[0]) + except ImportError: + return False + + # try to import each part + while parts: + part = parts.pop(0) + imports += part + '.' + + try: + importlib.import_module(imports.rstrip('.')) + except ImportError: + break + + # generate access to the object for later use + obj = getattr(obj, part) + + else: + # break did not happen in the cycle + # it means the whole string was importable + return True + + # part of the string was not importable, the rest can be attributes + + # get part back to parts to simplify the following loop + parts = [part] + parts + + while parts: + part = parts.pop(0) + obj = getattr(obj, part, None) + + if obj is None: + return False + + return True + + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage: python %s " % sys.argv[0]) + sys.exit(-1) + + print(is_supported(sys.argv[1]))