forge: served_drift_check summarise() DRY collapse 4 scans -> 1, is_fatal removed

This commit is contained in:
forge committed 2026-10-20 23:11:08 +00:00
1 parent 481052bffa
commit dfd06b3c27
3 files changed
+367 -11

No files matched your search

+25 -11
View File
@@ -77,6 +77,7 @@ import sys
import urllib.error
import urllib.parse
import urllib.request
from collections import Counter
FATAL = ("DIFFERS", "MISSING", "HTTP_ERROR", "UNREACHABLE", "READ_FAILED")
ADVISORY = ("SKIPPED_LARGE",)
@@ -189,15 +190,29 @@ def grade_one(root, rev, base, path, max_bytes, timeout):
return row
def is_fatal(row):
return row["verdict"] in FATAL
def summarise(rows):
n_fatal = sum(1 for r in rows if is_fatal(r))
n_ok = sum(1 for r in rows if r["verdict"] == "OK")
n_skip = sum(1 for r in rows if r["verdict"] in ADVISORY)
return n_ok, n_fatal, n_skip
"""One pass over `rows`: tally verdicts and the uncommitted worktree count.
Replaces the original's four `sum(1 for r in rows ...)` scans -- three
verdict passes across this function and the worktree pass in report() --
with a single iteration. Counter buckets verdicts; the dirty count is
incremented alongside, so each row is touched once, not four times.
n_fatal counts every row whose verdict is one of FATAL; n_ok is the OK
tally; n_skip the ADVISORY tally; n_dirty the rows carrying an
uncommitted worktree. Counter returns 0 for a verdict that never occurs,
matching `sum(1 for ...)` over an empty set.
"""
counts = Counter()
n_dirty = 0
for r in rows:
counts[r["verdict"]] += 1
if r.get("worktree") == "modified":
n_dirty += 1
n_fatal = sum(counts[v] for v in FATAL)
n_ok = counts["OK"]
n_skip = sum(counts[v] for v in ADVISORY)
return n_ok, n_fatal, n_skip, n_dirty
def worktree_dirty(root, rev):
@@ -241,8 +256,7 @@ def report(rows, out=sys.stdout, rev=None):
width = max([len(r["path"]) for r in rows] + [4])
for r in rows:
out.write(" %-*s %-12s %s\n" % (width, r["path"], r["verdict"], r["detail"]))
n_ok, n_fatal, n_skip = summarise(rows)
n_dirty = sum(1 for r in rows if r.get("worktree") == "modified")
n_ok, n_fatal, n_skip, n_dirty = summarise(rows)
if n_dirty:
out.write("\nnote: %d path(s) are uncommitted against %s. The serve root is the"
" work tree, so a DIFFERS on those rows is an edit already on the host"
@@ -492,7 +506,7 @@ def main(argv=None):
json.dump({"root": os.path.abspath(a.root), "rev": a.rev,
"base_url": a.base_url, "rows": rows}, fh, indent=1)
if a.quiet:
n_ok, n_fatal, n_skip = summarise(rows)
n_ok, n_fatal, n_skip, _ = summarise(rows)
print("served: %d files, %d OK, %d drift, %d skipped"
% (len(rows), n_ok, n_fatal, n_skip))
return 1 if n_fatal else 0