From 96be8c7eb57740f429da75932d57ffe0ac83811d Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Tue, 8 Sep 2026 16:46:19 -0700 Subject: [PATCH] check dependency footprint (#38825) --- scripts/lint/check_dependencies.py | 37 ++++++++++++++++++++++++++++++ scripts/lint/lint.sh | 2 ++ 2 files changed, 39 insertions(+) create mode 100755 scripts/lint/check_dependencies.py diff --git a/scripts/lint/check_dependencies.py b/scripts/lint/check_dependencies.py new file mode 100755 index 0000000000..f3707035b3 --- /dev/null +++ b/scripts/lint/check_dependencies.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +import sys +import tomllib +import importlib.metadata +from pathlib import Path + + +def main() -> int: + if sys.prefix == sys.base_prefix: + print("Dependency checks require a virtual environment. Run tools/op.sh setup first.") + return 1 + + project = tomllib.loads((Path(__file__).resolve().parents[2] / "pyproject.toml").read_text())["project"] + direct = len(project["dependencies"]) + sum(len(deps) for deps in project["optional-dependencies"].values()) + # Count each installed package once, including transitive dependencies and all extras. + packages = {dist.metadata["Name"].lower().replace("_", "-") for dist in importlib.metadata.distributions()} + # Logical file sizes avoid filesystem block-size differences. Don't follow links to the interpreter or source tree. + size = sum(path.stat().st_size for path in Path(sys.prefix).rglob("*") if not path.is_symlink() and path.is_file()) + + """ + This test prevents our depency footprint from growing. + These values are *not* intended to be increased, and we + expect to strictly drive these down over time. + """ + failed = False + for name, value, limit in ( + ("Direct dependencies (all extras)", direct, 37), + ("Total dependencies", len(packages), 65), + ("Venv size (MiB)", size / 1024**2, 550), + ): + print(f"{name}: {value:g} (limit: {limit})") + failed |= value > limit + return int(failed) + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/lint/lint.sh b/scripts/lint/lint.sh index bfd93f23a9..fe3644d670 100755 --- a/scripts/lint/lint.sh +++ b/scripts/lint/lint.sh @@ -46,6 +46,7 @@ function run_tests() { PYTHON_FILES=$2 run "ruff" ruff check openpilot --quiet + run "check_dependencies" python3 $DIR/check_dependencies.py run "check_indentation" $DIR/check_indentation.py $PYTHON_FILES run "check_added_large_files" $DIR/check_added_large_files.py --maxkb=120 $ALL_FILES run "check_shebang_scripts_are_executable" $DIR/check_shebang_scripts_are_executable.py $ALL_FILES @@ -67,6 +68,7 @@ function help() { echo "" echo -e "${BOLD}${UNDERLINE}Tests:${NC}" echo -e " ${BOLD}ruff${NC}" + echo -e " ${BOLD}check_dependencies${NC}" echo -e " ${BOLD}check_indentation${NC}" echo -e " ${BOLD}ty${NC}" echo -e " ${BOLD}codespell${NC}"