Skip to content

Commit 15df009

Browse files
authored
Merge pull request #340 from python/feature/top-level-not-setuptools
Infer top-level names for a package
2 parents 2be3507 + 73e5d6e commit 15df009

File tree

7 files changed

+48
-2
lines changed

7 files changed

+48
-2
lines changed

CHANGES.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
v4.7.0
2+
======
3+
4+
* #330: In ``packages_distributions``, now infer top-level
5+
names from ``.files()`` when a ``top-level.txt``
6+
(Setuptools-specific metadata) is not present.
7+
18
v4.6.4
29
======
310

importlib_metadata/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,18 @@ def packages_distributions() -> Mapping[str, List[str]]:
10131013
"""
10141014
pkg_to_dist = collections.defaultdict(list)
10151015
for dist in distributions():
1016-
for pkg in (dist.read_text('top_level.txt') or '').split():
1016+
for pkg in _top_level_declared(dist) or _top_level_inferred(dist):
10171017
pkg_to_dist[pkg].append(dist.metadata['Name'])
10181018
return dict(pkg_to_dist)
1019+
1020+
1021+
def _top_level_declared(dist):
1022+
return (dist.read_text('top_level.txt') or '').split()
1023+
1024+
1025+
def _top_level_inferred(dist):
1026+
return {
1027+
f.parts[0] if len(f.parts) > 1 else f.with_suffix('').name
1028+
for f in dist.files
1029+
if f.suffix == ".py"
1030+
}

prepare/example2/example2/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def main():
2+
return "example"

prepare/example2/pyproject.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[build-system]
2+
build-backend = 'trampolim'
3+
requires = ['trampolim']
4+
5+
[project]
6+
name = 'example2'
7+
version = '1.0.0'
8+
9+
[project.scripts]
10+
example = 'example2:main'
1.14 KB
Binary file not shown.

tests/fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ class ZipFixtures:
301301
def _fixture_on_path(self, filename):
302302
pkg_file = resources.files(self.root).joinpath(filename)
303303
file = self.resources.enter_context(resources.as_file(pkg_file))
304-
assert file.name.startswith('example-'), file.name
304+
assert file.name.startswith('example'), file.name
305305
sys.path.insert(0, str(file))
306306
self.resources.callback(sys.path.pop, 0)
307307

tests/test_main.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
distributions,
1818
entry_points,
1919
metadata,
20+
packages_distributions,
2021
version,
2122
)
2223

@@ -282,3 +283,17 @@ def test_unicode_dir_on_sys_path(self):
282283
prefix=self.site_dir,
283284
)
284285
list(distributions())
286+
287+
288+
class PackagesDistributionsTest(fixtures.ZipFixtures, unittest.TestCase):
289+
def test_packages_distributions_example(self):
290+
self._fixture_on_path('example-21.12-py3-none-any.whl')
291+
assert packages_distributions()['example'] == ['example']
292+
293+
def test_packages_distributions_example2(self):
294+
"""
295+
Test packages_distributions on a wheel built
296+
by trampolim.
297+
"""
298+
self._fixture_on_path('example2-1.0.0-py3-none-any.whl')
299+
assert packages_distributions()['example2'] == ['example2']

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy