Skip to content

Commit

Permalink
migrate to importlib.metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Cleber Rosa <[email protected]>
  • Loading branch information
clebergnu committed Jan 23, 2024
1 parent 54eac87 commit 435fa7d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
13 changes: 9 additions & 4 deletions avocado/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
# Author: Lucas Meneghel Rodrigues <[email protected]>


import importlib.metadata
import os

import pkg_resources

from avocado.core.dispatcher import InitDispatcher
from avocado.core.settings import settings as stgs
from avocado.core.streams import BUILTIN_STREAM_SETS, BUILTIN_STREAMS_DESCRIPTION
Expand Down Expand Up @@ -269,8 +268,14 @@ def initialize_plugin_infrastructure():
section="plugins", key="disable", key_type=list, default=[], help_msg=help_msg
)

kinds = list(pkg_resources.get_entry_map("avocado-framework").keys())
plugin_types = [kind[8:] for kind in kinds if kind.startswith("avocado.plugins.")]
dist = importlib.metadata.distribution("avocado-framework")
plugin_types = set(
[
i.group[8:]
for i in dist.entry_points
if i.group.startswith("avocado.plugins.")
]
)
for plugin_type in plugin_types:
help_msg = f'Execution order for "{plugin_type}" plugins'
stgs.register_option(
Expand Down
5 changes: 2 additions & 3 deletions avocado/core/extension_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
import logging
import sys

import pkg_resources

from avocado.utils import stacktrace
from avocado.utils.python import importlibmetadata

# This is also defined in avocado.core.output, but this avoids a
# circular import
Expand Down Expand Up @@ -89,7 +88,7 @@ def __init__(self, namespace, invoke_kwds=None):
invoke_kwds = {}

# load plugins
for ep in pkg_resources.iter_entry_points(self.namespace):
for ep in importlibmetadata.entry_points(self.namespace):
try:
plugin = ep.load()
obj = plugin(**invoke_kwds)
Expand Down
5 changes: 2 additions & 3 deletions avocado/core/nrunner/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import re
import sys

import pkg_resources

from avocado.core.nrunner.runnable import Runnable
from avocado.core.nrunner.task import TASK_DEFAULT_CATEGORY, Task
from avocado.utils.python import importlibmetadata


def _get_kind_options_from_executable_name():
Expand Down Expand Up @@ -227,7 +226,7 @@ def get_configuration_used_by_runners(self):
"""
config_used = []
for kind in self.RUNNABLE_KINDS_CAPABLE:
for ep in pkg_resources.iter_entry_points(
for ep in importlibmetadata.entry_points(
"avocado.plugins.runnable.runner", kind
):
try:
Expand Down
9 changes: 4 additions & 5 deletions avocado/core/nrunner/runnable.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import subprocess
import sys

import pkg_resources

from avocado.core.nrunner.config import ConfigDecoder, ConfigEncoder
from avocado.core.settings import settings
from avocado.core.utils.eggenv import get_python_path_env_if_egg
from avocado.utils.python import importlibmetadata

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -496,8 +495,8 @@ def pick_runner_module_from_entry_point_kind(kind):
:returns: a module that can be run with "python -m" or None"""
namespace = "console_scripts"
section = f"avocado-runner-{kind}"
for ep in pkg_resources.iter_entry_points(namespace, section):
return ep.module_name
for ep in importlibmetadata.entry_points(namespace, section):
return importlibmetadata.get_entry_point_module(ep)

@staticmethod
def pick_runner_class_from_entry_point_kind(kind):
Expand All @@ -510,7 +509,7 @@ def pick_runner_class_from_entry_point_kind(kind):
:returns: a class that inherits from :class:`BaseRunner` or None
"""
namespace = "avocado.plugins.runnable.runner"
for ep in pkg_resources.iter_entry_points(namespace, kind):
for ep in importlibmetadata.entry_points(namespace, kind):
try:
obj = ep.load()
return obj
Expand Down
6 changes: 4 additions & 2 deletions avocado/core/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from avocado.core.settings import settings
from avocado.core.streams import BUILTIN_STREAMS
from avocado.utils import path as utils_path
from avocado.utils.python import importlibmetadata

#: Handle cases of logging exceptions which will lead to recursion error
logging.raiseExceptions = False
Expand Down Expand Up @@ -766,10 +767,11 @@ def log_plugin_failures(failures):
config = settings.as_dict()
silenced = config.get("plugins.skip_broken_plugin_notification")
for failure in failures:
if failure[0].module_name in silenced:
module = importlibmetadata.get_entry_point_module(failure[0])
if module in silenced:
continue
if hasattr(failure[1], "__traceback__"):
str_tb = "".join(traceback.format_tb(failure[1].__traceback__))
else:
str_tb = "Traceback not available"
LOG_UI.error(msg_fmt, failure[0].module_name, repr(failure[1]), str_tb)
LOG_UI.error(msg_fmt, module, repr(failure[1]), str_tb)
13 changes: 7 additions & 6 deletions avocado/core/utils/eggenv.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import importlib.metadata
import os

import pkg_resources
import pathlib


def get_python_path_env_if_egg():
Expand All @@ -16,15 +16,16 @@ def get_python_path_env_if_egg():
:returns: environment mapping with an extra PYTHONPATH for the egg or None
:rtype: os.environ mapping or None
"""
dist = pkg_resources.get_distribution("avocado-framework")
if not (dist.location.endswith(".egg") and os.path.isfile(dist.location)):
dist = importlib.metadata.distribution("avocado-framework")
path = dist.locate_file("")
if isinstance(path, pathlib.Path)
return None

python_path = os.environ.get("PYTHONPATH", "")
python_path_entries = python_path.split(":")
if dist.location in python_path_entries:
if path.filename in python_path_entries:
return None

env = os.environ.copy()
env["PYTHONPATH"] = f"{dist.location}:{python_path}"
env["PYTHONPATH"] = f"{path.filename}:{python_path}"
return env
6 changes: 3 additions & 3 deletions avocado/core/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

__all__ = ["MAJOR", "MINOR", "VERSION"]

import pkg_resources
import importlib.metadata

try:
VERSION = pkg_resources.get_distribution("avocado-framework").version
except pkg_resources.DistributionNotFound:
VERSION = importlib.metadata.version("avocado-framework")
except importlib.metadata.PackageNotFoundError:
VERSION = "unknown.unknown"

MAJOR, MINOR = VERSION.split(".")

0 comments on commit 435fa7d

Please sign in to comment.