an iterator over instances of the ``Distribution`` abstract class. This
method must have the signature::
- def find_distributions(name=None, path=sys.path):
+ def find_distributions(name=None, path=None):
"""Return an iterable of all Distribution instances capable of
loading the metadata for packages matching the name
(or all names if not supplied) along the paths in the list
class EntryPoint(collections.namedtuple('EntryPointBase', 'name value group')):
- """An entry point as defined by Python packaging conventions."""
+ """An entry point as defined by Python packaging conventions.
+
+ See `the packaging docs on entry points
+ <https://packaging.python.org/specifications/entry-points/>`_
+ for more information.
+ """
pattern = re.compile(
r'(?P<module>[\w.]+)\s*'
)
return filter(None, declared)
- @classmethod
- def find_local(cls):
- dists = itertools.chain.from_iterable(
- resolver(path=['.'])
- for resolver in cls._discover_resolvers()
- )
- dist, = dists
- return dist
-
@property
def metadata(self):
"""Return the parsed metadata for this Distribution.
@abc.abstractmethod
def find_distributions(self, name=None, path=None):
"""
+ Find distributions.
+
Return an iterable of all Distribution instances capable of
- loading the metadata for packages matching the name
+ loading the metadata for packages matching the ``name``
(or all names if not supplied) along the paths in the list
of directories ``path`` (defaults to sys.path).
"""
return Distribution.discover()
-def local_distribution():
- """Get the ``Distribution`` instance for the package in CWD.
-
- :return: A ``Distribution`` instance (or subclass thereof).
- """
- return Distribution.find_local()
-
-
def metadata(package):
"""Get the metadata for the package.
class SiteDir:
+ def setUp(self):
+ self.fixtures = ExitStack()
+ self.addCleanup(self.fixtures.close)
+ self.site_dir = self.fixtures.enter_context(tempdir())
+
+
+class OnSysPath:
@staticmethod
@contextlib.contextmanager
- def site_dir():
- with tempdir() as tmp:
- sys.path[:0] = [str(tmp)]
- try:
- yield tmp
- finally:
- sys.path.remove(str(tmp))
+ def add_sys_path(dir):
+ sys.path[:0] = [str(dir)]
+ try:
+ yield
+ finally:
+ sys.path.remove(str(dir))
def setUp(self):
- self.fixtures = ExitStack()
- self.addCleanup(self.fixtures.close)
- self.site_dir = self.fixtures.enter_context(self.site_dir())
+ super(OnSysPath, self).setUp()
+ self.fixtures.enter_context(self.add_sys_path(self.site_dir))
-class DistInfoPkg(SiteDir):
+class DistInfoPkg(OnSysPath, SiteDir):
files = {
"distinfo_pkg-1.0.0.dist-info": {
"METADATA": """
build_files(DistInfoPkg.files, self.site_dir)
-class EggInfoPkg(SiteDir):
+class DistInfoPkgOffPath(SiteDir):
+ def setUp(self):
+ super(DistInfoPkgOffPath, self).setUp()
+ build_files(DistInfoPkg.files, self.site_dir)
+
+
+class EggInfoPkg(OnSysPath, SiteDir):
files = {
"egginfo_pkg.egg-info": {
"PKG-INFO": """
build_files(EggInfoPkg.files, prefix=self.site_dir)
-class EggInfoFile(SiteDir):
+class EggInfoFile(OnSysPath, SiteDir):
files = {
"egginfo_file.egg-info": """
Metadata-Version: 1.0
build_files(EggInfoFile.files, prefix=self.site_dir)
-class LocalPackage:
- def setUp(self):
- self.fixtures = ExitStack()
- self.addCleanup(self.fixtures.close)
- self.fixtures.enter_context(tempdir_as_cwd())
- build_files(EggInfoPkg.files)
-
-
def build_files(file_defs, prefix=pathlib.Path()):
"""Build a set of files/directories, as described by the
assert ep.load() is importlib.metadata
-class NameNormalizationTests(fixtures.SiteDir, unittest.TestCase):
+class NameNormalizationTests(
+ fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
@staticmethod
def pkg_with_dashes(site_dir):
"""
assert version(pkg_name.upper()) == '1.0'
-class NonASCIITests(fixtures.SiteDir, unittest.TestCase):
+class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
@staticmethod
def pkg_with_non_ascii_description(site_dir):
"""
assert all(
isinstance(dist, Distribution)
for dist in dists
- ), dists
+ )
assert any(
dist.metadata['Name'] == 'egginfo-pkg'
for dist in dists
import re
import textwrap
import unittest
+import itertools
from collections.abc import Iterator
from . import fixtures
from importlib.metadata import (
Distribution, PackageNotFoundError, distribution,
- entry_points, files, local_distribution, metadata, requires, version,
+ entry_points, files, metadata, requires, version,
)
assert deps == expected
-class LocalProjectTests(fixtures.LocalPackage, unittest.TestCase):
- def test_find_local(self):
- dist = local_distribution()
- assert dist.metadata['Name'] == 'egginfo-pkg'
+class OffSysPathTests(fixtures.DistInfoPkgOffPath, unittest.TestCase):
+ def test_find_distributions_specified_path(self):
+ dists = itertools.chain.from_iterable(
+ resolver(path=[str(self.site_dir)])
+ for resolver in Distribution._discover_resolvers()
+ )
+ assert any(
+ dist.metadata['Name'] == 'distinfo-pkg'
+ for dist in dists
+ )
egg = self.resources.enter_context(
path(self.root, 'example-21.12-py3.6.egg'))
sys.path.insert(0, str(egg))
- print('***', sys.path)
self.resources.callback(sys.path.pop, 0)
def test_files(self):
--- /dev/null
+Versions/Current/Resources
\ No newline at end of file