Skip to content

Commit

Permalink
Replaced use of iter_entry_points in setuptools.dist
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Feb 6, 2022
1 parent da71fab commit 2d6cc80
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions setuptools/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

from setuptools.extern import packaging
from setuptools.extern import ordered_set
from setuptools.extern.more_itertools import unique_everseen
from setuptools.extern.more_itertools import unique_everseen, always_iterable

from ._importlib import metadata

from . import SetuptoolsDeprecationWarning

Expand All @@ -38,7 +40,7 @@
from setuptools.monkey import get_unpatched
from setuptools.config import parse_configuration
import pkg_resources
from setuptools.extern.packaging import version
from setuptools.extern.packaging import version, requirements
from . import _reqs

if TYPE_CHECKING:
Expand Down Expand Up @@ -450,7 +452,7 @@ def __init__(self, attrs=None):
self.patch_missing_pkg_info(attrs)
self.dependency_links = attrs.pop('dependency_links', [])
self.setup_requires = attrs.pop('setup_requires', [])
for ep in pkg_resources.iter_entry_points('distutils.setup_keywords'):
for ep in metadata.entry_points(group='distutils.setup_keywords'):
vars(self).setdefault(ep.name, None)
_Distribution.__init__(
self,
Expand Down Expand Up @@ -720,7 +722,10 @@ def warn_dash_deprecation(self, opt, section):
return opt

underscore_opt = opt.replace('-', '_')
commands = distutils.command.__all__ + self._setuptools_commands()
commands = list(itertools.chain(
distutils.command.__all__,
self._setuptools_commands(),
))
if (
not section.startswith('options')
and section != 'metadata'
Expand All @@ -738,9 +743,8 @@ def warn_dash_deprecation(self, opt, section):

def _setuptools_commands(self):
try:
dist = pkg_resources.get_distribution('setuptools')
return list(dist.get_entry_map('distutils.commands'))
except pkg_resources.DistributionNotFound:
return metadata.distribution('setuptools').entry_points.names
except metadata.PackageNotFoundError:
# during bootstrapping, distribution doesn't exist
return []

Expand Down Expand Up @@ -839,7 +843,7 @@ def finalize_options(self):
def by_order(hook):
return getattr(hook, 'order', 0)

defined = pkg_resources.iter_entry_points(group)
defined = metadata.entry_points(group=group)
filtered = itertools.filterfalse(self._removed, defined)
loaded = map(lambda e: e.load(), filtered)
for ep in sorted(loaded, key=by_order):
Expand All @@ -860,12 +864,36 @@ def _removed(ep):
return ep.name in removed

def _finalize_setup_keywords(self):
for ep in pkg_resources.iter_entry_points('distutils.setup_keywords'):
for ep in metadata.entry_points(group='distutils.setup_keywords'):
value = getattr(self, ep.name, None)
if value is not None:
ep.require(installer=self.fetch_build_egg)
self._install_dependencies(ep)
ep.load()(self, ep.name, value)

def _install_dependencies(self, ep):
"""
Given an entry point, ensure that any declared extras for
its distribution are installed.
"""
reqs = {
req
for req in map(requirements.Requirement, always_iterable(ep.dist.requires))
for extra in ep.extras
if extra in req.extras
}
missing = itertools.filterfalse(self._is_installed, reqs)
for req in missing:
# fetch_build_egg expects pkg_resources.Requirement
self.fetch_build_egg(pkg_resources.Requirement(str(req)))

def _is_installed(self, req):
try:
dist = metadata.distribution(req.name)
except metadata.PackageNotFoundError:
return False
found_ver = packaging.version.Version(dist.version())
return found_ver in req.specifier

def get_egg_cache_dir(self):
egg_cache_dir = os.path.join(os.curdir, '.eggs')
if not os.path.exists(egg_cache_dir):
Expand Down Expand Up @@ -896,27 +924,25 @@ def get_command_class(self, command):
if command in self.cmdclass:
return self.cmdclass[command]

eps = pkg_resources.iter_entry_points('distutils.commands', command)
eps = metadata.entry_points(group='distutils.commands', name=command)
for ep in eps:
ep.require(installer=self.fetch_build_egg)
self._install_dependencies(ep)
self.cmdclass[command] = cmdclass = ep.load()
return cmdclass
else:
return _Distribution.get_command_class(self, command)

def print_commands(self):
for ep in pkg_resources.iter_entry_points('distutils.commands'):
for ep in metadata.entry_points(group='distutils.commands'):
if ep.name not in self.cmdclass:
# don't require extras as the commands won't be invoked
cmdclass = ep.resolve()
cmdclass = ep.load()
self.cmdclass[ep.name] = cmdclass
return _Distribution.print_commands(self)

def get_command_list(self):
for ep in pkg_resources.iter_entry_points('distutils.commands'):
for ep in metadata.entry_points(group='distutils.commands'):
if ep.name not in self.cmdclass:
# don't require extras as the commands won't be invoked
cmdclass = ep.resolve()
cmdclass = ep.load()
self.cmdclass[ep.name] = cmdclass
return _Distribution.get_command_list(self)

Expand Down

0 comments on commit 2d6cc80

Please sign in to comment.