This commit is contained in:
lz_db
2026-10-16 22:48:30 +08:00
parent 190de72571
commit 0b36fd3383
3161 changed files with 12787 additions and 77274 deletions
@@ -37,63 +37,57 @@ For the purposes of this module, the following nomenclature is used:
"""
from __future__ import annotations
import itertools
import os
from collections.abc import Iterable, Iterator, Mapping
from fnmatch import fnmatchcase
from glob import glob
from pathlib import Path
from typing import TYPE_CHECKING, ClassVar
from typing import (
TYPE_CHECKING,
Callable,
Dict,
Iterable,
Iterator,
List,
Mapping,
Optional,
Tuple,
Union
)
import _distutils_hack.override # noqa: F401
from ._path import StrPath
from distutils import log
from distutils.util import convert_path
if TYPE_CHECKING:
from setuptools import Distribution
_Path = Union[str, os.PathLike]
_Filter = Callable[[str], bool]
StrIter = Iterator[str]
chain_iter = itertools.chain.from_iterable
if TYPE_CHECKING:
from setuptools import Distribution # noqa
def _valid_name(path: StrPath) -> bool:
def _valid_name(path: _Path) -> bool:
# Ignore invalid names that cannot be imported directly
return os.path.basename(path).isidentifier()
class _Filter:
"""
Given a list of patterns, create a callable that will be true only if
the input matches at least one of the patterns.
"""
def __init__(self, *patterns: str) -> None:
self._patterns = dict.fromkeys(patterns)
def __call__(self, item: str) -> bool:
return any(fnmatchcase(item, pat) for pat in self._patterns)
def __contains__(self, item: str) -> bool:
return item in self._patterns
class _Finder:
"""Base class that exposes functionality for module/package finders"""
ALWAYS_EXCLUDE: ClassVar[tuple[str, ...]] = ()
DEFAULT_EXCLUDE: ClassVar[tuple[str, ...]] = ()
ALWAYS_EXCLUDE: Tuple[str, ...] = ()
DEFAULT_EXCLUDE: Tuple[str, ...] = ()
@classmethod
def find(
cls,
where: StrPath = '.',
where: _Path = '.',
exclude: Iterable[str] = (),
include: Iterable[str] = ('*',),
) -> list[str]:
include: Iterable[str] = ('*',)
) -> List[str]:
"""Return a list of all Python items (packages or modules, depending on
the finder implementation) found within directory 'where'.
@@ -117,17 +111,23 @@ class _Finder:
return list(
cls._find_iter(
convert_path(str(where)),
_Filter(*cls.ALWAYS_EXCLUDE, *exclude),
_Filter(*include),
cls._build_filter(*cls.ALWAYS_EXCLUDE, *exclude),
cls._build_filter(*include),
)
)
@classmethod
def _find_iter(
cls, where: StrPath, exclude: _Filter, include: _Filter
) -> Iterator[str]:
def _find_iter(cls, where: _Path, exclude: _Filter, include: _Filter) -> StrIter:
raise NotImplementedError
@staticmethod
def _build_filter(*patterns: str) -> _Filter:
"""
Given a list of patterns, return a callable that will be true only if
the input matches at least one of the patterns.
"""
return lambda name: any(fnmatchcase(name, pat) for pat in patterns)
class PackageFinder(_Finder):
"""
@@ -137,9 +137,7 @@ class PackageFinder(_Finder):
ALWAYS_EXCLUDE = ("ez_setup", "*__pycache__")
@classmethod
def _find_iter(
cls, where: StrPath, exclude: _Filter, include: _Filter
) -> Iterator[str]:
def _find_iter(cls, where: _Path, exclude: _Filter, include: _Filter) -> StrIter:
"""
All the packages found in 'where' that pass the 'include' filter, but
not the 'exclude' filter.
@@ -162,23 +160,19 @@ class PackageFinder(_Finder):
if include(package) and not exclude(package):
yield package
# Early pruning if there is nothing else to be scanned
if f"{package}*" in exclude or f"{package}.*" in exclude:
continue
# Keep searching subdirectories, as there may be more packages
# down there, even if the parent was excluded.
dirs.append(dir)
@staticmethod
def _looks_like_package(path: StrPath, _package_name: str) -> bool:
def _looks_like_package(path: _Path, _package_name: str) -> bool:
"""Does a directory look like a package?"""
return os.path.isfile(os.path.join(path, '__init__.py'))
class PEP420PackageFinder(PackageFinder):
@staticmethod
def _looks_like_package(_path: StrPath, _package_name: str) -> bool:
def _looks_like_package(_path: _Path, _package_name: str) -> bool:
return True
@@ -188,9 +182,7 @@ class ModuleFinder(_Finder):
"""
@classmethod
def _find_iter(
cls, where: StrPath, exclude: _Filter, include: _Filter
) -> Iterator[str]:
def _find_iter(cls, where: _Path, exclude: _Filter, include: _Filter) -> StrIter:
for file in glob(os.path.join(where, "*.py")):
module, _ext = os.path.splitext(os.path.basename(file))
@@ -211,13 +203,11 @@ class FlatLayoutPackageFinder(PEP420PackageFinder):
_EXCLUDE = (
"ci",
"bin",
"debian",
"doc",
"docs",
"documentation",
"manpages",
"news",
"newsfragments",
"changelog",
"test",
"tests",
@@ -244,7 +234,6 @@ class FlatLayoutPackageFinder(PEP420PackageFinder):
"benchmarks",
"exercise",
"exercises",
"htmlcov", # Coverage.py
# ---- Hidden directories/Private packages ----
"[._]*",
)
@@ -253,7 +242,7 @@ class FlatLayoutPackageFinder(PEP420PackageFinder):
"""Reserved package names"""
@staticmethod
def _looks_like_package(_path: StrPath, package_name: str) -> bool:
def _looks_like_package(_path: _Path, package_name: str) -> bool:
names = package_name.split('.')
# Consider PEP 561
root_pkg_is_valid = names[0].isidentifier() or names[0].endswith("-stubs")
@@ -290,7 +279,7 @@ class FlatLayoutModuleFinder(ModuleFinder):
"""Reserved top-level module names"""
def _find_packages_within(root_pkg: str, pkg_dir: StrPath) -> list[str]:
def _find_packages_within(root_pkg: str, pkg_dir: _Path) -> List[str]:
nested = PEP420PackageFinder.find(pkg_dir)
return [root_pkg] + [".".join((root_pkg, n)) for n in nested]
@@ -300,7 +289,7 @@ class ConfigDiscovery:
(from other metadata/options, the file system or conventions)
"""
def __init__(self, distribution: Distribution) -> None:
def __init__(self, distribution: "Distribution"):
self.dist = distribution
self._called = False
self._disabled = False
@@ -323,19 +312,17 @@ class ConfigDiscovery:
self._skip_ext_modules = True
@property
def _root_dir(self) -> StrPath:
def _root_dir(self) -> _Path:
# The best is to wait until `src_root` is set in dist, before using _root_dir.
return self.dist.src_root or os.curdir
@property
def _package_dir(self) -> dict[str, str]:
def _package_dir(self) -> Dict[str, str]:
if self.dist.package_dir is None:
return {}
return self.dist.package_dir
def __call__(
self, force: bool = False, name: bool = True, ignore_ext_modules: bool = False
):
def __call__(self, force=False, name=True, ignore_ext_modules=False):
"""Automatically discover missing configuration fields
and modifies the given ``distribution`` object in-place.
@@ -364,8 +351,7 @@ class ConfigDiscovery:
self.dist.packages is not None
or self.dist.py_modules is not None
or ext_modules
or hasattr(self.dist, "configuration")
and self.dist.configuration
or hasattr(self.dist, "configuration") and self.dist.configuration
# ^ Some projects use numpy.distutils.misc_util.Configuration
)
@@ -456,7 +442,7 @@ class ConfigDiscovery:
self._ensure_no_accidental_inclusion(self.dist.py_modules, "modules")
return bool(self.dist.py_modules)
def _ensure_no_accidental_inclusion(self, detected: list[str], kind: str):
def _ensure_no_accidental_inclusion(self, detected: List[str], kind: str):
if len(detected) > 1:
from inspect import cleandoc
@@ -479,13 +465,13 @@ class ConfigDiscovery:
"""
raise PackageDiscoveryError(cleandoc(msg))
def analyse_name(self) -> None:
def analyse_name(self):
"""The packages/modules are the essential contribution of the author.
Therefore the name of the distribution can be derived from them.
"""
if self.dist.metadata.name or self.dist.name:
# get_name() is not reliable (can return "UNKNOWN")
return
return None
log.debug("No `name` configuration, performing automatic discovery")
@@ -496,7 +482,7 @@ class ConfigDiscovery:
if name:
self.dist.metadata.name = name
def _find_name_single_package_or_module(self) -> str | None:
def _find_name_single_package_or_module(self) -> Optional[str]:
"""Exactly one module or package"""
for field in ('packages', 'py_modules'):
items = getattr(self.dist, field, None) or []
@@ -506,7 +492,7 @@ class ConfigDiscovery:
return None
def _find_name_from_packages(self) -> str | None:
def _find_name_from_packages(self) -> Optional[str]:
"""Try to find the root package that is not a PEP 420 namespace"""
if not self.dist.packages:
return None
@@ -523,7 +509,7 @@ class ConfigDiscovery:
return None
def remove_nested_packages(packages: list[str]) -> list[str]:
def remove_nested_packages(packages: List[str]) -> List[str]:
"""Remove nested packages from a list of packages.
>>> remove_nested_packages(["a", "a.b1", "a.b2", "a.b1.c1"])
@@ -541,7 +527,7 @@ def remove_nested_packages(packages: list[str]) -> list[str]:
return top_level
def remove_stubs(packages: list[str]) -> list[str]:
def remove_stubs(packages: List[str]) -> List[str]:
"""Remove type stubs (:pep:`561`) from a list of packages.
>>> remove_stubs(["a", "a.b", "a-stubs", "a-stubs.b.c", "b", "c-stubs"])
@@ -551,13 +537,13 @@ def remove_stubs(packages: list[str]) -> list[str]:
def find_parent_package(
packages: list[str], package_dir: Mapping[str, str], root_dir: StrPath
) -> str | None:
packages: List[str], package_dir: Mapping[str, str], root_dir: _Path
) -> Optional[str]:
"""Find the parent package that is not a namespace."""
packages = sorted(packages, key=len)
common_ancestors = []
for i, name in enumerate(packages):
if not all(n.startswith(f"{name}.") for n in packages[i + 1 :]):
if not all(n.startswith(f"{name}.") for n in packages[i+1:]):
# Since packages are sorted by length, this condition is able
# to find a list of all common ancestors.
# When there is divergence (e.g. multiple root packages)
@@ -575,7 +561,7 @@ def find_parent_package(
def find_package_path(
name: str, package_dir: Mapping[str, str], root_dir: StrPath
name: str, package_dir: Mapping[str, str], root_dir: _Path
) -> str:
"""Given a package name, return the path where it should be found on
disk, considering the ``package_dir`` option.
@@ -608,7 +594,7 @@ def find_package_path(
return os.path.join(root_dir, *parent.split("/"), *parts)
def construct_package_dir(packages: list[str], package_path: StrPath) -> dict[str, str]:
def construct_package_dir(packages: List[str], package_path: _Path) -> Dict[str, str]:
parent_pkgs = remove_nested_packages(packages)
prefix = Path(package_path).parts
return {pkg: "/".join([*prefix, *pkg.split(".")]) for pkg in parent_pkgs}