From 87dcb3452f8eedc0d993b70bf0856ec3349be238 Mon Sep 17 00:00:00 2001 From: poli0981 <127664709+poli0981@users.noreply.github.com> Date: Sat, 17 Oct 2026 00:40:39 +0700 Subject: [PATCH] fix(spikes): scrub progress bars line by line, not with a backtracking regex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SonarCloud's PR analysis flagged the progress-bar regex in capture.py's redaction again (S8786, super-linear backtracking) — the second regex form of this rule it has flagged. A per-line test cannot backtrack at all. It replaces exactly the lines the regex did on the LF text `subprocess.run(text=True)` produces, checked against seventeen sample lines including the edge cases (no space after the dashes, trailing whitespace, several "eta"s, a final line without a newline); no committed fixture changes. Co-Authored-By: Claude Opus 5.5 --- spikes/capture.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/spikes/capture.py b/spikes/capture.py index 1418234..dcd3d7b 100644 --- a/spikes/capture.py +++ b/spikes/capture.py @@ -342,6 +342,15 @@ _ABS_PATH = re.compile( ) +def _is_progress(line: str) -> bool: + """A pip progress-bar line: dashes, a space, anything, then `eta `.""" + body = line.strip(" \t") + if not body.startswith("-") or body.lstrip("-")[:1] not in (" ", "\t"): + return False + _, sep, tail = body.rpartition("eta ") + return bool(sep) and tail != "" and all(c in "0123456789:" for c in tail) + + def redact(text: str, work: Path) -> str: """Strip machine- and run-specific paths so a capture is comparable across machines. @@ -380,10 +389,16 @@ def redact(text: str, work: Path) -> str: text = re.sub(r"0x[0-9A-Fa-f]{6,}", "0xADDR", text) # pip's download progress bar carries a transfer rate and an ETA, so it is different on - # every run and wildly different between a dev machine and a CI runner. `[ \t]` rather than - # `\s`: under (?m) a leading `\s*` could also consume line breaks, which is both wrong (the - # match could start on an earlier line) and the backtracking SonarCloud flagged. - text = re.sub(r"(?m)^[ \t]*-+[ \t]+[^\r\n]*?eta [0-9:]+[ \t]*$", " ", text) + # every run and wildly different between a dev machine and a CI runner. Decided line by line + # rather than by one multiline regex: SonarCloud flagged two regex forms of this rule for + # super-linear backtracking (S8786), and a line test cannot backtrack at all. It replaces the + # same lines the regex did on the LF text `text=True` produces. + text = "".join( + " " + line[len(line.rstrip("\n")) :] + if _is_progress(line.rstrip("\n")) + else line + for line in text.splitlines(keepends=True) + ) # pip lists every release it considered when nothing satisfies a requirement -- "(from # versions: 0.8.0, ..., 1.18.1)" -- so the requires-python fixture grew by one version