authored_source_inventory.py (3350B)
1 """One authored inventory for standalone quality, tests and the size ratchet.""" 2 3 from __future__ import annotations 4 5 import argparse 6 import os 7 import sys 8 from pathlib import Path 9 10 SWIFT_ROOTS = ( 11 Path("Tera"), 12 Path("TeraPublicAPITests"), 13 Path("TeraTests"), 14 Path("TeraUITests"), 15 Path("scripts/legacy_fixture_writers"), 16 ) 17 PYTHON_ROOT = Path("scripts") 18 GENERATED_SWIFT = Path("Tera/Generated") 19 IGNORED_DIRECTORIES = frozenset( 20 ("__pycache__", ".venv", ".pytest_cache", ".ruff_cache", ".mypy_cache") 21 ) 22 23 24 class InventoryError(ValueError): 25 """An authored source root cannot be inventoried safely.""" 26 27 28 def _walk_error(error: OSError) -> None: 29 raise InventoryError("authored source inventory cannot be read") from error 30 31 32 def _regular(path: Path) -> Path: 33 if path.is_symlink() or not path.is_file(): 34 raise InventoryError("authored source is not a regular file") 35 return path 36 37 38 def _directories(root: Path, current: Path, names: list[str]) -> list[str]: 39 retained = [] 40 for name in sorted(names): 41 path = current / name 42 if name in IGNORED_DIRECTORIES or path.relative_to(root) == GENERATED_SWIFT: 43 continue 44 if path.is_symlink(): 45 raise InventoryError("authored source directory is a link") 46 retained.append(name) 47 return retained 48 49 50 def _files(root: Path, relative: Path, suffix: str) -> list[Path]: 51 directory = root / relative 52 if directory.is_symlink() or not directory.is_dir(): 53 raise InventoryError("authored source root is unavailable") 54 found = [] 55 for current, directories, filenames in os.walk(directory, onerror=_walk_error): 56 current = Path(current) 57 directories[:] = _directories(root, current, directories) 58 found.extend( 59 _regular(current / name) 60 for name in sorted(filenames) 61 if name.endswith(suffix) 62 ) 63 return found 64 65 66 def source_files(root: Path) -> tuple[list[Path], list[Path]]: 67 swift = [_regular(root / "Package.swift")] 68 for relative in SWIFT_ROOTS: 69 swift.extend(_files(root, relative, ".swift")) 70 python = _files(root, PYTHON_ROOT, ".py") 71 if not python: 72 raise InventoryError("authored Python source inventory is empty") 73 return sorted(swift), sorted(python) 74 75 76 def command(root: Path, lane: str) -> list[str]: 77 _, python = source_files(root) 78 paths = [path.relative_to(root).as_posix() for path in python] 79 if lane == "test": 80 tests = [path for path in paths if Path(path).name.startswith("test_")] 81 if not tests: 82 raise InventoryError("authored test inventory is empty") 83 return [sys.executable, "-m", "unittest", *tests] 84 arguments = ["format", "--check"] if lane == "format" else ["check"] 85 return ["ruff", *arguments, *paths] 86 87 88 def main() -> int: 89 parser = argparse.ArgumentParser(description=__doc__) 90 parser.add_argument("lane", choices=("format", "lint", "test")) 91 arguments = parser.parse_args() 92 root = Path(__file__).resolve().parent.parent 93 try: 94 argv = command(root, arguments.lane) 95 except (InventoryError, OSError) as error: 96 print(f"authored inventory: {error}", file=sys.stderr) 97 return 1 98 os.chdir(root) 99 os.execvp(argv[0], argv) 100 return 1 101 102 103 if __name__ == "__main__": 104 raise SystemExit(main())