示例工程 27-pyproject-dependency-management

27-pyproject-dependency-management/04-lock-reproducibility/lock_reproducibility.py

python · 0.8 KB

"""用 uv lock --check --offline 验证锁文件与 pyproject 一致。"""
 
from __future__ import annotations
 
import shutil
import subprocess
from pathlib import Path
 
 
def uv_available() -> bool:
    return shutil.which("uv") is not None
 
 
def lock_check(root: Path) -> tuple[int, str]:
    result = subprocess.run(
        ["uv", "lock", "--check", "--offline"],
        cwd=root,
        capture_output=True,
        text=True,
        check=False,
    )
    return result.returncode, (result.stdout + result.stderr).strip()
 
 
def main() -> None:
    if not uv_available():
        print("uv: missing")
        return
    root = Path(__file__).resolve().parents[3]
    code, output = lock_check(root)
    print(f"exit: {code}")
    print(f"output: {output}")
 
 
if __name__ == "__main__":
    main()