-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathall.py
executable file
·307 lines (275 loc) · 9.29 KB
/
all.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python3
"""
A tool to easily bulk-run commands like ./build.sh or ./format.sh
"""
import subprocess
import os
import re
import sys
import argparse
from multiprocessing.pool import ThreadPool
import itertools
from pathlib import Path
from time import time
TEST_ROM_URL = "https://github.com/sjl/cl-gameboy/blob/master/roms/opus5.gb?raw=true"
TEST_DIR = "gb-autotest-roms"
RED = "\033[0;31m"
GREEN = "\033[0;32m"
END = "\033[0m"
if not os.path.exists(TEST_DIR):
subprocess.run(
["git", "clone", "https://github.com/shish/gb-autotest-roms", TEST_DIR]
)
def build(lang: str, builder: str, sub: str) -> bool:
log = f"{lang}/{builder.replace('.sh', '.log')}"
with open(log, "w") as fp:
t1 = time()
proc = subprocess.run(
[f"./{builder}"],
cwd=lang,
stdout=fp,
stderr=subprocess.STDOUT,
text=True,
)
t2 = time()
duration = t2 - t1
if proc.returncode != 0:
print(f"{lang:>5s} / {sub:7s}: {RED}Failed in {duration:.2f}s - see {log}{END}")
return False
else:
print(f"{lang:>5s} / {sub:7s}: {GREEN}Built in {duration:.2f}s{END}")
return True
def bench(lang: str, runner: str, sub: str, frames: int, profile: int, rom: Path) -> bool:
try:
proc = subprocess.run(
[
f"./{runner}",
"--frames",
str(frames),
"--profile",
str(profile),
"--silent",
"--headless",
"--turbo",
f"{rom}",
],
cwd=lang,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)
if proc.returncode != 0:
print(f"{lang:>5s} / {sub:7s}: {RED}Failed{END}\n{proc.stdout}")
return False
else:
frames = ""
for line in proc.stdout.split("\n"):
if "frames" in line:
frames = line
print(f"{lang:>5s} / {sub:7s}: {frames}")
return True
except Exception as e:
print(f"{lang:>5s} / {sub:7s}: {RED}Failed{END}\n{e}")
return False
def blargg(lang: str, rom: Path) -> bool:
p = subprocess.run(
[
"./rosettaboy-release",
"--turbo",
"--silent",
"--headless",
"--frames",
"2000",
f"{rom}",
],
cwd=lang,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
errors="replace",
)
if p.returncode == 0 and "Unit test passed" in p.stdout:
print(f"{lang} {rom.name} = {GREEN}Passed{END}")
elif p.returncode == 2:
print(f"{lang} {rom.name} = {RED}Failed{END}")
else:
print(f"{lang} {rom.name} = {RED}Crashed{END}\n{p.stdout}")
return p.returncode == 0 and "Unit test passed" in p.stdout
def format(lang: str) -> bool:
proc = subprocess.run(
[f"./format.sh"],
cwd=lang,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)
if proc.returncode != 0:
print(f"{lang:>5s}: {RED}Failed{END}\n{proc.stdout}")
return False
else:
print(f"{lang:>5s}: {GREEN}Formatted{END}")
return True
def trace(lang: str, rom: Path) -> bool:
with open(f"{lang}.cpu", "w") as fp:
proc = subprocess.run(
[
f"./rosettaboy-release",
"--frames", "10",
"--silent",
"--headless",
"--turbo",
"--debug-cpu",
"--debug-ram",
f"{rom}",
],
cwd=lang,
stdout=fp,
stderr=subprocess.STDOUT,
text=True,
)
if proc.returncode != 0:
print(f"{lang:>5s}: {RED}Failed, check {lang}.cpu{END}")
return False
else:
print(f"{lang:>5s}: {GREEN}Wrote {lang}.cpu{END}")
return True
def version(lang: str, runner: str, sub: str) -> bool:
proc = subprocess.run(
[
f"./{runner}",
"--version",
],
cwd=lang,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
if proc.returncode != 0:
print(f"{lang:>5s} / {sub:7s}: {RED}{proc.stdout.strip()} / {proc.stderr.strip()}{END}")
return False
else:
print(f"{lang:>5s} / {sub:7s}: {GREEN}{proc.stdout.strip()}{END}")
return True
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--default",
default=False,
action="store_true",
help="Only run the default build, not variants",
)
parser.add_argument(
"--threads",
type=int,
default=1,
help="How many tests to run in parallel",
)
langs = [os.path.dirname(n) for n in Path(".").glob("*/build.sh")]
parser.add_argument("command", help="build, bench, blargg, format, or trace")
parser.add_argument("langs", default=langs, nargs="*", help="Which languages to test")
parser.add_argument(
"--frames", type=int, default=0, help="[bench] Run for this many frames"
)
parser.add_argument(
"--profile", type=int, default=0, help="[bench] Run for this many seconds"
)
parser.add_argument(
"--no-rebuild", default=False, action="store_true", help="[build] Don't build if rosettaboy-* exists"
)
parser.add_argument(
"--test-rom",
type=Path,
default=Path(os.environ.get("GB_DEFAULT_BENCH_ROM", "opus5.gb")),
help="Which test rom to run",
metavar="ROM"
)
parser.add_argument(
"--test-rom-dir",
type=Path,
default=os.environ.get("GB_DEFAULT_AUTOTEST_ROM_DIR", "gb-autotest-roms"),
help="The directory with the test ROMS",
metavar="DIR"
)
args = parser.parse_args()
if not args.test_rom.exists():
subprocess.run(
["wget", TEST_ROM_URL, "-O", args.test_rom],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
check=True,
)
if not args.test_rom.is_absolute():
args.test_rom = args.test_rom.absolute()
if not args.test_rom_dir.is_absolute():
args.test_rom_dir = args.test_rom_dir.absolute()
return args
def main() -> int:
args = parse_args()
# FIXME: nim explodes when doing parallel builds :(
if args.command == "build" and "nim" in args.langs and not args.default:
args.threads = 1
p = ThreadPool(args.threads)
if args.command == "build":
builds_to_run = []
for lang in args.langs:
for lang_builder in Path(lang).glob("build*.sh"):
builder = os.path.basename(lang_builder)
sub = "release"
if match := re.match("build_(.*).sh", builder):
sub = match.group(1)
if args.no_rebuild and os.path.exists(f"{lang}/rosettaboy-{sub}"):
continue
if args.default and sub != "release":
continue
builds_to_run.append((lang, builder, sub))
results = p.starmap(build, builds_to_run)
if args.command == "bench":
if args.frames == 0 and args.profile == 0:
args.profile = 10
tests_to_run = []
for lang in args.langs:
for lang_runner in Path(lang).glob("rosettaboy*"):
runner = os.path.basename(lang_runner)
sub = "release"
if match := re.match("rosettaboy-(.*)", runner):
sub = match.group(1)
if lang_runner.suffix == ".nimble":
continue # rosettaboy.nimble is detected as executable??
if not os.access(lang_runner, os.X_OK):
continue
if args.default and sub != "release":
continue
tests_to_run.append((lang, runner, sub, args.frames, args.profile, args.test_rom))
results = p.starmap(bench, tests_to_run)
if args.command == "blargg":
results = p.starmap(blargg, itertools.product(
args.langs,
args.test_rom_dir.glob("*/*.gb"),
))
if args.command == "format":
results = p.starmap(format, [(l, ) for l in args.langs])
if args.command == "trace":
results = p.starmap(trace, [(l, args.test_rom) for l in args.langs])
if args.command == "version":
tests_to_run = []
for lang in args.langs:
for lang_runner in Path(lang).glob("rosettaboy*"):
runner = os.path.basename(lang_runner)
sub = "release"
if match := re.match("rosettaboy-(.*)", runner):
sub = match.group(1)
if lang_runner.suffix == ".nimble":
continue # rosettaboy.nimble is detected as executable??
if not os.access(lang_runner, os.X_OK):
continue
if args.default and sub != "release":
continue
tests_to_run.append((lang, runner, sub))
results = p.starmap(version, tests_to_run)
if all(results):
return 0
else:
return 1
if __name__ == "__main__":
sys.exit(main())