Skip to content

Commit

Permalink
replace entrypoints use with importlib.metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
abn committed Jun 13, 2022
1 parent c1475a0 commit c33e3c6
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 240 deletions.
26 changes: 1 addition & 25 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ cachecontrol = { version = "^0.12.9", extras = ["filecache"] }
cachy = "^0.3.0"
cleo = "^1.0.0a5"
crashtest = "^0.3.0"
entrypoints = "^0.4"
html5lib = "^1.0"
importlib-metadata = { version = "^4.4", python = "<3.10" }
# keyring uses calver, so version is unclamped
Expand Down Expand Up @@ -85,7 +84,6 @@ typing-extensions = { version = "^4.0.0", python = "<3.8" }
zipp = { version = "^3.4", python = "<3.8" }
flatdict = "^4.0.1"
mypy = ">=0.960"
types-entrypoints = ">=0.3.7"
types-html5lib = ">=1.1.7"
types-jsonschema = ">=4.4.4"
types-requests = ">=2.27.11"
Expand Down Expand Up @@ -140,8 +138,10 @@ enable_error_code = [
# warning.
[[tool.mypy.overrides]]
module = [
'poetry.console.commands.self.show.plugins',
'poetry.installation.executor',
'poetry.mixology.version_solver',
'poetry.plugins.plugin_manager',
'poetry.repositories.installed_repository',
'poetry.utils.env',
]
Expand Down
47 changes: 30 additions & 17 deletions src/poetry/console/commands/self/show/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,32 @@


if TYPE_CHECKING:
from entrypoints import EntryPoint
from poetry.core.packages.package import Package

from poetry.utils._compat import metadata


@dataclasses.dataclass
class PluginPackage:
package: Package
plugins: list[EntryPoint] = dataclasses.field(default_factory=list)
application_plugins: list[EntryPoint] = dataclasses.field(default_factory=list)
plugins: list[metadata.EntryPoint] = dataclasses.field(default_factory=list)
application_plugins: list[metadata.EntryPoint] = dataclasses.field(
default_factory=list
)

def append(self, entry_point: metadata.EntryPoint) -> None:
from poetry.plugins.application_plugin import ApplicationPlugin
from poetry.plugins.plugin import Plugin

group = entry_point.group # type: ignore[attr-defined]

if group == ApplicationPlugin.group:
self.application_plugins.append(entry_point)
elif group == Plugin.group:
self.plugins.append(entry_point)
else:
name = entry_point.name # type: ignore[attr-defined]
raise ValueError(f"Unknown plugin group ({group}) for {name}")


class SelfShowPluginsCommand(SelfCommand):
Expand All @@ -43,9 +60,6 @@ def _system_project_handle(self) -> int:
plugins: dict[str, PluginPackage] = {}

system_env = EnvManager.get_system_env(naive=True)
entry_points = PluginManager(ApplicationPlugin.group).get_plugin_entry_points(
env=system_env
) + PluginManager(Plugin.group).get_plugin_entry_points(env=system_env)
installed_repository = InstalledRepository.load(
system_env, with_dependencies=True
)
Expand All @@ -54,21 +68,20 @@ def _system_project_handle(self) -> int:
pkg.name: pkg for pkg in installed_repository.packages
}

for entry_point in entry_points:
plugin = entry_point.load()
for group in [ApplicationPlugin.group, Plugin.group]:
for entry_point in PluginManager(group).get_plugin_entry_points(
env=system_env
):
assert entry_point.dist is not None

assert entry_point.distro is not None
package = packages_by_name[canonicalize_name(entry_point.distro.name)]
package = packages_by_name[canonicalize_name(entry_point.dist.name)]

name = package.pretty_name
info = plugins.get(name) or PluginPackage(package=package)
name = package.pretty_name

if issubclass(plugin, ApplicationPlugin):
info.application_plugins.append(entry_point)
else:
info.plugins.append(entry_point)
info = plugins.get(name) or PluginPackage(package=package)
info.append(entry_point)

plugins[name] = info
plugins[name] = info

for name, info in plugins.items():
package = info.package
Expand Down
37 changes: 24 additions & 13 deletions src/poetry/plugins/plugin_manager.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from __future__ import annotations

import logging
import sys

from typing import TYPE_CHECKING

import entrypoints

from poetry.plugins.application_plugin import ApplicationPlugin
from poetry.plugins.plugin import Plugin
from poetry.utils._compat import metadata


if TYPE_CHECKING:
Expand Down Expand Up @@ -36,16 +34,29 @@ def load_plugins(self, env: Env | None = None) -> None:

plugin_entrypoints = self.get_plugin_entry_points(env=env)

for entrypoint in plugin_entrypoints:
self._load_plugin_entrypoint(entrypoint)
for ep in plugin_entrypoints:
self._load_plugin_entry_point(ep)

@staticmethod
def _is_plugin_candidate(ep: metadata.EntryPoint, env: Env | None = None) -> bool:
"""
Helper method to check if given entry point is a valid as a plugin candidate.
When an environment is specified, the entry point's associated distribution
should be installed, and discoverable in the given environment.
"""
return env is None or (
ep.dist is not None
and env.site_packages.find_distribution(ep.dist.name) is not None
)

def get_plugin_entry_points(
self, env: Env | None = None
) -> list[entrypoints.EntryPoint]:
entry_points: list[entrypoints.EntryPoint] = entrypoints.get_group_all(
self._group, path=env.sys_path if env else sys.path
)
return entry_points
) -> list[metadata.EntryPoint]:
return [
ep
for ep in metadata.entry_points(group=self._group)
if self._is_plugin_candidate(ep, env)
]

def add_plugin(self, plugin: Plugin) -> None:
if not isinstance(plugin, (Plugin, ApplicationPlugin)):
Expand All @@ -59,10 +70,10 @@ def activate(self, *args: Any, **kwargs: Any) -> None:
for plugin in self._plugins:
plugin.activate(*args, **kwargs)

def _load_plugin_entrypoint(self, entrypoint: entrypoints.EntryPoint) -> None:
logger.debug(f"Loading the {entrypoint.name} plugin")
def _load_plugin_entry_point(self, ep: metadata.EntryPoint) -> None:
logger.debug(f"Loading the {ep.name} plugin") # type: ignore[attr-defined]

plugin = entrypoint.load()
plugin = ep.load() # type: ignore[no-untyped-call]

if not issubclass(plugin, (Plugin, ApplicationPlugin)):
raise ValueError(
Expand Down
Loading

0 comments on commit c33e3c6

Please sign in to comment.