forge: DRY refactor queue_append_audit _grade_error + shutil.rmtree (verified byte-identical)

This commit is contained in:
forge committed 2026-10-22 05:59:38 +00:00
1 parent 009cd8525f
commit 2cd7e2cf50
3 files changed
+337 -48

No files matched your search

+21 -48
View File
@@ -70,6 +70,7 @@ import argparse
import io
import json
import os
import shutil
import subprocess
import sys
import tempfile
@@ -222,21 +223,7 @@ def audit_commit(repo, rev, queue, index, timeout):
row["verdict"] = "ERROR"
return row
finally:
for root, dirs, files in os.walk(td, topdown=False):
for f in files:
try:
os.unlink(os.path.join(root, f))
except OSError:
pass
for d in dirs:
try:
os.rmdir(os.path.join(root, d))
except OSError:
pass
try:
os.rmdir(td)
except OSError:
pass
shutil.rmtree(td, ignore_errors=True)
# ------------------------------------------------------------------ the range
@@ -268,28 +255,32 @@ def audit(repo, queue, index, rev_from, rev_to, guard=False, timeout=30,
"from": None, "to": None, "guard": bool(guard),
"auto_from": False, "pre_gate": 0,
"rows": [], "counts": {}, "graded": 0, "rc": None}
def _grade_error(msg):
"""Attach a grade error to the payload and return the (2, payload) verdict.
Every precondition failure in this function is the same three-statement
shape: set the message, set rc=2, return. Six inline copies were the
smell; this helper is the single source of that shape.
"""
payload["error"] = msg
payload["rc"] = 2
return 2, payload
if not os.path.isdir(repo):
payload["error"] = "not a directory: %s" % repo
payload["rc"] = 2
return 2, payload
return _grade_error("not a directory: %s" % repo)
if not is_repo(repo):
payload["error"] = "not a git repository: %s" % repo
payload["rc"] = 2
return 2, payload
return _grade_error("not a git repository: %s" % repo)
to_sha = resolve(repo, rev_to)
if to_sha is None:
payload["error"] = "unknown revision: %s" % rev_to
payload["rc"] = 2
return 2, payload
return _grade_error("unknown revision: %s" % rev_to)
payload["to"] = to_sha
from_sha = None
if rev_from:
from_sha = resolve(repo, rev_from)
if from_sha is None:
payload["error"] = "unknown revision: %s" % rev_from
payload["rc"] = 2
return 2, payload
return _grade_error("unknown revision: %s" % rev_from)
elif not all_history:
rc, out, _ = git(repo, "log", "--diff-filter=A", "--format=%H", "-1",
to_sha, "--", index)
@@ -304,17 +295,13 @@ def audit(repo, queue, index, rev_from, rev_to, guard=False, timeout=30,
rc, revs, err = queue_revs(repo, queue, from_sha, to_sha)
if rc != 0:
payload["error"] = "git rev-list failed: %s" % ((err.strip().splitlines() or [""])[0])
payload["rc"] = 2
return 2, payload
return _grade_error("git rev-list failed: %s" % ((err.strip().splitlines() or [""])[0]))
if guard:
revs = revs[-1:]
if not revs:
where = ("%s..%s" % (rev_from, rev_to) if rev_from else rev_to)
payload["error"] = ("no commit in %s touches %s, so there is no index "
return _grade_error("no commit in %s touches %s, so there is no index "
"currency to grade" % (where, queue))
payload["rc"] = 2
return 2, payload
if payload["auto_from"] and not guard:
rc_n, out_n, _ = git(repo, "rev-list", "--count", to_sha, "--", queue)
@@ -509,21 +496,7 @@ def selftest():
check("a report with a fault never prints a clean verdict",
"every graded commit" in buf.getvalue(), False)
finally:
for root, dirs, files in os.walk(base, topdown=False):
for f in files:
try:
os.unlink(os.path.join(root, f))
except OSError:
pass
for dd in dirs:
try:
os.rmdir(os.path.join(root, dd))
except OSError:
pass
try:
os.rmdir(base)
except OSError:
pass
shutil.rmtree(base, ignore_errors=True)
bad = [c for c in cells if not c[1]]
for name, ok, got, want in bad: