6
0
mirror of https://github.com/ChristianLight/tutor.git synced 2025-01-10 00:37:54 +00:00

fix: remove pkg_resources for python 3.12 compatibility

pkg_resources is available in python 3.12 only if setuptools is
explicitely installed, which is not always the case. We fix that by
replacing all usage of pkg_resources with importlib_resources and
importlib_metadata.

Close #966
This commit is contained in:
Abdul-Muqadim-Arbisoft 2024-01-11 18:20:04 +05:00 committed by Régis Behmo
parent 4a30c4ab35
commit d99b2feeb3
9 changed files with 44 additions and 22 deletions

View File

@ -0,0 +1 @@
[Depreciation] Replace pkg_resources with importlib_metadata and importlib_resources. (by @Danyal-Faheem)

View File

@ -6,3 +6,5 @@ mypy
pycryptodome>=3.17.0 pycryptodome>=3.17.0
pyyaml>=6.0 pyyaml>=6.0
typing-extensions>=4.4.0 typing-extensions>=4.4.0
importlib-metadata>=7.0.1
importlib-resources>=6.1.1

View File

@ -20,6 +20,10 @@ google-auth==2.23.3
# via kubernetes # via kubernetes
idna==3.4 idna==3.4
# via requests # via requests
importlib-metadata==7.0.1
# via -r requirements/base.in
importlib-resources==6.1.1
# via -r requirements/base.in
jinja2==3.1.3 jinja2==3.1.3
# via -r requirements/base.in # via -r requirements/base.in
kubernetes==28.1.0 kubernetes==28.1.0
@ -72,3 +76,7 @@ urllib3==1.26.18
# requests # requests
websocket-client==1.6.4 websocket-client==1.6.4
# via kubernetes # via kubernetes
zipp==3.17.0
# via
# importlib-metadata
# importlib-resources

View File

@ -58,14 +58,17 @@ idna==3.4
# via # via
# -r requirements/base.txt # -r requirements/base.txt
# requests # requests
importlib-metadata==6.8.0 importlib-metadata==7.0.1
# via # via
# -r requirements/base.txt
# build # build
# keyring # keyring
# pyinstaller # pyinstaller
# twine # twine
importlib-resources==6.1.1 importlib-resources==6.1.1
# via keyring # via
# -r requirements/base.txt
# keyring
isort==5.12.0 isort==5.12.0
# via pylint # via pylint
jaraco-classes==3.3.0 jaraco-classes==3.3.0
@ -232,6 +235,7 @@ wheel==0.41.2
# via pip-tools # via pip-tools
zipp==3.17.0 zipp==3.17.0
# via # via
# -r requirements/base.txt
# importlib-metadata # importlib-metadata
# importlib-resources # importlib-resources

View File

@ -42,8 +42,12 @@ idna==3.4
# requests # requests
imagesize==1.4.1 imagesize==1.4.1
# via sphinx # via sphinx
importlib-metadata==6.8.0 importlib-metadata==7.0.1
# via sphinx # via
# -r requirements/base.txt
# sphinx
importlib-resources==6.1.1
# via -r requirements/base.txt
jinja2==3.1.3 jinja2==3.1.3
# via # via
# -r requirements/base.txt # -r requirements/base.txt
@ -153,4 +157,7 @@ websocket-client==1.6.4
# -r requirements/base.txt # -r requirements/base.txt
# kubernetes # kubernetes
zipp==3.17.0 zipp==3.17.0
# via importlib-metadata # via
# -r requirements/base.txt
# importlib-metadata
# importlib-resources

View File

@ -1,7 +1,7 @@
# -*- mode: python -*- # -*- mode: python -*-
import importlib import importlib
import os import os
import pkg_resources from importlib_metadata
block_cipher = None block_cipher = None
@ -10,10 +10,10 @@ hidden_imports = []
# Auto-discover plugins and include patches & templates folders # Auto-discover plugins and include patches & templates folders
for entrypoint_version in ["tutor.plugin.v0", "tutor.plugin.v1"]: for entrypoint_version in ["tutor.plugin.v0", "tutor.plugin.v1"]:
for entrypoint in pkg_resources.iter_entry_points(entrypoint_version): for entrypoint in importlib_metadata.entry_points(group=entrypoint_version):
plugin_name = entrypoint.name plugin_name = entrypoint.name
try: try:
plugin = entrypoint.load() plugin = importlib.import_module(entrypoint.value)
except Exception as e: except Exception as e:
print(f"ERROR Failed to load plugin {plugin_name}: {e}") print(f"ERROR Failed to load plugin {plugin_name}: {e}")
continue continue

View File

