Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #37377 - Add interop method for find_module #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sbernhard
Copy link
Member

@sbernhard sbernhard commented Apr 22, 2024

In python2, only module "imp" exists.
In python3 the importlib module exists to check if a module exists.

@ianballou
Copy link
Member

Mind if we get a redmine on here?

@sbernhard sbernhard changed the title Add interop method for find_module Fixes #37377 - Add interop method for find_module Apr 22, 2024
Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which supported platforms still have Python 2? Isn't that jut EL 6 & 7? Or is there also a SLES with Python 2?

I was also thinking about refactoring the whole code to something like this:

MODULES = {
    'dnf': 'katello.tracer.dnf',
    'yum': 'katello.tracer.yum',
    'apt': 'katello.tracer.deb',
    'zypp_plugin': 'katello.tracer.zypper',
}

for mod, collector in MODULES.items():
    if importlib.util.find_spec(mod):
        importlib.import_module(collector, 'collect_apps')
        break
else:
    def collect_apps():
        raise NotImplementedError("Couldn't detect package manager. Failed to query affected apps!")

There are several problems with this:

  • importlib isn't available everywhere
  • Only in modern Python 3 is the order of a dictionary guaranteed
  • It assumes a katello.tracer.yum is created

The first you could capture with the compatibility code, similar to what you wrote. The second could be resolved by renaming deb and zypper to match the top level name and use a simple list. The last is simple.

If only EL 6 & 7 are on Python 2, then you can also conclude that importlib's absence implies you have yum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants