Migrate from pkg_resources to importlib (#3232)

This commit is contained in:
Raphael Michel
2023-04-18 12:46:13 +02:00
committed by GitHub
parent ff86fcf000
commit 2427421945
4 changed files with 30 additions and 31 deletions

View File

@@ -24,9 +24,11 @@ import sys
from enum import Enum from enum import Enum
from typing import List from typing import List
import importlib_metadata as metadata
from django.apps import AppConfig, apps from django.apps import AppConfig, apps
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from packaging.requirements import Requirement
class PluginType(Enum): class PluginType(Enum):
@@ -81,12 +83,11 @@ class PluginConfig(AppConfig, metaclass=PluginConfigMeta):
raise ImproperlyConfigured("A pretix plugin config should have a PretixPluginMeta inner class.") 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": if hasattr(self.PretixPluginMeta, 'compatibility') and not os.environ.get("PRETIX_IGNORE_CONFLICTS") == "True":
import pkg_resources req = Requirement(self.PretixPluginMeta.compatibility)
try: requirement_version = metadata.version(req.name)
pkg_resources.require(self.PretixPluginMeta.compatibility) if not req.specifier.contains(requirement_version, prereleases=True):
except pkg_resources.VersionConflict as e:
print("Incompatible plugins found!") print("Incompatible plugins found!")
print("Plugin {} requires you to have {}, but you installed {}.".format( print("Plugin {} requires you to have {}, but you installed {}.".format(
self.name, e.req, e.dist self.name, req, requirement_version
)) ))
sys.exit(1) sys.exit(1)

View File

@@ -31,8 +31,7 @@
# Unless required by applicable law or agreed to in writing, software distributed under the Apache License 2.0 is # Unless required by applicable law or agreed to in writing, software distributed under the Apache License 2.0 is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under the License. # License for the specific language governing permissions and limitations under the License.
import importlib_metadata as metadata
import pkg_resources
from django.contrib import messages from django.contrib import messages
from django.http import JsonResponse from django.http import JsonResponse
from django.shortcuts import get_object_or_404, redirect, reverse from django.shortcuts import get_object_or_404, redirect, reverse
@@ -132,14 +131,14 @@ class LicenseCheckView(StaffMemberRequiredMixin, FormView):
if not d: if not d:
d['source_notice'] = 'pretix (AGPLv3 with additional terms): https://github.com/pretix/pretix' d['source_notice'] = 'pretix (AGPLv3 with additional terms): https://github.com/pretix/pretix'
seen = set() seen = set()
for entry_point in pkg_resources.iter_entry_points(group='pretix.plugin', name=None): for entry_point in metadata.entry_points(group='pretix.plugin'):
if entry_point.dist.key not in seen: if entry_point.dist.name not in seen:
try: try:
license, url = self._get_license_for_pkg(entry_point.dist.key) license, url = self._get_license_for_pkg(entry_point.dist.name)
except FileNotFoundError: except FileNotFoundError:
license, url = '?', '?' license, url = '?', '?'
d['source_notice'] += f'\n{entry_point.dist.key} ({license}): {url}' d['source_notice'] += f'\n{entry_point.dist.name} ({license}): {url}'
seen.add(entry_point.dist.key) seen.add(entry_point.dist.name)
return d return d
@@ -168,17 +167,15 @@ class LicenseCheckView(StaffMemberRequiredMixin, FormView):
def _get_license_for_pkg(self, pkg): def _get_license_for_pkg(self, pkg):
license, url = None, None license, url = None, None
try: try:
pkg = pkg_resources.get_distribution(pkg) pkg = metadata.distribution(pkg)
except: except:
return None, None return None, None
try: try:
for line in pkg.get_metadata_lines(pkg.PKG_INFO): for k, v in pkg.metadata.items():
if ': ' in line: if k == "License":
(k, v) = line.split(': ', 1) license = v
if k == "License": if k == "Home-page":
license = v url = v
if k == "Home-page":
url = v
except FileNotFoundError: except FileNotFoundError:
license = '?' license = '?'
url = '?' url = '?'
@@ -232,14 +229,14 @@ class LicenseCheckView(StaffMemberRequiredMixin, FormView):
'restrictions). Make sure to keep it up to date!') 'restrictions). Make sure to keep it up to date!')
)) ))
for entry_point in pkg_resources.iter_entry_points(group='pretix.plugin', name=None): for entry_point in metadata.entry_points(group='pretix.plugin'):
license, url = self._get_license_for_pkg(entry_point.dist.key) license, url = self._get_license_for_pkg(entry_point.dist.name)
if not license or not any(l in license for l in ('Apache', 'MIT', 'BSD', 'pretix Enterprise', 'GPL')): if not license or not any(l in license for l in ('Apache', 'MIT', 'BSD', 'pretix Enterprise', 'GPL')):
res.append(( res.append((
'muted', 'warning', 'muted', 'warning',
_('We found the plugin "{plugin}" with license "{license}" which this tool does not know about and ' _('We found the plugin "{plugin}" with license "{license}" which this tool does not know about and '
'therefore cannot give any recommendations.').format(plugin=entry_point.dist.key, license=license) 'therefore cannot give any recommendations.').format(plugin=entry_point.dist.name, license=license)
)) ))
continue continue
@@ -247,21 +244,21 @@ class LicenseCheckView(StaffMemberRequiredMixin, FormView):
res.append(( res.append((
'danger', 'exclamation-circle', 'danger', 'exclamation-circle',
_('You selected that you have no active pretix Enterprise licenses, but we found the following ' _('You selected that you have no active pretix Enterprise licenses, but we found the following '
'Enterprise plugin: {plugin}').format(plugin=entry_point.dist.key) 'Enterprise plugin: {plugin}').format(plugin=entry_point.dist.name)
)) ))
if not input.get('plugins_copyleft') and any(l in license for l in ('GPL',)): if not input.get('plugins_copyleft') and any(l in license for l in ('GPL',)):
res.append(( res.append((
'danger', 'exclamation-circle', 'danger', 'exclamation-circle',
_('You selected that you have no copyleft-licensed plugins installed, but we found the ' _('You selected that you have no copyleft-licensed plugins installed, but we found the '
'plugin "{plugin}" with license "{license}".').format(plugin=entry_point.dist.key, license=license) 'plugin "{plugin}" with license "{license}".').format(plugin=entry_point.dist.name, license=license)
)) ))
if not input.get('plugins_free') and any(l in license for l in ('Apache', 'MIT', 'BSD')): if not input.get('plugins_free') and any(l in license for l in ('Apache', 'MIT', 'BSD')):
res.append(( res.append((
'danger', 'exclamation-circle', 'danger', 'exclamation-circle',
_('You selected that you have no free plugins installed, but we found the ' _('You selected that you have no free plugins installed, but we found the '
'plugin "{plugin}" with license "{license}".').format(plugin=entry_point.dist.key, license=license) 'plugin "{plugin}" with license "{license}".').format(plugin=entry_point.dist.name, license=license)
)) ))
return res return res

