""" Generate requirements.lock from the current Python environment. The lock is the dependency closure of the top-level requirements in ``requirements.txt`` (including the extras requested there), so unrelated packages installed globally are not captured. Run from the project root with the project virtualenv active: python scripts/generate_lock.py > requirements.lock """ from __future__ import annotations import importlib.metadata as md import re import sys from pathlib import Path from packaging.requirements import Requirement from packaging.utils import canonicalize_name ROOT = Path(__file__).resolve().parent.parent REQUIREMENTS = ROOT / "requirements.txt" # The verified environment reports cascadeflow metadata 1.2.0 while the module # exposes the 1.1.x API; requirements.txt deliberately stays below 1.2.0. PIN_OVERRIDES = { "cascadeflow": "1.1.0", } def _read_roots() -> list[Requirement]: roots: list[Requirement] = [] for raw_line in REQUIREMENTS.read_text(encoding="utf-8").splitlines(): line = raw_line.split("#", 1)[0].strip() if not line or line.startswith("-"): continue # Strip an inline pip option such as ``--index-url``; none are present # today, but this keeps the parser tolerant. line = re.sub(r"\s+--.*$", "", line).strip() try: roots.append(Requirement(line)) except Exception as exc: # pragma: no cover - malformed requirements print(f"# skipping unparseable requirement {line!r}: {exc}", file=sys.stderr) return roots def _closure(roots: list[Requirement]) -> dict[str, str]: """Walk installed metadata from ``roots`` through their dependencies.""" resolved: dict[str, str] = {} stack: list[tuple[str, frozenset[str]]] = [ (req.name, frozenset(req.extras)) for req in roots ] seen: set[tuple[str, frozenset[str]]] = set() while stack: name, extras = stack.pop() key_name = canonicalize_name(name) key = (key_name, extras) if key in seen: continue seen.add(key) try: dist = md.distribution(name) except md.PackageNotFoundError: print(f"# WARNING: {name} is not installed", file=sys.stderr) continue canonical = canonicalize_name(dist.metadata["Name"]) resolved[canonical] = PIN_OVERRIDES.get(canonical, dist.version) # Base requirements always apply; extra requirements apply only when # that extra was requested by a parent. for raw in dist.requires or []: try: req = Requirement(raw) except Exception: # pragma: no cover continue if req.marker is not None: active = list(extras) + [""] if not any(req.marker.evaluate({"extra": extra}) for extra in active): continue stack.append((req.name, frozenset(req.extras))) return resolved def main() -> None: resolved = _closure(_read_roots()) print("# Sentinel-AI verified Python dependency lock") print("# Generated from the tested Windows / Python 3.11.5 environment.") print("# Install with:") print("# pip install -r requirements.lock --extra-index-url https://download.pytorch.org/whl/cu126") print("# Regenerate with: python scripts/generate_lock.py > requirements.lock") print() for name in sorted(resolved): print(f"{name}=={resolved[name]}") if __name__ == "__main__": main()