Verify dependency hashes when running Python repository tools (#62346)

## Why

Using `uv run --frozen` keeps dependency resolution fixed but does not require
installed packages to match the hashes in `uv.lock`.

## What changed

Run Ruff for the Python SDK and repository scripts, and regenerate Python SDK
types during repository schema refreshes, through a shared runner. It exports
the selected locked dependencies, installs them into a temporary virtual
environment with `uv pip sync --require-hashes`, and runs the tool without
resyncing the environment.

## Testing

Update tests to check the formatter invocations and verify that only repository
schema refreshes invoke Python type generation through the runner.

GitOrigin-RevId: 4e84da5d227eb99c24efb9a1cc7647af82105536
This commit is contained in:
colby-oai
2026-11-18 19:21:39 +00:00
committed by copyberry
parent 42cf4edfcd
commit 0e011c0092
5 changed files with 69 additions and 15 deletions
@@ -2,8 +2,9 @@
import argparse
import os
from pathlib import Path
import subprocess
import sys
from pathlib import Path
def main() -> None:
@@ -63,11 +64,10 @@ def main() -> None:
sdk_root = workspace_root.parent / "sdk" / "python"
subprocess.run(
[
"uv",
"run",
sys.executable,
str(workspace_root / "scripts" / "run_locked_python_tool.py"),
"--project",
str(sdk_root),
"--frozen",
"--only-group",
"test",
"python",
@@ -0,0 +1,47 @@
"""Run a tool with dependencies verified against its uv.lock."""
import argparse
import os
import subprocess
from pathlib import Path
from tempfile import TemporaryDirectory
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--project", required=True)
parser.add_argument("--only-group")
parser.add_argument("tool", choices=("python", "ruff"))
parser.add_argument("tool_args", nargs=argparse.REMAINDER)
args = parser.parse_args()
uv = ["uv", "--project", args.project]
group_args = ["--only-group", args.only_group] if args.only_group else []
requirements = subprocess.check_output(
[*uv, "export", "--frozen", "--no-emit-project", *group_args]
)
with TemporaryDirectory(prefix="codex-python-tool-") as temp:
venv = str(Path(temp) / ".venv")
subprocess.run([*uv, "venv", venv], check=True)
subprocess.run(
[*uv, "pip", "sync", "--require-hashes", "--python", venv, "-"],
input=requirements,
check=True,
)
tool = args.tool
if tool == "ruff":
# Fail if the locked Ruff executable is missing from the environment.
tool = str(
Path(venv) / ("Scripts/ruff.exe" if os.name == "nt" else "bin/ruff")
)
return subprocess.call(
[*uv, "run", "--no-sync", "--active", "--", tool, *args.tool_args],
env={**os.environ, "VIRTUAL_ENV": venv},
)
if __name__ == "__main__":
try:
raise SystemExit(main())
except subprocess.CalledProcessError as error:
raise SystemExit(error.returncode) from None
+11 -4
View File
@@ -12,6 +12,7 @@ from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1]
LOCKED_PYTHON_RUNNER = "codex-rs/scripts/run_locked_python_tool.py"
@dataclass(frozen=True)
@@ -116,9 +117,8 @@ def buildifier_formatter_group(*, check: bool) -> FormatterGroup:
def python_sdk_formatter_group(*, check: bool) -> FormatterGroup:
# Each `--project` retains its local dependency and Ruff configuration context.
uv_run_args = [
"uv",
"run",
"--frozen",
sys.executable,
LOCKED_PYTHON_RUNNER,
"--project",
"sdk/python",
"--only-group",
@@ -149,7 +149,14 @@ def python_sdk_formatter_group(*, check: bool) -> FormatterGroup:
def python_scripts_formatter_group(*, check: bool) -> FormatterGroup:
args = ["uv", "run", "--frozen", "--project", "scripts", "ruff", "format"]
args = [
sys.executable,
LOCKED_PYTHON_RUNNER,
"--project",
"scripts",
"ruff",
"format",
]
if check:
args.append("--check")
args.append(".")
@@ -190,18 +190,16 @@ def test_root_format_driver_covers_all_formatter_groups(
len(group.commands) for group in formatters
]
sdk_uv_run_args = (
"uv",
"run",
"--frozen",
sys.executable,
"codex-rs/scripts/run_locked_python_tool.py",
"--project",
"sdk/python",
"--only-group",
"format",
)
scripts_uv_run_args = (
"uv",
"run",
"--frozen",
sys.executable,
"codex-rs/scripts/run_locked_python_tool.py",
"--project",
"scripts",
)
+3 -1
View File
@@ -95,10 +95,12 @@ def test_schema_refresh_only_updates_python_for_repository_schemas(monkeypatch,
runpy.run_path(str(script), run_name="__main__")
assert [args[0] for args, _kwargs in calls] == (
["cargo", "uv"] if mode == "repository" else ["cargo"]
["cargo", sys.executable] if mode == "repository" else ["cargo"]
)
assert all(kwargs["check"] for _args, kwargs in calls)
if mode == "repository":
runner = ROOT.parents[1] / "codex-rs/scripts/run_locked_python_tool.py"
assert calls[1][0][1] == str(runner)
assert calls[1][0][-3:] == [
"generate-types",
"--schema-dir",