File size: 3,555 Bytes
6920e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
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()