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 by importlib.metadata #767

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 11 additions & 5 deletions catkin_tools/commands/catkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,38 @@
# limitations under the License.

import argparse
import functools
import importlib.metadata
import os
import sys
from datetime import date
from shlex import quote as cmd_quote

import pkg_resources

from catkin_tools.common import is_tty
from catkin_tools.config import get_verb_aliases
from catkin_tools.config import initialize_config
from catkin_tools.terminal_color import fmt
from catkin_tools.terminal_color import set_color
from catkin_tools.terminal_color import test_colors
from catkin_tools.utils import entry_points

CATKIN_COMMAND_VERB_GROUP = 'catkin_tools.commands.catkin.verbs'


@functools.lru_cache(maxsize=None)
def _get_verb_entrypoints():
return list(entry_points(group=CATKIN_COMMAND_VERB_GROUP))


def list_verbs():
verbs = []
for entry_point in pkg_resources.iter_entry_points(group=CATKIN_COMMAND_VERB_GROUP):
for entry_point in _get_verb_entrypoints():
verbs.append(entry_point.name)
return verbs


def load_verb_description(verb_name):
for entry_point in pkg_resources.iter_entry_points(group=CATKIN_COMMAND_VERB_GROUP):
for entry_point in _get_verb_entrypoints():
if entry_point.name == verb_name:
return entry_point.load()

