Skip to content

Commit 17a00de

Browse files
authored
[ty] Reuse primer commands in memory reports (#27553)
1 parent 6ea296b commit 17a00de

2 files changed

Lines changed: 50 additions & 22 deletions

File tree

‎scripts/memory_report.py‎

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import argparse
2323
import json
2424
import os
25+
import shlex
2526
import subprocess
2627
import sys
2728
import tempfile
@@ -244,36 +245,43 @@ def render_summary(projects: list[ProjectComparison]) -> str:
244245
return "\n".join(lines)
245246

246247

247-
def setup_project(*, name: str, dest: Path) -> Path:
248+
def setup_project(*, name: str, dest: Path) -> tuple[Path, list[str]]:
248249
"""Clone a project and install its mypy-primer dependencies."""
249250
project_path = dest / name
251+
setup_script = Path(__file__).with_name("setup_primer_project.py")
252+
setup_command = [
253+
"uv",
254+
"run",
255+
"--locked",
256+
"--python",
257+
sys.executable,
258+
"--script",
259+
str(setup_script),
260+
]
261+
250262
if project_path.exists():
251263
print(f"Project {name} already exists at {project_path}", file=sys.stderr)
252-
return project_path
264+
else:
265+
print(f"Setting up {name} and its dependencies...", file=sys.stderr)
266+
subprocess.run(
267+
[*setup_command, name, str(project_path)],
268+
check=True,
269+
stdout=sys.stderr,
270+
)
253271

254-
setup_script = Path(__file__).with_name("setup_primer_project.py")
255-
print(f"Setting up {name} and its dependencies...", file=sys.stderr)
256-
subprocess.run(
257-
[
258-
"uv",
259-
"run",
260-
"--locked",
261-
"--python",
262-
sys.executable,
263-
"--script",
264-
str(setup_script),
265-
name,
266-
str(project_path),
267-
],
272+
ty_command = subprocess.run(
273+
[*setup_command, "--print-ty-command", name, str(project_path)],
268274
check=True,
269-
stdout=sys.stderr,
275+
capture_output=True,
276+
text=True,
270277
)
271-
return project_path
278+
return project_path, shlex.split(ty_command.stdout)
272279

273280

274281
def run_ty_memory_check(
275282
*,
276283
ty_path: str,
284+
ty_command: list[str],
277285
project_path: Path,
278286
output_path: Path,
279287
) -> None:
@@ -283,8 +291,14 @@ def run_ty_memory_check(
283291
env["TY_MAX_PARALLELISM"] = "1" # For deterministic memory numbers
284292

285293
print(f"Running {ty_path} on {project_path.name}...", file=sys.stderr)
294+
command = [
295+
str(Path(ty_path).resolve()) if argument == "{ty}" else argument
296+
for argument in ty_command
297+
]
286298
result = subprocess.run(
287-
[ty_path, "check", "--project", str(project_path), "--exit-zero"],
299+
command,
300+
cwd=project_path,
301+
check=True,
288302
capture_output=True,
289303
text=True,
290304
env=env,
@@ -306,18 +320,24 @@ def run_memory_tests(
306320
new_reports_dir.mkdir(parents=True, exist_ok=True)
307321

308322
for project_name in KNOWN_PROJECTS:
309-
project_path = setup_project(name=project_name, dest=projects_dir)
323+
project_path, ty_command = setup_project(name=project_name, dest=projects_dir)
310324

311325
# Run old ty
312326
old_report_path = old_reports_dir / f"{project_name}.json"
313327
run_ty_memory_check(
314-
ty_path=old_ty, project_path=project_path, output_path=old_report_path
328+
ty_path=old_ty,
329+
ty_command=ty_command,
330+
project_path=project_path,
331+
output_path=old_report_path,
315332
)
316333

317334
# Run new ty
318335
new_report_path = new_reports_dir / f"{project_name}.json"
319336
run_ty_memory_check(
320-
ty_path=new_ty, project_path=project_path, output_path=new_report_path
337+
ty_path=new_ty,
338+
ty_command=ty_command,
339+
project_path=project_path,
340+
output_path=new_report_path,
321341
)
322342

323343

‎scripts/setup_primer_project.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,20 @@ def main() -> None:
9393
"--exclude-newer",
9494
help="Limit dependency resolution to packages uploaded before this timestamp",
9595
)
96+
parser.add_argument(
97+
"--print-ty-command",
98+
action="store_true",
99+
help="Print the project-specific ty command without setting up the project",
100+
)
96101
args = parser.parse_args()
97102

98103
project = find_project(args.project)
99104
revision = args.revision or project.revision
100105

101106
target_dir = Path(args.directory or project.name).resolve()
107+
if args.print_ty_command:
108+
print(get_ty_command(project, ty_binary="{ty}", venv_dir=target_dir / ".venv"))
109+
return
102110

103111
# Use a full clone only when a historical ecosystem report revision must be checked out.
104112
clone_cmd = [

0 commit comments

Comments
 (0)