|
| 1 | +"""Builds the agentex/lib force-include map per-file so test files can be pruned |
| 2 | +— force-include ignores `exclude` (hatchling #1395).""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import os |
| 7 | + |
| 8 | +from hatchling.builders.hooks.plugin.interface import BuildHookInterface |
| 9 | + |
| 10 | +_SKIP_DIRS = {"__pycache__", "tests"} |
| 11 | +_SKIP_NAMES = {"conftest.py", "pytest.ini", "run_tests.py"} |
| 12 | +# Floor below the ~333 shippable files: a collapse means the walk broke — fail |
| 13 | +# loud rather than ship a near-empty wheel. |
| 14 | +_MIN_FILES = 320 |
| 15 | + |
| 16 | + |
| 17 | +def _is_test_file(name: str) -> bool: |
| 18 | + return name in _SKIP_NAMES or (name.startswith("test_") and name.endswith(".py")) |
| 19 | + |
| 20 | + |
| 21 | +class CustomBuildHook(BuildHookInterface): |
| 22 | + PLUGIN_NAME = "custom" |
| 23 | + |
| 24 | + def initialize(self, version: str, build_data: dict) -> None: # noqa: ARG002 |
| 25 | + lib_root = os.path.normpath(os.path.join(self.root, "..", "src", "agentex", "lib")) |
| 26 | + force_include = build_data.setdefault("force_include", {}) |
| 27 | + collected = 0 |
| 28 | + for dirpath, dirnames, filenames in os.walk(lib_root): |
| 29 | + dirnames[:] = [d for d in dirnames if d not in _SKIP_DIRS] |
| 30 | + for name in filenames: |
| 31 | + if _is_test_file(name): |
| 32 | + continue |
| 33 | + src = os.path.join(dirpath, name) |
| 34 | + rel = os.path.relpath(src, lib_root) |
| 35 | + force_include[src] = os.path.join("agentex", "lib", rel) |
| 36 | + collected += 1 |
| 37 | + if collected < _MIN_FILES: |
| 38 | + raise RuntimeError( |
| 39 | + f"agentex/lib force-include collected only {collected} files " |
| 40 | + f"(expected >= {_MIN_FILES}); aborting build." |
| 41 | + ) |
0 commit comments