|
10 | 10 | from tools.api_guidelines.owners import owner_for |
11 | 11 | from tools.api_guidelines.rules import BASELINED_RULES |
12 | 12 | from tools.api_guidelines.scan import collect_findings |
13 | | -from tools.api_guidelines.signature import BaselineSignature, normalize_path |
| 13 | +from tools.api_guidelines.signature import BaselineSignature |
14 | 14 | from tools.flake8_plugin import ApiInputViolation |
15 | 15 |
|
16 | 16 | EXCEPTIONS_PATH = Path("api_exceptions/exceptions.json") |
@@ -184,6 +184,46 @@ def diff_entries( |
184 | 184 | return added, removed, changed |
185 | 185 |
|
186 | 186 |
|
| 187 | +def _possible_moves( |
| 188 | + removed: list[dict[str, object]], added: list[dict[str, object]] |
| 189 | +) -> list[tuple[dict[str, object], dict[str, object]]]: |
| 190 | + """Finds entries that look like they moved to another file, i.e because the file was renamed |
| 191 | + `removed` the entries a run removed |
| 192 | + `added` the entries a run added |
| 193 | + Returns (removed, added) pairs with the same rule, scope and key in different files. A moved |
| 194 | + entry starts over with the defaults, so its old status and reason need copying by hand |
| 195 | + """ |
| 196 | + # {(rule, scope, key) -> [added entries]} |
| 197 | + added_by_finding: dict[tuple[object, object, object], list[dict[str, object]]] = {} |
| 198 | + for entry in added: |
| 199 | + added_by_finding.setdefault((entry["rule"], entry["scope"], entry["key"]), []).append(entry) |
| 200 | + |
| 201 | + moves: list[tuple[dict[str, object], dict[str, object]]] = [] |
| 202 | + for old in removed: |
| 203 | + for new in added_by_finding.get((old["rule"], old["scope"], old["key"]), []): |
| 204 | + moves.append((old, new)) |
| 205 | + return moves |
| 206 | + |
| 207 | + |
| 208 | +def _rescan_path(path: str, root: Path) -> str | None: |
| 209 | + """A `--files` path as a repo-relative path in `root` |
| 210 | + `path` the path as given, i.e "src/sentry/a.py", "./src/sentry/a.py" or an absolute path inside `root` |
| 211 | + `root` the checkout being scanned |
| 212 | + Returns the path like "src/sentry/a.py", or None when it isn't a Python file under root/src |
| 213 | + """ |
| 214 | + path = path.replace("\\", "/") |
| 215 | + if path.startswith("/"): |
| 216 | + try: |
| 217 | + path = Path(path).resolve().relative_to(root.resolve()).as_posix() |
| 218 | + except ValueError: |
| 219 | + return None |
| 220 | + |
| 221 | + path = path.removeprefix("./") |
| 222 | + if not (path.startswith("src/") and path.endswith(".py")): |
| 223 | + return None |
| 224 | + return path |
| 225 | + |
| 226 | + |
187 | 227 | def _describe(entry: dict[str, object]) -> str: |
188 | 228 | """One line naming an entry for the printed summary: rule, path, scope, key and owner""" |
189 | 229 | return ( |
@@ -248,18 +288,46 @@ def main(argv: list[str] | None = None) -> int: |
248 | 288 | old_entries: list[dict[str, object]] = [] |
249 | 289 | kept_entries: list[dict[str, object]] = [] |
250 | 290 |
|
| 291 | + # {path of a file that's gone but wasn't in --files -> how many entries it had} |
| 292 | + unlisted_gone: Counter[str] = Counter() |
| 293 | + |
251 | 294 | if args.files: |
| 295 | + old_entries = json.loads(args.output.read_text())["entries"] |
| 296 | + paths_with_entries: set[object] = set() |
| 297 | + for entry in old_entries: |
| 298 | + paths_with_entries.add(entry["path"]) |
| 299 | + |
252 | 300 | rescan_paths = [] |
253 | | - for path in args.files: |
254 | | - path = normalize_path(path) |
255 | | - # skip repeats too i.e a.py and ./a.py, so the "Rescanned N files" count is right |
256 | | - if path.startswith("src/") and path.endswith(".py") and path not in rescan_paths: |
| 301 | + for given in args.files: |
| 302 | + path = _rescan_path(given, args.root) |
| 303 | + if path is None: |
| 304 | + print(f"Skipped {given}: not a .py file under {args.root}/src", file=sys.stderr) |
| 305 | + # a missing file with entries was deleted so rescanning it removes them. Without |
| 306 | + # entries there's nothing to remove and it's often a typo like exampel.py |
| 307 | + elif not (args.root / path).exists() and path not in paths_with_entries: |
| 308 | + print( |
| 309 | + f"Skipped {given}: it's not in {args.root} and has no entries (a typo, or a deleted file with no findings)", |
| 310 | + file=sys.stderr, |
| 311 | + ) |
| 312 | + # skip repeats too i.e file.py and ./file.py, so the "Rescanned N files" count is right |
| 313 | + elif path not in rescan_paths: |
257 | 314 | rescan_paths.append(path) |
258 | 315 |
|
259 | | - old_entries = json.loads(args.output.read_text())["entries"] |
| 316 | + if not rescan_paths: |
| 317 | + print( |
| 318 | + "None of the --files paths can be rescanned. Nothing was changed.", file=sys.stderr |
| 319 | + ) |
| 320 | + return 1 |
| 321 | + |
260 | 322 | for entry in old_entries: |
261 | | - if entry["path"] not in rescan_paths: |
262 | | - kept_entries.append(entry) |
| 323 | + if entry["path"] in rescan_paths: |
| 324 | + continue |
| 325 | + # a file that's gone from the checkout has no findings left, i.e it was renamed and only |
| 326 | + # its new path was listed. Keeping its entries would leave them next to the new ones |
| 327 | + if not (args.root / str(entry["path"])).exists(): |
| 328 | + unlisted_gone[str(entry["path"])] += 1 |
| 329 | + continue |
| 330 | + kept_entries.append(entry) |
263 | 331 |
|
264 | 332 | # the scan returns every rule's findings, and only the rules with a baseline are kept. This |
265 | 333 | # happens before build_entries so the count check below still adds up |
@@ -304,6 +372,22 @@ def main(argv: list[str] | None = None) -> int: |
304 | 372 | print(f" fixed {_describe(entry)}") |
305 | 373 | for old, new in changed: |
306 | 374 | print(f" count {_describe(new)}: {old['count']} -> {new['count']}") |
| 375 | + moved_from: set[object] = set() |
| 376 | + for old, new in _possible_moves(removed, added): |
| 377 | + moved_from.add(old["path"]) |
| 378 | + print( |
| 379 | + f"Possibly moved: {_describe(old)} -> {new['path']}. Its status and reason were not " |
| 380 | + "carried over, so copy them by hand if they were reviewed.", |
| 381 | + file=sys.stderr, |
| 382 | + ) |
| 383 | + # a renamed file whose new path wasn't listed: its findings show up as new on the next run |
| 384 | + for path in sorted(unlisted_gone): |
| 385 | + if path not in moved_from: |
| 386 | + print( |
| 387 | + f"Removed {unlisted_gone[path]} entries for {path}: it's not in {args.root} and wasn't " |
| 388 | + "in --files. If it was renamed, rescan its new path too.", |
| 389 | + file=sys.stderr, |
| 390 | + ) |
307 | 391 | else: |
308 | 392 | print(f"Wrote {len(entries)} entries ({len(finding)} findings) to {args.output}") |
309 | 393 |
|
|
0 commit comments