Skip to content

Commit f27c7d4

Browse files
committed
Merge branch 'feature/pep517-metadata' into 'master'
Add routine for loading metadata from a source package. See merge request python-devs/importlib_metadata!113
2 parents 95a187b + e6fa017 commit f27c7d4

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

importlib_metadata/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,17 @@ def _discover_resolvers():
242242
)
243243
return filter(None, declared)
244244

245+
@classmethod
246+
def _local(cls, root='.'):
247+
from pep517 import build, meta
248+
system = build.compat_system(root)
249+
builder = functools.partial(
250+
meta.build,
251+
source_dir=root,
252+
system=system,
253+
)
254+
return PathDistribution(zipp.Path(meta.build_as_zip(builder)))
255+
245256
@property
246257
def metadata(self):
247258
"""Return the parsed metadata for this Distribution.

importlib_metadata/docs/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
v1.6.1
66
======
77

8+
* Added ``Distribution._local()`` as a provisional
9+
demonstration of how to load metadata for a local
10+
package. Implicitly requires that
11+
`pep517 <https://pypi.org/project/pep517>`_ is
12+
installed. Ref #42.
813
* Ensure inputs to FastPath are Unicode. Closes #121.
914
* Tests now rely on ``importlib.resources.files`` (and
1015
backport) instead of the older ``path`` function.

importlib_metadata/tests/fixtures.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ def setUp(self):
166166
build_files(EggInfoFile.files, prefix=self.site_dir)
167167

168168

169+
class LocalPackage:
170+
files = {
171+
"setup.py": """
172+
import setuptools
173+
setuptools.setup(name="local-pkg", version="2.0.1")
174+
""",
175+
}
176+
177+
def setUp(self):
178+
self.fixtures = contextlib.ExitStack()
179+
self.addCleanup(self.fixtures.close)
180+
self.fixtures.enter_context(tempdir_as_cwd())
181+
build_files(self.files)
182+
183+
169184
def build_files(file_defs, prefix=pathlib.Path()):
170185
"""Build a set of files/directories, as described by the
171186

importlib_metadata/tests/test_api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,10 @@ def test_distribution_at_str(self):
174174
dist_info_path = self.site_dir / 'distinfo_pkg-1.0.0.dist-info'
175175
dist = Distribution.at(str(dist_info_path))
176176
assert dist.version == '1.0.0'
177+
178+
179+
class LocalProjectTests(fixtures.LocalPackage, unittest.TestCase):
180+
def test_find_local(self):
181+
dist = Distribution._local()
182+
assert dist.metadata['Name'] == 'local-pkg'
183+
assert dist.version == '2.0.1'

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ universal=1
5555
testing =
5656
importlib_resources>=1.3; python_version < "3.9"
5757
packaging
58+
pep517
5859
docs =
5960
sphinx
6061
rst.linker

0 commit comments

Comments
 (0)