diff --git a/rpm/__init__.py b/rpm/__init__.py index d9e5582..17c8e6b 100644 --- a/rpm/__init__.py +++ b/rpm/__init__.py @@ -20,6 +20,10 @@ logger = logging.getLogger(PROJECT_NAME) +class ShimAlreadyInitializingError(Exception): + pass + + def get_system_sitepackages() -> List[str]: """ Gets a list of sitepackages directories of system Python interpreter(s). @@ -71,7 +75,7 @@ def try_path(path: str) -> bool: return False -def init_module() -> None: +def initialize() -> None: """ Initializes the shim. Tries to load system RPM module and replace itself with it. """ @@ -81,11 +85,23 @@ def init_module() -> None: if try_path(path): logger.debug("Import successfull") return + except ShimAlreadyInitializingError: + continue except Exception as e: - logger.error(f"Exception: {e}") + logger.debug(f"Exception: {type(e)}: {e}") continue else: - raise ImportError("Failed to import system RPM module") - - -init_module() + raise ImportError( + "Failed to import system RPM module. " + "Make sure RPM Python bindings are installed on your system." + ) + + +# avoid repeated initialization of the shim module +try: + _shim_module_initializing_ +except NameError: + _shim_module_initializing_: bool = True + initialize() +else: + raise ShimAlreadyInitializingError