forked from RuyiAI-Stack/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregenerate_cpython_diffs.py
More file actions
228 lines (196 loc) · 7.49 KB
/
Copy pathregenerate_cpython_diffs.py
File metadata and controls
228 lines (196 loc) · 7.49 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python3
"""Reconstruct pristine CPython sources and regenerate test/cpython/v3_13/*.diff.
Diffs are written with `git diff --full-index` so the `index <before>..<after>`
line carries blob hashes used by the offline sync check (no manifest file).
This tool never downloads over the network. It reconstructs pristine bytes by
reverse-applying the checked-in .diff. If that fails, or if reverse-apply
would move the checked-in index <before> hash (an edit outside existing
hunks), pass a locally downloaded file via --pristine (see the error hint
for curl/wget). --force still refuses to move <before> without --pristine.
Usage:
python tools/regenerate_cpython_diffs.py # all pairs
python tools/regenerate_cpython_diffs.py --only test_bool.py
python tools/regenerate_cpython_diffs.py --check # verify only
python tools/regenerate_cpython_diffs.py --force # rewrite every .diff
python tools/regenerate_cpython_diffs.py --force --pristine /tmp/pristine.py --only test_bool.py
"""
from __future__ import annotations
import argparse
import sys
import tempfile
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from types import ModuleType
REPO_ROOT = Path(__file__).resolve().parents[1]
def _load_diff_sync() -> ModuleType:
import importlib.util
path = REPO_ROOT / "tools" / "cpython_diff_sync.py"
spec = importlib.util.spec_from_file_location("torch_cpython_diff_sync", path)
if spec is None or spec.loader is None:
raise ImportError(f"cannot load {path}")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
diff_sync = _load_diff_sync()
def _resolve_pristine(
py_path: Path,
diff_path: Path,
tag: str,
upstream: str,
pristine_path: Path | None,
) -> bytes:
"""Prefer offline reverse-apply; optional local --pristine file (no network)."""
if pristine_path is not None:
return diff_sync.normalize_bytes(pristine_path.read_bytes())
try:
return diff_sync.reverse_apply_to_pristine(py_path, diff_path)
except Exception as e:
raise RuntimeError(diff_sync.pristine_download_hint(tag, upstream)) from e
def _filter_pairs(
pairs: list[tuple[Path, Path]], only: str, cpython_dir: Path
) -> list[tuple[Path, Path]]:
"""Exact relative path, basename, or unique stem (no substring surprises)."""
exact = [
(py, diff)
for py, diff in pairs
if only == py.relative_to(cpython_dir).as_posix() or only == py.name
]
if exact:
return exact
# --only test_bool (no .py): must be unique (test_int != test_int_literal).
stem = [(py, diff) for py, diff in pairs if only == py.stem]
if len(stem) == 1:
return stem
if len(stem) > 1:
names = ", ".join(py.name for py, _ in stem)
raise ValueError(
f"--only {only!r} matches multiple files ({names}); "
f"pass an exact basename like {stem[0][0].name!r}"
)
return []
def regenerate(
only: str | None,
*,
force: bool = False,
pristine_path: Path | None = None,
) -> int:
"""Rewrite .diff files when stale (or --force). Writes only if all pairs OK."""
CPYTHON_DIR = diff_sync.CPYTHON_DIR
diff_paths = diff_sync.iter_diff_paths()
pairs = [
(p.with_suffix(".py"), p) for p in diff_paths if p.with_suffix(".py").is_file()
]
if only:
try:
pairs = _filter_pairs(pairs, only, CPYTHON_DIR)
except ValueError as e:
print(str(e), file=sys.stderr)
return 1
if not pairs:
print(f"no pairs matched --only {only!r}", file=sys.stderr)
return 1
matched = ", ".join(py.relative_to(CPYTHON_DIR).as_posix() for py, _ in pairs)
print(f"--only {only!r} matched: {matched}")
if pristine_path is not None and len(pairs) != 1:
print(
"--pristine requires exactly one pair; pass --only <test_name.py>",
file=sys.stderr,
)
return 1
planned: list[tuple[Path, str]] = []
failures = 0
rewritten = 0
kept = 0
for py_path, diff_path in pairs:
rel = py_path.relative_to(CPYTHON_DIR).as_posix()
repo_rel = py_path.relative_to(REPO_ROOT).as_posix()
try:
tag, upstream = diff_sync.parse_header(py_path)
pristine = _resolve_pristine(
py_path, diff_path, tag, upstream, pristine_path
)
adapted = diff_sync.normalize_bytes(py_path.read_bytes())
needs_rewrite = force
if not needs_rewrite:
errors = diff_sync.verify_pair(py_path, diff_path)
needs_rewrite = bool(errors)
if needs_rewrite:
if pristine_path is None:
diff_sync.check_pristine_anchor(
pristine,
diff_path.read_text(encoding="utf-8", errors="strict"),
tag,
upstream,
)
new_diff = diff_sync.make_unified_diff(pristine, adapted, repo_rel)
with tempfile.TemporaryDirectory() as tmp:
tmp_diff = Path(tmp) / "patch.diff"
diff_sync.write_utf8(tmp_diff, new_diff)
applied = diff_sync.apply_diff_to_adapted(
pristine, py_path, tmp_diff
)
if applied != adapted:
raise RuntimeError(
"regenerated diff does not reproduce adapted file"
)
planned.append((diff_path, new_diff))
rewritten += 1
print(f"REWRITE {rel} ({tag})")
else:
kept += 1
print(f"KEEP {rel} ({tag})")
except Exception as e:
failures += 1
print(f"FAIL {rel}: {e}", file=sys.stderr)
if failures:
print(
f"NOT writing diffs ({failures} failure(s)); "
f"kept={kept} rewritten={rewritten}",
file=sys.stderr,
)
return 1
for diff_path, new_diff in planned:
diff_sync.write_utf8(diff_path, new_diff)
print(f"done: kept={kept} rewritten={rewritten} failures=0")
return 0
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument(
"--only",
default=None,
help="Exact basename, relative path, or unique stem (e.g. test_bool.py)",
)
ap.add_argument(
"--force",
action="store_true",
help=(
"Rewrite every .diff even if verify already passes "
"(still refuses to move index <before> without --pristine)"
),
)
ap.add_argument(
"--pristine",
type=Path,
default=None,
help="Local pristine upstream file (no network fetch); requires --only",
)
ap.add_argument(
"--check",
action="store_true",
help="Verify sync offline via full-index lines (no network)",
)
args = ap.parse_args()
if args.check:
errors = diff_sync.verify_all()
if errors:
print("cpython diff sync check FAILED:")
for rel, err in errors:
print(f" - {rel}: {err}")
return 1
n = len(list(diff_sync.iter_diff_paths()))
print(f"OK: {n} cpython .py/.diff pairs in sync")
return 0
return regenerate(args.only, force=args.force, pristine_path=args.pristine)
if __name__ == "__main__":
sys.exit(main())