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 19a93a8
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 52 deletions.
16 changes: 0 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,6 @@ jobs:
mv dist/avocado_framework-*egg /tmp
python3 setup.py clean --all
python3 -c 'import sys; import glob; sys.path.insert(0, glob.glob("/tmp/avocado_framework-*.egg")[0]); from avocado.core.main import main; sys.exit(main())' run /bin/true
cd /tmp
python3 -c 'import sys; from pkg_resources import require; require("avocado-framework"); from avocado.core.main import main; sys.exit(main())' run /bin/true
egg_task_fedora_38:

Expand All @@ -279,8 +277,6 @@ jobs:
mv dist/avocado_framework-*egg /tmp
python3 setup.py clean --all
python3 -c 'import sys; import glob; sys.path.insert(0, glob.glob("/tmp/avocado_framework-*.egg")[0]); from avocado.core.main import main; sys.exit(main())' run /bin/true
cd /tmp
python3 -c 'import sys; from pkg_resources import require; require("avocado-framework"); from avocado.core.main import main; sys.exit(main())' run /bin/true
egg_task_ubi_8:

Expand All @@ -299,8 +295,6 @@ jobs:
mv dist/avocado_framework-*egg /tmp
python3 setup.py clean --all
python3 -c 'import sys; import glob; sys.path.insert(0, glob.glob("/tmp/avocado_framework-*.egg")[0]); from avocado.core.main import main; sys.exit(main())' run /bin/true
cd /tmp
python3 -c 'import sys; from pkg_resources import require; require("avocado-framework"); from avocado.core.main import main; sys.exit(main())' run /bin/true
egg_task_ubi_9:

Expand All @@ -317,8 +311,6 @@ jobs:
mv dist/avocado_framework-*egg /tmp
python3 setup.py clean --all
python3 -c 'import sys; import glob; sys.path.insert(0, glob.glob("/tmp/avocado_framework-*.egg")[0]); from avocado.core.main import main; sys.exit(main())' run /bin/true
cd /tmp
python3 -c 'import sys; from pkg_resources import require; require("avocado-framework"); from avocado.core.main import main; sys.exit(main())' run /bin/true
egg_task_debian_12:

Expand All @@ -337,8 +329,6 @@ jobs:
mv dist/avocado_framework-*egg /tmp
python3 setup.py clean --all
python3 -c 'import sys; import glob; sys.path.insert(0, glob.glob("/tmp/avocado_framework-*.egg")[0]); from avocado.core.main import main; sys.exit(main())' run /bin/true
cd /tmp
python3 -c 'import sys; from pkg_resources import require; require("avocado-framework"); from avocado.core.main import main; sys.exit(main())' run /bin/true
egg_task_debian_11:

Expand All @@ -357,8 +347,6 @@ jobs:
mv dist/avocado_framework-*egg /tmp
python3 setup.py clean --all
python3 -c 'import sys; import glob; sys.path.insert(0, glob.glob("/tmp/avocado_framework-*.egg")[0]); from avocado.core.main import main; sys.exit(main())' run /bin/true
cd /tmp
python3 -c 'import sys; from pkg_resources import require; require("avocado-framework"); from avocado.core.main import main; sys.exit(main())' run /bin/true
egg_task_ubuntu_22:

Expand All @@ -377,8 +365,6 @@ jobs:
mv dist/avocado_framework-*egg /tmp
python3 setup.py clean --all
python3 -c 'import sys; import glob; sys.path.insert(0, glob.glob("/tmp/avocado_framework-*.egg")[0]); from avocado.core.main import main; sys.exit(main())' run /bin/true
cd /tmp
python3 -c 'import sys; from pkg_resources import require; require("avocado-framework"); from avocado.core.main import main; sys.exit(main())' run /bin/true
egg_task_ubuntu_20:

Expand All @@ -397,8 +383,6 @@ jobs:
mv dist/avocado_framework-*egg /tmp
python3 setup.py clean --all
python3 -c 'import sys; import glob; sys.path.insert(0, glob.glob("/tmp/avocado_framework-*.egg")[0]); from avocado.core.main import main; sys.exit(main())' run /bin/true
cd /tmp
python3 -c 'import sys; from pkg_resources import require; require("avocado-framework"); from avocado.core.main import main; sys.exit(main())' run /bin/true
podman_egg_task:

Expand Down
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.root.filename in python_path_entries:
return None

env = os.environ.copy()
env["PYTHONPATH"] = f"{dist.location}:{python_path}"
env["PYTHONPATH"] = f"{path.root.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(".")
7 changes: 3 additions & 4 deletions avocado/plugins/runners/exec_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import importlib.metadata
import multiprocessing
import os
import shutil
import subprocess
import sys
import tempfile

import pkg_resources

from avocado.core.nrunner.app import BaseRunnerApp
from avocado.core.nrunner.runner import BaseRunner

Expand Down Expand Up @@ -81,8 +80,8 @@ def _get_avocado_version():
"""Return the Avocado package version, if installed"""
version = "unknown.unknown"
try:
version = pkg_resources.get_distribution("avocado-framework").version
except pkg_resources.DistributionNotFound:
version = importlib.metadata.version("avocado-framework")
except importlib.metadata.PackageNotFoundError:
pass
return version

Expand Down
6 changes: 3 additions & 3 deletions optional_plugins/robot/tests/resolver.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import importlib.metadata
import os
import unittest

import avocado_robot.robot
import pkg_resources

from avocado.core.resolver import ReferenceResolutionResult

Expand All @@ -17,9 +17,9 @@ def python_module_available(module_name):
:rtype: bool
"""
try:
pkg_resources.require(module_name)
_ = importlib.metadata.version(module_name)
return True
except pkg_resources.DistributionNotFound:
except importlib.metadata.PackageNotFoundError:
return False


Expand Down
6 changes: 3 additions & 3 deletions selftests/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import importlib.metadata
import logging
import os
import sys
import tempfile
import unittest

import pkg_resources

#: The base directory for the avocado source tree
BASEDIR = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -24,9 +24,9 @@ def python_module_available(module_name):
:rtype: bool
"""
try:
pkg_resources.require(module_name)
_ = importlib.metadata.version(module_name)
return True
except pkg_resources.DistributionNotFound:
except importlib.metadata.PackageNotFoundError:
return False


Expand Down

0 comments on commit 19a93a8

Please sign in to comment.