Expand Down Expand Up @@ -184,7 +190,7 @@ def catkin_main(sysargs):
# Check for version
if '--version' in sysargs:
print('catkin_tools {} (C) 2014-{} Open Source Robotics Foundation'.format(
pkg_resources.get_distribution('catkin_tools').version,
importlib.metadata.version('catkin_tools'),
date.today().year)
)
print('catkin_tools is released under the Apache License,'
Expand Down
5 changes: 2 additions & 3 deletions catkin_tools/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .metadata import find_enclosing_workspace
from .resultspace import get_resultspace_environment
from .terminal_color import ColorMapper
from .utils import entry_points

color_mapper = ColorMapper()
clr = color_mapper.clr
Expand Down Expand Up @@ -120,9 +121,7 @@ class members and associated member functions based on available
if cls.KEYS:
return

from pkg_resources import iter_entry_points

for entry_point in iter_entry_points(group=cls.CATKIN_SPACES_GROUP):
for entry_point in entry_points(group=cls.CATKIN_SPACES_GROUP):
ep_dict = entry_point.load()
cls.STORED_KEYS.append(entry_point.name + '_space')
cls.SPACES[entry_point.name] = ep_dict
Expand Down
4 changes: 2 additions & 2 deletions catkin_tools/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import importlib.metadata
import os
import shutil

import pkg_resources
import yaml

from .common import mkdir_p
Expand Down Expand Up @@ -138,7 +138,7 @@ def migrate_metadata(workspace_path):

# Check metadata version
last_version = None
current_version = pkg_resources.require("catkin_tools")[0].version
current_version = importlib.metadata.version("catkin_tools")
version_file_path = os.path.join(metadata_root_path, 'VERSION')

# Read the VERSION file
Expand Down
10 changes: 10 additions & 0 deletions catkin_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
# limitations under the License.

import os
import sys

if sys.version_info >= (3, 10):
from importlib.metadata import entry_points
else:
import importlib.metadata

def entry_points(*, group):
for ep in importlib.metadata.entry_points().get(group, []):
yield ep


def which(program):
Expand Down
4 changes: 2 additions & 2 deletions catkin_tools/verbs/catkin_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import traceback
from queue import Queue

import pkg_resources
import yaml

try:
Expand All @@ -48,6 +47,7 @@
from catkin_tools.jobs.catkin import create_catkin_build_job
from catkin_tools.jobs.catkin import create_catkin_clean_job
from catkin_tools.jobs.catkin import get_prebuild_package
from catkin_tools.utils import entry_points

from .color import clr

Expand Down Expand Up @@ -480,7 +480,7 @@ def build_isolated_workspace(
# Get all build type plugins
build_job_creators = {
ep.name: ep.load()['create_build_job']
for ep in pkg_resources.iter_entry_points(group='catkin_tools.jobs')
for ep in entry_points(group='catkin_tools.jobs')
}

# It's a problem if there aren't any build types available
Expand Down
5 changes: 2 additions & 3 deletions catkin_tools/verbs/catkin_clean/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import traceback
from queue import Queue

import pkg_resources

from catkin_tools.terminal_color import fmt

try:
Expand All @@ -39,6 +37,7 @@
from catkin_tools.execution.controllers import ConsoleStatusController
from catkin_tools.execution.executor import execute_jobs
from catkin_tools.execution.executor import run_until_complete
from catkin_tools.utils import entry_points


def determine_packages_to_be_cleaned(context, include_dependents, packages):
Expand Down Expand Up @@ -126,7 +125,7 @@ def clean_packages(
# Get all build type plugins
clean_job_creators = {
ep.name: ep.load()['create_clean_job']
for ep in pkg_resources.iter_entry_points(group='catkin_tools.jobs')
for ep in entry_points(group='catkin_tools.jobs')
}

# It's a problem if there aren't any build types available
Expand Down
4 changes: 2 additions & 2 deletions catkin_tools/verbs/catkin_test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import traceback
from queue import Queue

import pkg_resources
from catkin_pkg.package import InvalidPackage
from catkin_pkg.packages import find_packages
from catkin_pkg.topological_order import topological_order_packages
Expand All @@ -26,6 +25,7 @@
from catkin_tools.execution.controllers import ConsoleStatusController
from catkin_tools.execution.executor import execute_jobs
from catkin_tools.execution.executor import run_until_complete
from catkin_tools.utils import entry_points


def test_workspace(
Expand Down Expand Up @@ -86,7 +86,7 @@ def test_workspace(
# Get all build type plugins
test_job_creators = {
ep.name: ep.load()['create_test_job']
for ep in pkg_resources.iter_entry_points(group='catkin_tools.jobs')
for ep in entry_points(group='catkin_tools.jobs')
}

# It's a problem if there aren't any build types available
Expand Down
3 changes: 1 addition & 2 deletions docs/development/adding_build_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Regardless of what package the ``entry_point`` is defined in, it will be defined
This entry in the ``setup.py`` places a file in the ``PYTHONPATH`` when either the ``install`` or the ``develop`` verb is given to ``setup.py``.
This file relates the key (in this case ``mybuild``) to a module and attribute (in this case ``my_package.some.module`` and ``description``).

Then the ``catkin`` command will use the ``pkg_resources`` modules to retrieve these mapping at run time.
Then the ``catkin`` command will use the ``importlib.metadata`` modules to retrieve these mapping at run time.
Any entry for the ``catkin_tools.jobs`` group must point to a ``description`` attribute of a module, where the ``description`` attribute is a ``dict``.
The ``description`` ``dict`` should take this form:

Expand Down Expand Up @@ -63,4 +63,3 @@ The signature of the factory callable should be similar to the following:
jid=package.name,
deps=dependencies,
stages=stages)

2 changes: 1 addition & 1 deletion docs/development/extending_the_catkin_command.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Regardless of what package the ``entry_point`` is defined in, it will be defined

This entry in the ``setup.py`` places a file in the ``PYTHONPATH`` when either the ``install`` or the ``develop`` verb is given to ``setup.py``.
This file relates the key (in this case ``my_verb``) to a module and attribute (in this case ``my_package.some.module`` and ``description``).
Then the ``catkin`` command will use the ``pkg_resources`` modules to retrieve these mapping at run time.
Then the ``catkin`` command will use the ``importlib.metadata`` modules to retrieve these mapping at run time.
Any entry for the ``catkin_tools.commands.catkin.verbs`` group must point to a ``description`` attribute of a module, where the ``description`` attribute is a ``dict``.
The ``description`` ``dict`` should take this form (the description from the ``build`` verb for example):

Expand Down