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

Replace pkg_resources #232

Merged
merged 15 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions src/pretix/base/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.apps import AppConfig, apps
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
import importlib.metadata
Copy link
Member

Choose a reason for hiding this comment

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

Please move this import line to the top. Separate import lines to 3 groups as PEP8 recommendation.



class PluginType(Enum):
Expand Down Expand Up @@ -53,12 +54,38 @@ def __init__(self, *args, **kwargs):
raise ImproperlyConfigured("A pretix plugin config should have a PretixPluginMeta inner class.")

if hasattr(self.PretixPluginMeta, 'compatibility') and not os.environ.get("PRETIX_IGNORE_CONFLICTS") == "True":
import pkg_resources
try:
pkg_resources.require(self.PretixPluginMeta.compatibility)
except pkg_resources.VersionConflict as e:
print("Incompatible plugins found!")
print("Plugin {} requires you to have {}, but you installed {}.".format(
self.name, e.req, e.dist
))
sys.exit(1)
self.check_compatibility()

def check_compatibility(self):
"""
Checks for compatibility of the plugin based on specified version requirements.

This method verifies if the currently installed versions of required packages match
the versions specified in the plugin's compatibility requirements. If a version
mismatch is found or a required package is not installed, it prints an error message
and exits the program.

Steps:
1. Iterates over the compatibility requirements specified in `self.PretixPluginMeta.compatibility`.
2. For each requirement, it splits the package name and the required version.
3. Fetches the installed version of the package using `importlib.metadata.version`.
4. Compares the installed version with the required version.
5. If a mismatch is found, prints an error message and exits the program.
6. If a required package is not found, catches the `PackageNotFoundError`, prints an error message,
and exits the program.

Raises:
SystemExit: If a version conflict or missing package is detected, the program exits.
"""
try:
for requirement in self.PretixPluginMeta.compatibility:
package_name, _, required_version = requirement.partition("==")
installed_version = importlib.metadata.version(package_name)
if installed_version != required_version:
print("Incompatible plugins found!")
Copy link
Member

Choose a reason for hiding this comment

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

Don't use print. Use logger instead.

print(f"Plugin {self.name} requires you to have {package_name}=={required_version}, "
f"but you installed {package_name}=={installed_version}.")
sys.exit(1)
except importlib.metadata.PackageNotFoundError as e:
print(f"Package not found: {e}")
sys.exit(1)
13 changes: 7 additions & 6 deletions src/pretix/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.core.exceptions import ImproperlyConfigured
from django.utils.crypto import get_random_string
from kombu import Queue
from pkg_resources import iter_entry_points
import importlib_metadata
from pycountry import currencies

from . import __version__
Expand Down Expand Up @@ -321,11 +321,12 @@
pass

PLUGINS = []
for entry_point in iter_entry_points(group='pretix.plugin', name=None):
if entry_point.module_name in PRETIX_PLUGINS_EXCLUDE:
continue
PLUGINS.append(entry_point.module_name)
INSTALLED_APPS.append(entry_point.module_name)
entry_points = importlib_metadata.entry_points()

for entry_point in entry_points.select(group='pretix.plugin'):
if entry_point.module not in PRETIX_PLUGINS_EXCLUDE:
PLUGINS.append(entry_point.module)
INSTALLED_APPS.append(entry_point.module)

HIJACK_AUTHORIZE_STAFF = True

Expand Down
Loading