Skip to content

Commit

Permalink
Add fallback to infer top-level names for a distribution. Fixes #330.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Aug 14, 2021
1 parent 43afaeb commit 1a8e808
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion importlib_metadata/__init__.py
Expand Up @@ -1013,6 +1013,18 @@ def packages_distributions() -> Mapping[str, List[str]]:
"""
pkg_to_dist = collections.defaultdict(list)
for dist in distributions():
for pkg in (dist.read_text('top_level.txt') or '').split():
for pkg in _top_level_declared(dist) or _top_level_inferred(dist):
pkg_to_dist[pkg].append(dist.metadata['Name'])
return dict(pkg_to_dist)


def _top_level_declared(dist):
return (dist.read_text('top_level.txt') or '').split()


def _top_level_inferred(dist):
return {
f.parts[0] if len(f.parts) > 1 else f.with_suffix('').name
for f in dist.files
if f.suffix == ".py"
}

0 comments on commit 1a8e808

Please sign in to comment.