Upload scripts/generate_lock.py with huggingface_hub
Browse files- scripts/generate_lock.py +104 -0
scripts/generate_lock.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Generate requirements.lock from the current Python environment.
|
| 3 |
+
|
| 4 |
+
The lock is the dependency closure of the top-level requirements in
|
| 5 |
+
``requirements.txt`` (including the extras requested there), so unrelated
|
| 6 |
+
packages installed globally are not captured. Run from the project root with
|
| 7 |
+
the project virtualenv active:
|
| 8 |
+
|
| 9 |
+
python scripts/generate_lock.py > requirements.lock
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
from __future__ import annotations
|
| 13 |
+
|
| 14 |
+
import importlib.metadata as md
|
| 15 |
+
import re
|
| 16 |
+
import sys
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
|
| 19 |
+
from packaging.requirements import Requirement
|
| 20 |
+
from packaging.utils import canonicalize_name
|
| 21 |
+
|
| 22 |
+
ROOT = Path(__file__).resolve().parent.parent
|
| 23 |
+
REQUIREMENTS = ROOT / "requirements.txt"
|
| 24 |
+
|
| 25 |
+
# The verified environment reports cascadeflow metadata 1.2.0 while the module
|
| 26 |
+
# exposes the 1.1.x API; requirements.txt deliberately stays below 1.2.0.
|
| 27 |
+
PIN_OVERRIDES = {
|
| 28 |
+
"cascadeflow": "1.1.0",
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def _read_roots() -> list[Requirement]:
|
| 33 |
+
roots: list[Requirement] = []
|
| 34 |
+
for raw_line in REQUIREMENTS.read_text(encoding="utf-8").splitlines():
|
| 35 |
+
line = raw_line.split("#", 1)[0].strip()
|
| 36 |
+
if not line or line.startswith("-"):
|
| 37 |
+
continue
|
| 38 |
+
# Strip an inline pip option such as ``--index-url``; none are present
|
| 39 |
+
# today, but this keeps the parser tolerant.
|
| 40 |
+
line = re.sub(r"\s+--.*$", "", line).strip()
|
| 41 |
+
try:
|
| 42 |
+
roots.append(Requirement(line))
|
| 43 |
+
except Exception as exc: # pragma: no cover - malformed requirements
|
| 44 |
+
print(f"# skipping unparseable requirement {line!r}: {exc}", file=sys.stderr)
|
| 45 |
+
return roots
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def _closure(roots: list[Requirement]) -> dict[str, str]:
|
| 49 |
+
"""Walk installed metadata from ``roots`` through their dependencies."""
|
| 50 |
+
resolved: dict[str, str] = {}
|
| 51 |
+
stack: list[tuple[str, frozenset[str]]] = [
|
| 52 |
+
(req.name, frozenset(req.extras)) for req in roots
|
| 53 |
+
]
|
| 54 |
+
seen: set[tuple[str, frozenset[str]]] = set()
|
| 55 |
+
|
| 56 |
+
while stack:
|
| 57 |
+
name, extras = stack.pop()
|
| 58 |
+
key_name = canonicalize_name(name)
|
| 59 |
+
key = (key_name, extras)
|
| 60 |
+
if key in seen:
|
| 61 |
+
continue
|
| 62 |
+
seen.add(key)
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
dist = md.distribution(name)
|
| 66 |
+
except md.PackageNotFoundError:
|
| 67 |
+
print(f"# WARNING: {name} is not installed", file=sys.stderr)
|
| 68 |
+
continue
|
| 69 |
+
|
| 70 |
+
canonical = canonicalize_name(dist.metadata["Name"])
|
| 71 |
+
resolved[canonical] = PIN_OVERRIDES.get(canonical, dist.version)
|
| 72 |
+
|
| 73 |
+
# Base requirements always apply; extra requirements apply only when
|
| 74 |
+
# that extra was requested by a parent.
|
| 75 |
+
for raw in dist.requires or []:
|
| 76 |
+
try:
|
| 77 |
+
req = Requirement(raw)
|
| 78 |
+
except Exception: # pragma: no cover
|
| 79 |
+
continue
|
| 80 |
+
|
| 81 |
+
if req.marker is not None:
|
| 82 |
+
active = list(extras) + [""]
|
| 83 |
+
if not any(req.marker.evaluate({"extra": extra}) for extra in active):
|
| 84 |
+
continue
|
| 85 |
+
|
| 86 |
+
stack.append((req.name, frozenset(req.extras)))
|
| 87 |
+
|
| 88 |
+
return resolved
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def main() -> None:
|
| 92 |
+
resolved = _closure(_read_roots())
|
| 93 |
+
print("# Sentinel-AI verified Python dependency lock")
|
| 94 |
+
print("# Generated from the tested Windows / Python 3.11.5 environment.")
|
| 95 |
+
print("# Install with:")
|
| 96 |
+
print("# pip install -r requirements.lock --extra-index-url https://download.pytorch.org/whl/cu126")
|
| 97 |
+
print("# Regenerate with: python scripts/generate_lock.py > requirements.lock")
|
| 98 |
+
print()
|
| 99 |
+
for name in sorted(resolved):
|
| 100 |
+
print(f"{name}=={resolved[name]}")
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
main()
|