View File

@@ -41,9 +41,9 @@ from urllib.parse import urlparse
from json import loads from json import loads
import django.conf.locale import django.conf.locale
import importlib_metadata as metadata
from django.utils.crypto import get_random_string from django.utils.crypto import get_random_string
from kombu import Queue from kombu import Queue
from pkg_resources import iter_entry_points
from pycountry import currencies from pycountry import currencies
from . import __version__ from . import __version__
@@ -401,11 +401,11 @@ except ImportError:
pass pass
PLUGINS = [] PLUGINS = []
for entry_point in iter_entry_points(group='pretix.plugin', name=None): for entry_point in metadata.entry_points(group='pretix.plugin'):
if entry_point.module_name in PRETIX_PLUGINS_EXCLUDE: if entry_point.module in PRETIX_PLUGINS_EXCLUDE:
continue continue
PLUGINS.append(entry_point.module_name) PLUGINS.append(entry_point.module)
INSTALLED_APPS.append(entry_point.module_name) INSTALLED_APPS.append(entry_point.module)
HIJACK_PERMISSION_CHECK = "hijack.permissions.superusers_and_staff" HIJACK_PERMISSION_CHECK = "hijack.permissions.superusers_and_staff"
HIJACK_INSERT_BEFORE = None HIJACK_INSERT_BEFORE = None

View File

@@ -192,6 +192,7 @@ setup(
'dnspython==2.2.*', 'dnspython==2.2.*',
'drf_ujson2==1.7.*', 'drf_ujson2==1.7.*',
'geoip2==4.*', 'geoip2==4.*',
'importlib-metadata==6.4.*', # Polyfill, we can probably drop this once we require Python 3.10+
'isoweek', 'isoweek',
'jsonschema', 'jsonschema',
'kombu==5.2.*', 'kombu==5.2.*',