|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""path_hermetic_lint.py - a negative grep for a path-segment word is not hermetic: the checkout path is part of every absolute path in a log. |
| 3 | +
|
| 4 | +WHY: `! grep -E 'worktrees' "$LOG"` passes in CI (checkout /home/runner/work/...) but fails in a lane worktree |
| 5 | +(/repo/.claude/worktrees/lane/...), because the scanner argv/log holds absolute paths that contain the word. |
| 6 | +Flags, in scripts/test-*.sh, every `! grep` command segment (a line is split on ; && || | outside quotes) whose literal, |
| 7 | +non-$var text holds a bare path-segment word (worktrees, Desktop, Users, home, tmp, var, ...) as a whole word, unless the |
| 8 | +grep's input (its last arg) is written, on that line or the one before, by a sed/`${x//...}`/replace that uses $ROOT/$PWD. |
| 9 | +Existing offenders are frozen in scripts/path-hermetic-baseline.tsv (`path<TAB>stripped line text`; a row whose line no longer |
| 10 | +flags also fails, so the baseline only shrinks). Usage: path_hermetic_lint.py [root]. Exit 0 clean, 1 on a violation. |
| 11 | +Side effects: none. Edge case: a pattern built from a variable is not seen (false negative, accepted). |
| 12 | +""" |
| 13 | +import glob |
| 14 | +import os |
| 15 | +import re |
| 16 | +import sys |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +WORDS = {"worktree", "worktrees", "desktop", "users", "home", "tmp", "private", "var", "checkout", "deep-code-review", "documents"} |
| 20 | +STRIP = re.compile(r"(\bsed\b|\$\{[^}]*//|\breplace\b)") |
| 21 | +ROOTS = re.compile(r"\$\{?(ROOT|PWD|REPO\w*|CHECKOUT\w*|SRC)\b|\$\(pwd\)|\$\(git rev-parse --show-toplevel\)") |
| 22 | + |
| 23 | + |
| 24 | +def segments(line): |
| 25 | + """Split a shell line on ; && || | outside quotes.""" |
| 26 | + out, cur, q, i = [], "", "", 0 |
| 27 | + while i < len(line): |
| 28 | + c = line[i] |
| 29 | + if q: |
| 30 | + q = "" if c == q else q |
| 31 | + elif c in "\"'": |
| 32 | + q = c |
| 33 | + elif (c == "|" and cur.endswith(">")) is False and c in ";|" or (c == "&" and line[i:i + 2] == "&&"): |
| 34 | + out.append(cur) |
| 35 | + cur = "" |
| 36 | + i += 2 if line[i:i + 2] in ("&&", "||") else 1 |
| 37 | + continue |
| 38 | + cur += c |
| 39 | + i += 1 |
| 40 | + return out + [cur] |
| 41 | + |
| 42 | + |
| 43 | +def hit_words(seg): |
| 44 | + """Return the WORDS found as whole words in the literal (non-$var) text of a `! grep` segment, else [].""" |
| 45 | + seg = seg.strip() |
| 46 | + if not re.match(r"!\s+grep\b", seg): |
| 47 | + return [] |
| 48 | + lit = re.sub(r"\$\{[^}]*\}|\$\([^)]*\)|\$\w+", " ", seg).lower() |
| 49 | + lit = re.sub(r"(?<=\s)-[a-z]*?e(?=\w)", r"\g<0> ", lit) # -ePATTERN / -qePATTERN glue |
| 50 | + return [w for w in WORDS if re.search(r"(?<![\w-])" + re.escape(w) + r"(?![\w-])", lit)] |
| 51 | + |
| 52 | + |
| 53 | +def exempt(seg, lines): |
| 54 | + """True if this or the previous line has a non-grep segment with STRIP and ROOTS and a non-grep segment that redirects (> f, >f, >| f) exactly into the grep's input file (last arg, quotes stripped).""" |
| 55 | + f = seg.split()[-1].strip("\"'") |
| 56 | + for l in lines: |
| 57 | + segs = [s for s in segments(l) if not s.strip().startswith("! grep")] |
| 58 | + outs = [[t.strip("\"'") for t in re.findall(r"(?<![>&\d])>\|?\s*(\"[^\"]*\"|'[^']*'|[^\s;&|]+)", s)] for s in segs] |
| 59 | + # A strip counts if it writes f or nothing (a loop body: while read; do ${l//"$ROOT"/}; done >f); a strip into another file does not. |
| 60 | + if any(STRIP.search(s) and ROOTS.search(s) and o in ([], [f]) for s, o in zip(segs, outs)) and any(f in o for o in outs): |
| 61 | + return True |
| 62 | + return False |
| 63 | + |
| 64 | + |
| 65 | +def lint(root="."): |
| 66 | + """Return (new, stale): new = `path:line` of unexempt hits whose `path<TAB>text` key is not in the baseline, stale = baseline keys that no longer hit.""" |
| 67 | + with open(os.path.join(root, "scripts/path-hermetic-baseline.tsv"), encoding="utf-8") as f: # missing = fail closed |
| 68 | + base = {l.rstrip("\n") for l in f if l.strip() and not l.startswith("#")} |
| 69 | + hits = {} |
| 70 | + for p in sorted(glob.glob(os.path.join(root, "scripts/test-*.sh"))): |
| 71 | + rel = os.path.relpath(p, root) |
| 72 | + ls = Path(p).read_text(encoding="utf-8").splitlines() |
| 73 | + for n, l in enumerate(ls): |
| 74 | + if l.lstrip().startswith("#"): |
| 75 | + continue |
| 76 | + for seg in segments(l): |
| 77 | + if hit_words(seg) and not exempt(seg, [l] + ls[n - 1:n]): |
| 78 | + hits[f"{rel}\t{l.strip()}"] = f"{rel}:{n + 1}" |
| 79 | + return sorted(v for k, v in hits.items() if k not in base), sorted(base - set(hits)) |
| 80 | + |
| 81 | + |
| 82 | +def main(argv): |
| 83 | + new, stale = lint(argv[1] if len(argv) > 1 else ".") |
| 84 | + for k in new: |
| 85 | + print(f"path_hermetic_lint: {k} has a `! grep` for a path-segment word that is also in the checkout path; strip $ROOT/$PWD first (sed \"s|$ROOT||g\")", file=sys.stderr) |
| 86 | + for k in stale: |
| 87 | + print(f"path_hermetic_lint: baseline row no longer flags (delete it from scripts/path-hermetic-baseline.tsv): {k.replace(chr(9), ' :: ')}", file=sys.stderr) |
| 88 | + return 1 if new or stale else 0 |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + sys.exit(main(sys.argv)) |
0 commit comments