upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorTheGeogeo <57634077+TheGeogeo@users.noreply.github.com>2026-12-02 05:19:38 +0100
committerTheGeogeo <57634077+TheGeogeo@users.noreply.github.com>2026-12-02 05:19:44 +0100
commitbf65aa4505aeccd8feb9bfca2c93a33d103747c8 (patch)
tree79fceced2674c1df24690af079994e6f11885d40 /scripts
parent673ac44a1bd5a8fc642c7ada4a8764fcaf497a37 (diff)
scripts: red_green.py skips a folder it cannot list, stops with the tree put back on a __pycache__ it cannot remove, and four smaller fixes (review)
drop_bytecode skips a folder it may not list (Python cannot import from it either). A __pycache__ it cannot remove is now a usage error naming it (exit 2), and any such error during a run puts the tree back on the base, where before the undone hunk stayed. --mutate-timeout must also be finite. In a mutated run, a Python test stopped while running outweighs a cargo build that was stopped. job_of says once on stderr when it has no job object. docs/testing.md: one line rewrapped.
Diffstat (limited to 'scripts')
-rw-r--r--scripts/red_green.py65
1 files changed, 46 insertions, 19 deletions
diff --git a/scripts/red_green.py b/scripts/red_green.py
index ec6c31d49..e15b1c23f 100644
--- a/scripts/red_green.py
+++ b/scripts/red_green.py
@@ -31,6 +31,7 @@ from __future__ import annotations
31 31
32import argparse 32import argparse
33import json 33import json
34import math
34import os 35import os
35import re 36import re
36import shutil 37import shutil
@@ -81,25 +82,33 @@ if os.name == "nt":
81 _KERNEL32.CloseHandle.argtypes = (wintypes.HANDLE,) 82 _KERNEL32.CloseHandle.argtypes = (wintypes.HANDLE,)
82 83
83 84
85_JOB_WARNED = False # job_of() says once that it has no job object
86
87
84def job_of(p: subprocess.Popen): 88def job_of(p: subprocess.Popen):
85 """On Windows, a job object holding `p`, or None. The processes `p` starts join it, and closing it stops every 89 """On Windows, a job object holding `p`, or None. The processes `p` starts join it, and closing it stops every
86 process in it (JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE): a process that outlives `p` is stopped with the job, where 90 process in it (JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE): a process that outlives `p` is stopped with the job, where
87 `taskkill /T` no longer finds it once `p` has ended. A process `p` starts before it joins (its first instants) 91 `taskkill /T` no longer finds it once `p` has ended. A process `p` starts before it joins (its first instants)
88 stays out.""" 92 stays out. Without one, it says so on stderr once."""
93 global _JOB_WARNED
89 job = _KERNEL32.CreateJobObjectW(None, None) 94 job = _KERNEL32.CreateJobObjectW(None, None)
90 if not job:
91 return None
92 limits = _JobLimits(limit_flags=0x2000) # JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE 95 limits = _JobLimits(limit_flags=0x2000) # JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
93 handle = _KERNEL32.OpenProcess(0x0101, False, p.pid) # PROCESS_TERMINATE | PROCESS_SET_QUOTA 96 handle = job and _KERNEL32.OpenProcess(0x0101, False, p.pid) # PROCESS_TERMINATE | PROCESS_SET_QUOTA
94 # 9: JobObjectExtendedLimitInformation 97 # 9: JobObjectExtendedLimitInformation
95 joined = (handle and _KERNEL32.SetInformationJobObject(job, 9, ctypes.byref(limits), ctypes.sizeof(limits)) 98 joined = (handle and _KERNEL32.SetInformationJobObject(job, 9, ctypes.byref(limits), ctypes.sizeof(limits))
96 and _KERNEL32.AssignProcessToJobObject(job, handle)) 99 and _KERNEL32.AssignProcessToJobObject(job, handle))
100 error = ctypes.get_last_error()
97 if handle: 101 if handle:
98 _KERNEL32.CloseHandle(handle) 102 _KERNEL32.CloseHandle(handle)
99 if not joined: 103 if joined:
104 return job
105 if job:
100 _KERNEL32.CloseHandle(job) 106 _KERNEL32.CloseHandle(job)
101 return None 107 if not _JOB_WARNED:
102 return job 108 _JOB_WARNED = True
109 print(f"red_green: no Windows job object for the mutated runs (error {error}): a process one leaves behind "
110 "is not stopped", file=sys.stderr)
111 return None
103 112
104 113
105def stop_all(p: subprocess.Popen, job) -> None: 114def stop_all(p: subprocess.Popen, job) -> None:
@@ -424,18 +433,26 @@ def drop_bytecode(tree: Path) -> None:
424 time and size it records still match (an undone change of the same size within the same second) or when it is an 433 time and size it records still match (an undone change of the same size within the same second) or when it is an
425 unchecked-hash one, and `git clean` keeps them (they are ignored). An option of the run's own Python, such as 434 unchecked-hash one, and `git clean` keeps them (they are ignored). An option of the run's own Python, such as
426 `-X pycache_prefix`, would not reach the processes its tests start. Links (symbolic links, and junctions, which 435 `-X pycache_prefix`, would not reach the processes its tests start. Links (symbolic links, and junctions, which
427 `is_symlink()` does not see) are neither followed nor removed: what they point at is not the tree's.""" 436 `is_symlink()` does not see) are neither followed nor removed: what they point at is not the tree's. A folder it
437 cannot list is skipped: Python cannot import from it either. A `__pycache__` it cannot remove stops the run."""
428 folders, todo = [], [tree] 438 folders, todo = [], [tree]
429 while todo: 439 while todo:
430 with os.scandir(todo.pop()) as entries: 440 try:
431 for e in entries: 441 with os.scandir(todo.pop()) as entries:
432 if not e.is_dir(follow_symlinks=False) or e.is_symlink(): 442 for e in entries:
433 continue 443 if not e.is_dir(follow_symlinks=False) or e.is_symlink():
434 if getattr(e.stat(follow_symlinks=False), "st_file_attributes", 0) & stat.FILE_ATTRIBUTE_REPARSE_POINT: 444 continue
435 continue # a junction (Windows) 445 if getattr(e.stat(follow_symlinks=False), "st_file_attributes", 0) \
436 (folders if e.name == "__pycache__" else todo).append(e.path) 446 & stat.FILE_ATTRIBUTE_REPARSE_POINT:
447 continue # a junction (Windows)
448 (folders if e.name == "__pycache__" else todo).append(e.path)
449 except OSError:
450 continue
437 for folder in folders: 451 for folder in folders:
438 shutil.rmtree(folder) 452 try:
453 shutil.rmtree(folder)
454 except OSError as e:
455 raise Failure(f"cannot remove {folder}, whose bytecode could run instead of the code: {e}") from None
439 456
440 457
441def python_tests(tree: Path, files: list[str], timeout: float | None) -> dict: 458def python_tests(tree: Path, files: list[str], timeout: float | None) -> dict:
@@ -473,8 +490,10 @@ def side(tree: Path, crates: list[str], py_files: list[str], cargo: str, target:
473 result["tests"].update(part["tests"]) 490 result["tests"].update(part["tests"])
474 if part["no_build"]: 491 if part["no_build"]:
475 result["no_build"].append(part["no_build"]) 492 result["no_build"].append(part["no_build"])
476 for key in ("compiler_crashed", "rerun_after", "timed_out"): 493 for key in ("compiler_crashed", "rerun_after"):
477 result[key] = result[key] or part.get(key) 494 result[key] = result[key] or part.get(key)
495 if part.get("timed_out") and result["timed_out"] != "running": # a test stopped running outweighs a build
496 result["timed_out"] = part["timed_out"]
478 return result 497 return result
479 498
480 499
@@ -588,8 +607,8 @@ def main(argv: list[str] | None = None) -> int:
588 p.error("--mutate-max and --mutate-timeout go with --mutate") 607 p.error("--mutate-max and --mutate-timeout go with --mutate")
589 if a.mutate_max is not None and a.mutate_max < 1: 608 if a.mutate_max is not None and a.mutate_max < 1:
590 p.error("--mutate-max must be 1 or more") 609 p.error("--mutate-max must be 1 or more")
591 if a.mutate_timeout is not None and not a.mutate_timeout > 0: # NaN too 610 if a.mutate_timeout is not None and not (math.isfinite(a.mutate_timeout) and a.mutate_timeout > 0):
592 p.error("--mutate-timeout must be more than 0 seconds") 611 p.error("--mutate-timeout must be a number of seconds above 0")
593 a.mutate_max = 10 if a.mutate_max is None else a.mutate_max 612 a.mutate_max = 10 if a.mutate_max is None else a.mutate_max
594 a.mutate_timeout = 600.0 if a.mutate_timeout is None else a.mutate_timeout 613 a.mutate_timeout = 600.0 if a.mutate_timeout is None else a.mutate_timeout
595 try: 614 try:
@@ -615,6 +634,14 @@ def main(argv: list[str] | None = None) -> int:
615 os.close(fd) 634 os.close(fd)
616 try: 635 try:
617 result = check(repo, work, a, base, base_commit, head, merge_base) 636 result = check(repo, work, a, base, base_commit, head, merge_base)
637 except Failure:
638 # Stopped midway (a side, a mutation): the tree goes back on the base, as at the end of a run.
639 if (work / "tree" / ".git").exists():
640 try:
641 checkout(repo, work / "tree", base_commit)
642 except Failure:
643 pass
644 raise
618 finally: 645 finally:
619 lock.unlink(missing_ok=True) 646 lock.unlink(missing_ok=True)
620 except Failure as e: 647 except Failure as e: