omega-gpu-bench
First-party Omega GPU kernel benchmark harness and receipts
Brought by drakestapleton
omega-gpu-bench / tools / pr_gate.py
| 1 | #!/usr/bin/env python3 |
| 2 | # tools/pr_gate.py -- the rules for a pull request that submits receipts. CI runs this file from the |
| 3 | # base branch (workflow receipts.yml, pull_request_target) and treats the pull request as data only: |
| 4 | # nothing from the pull request is executed. |
| 5 | # |
| 6 | # pr_gate.py --pr PR_CHECKOUT --base BASE_SHA --head HEAD_SHA --same-repo yes|no |
| 7 | # |
| 8 | # Rules: |
| 9 | # * receipts are append-only: a pull request may add receipts/*.json, never change or delete one; |
| 10 | # * a pull request from a fork may change nothing but added receipts; one from a branch of this |
| 11 | # repository (only people with write access can push those) may also change the kit, |
| 12 | # imports.sha256 and evidence/; |
| 13 | # * every added receipt passes tools/validate.py, using the base branch's validator, the base branch's |
| 14 | # kernels/SHA256SUMS, and the base branch's import allowlist (a maintainer's own list for theirs); |
| 15 | # * the commit that adds a kit-run receipt carries "Signed-off-by: <the receipt's signed_off_by>". |
| 16 | # INTERIM host glue (Python); Omega successor: see DEPENDENCIES.md. |
| 17 | import argparse, json, os, re, subprocess, sys |
| 18 | |
| 19 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 20 | sys.path.insert(0, HERE) |
| 21 | import validate as V # noqa: E402 |
| 22 | |
| 23 | RECEIPT = re.compile(r"receipts/[^/]+\.json") |
| 24 | |
| 25 | |
| 26 | def git(repo, *a): |
| 27 | return subprocess.run(["git", "-C", repo, *a], check=True, capture_output=True, text=True).stdout |
| 28 | |
| 29 | |
| 30 | def main(): |
| 31 | ap = argparse.ArgumentParser() |
| 32 | ap.add_argument("--pr", required=True) |
| 33 | ap.add_argument("--base", required=True) |
| 34 | ap.add_argument("--head", required=True) |
| 35 | ap.add_argument("--same-repo", required=True, choices=("yes", "no")) |
| 36 | o = ap.parse_args() |
| 37 | trusted = os.path.dirname(HERE) |
| 38 | maint = o.same_repo == "yes" |
| 39 | errs = [] |
| 40 | added = [] |
| 41 | diff = git(o.pr, "diff", "--name-status", "--no-renames", "%s...%s" % (o.base, o.head)) |
| 42 | for line in diff.splitlines(): |
| 43 | status, path = line.split("\t", 1) |
| 44 | if RECEIPT.fullmatch(path): |
| 45 | if status == "A": |
| 46 | added.append(path) |
| 47 | else: |
| 48 | errs.append("%s: receipts are append-only (status %s)" % (path, status)) |
| 49 | elif not maint: |
| 50 | errs.append("%s: a receipt pull request may only add files under receipts/" % path) |
| 51 | imports = os.path.join(o.pr if maint else trusted, "receipts", "imports.sha256") |
| 52 | c = V.Checker(o.pr, os.path.join(trusted, "kernels", "SHA256SUMS"), imports, False) |
| 53 | for path in added: |
| 54 | try: |
| 55 | rec = json.loads(git(o.pr, "show", "%s:%s" % (o.head, path))) |
| 56 | except ValueError as x: |
| 57 | errs.append("%s: not JSON (%s)" % (path, x)) |
| 58 | continue |
| 59 | errs += ["%s: %s" % (path, m) for m in c.check(rec)] |
| 60 | if not isinstance(rec, dict) or rec.get("origin") != "kit-run": |
| 61 | continue |
| 62 | who = (rec.get("submitter") or {}).get("signed_off_by") |
| 63 | shas = git(o.pr, "log", "--format=%H", "--diff-filter=A", "%s..%s" % (o.base, o.head), "--", path).split() |
| 64 | msgs = [git(o.pr, "log", "-1", "--format=%B", s) for s in shas] |
| 65 | if not shas or not any(("Signed-off-by: %s" % who) in m.splitlines() for m in msgs): |
| 66 | errs.append('%s: the commit adding it needs the line "Signed-off-by: %s" (git commit -s)' % (path, who)) |
| 67 | commit = str((rec.get("kit") or {}).get("commit")) |
| 68 | if not re.fullmatch(r"[0-9a-f]{40}", commit) or subprocess.run( |
| 69 | ["git", "-C", o.pr, "merge-base", "--is-ancestor", commit, o.base], |
| 70 | capture_output=True).returncode != 0: |
| 71 | errs.append("%s: kit.commit %s is not on the base branch" % (path, commit)) |
| 72 | print("pull request adds %d receipt(s); from %s" % (len(added), "a branch of this repository" if maint else "a fork")) |
| 73 | for e in errs: |
| 74 | print("REJECT " + e) |
| 75 | if not added and not maint: |
| 76 | print("REJECT: no receipt added") |
| 77 | return 1 |
| 78 | print("gate: %s" % ("FAIL" if errs else "PASS")) |
| 79 | return 1 if errs else 0 |
| 80 | |
| 81 | |
| 82 | if __name__ == "__main__": |
| 83 | sys.exit(main()) |