From 990d75ca607597d7653dccb0d8337f40e9a7f1a6 Mon Sep 17 00:00:00 2001 From: Bernhard Suttner Date: Mon, 22 Apr 2024 18:12:12 +0200 Subject: [PATCH] Fixes #37377 - Add interop method for find_module --- src/katello/constants.py | 6 +++--- src/katello/find_module.py | 10 ++++++++++ src/katello/tracer/__init__.py | 10 +++++----- 3 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 src/katello/find_module.py diff --git a/src/katello/constants.py b/src/katello/constants.py index 63dfdf417..b0a530d4a 100644 --- a/src/katello/constants.py +++ b/src/katello/constants.py @@ -1,4 +1,4 @@ -import imp +from katello.find_module import find_module ENABLED_REPOS_CACHE_FILE = '/var/cache/katello-agent/enabled_repos.json' PACKAGE_CACHE_FILE = '/var/lib/rhsm/packages/packages.json' @@ -13,13 +13,13 @@ PROFILE_CACHE_FILE = '/var/lib/rhsm/cache/profile.json' try: - imp.find_module('zypp_plugin') + find_module('zypp_plugin') ZYPPER = True except ImportError: ZYPPER = False try: - imp.find_module('yum') + find_module('yum') YUM = True except ImportError: YUM = False diff --git a/src/katello/find_module.py b/src/katello/find_module.py new file mode 100644 index 000000000..30a73a03e --- /dev/null +++ b/src/katello/find_module.py @@ -0,0 +1,10 @@ +def find_module(module_name): + try: + import importlib + module_spec = importlib.util.find_spec(module_name) + if module_spec is None: + raise ImportError("Module not found: {}".format(module_name)) + return module_spec + except (ImportError, AttributeError): + import imp + return imp.find_module(module_name) diff --git a/src/katello/tracer/__init__.py b/src/katello/tracer/__init__.py index cc3e0ce96..125bbe1e2 100644 --- a/src/katello/tracer/__init__.py +++ b/src/katello/tracer/__init__.py @@ -1,18 +1,18 @@ from __future__ import absolute_import from katello.uep import get_uep, lookup_consumer_id import sys -import imp +from katello.find_module import find_module def collect_apps(): raise NotImplementedError("Couldn't detect package manager. Failed to query affected apps!") # RHEL based systems try: - imp.find_module('dnf') + find_module('dnf') from katello.tracer.dnf import collect_apps except ImportError: try: - imp.find_module('yum') + find_module('yum') from tracer.query import Query def collect_apps(): query = Query() @@ -22,14 +22,14 @@ def collect_apps(): # debian based systems try: - imp.find_module('apt') + find_module('apt') from katello.tracer.deb import collect_apps except ImportError: pass # SUSE based systems try: - imp.find_module('zypp_plugin') + find_module('zypp_plugin') from katello.tracer.zypper import collect_apps except ImportError: pass