Skip to content

Commit cbcf5f0

Browse files
authored
Merge pull request #132 from emeryberger/tools/allocbench-helpers
allocbench: add wait_pr_checks.py and compare_with_ref.py helpers
2 parents cf21a96 + de64671 commit cbcf5f0

3 files changed

Lines changed: 112 additions & 0 deletions

File tree

‎scripts/allocbench/README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ ssh host python3 hoard-bench/allocbench/build_hoard.py master=master # `remote
2626
Specs can use `${WORK}` and `${LIBNAME}` (`libhoard.dylib` / `libhoard.so`), so
2727
one spec runs unchanged on macOS and Linux.
2828

29+
Small helpers:
30+
31+
```sh
32+
# Block until a PR's checks finish; exit 0 only if all passed. --min-checks
33+
# stops a PR whose checks haven't appeared yet from looking "all green".
34+
python3 wait_pr_checks.py emeryberger/Hoard 123 --min-checks 5
35+
36+
# Before deleting local copies of files that were merged elsewhere: which are
37+
# byte-identical to a ref, which hold edits the ref lacks (exit 1), which are new.
38+
python3 compare_with_ref.py origin/master benchmarks/some-dir scripts/foo.py
39+
```
40+
2941
Why each piece exists (all were real failures):
3042

3143
- **verify before timing.** macOS SIP and hardened runtimes silently drop
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python3
2+
"""Compare working-tree files with their versions at a git ref.
3+
4+
compare_with_ref.py REF PATH [PATH ...]
5+
6+
Directories are expanded recursively. Prints one line per file:
7+
same identical to REF
8+
DIFFERENT exists at REF with different content
9+
NEW not present at REF
10+
Exit status 1 if any file is DIFFERENT (i.e. holds edits REF doesn't have).
11+
"""
12+
13+
import os
14+
import subprocess
15+
import sys
16+
17+
18+
def files(paths):
19+
for p in paths:
20+
if os.path.isdir(p):
21+
for root, _, names in os.walk(p):
22+
for n in sorted(names):
23+
yield os.path.join(root, n)
24+
else:
25+
yield p
26+
27+
28+
def main():
29+
if len(sys.argv) < 3:
30+
sys.exit(__doc__)
31+
ref, paths = sys.argv[1], sys.argv[2:]
32+
different = 0
33+
for f in files(paths):
34+
r = subprocess.run(["git", "show", "%s:%s" % (ref, f)], capture_output=True)
35+
if r.returncode != 0:
36+
print("NEW %s" % f)
37+
continue
38+
with open(f, "rb") as fh:
39+
same = fh.read() == r.stdout
40+
print("%s %s" % ("same " if same else "DIFFERENT", f))
41+
different += not same
42+
sys.exit(1 if different else 0)
43+
44+
45+
if __name__ == "__main__":
46+
main()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
"""Wait until a GitHub PR's checks have all finished, then print them.
3+
4+
wait_pr_checks.py REPO PR [--min-checks N] [--interval S] [--timeout S]
5+
6+
Exit 0 if every check passed (or was skipped), 1 if any failed or was
7+
cancelled, 2 on timeout. Waits for at least --min-checks checks to exist
8+
first, since checks for a just-pushed commit appear with a delay (and a PR
9+
with no checks yet would otherwise look "all green").
10+
"""
11+
12+
import argparse
13+
import json
14+
import subprocess
15+
import sys
16+
import time
17+
18+
19+
def checks(repo, pr):
20+
r = subprocess.run(["gh", "pr", "checks", str(pr), "-R", repo, "--json", "name,bucket,link"],
21+
capture_output=True, text=True)
22+
if r.returncode not in (0, 1, 8): # gh exits 1 when checks failed, 8 while pending
23+
return None
24+
try:
25+
return json.loads(r.stdout or "[]")
26+
except json.JSONDecodeError:
27+
return None
28+
29+
30+
def main():
31+
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
32+
p.add_argument("repo")
33+
p.add_argument("pr", type=int)
34+
p.add_argument("--min-checks", type=int, default=1)
35+
p.add_argument("--interval", type=int, default=30)
36+
p.add_argument("--timeout", type=int, default=3600)
37+
args = p.parse_args()
38+
39+
deadline = time.time() + args.timeout
40+
while time.time() < deadline:
41+
cs = checks(args.repo, args.pr)
42+
if cs is not None and len(cs) >= args.min_checks and all(c["bucket"] != "pending" for c in cs):
43+
bad = [c for c in cs if c["bucket"] in ("fail", "cancel")]
44+
for c in cs:
45+
print("%-30s %s" % (c["name"], c["bucket"]))
46+
print("ALL PASSED" if not bad else "FAILED: %d check(s)" % len(bad))
47+
sys.exit(1 if bad else 0)
48+
time.sleep(args.interval)
49+
print("TIMEOUT waiting for checks on %s#%d" % (args.repo, args.pr))
50+
sys.exit(2)
51+
52+
53+
if __name__ == "__main__":
54+
main()

0 commit comments

Comments
 (0)