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

refactor: use importlib instead of pkg_resources #1669

Open
wants to merge 5 commits into
base: master
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
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fixes:
- chore: add python versions to test (#1705)
- chore: remove python 3.8 support (#1707)
- chore: use ruff for formatting (#1706)
- refactor: use importlib to find plugins in entry_points (#1669)

v6.2.0 (2024-01-01)
-------------------
Expand Down
53 changes: 22 additions & 31 deletions errbot/repo_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import tarfile
from collections import namedtuple
from datetime import datetime, timedelta
from importlib.metadata import distribution
from os import path
from pathlib import Path
from typing import Dict, Generator, List, Optional, Sequence, Tuple
Expand Down Expand Up @@ -91,40 +92,30 @@ def check_dependencies(req_path: Path) -> Tuple[Optional[str], Sequence[str]]:
Or None, [] if everything is OK.
"""
log.debug("check dependencies of %s", req_path)
# noinspection PyBroadException
try:
from pkg_resources import get_distribution

missing_pkg = []

if not req_path.is_file():
log.debug("%s has no requirements.txt file", req_path)
return None, missing_pkg

with req_path.open() as f:
for line in f:
stripped = line.strip()
# skip empty lines.
if not stripped:
continue

# noinspection PyBroadException
try:
get_distribution(stripped)
except Exception:
missing_pkg.append(stripped)
if missing_pkg:
return (
f"You need these dependencies for {req_path}: " + ",".join(missing_pkg),
missing_pkg,
)
missing_pkg = []

if not req_path.is_file():
log.debug("%s has no requirements.txt file", req_path)
return None, missing_pkg
except Exception:
log.exception("Problem checking for dependencies.")

with req_path.open() as f:
for line in f:
stripped = line.strip()
# skip empty lines.
if not stripped:
continue

# noinspection PyBroadException
try:
distribution(stripped)
except Exception:
missing_pkg.append(stripped)
if missing_pkg:
return (
"You need to have setuptools installed for the dependency check of the plugins",
[],
f"You need these dependencies for {req_path}: " + ",".join(missing_pkg),
missing_pkg,
)
return None, missing_pkg


class BotRepoManager(StoreMixin):
Expand Down
31 changes: 27 additions & 4 deletions errbot/utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import collections
import fnmatch
import importlib.metadata
import inspect
import logging
import os
import pathlib
import re
import sys
import time
from functools import wraps
from platform import system
from typing import List, Tuple, Union

import pkg_resources
from dulwich import porcelain

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -199,9 +200,31 @@ def collect_roots(base_paths: List, file_sig: str = "*.plug") -> List:

def entry_point_plugins(group):
paths = []
for entry_point in pkg_resources.iter_entry_points(group):
ep = next(pkg_resources.iter_entry_points(group, entry_point.name))
paths.append(f"{ep.dist.module_path}/{entry_point.module_name}")

eps = importlib.metadata.entry_points()
try:
entry_points = eps.select(group=group)
except AttributeError:
# workaround to support python 3.9 and older
entry_points = eps.get(group, ())

for entry_point in entry_points:
module_name = entry_point.module
file_name = module_name.replace(".", "/") + ".py"
try:
files = entry_point.dist.files
except AttributeError:
# workaround to support python 3.9 and older
try:
files = importlib.metadata.distribution(entry_point.name).files
except importlib.metadata.PackageNotFoundError:
# entrypoint is not a distribution, so let's skip looking for files
continue

for f in files:
if file_name == str(f):
parent = str(pathlib.Path(f).resolve().parent)
paths.append(f"{parent}/{module_name}")
return paths


Expand Down
22 changes: 20 additions & 2 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# coding=utf-8
import logging
import sys

from datetime import timedelta

import pytest
Expand All @@ -11,7 +10,12 @@
from errbot.bootstrap import CORE_STORAGE, bot_config_defaults
from errbot.storage import StoreMixin
from errbot.storage.base import StoragePluginBase
from errbot.utils import version2tuple, format_timedelta, split_string_after
from errbot.utils import (
entry_point_plugins,
format_timedelta,
split_string_after,
version2tuple,
)

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -103,3 +107,17 @@ def test_split_string_after_returns_two_chunks_when_chunksize_equals_half_length
splitter = split_string_after(str_, int(len(str_) / 2))
split = [chunk for chunk in splitter]
assert ["foobar2000", "foobar2000"] == split


def test_entry_point_plugins_no_groups():
result = entry_point_plugins("does_not_exist")
assert [] == result


def test_entry_point_plugins_valid_groups():
results = entry_point_plugins("console_scripts")
match = False
for result in results:
if result.endswith("errbot/errbot.cli"):
match = True
assert match