Skip to content

Commit 2d6cc80

Browse files
committed
Replaced use of iter_entry_points in setuptools.dist
1 parent da71fab commit 2d6cc80

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

setuptools/dist.py

+44-18
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828

2929
from setuptools.extern import packaging
3030
from setuptools.extern import ordered_set
31-
from setuptools.extern.more_itertools import unique_everseen
31+
from setuptools.extern.more_itertools import unique_everseen, always_iterable
32+
33+
from ._importlib import metadata
3234

3335
from . import SetuptoolsDeprecationWarning
3436

@@ -38,7 +40,7 @@
3840
from setuptools.monkey import get_unpatched
3941
from setuptools.config import parse_configuration
4042
import pkg_resources
41-
from setuptools.extern.packaging import version
43+
from setuptools.extern.packaging import version, requirements
4244
from . import _reqs
4345

4446
if TYPE_CHECKING:
@@ -450,7 +452,7 @@ def __init__(self, attrs=None):
450452
self.patch_missing_pkg_info(attrs)
451453
self.dependency_links = attrs.pop('dependency_links', [])
452454
self.setup_requires = attrs.pop('setup_requires', [])
453-
for ep in pkg_resources.iter_entry_points('distutils.setup_keywords'):
455+
for ep in metadata.entry_points(group='distutils.setup_keywords'):
454456
vars(self).setdefault(ep.name, None)
455457
_Distribution.__init__(
456458
self,
@@ -720,7 +722,10 @@ def warn_dash_deprecation(self, opt, section):
720722
return opt
721723

722724
underscore_opt = opt.replace('-', '_')
723-
commands = distutils.command.__all__ + self._setuptools_commands()
725+
commands = list(itertools.chain(
726+
distutils.command.__all__,
727+
self._setuptools_commands(),
728+
))
724729
if (
725730
not section.startswith('options')
726731
and section != 'metadata'
@@ -738,9 +743,8 @@ def warn_dash_deprecation(self, opt, section):
738743

739744
def _setuptools_commands(self):
740745
try:
741-
dist = pkg_resources.get_distribution('setuptools')
742-
return list(dist.get_entry_map('distutils.commands'))
743-
except pkg_resources.DistributionNotFound:
746+
return metadata.distribution('setuptools').entry_points.names
747+
except metadata.PackageNotFoundError:
744748
# during bootstrapping, distribution doesn't exist
745749
return []
746750

@@ -839,7 +843,7 @@ def finalize_options(self):
839843
def by_order(hook):
840844
return getattr(hook, 'order', 0)
841845

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

862866
def _finalize_setup_keywords(self):
863-
for ep in pkg_resources.iter_entry_points('distutils.setup_keywords'):
867+
for ep in metadata.entry_points(group='distutils.setup_keywords'):
864868
value = getattr(self, ep.name, None)
865869
if value is not None:
866-
ep.require(installer=self.fetch_build_egg)
870+
self._install_dependencies(ep)
867871
ep.load()(self, ep.name, value)
868872

873+
def _install_dependencies(self, ep):
874+
"""
875+
Given an entry point, ensure that any declared extras for
876+
its distribution are installed.
877+
"""
878+
reqs = {
879+
req
880+
for req in map(requirements.Requirement, always_iterable(ep.dist.requires))
881+
for extra in ep.extras
882+
if extra in req.extras
883+
}
884+
missing = itertools.filterfalse(self._is_installed, reqs)
885+
for req in missing:
886+
# fetch_build_egg expects pkg_resources.Requirement
887+
self.fetch_build_egg(pkg_resources.Requirement(str(req)))
888+
889+
def _is_installed(self, req):
890+
try:
891+
dist = metadata.distribution(req.name)
892+
except metadata.PackageNotFoundError:
893+
return False
894+
found_ver = packaging.version.Version(dist.version())
895+
return found_ver in req.specifier
896+
869897
def get_egg_cache_dir(self):
870898
egg_cache_dir = os.path.join(os.curdir, '.eggs')
871899
if not os.path.exists(egg_cache_dir):
@@ -896,27 +924,25 @@ def get_command_class(self, command):
896924
if command in self.cmdclass:
897925
return self.cmdclass[command]
898926

899-
eps = pkg_resources.iter_entry_points('distutils.commands', command)
927+
eps = metadata.entry_points(group='distutils.commands', name=command)
900928
for ep in eps:
901-
ep.require(installer=self.fetch_build_egg)
929+
self._install_dependencies(ep)
902930
self.cmdclass[command] = cmdclass = ep.load()
903931
return cmdclass
904932
else:
905933
return _Distribution.get_command_class(self, command)
906934

907935
def print_commands(self):
908-
for ep in pkg_resources.iter_entry_points('distutils.commands'):
936+
for ep in metadata.entry_points(group='distutils.commands'):
909937
if ep.name not in self.cmdclass:
910-
# don't require extras as the commands won't be invoked
911-
cmdclass = ep.resolve()
938+
cmdclass = ep.load()
912939
self.cmdclass[ep.name] = cmdclass
913940
return _Distribution.print_commands(self)
914941

915942
def get_command_list(self):
916-
for ep in pkg_resources.iter_entry_points('distutils.commands'):
943+
for ep in metadata.entry_points(group='distutils.commands'):
917944
if ep.name not in self.cmdclass:
918-
# don't require extras as the commands won't be invoked
919-
cmdclass = ep.resolve()
945+
cmdclass = ep.load()
920946
self.cmdclass[ep.name] = cmdclass
921947
return _Distribution.get_command_list(self)
922948

0 commit comments

Comments
 (0)