@ -7,13 +7,13 @@ import typing as t
from copy import deepcopy from copy import deepcopy
import jinja2 import jinja2
import pkg_resources import importlib_resources
from tutor import exceptions, fmt, hooks, plugins, utils from tutor import exceptions, fmt, hooks, plugins, utils
from tutor.__about__ import __app__, __version__ from tutor.__about__ import __app__, __version__
from tutor.types import Config, ConfigValue from tutor.types import Config, ConfigValue
TEMPLATES_ROOT = pkg_resources.resource_filename("tutor", "templates") TEMPLATES_ROOT = str(importlib_resources.files("tutor") / "templates")
VERSION_FILENAME = "version" VERSION_FILENAME = "version"
BIN_FILE_EXTENSIONS = [ BIN_FILE_EXTENSIONS = [
".ico", ".ico",

View File

@ -5,7 +5,7 @@ import typing as t
from glob import glob from glob import glob
import click import click
import pkg_resources import importlib_metadata
from tutor import env, exceptions, fmt, hooks, serialize from tutor import env, exceptions, fmt, hooks, serialize
from tutor.__about__ import __app__ from tutor.__about__ import __app__
@ -246,12 +246,12 @@ class EntrypointPlugin(BasePlugin):
ENTRYPOINT = "tutor.plugin.v0" ENTRYPOINT = "tutor.plugin.v0"
def __init__(self, entrypoint: pkg_resources.EntryPoint) -> None: def __init__(self, entrypoint: importlib_metadata.EntryPoint) -> None:
self.loader: pkg_resources.EntryPoint self.loader: importlib_metadata.EntryPoint = entrypoint
super().__init__(entrypoint.name, entrypoint) super().__init__(entrypoint.name, entrypoint)
def _load_obj(self) -> None: def _load_obj(self) -> None:
self.obj = self.loader.load() self.obj = importlib.import_module(self.loader.value)
def _version(self) -> t.Optional[str]: def _version(self) -> t.Optional[str]:
if not self.loader.dist: if not self.loader.dist:
@ -260,12 +260,11 @@ class EntrypointPlugin(BasePlugin):
@classmethod @classmethod
def discover_all(cls) -> None: def discover_all(cls) -> None:
for entrypoint in pkg_resources.iter_entry_points(cls.ENTRYPOINT): entrypoints = importlib_metadata.entry_points(group=cls.ENTRYPOINT)
for entrypoint in entrypoints:
try: try:
error: t.Optional[str] = None error: t.Optional[str] = None
cls(entrypoint) cls(entrypoint)
except pkg_resources.VersionConflict as e:
error = e.report()
except Exception as e: # pylint: disable=broad-except except Exception as e: # pylint: disable=broad-except
error = str(e) error = str(e)
if error: if error:

View File

@ -2,7 +2,7 @@ import importlib.util
import os import os
from glob import glob from glob import glob
import pkg_resources import importlib_metadata
from tutor import hooks from tutor import hooks
@ -26,7 +26,7 @@ def _discover_entrypoint_plugins() -> None:
""" """
with hooks.Contexts.PLUGINS.enter(): with hooks.Contexts.PLUGINS.enter():
if "TUTOR_IGNORE_ENTRYPOINT_PLUGINS" not in os.environ: if "TUTOR_IGNORE_ENTRYPOINT_PLUGINS" not in os.environ:
for entrypoint in pkg_resources.iter_entry_points("tutor.plugin.v1"): for entrypoint in importlib_metadata.entry_points(group="tutor.plugin.v1"):
discover_package(entrypoint) discover_package(entrypoint)
@ -56,7 +56,7 @@ def discover_module(path: str) -> None:
spec.loader.exec_module(module) spec.loader.exec_module(module)
def discover_package(entrypoint: pkg_resources.EntryPoint) -> None: def discover_package(entrypoint: importlib_metadata.EntryPoint) -> None:
""" """
Install a plugin from a python package. Install a plugin from a python package.
""" """
@ -68,10 +68,11 @@ def discover_package(entrypoint: pkg_resources.EntryPoint) -> None:
# Add plugin information # Add plugin information
if entrypoint.dist is None: if entrypoint.dist is None:
raise ValueError(f"Could not read plugin version: {name}") raise ValueError(f"Could not read plugin version: {name}")
hooks.Filters.PLUGINS_INFO.add_item((name, entrypoint.dist.version)) dist_version = entrypoint.dist.version if entrypoint.dist else "Unknown"
hooks.Filters.PLUGINS_INFO.add_item((name, dist_version))
# Import module on enable # Import module on enable
@hooks.Actions.PLUGIN_LOADED.add() @hooks.Actions.PLUGIN_LOADED.add()
def load(plugin_name: str) -> None: def load(plugin_name: str) -> None:
if name == plugin_name: if name == plugin_name:
entrypoint.load() importlib.import_module(entrypoint.value)