|
1 | 1 | import os |
2 | | -import subprocess |
| 2 | +import re |
| 3 | + |
3 | 4 | from importlib import import_module |
4 | 5 | from pathlib import Path |
5 | | -import re |
| 6 | +from importlib.metadata import distribution |
| 7 | + |
6 | 8 |
|
7 | 9 | import pytest |
8 | | -from pkg_resources import Requirement, get_provider |
9 | 10 |
|
10 | 11 |
|
11 | 12 | # packages that have no way to detect their importable name |
|
15 | 16 | "qtpy": None, # required dependency of jupyter-lab |
16 | 17 | } |
17 | 18 |
|
| 19 | + |
18 | 20 | def get_module_names(pkg_name): |
19 | | - """Load pkg metadata to find out its importable module name(s).""" |
| 21 | + """Load distribution to find out its importable module name(s).""" |
20 | 22 | # remove any extras |
21 | | - pkg_name = re.sub(r'\[.*\]', '', pkg_name) |
| 23 | + pkg_name = re.sub(r"\[.*\]", "", pkg_name) |
22 | 24 | modules = set() |
23 | | - provider = get_provider(Requirement.parse(pkg_name)) |
24 | 25 | # top level package name is typically all we need |
| 26 | + dist = distribution(pkg_name) |
25 | 27 | if pkg_name in BAD_PACKAGES: |
26 | 28 | name = BAD_PACKAGES[pkg_name] |
27 | | - if name is None: # unimportably package |
| 29 | + if name is None: # unimportable package |
28 | 30 | return [] |
29 | 31 | modules.add(BAD_PACKAGES[pkg_name]) |
30 | | - elif provider.has_metadata("top_level.txt"): |
31 | | - first_line = list(provider.get_metadata_lines("top_level.txt"))[0] |
32 | | - modules.add(first_line) |
| 32 | + elif top_level_names := dist.read_text("top_level.txt"): |
| 33 | + # Find the first non-_ prefixed module |
| 34 | + if first_public_name := next( |
| 35 | + ( |
| 36 | + name |
| 37 | + for name in top_level_names.strip().split("\n") |
| 38 | + if not name.startswith("_") |
| 39 | + ), |
| 40 | + None, |
| 41 | + ): |
| 42 | + modules.add(first_public_name) |
33 | 43 | else: |
34 | 44 | # badly packaged dependency, make an educated guess |
35 | 45 | name = pkg_name |
36 | 46 | if pkg_name.endswith("-cffi"): |
37 | 47 | name = pkg_name[:-5] |
38 | 48 | elif pkg_name.endswith("-py"): |
39 | 49 | name = pkg_name[:-3] |
40 | | - |
| 50 | + |
41 | 51 | modules.add(name.replace("-", "_")) |
42 | 52 |
|
43 | | - if provider.has_metadata("namespace_packages.txt"): |
44 | | - modules |= set(provider.get_metadata_lines("namespace_packages.txt")) |
| 53 | + if namespace_packages := dist.read_text("namespace_packages.txt"): |
| 54 | + modules |= set(namespace_packages.strip().split("\n")) |
45 | 55 |
|
46 | 56 | # _ prefixed modules are typically C modules and not directly importable |
47 | 57 | return [n for n in modules if n[0] != "_"] |
|
0 commit comments