-
Notifications
You must be signed in to change notification settings - Fork 7
/
gentest.py
153 lines (125 loc) · 4.12 KB
/
gentest.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
import os
import re
import subprocess
import sys
import threading
# Note that PS2HOSTNAME is expected to be set in env.
PS2CLIENT = "ps2client"
MAKE = "make"
TEST_ROOT = "tests/"
TIMEOUT = 10
RECONNECT_TIMEOUT = 10
tests_to_generate = [
"cpu/ee/alu",
"cpu/ee/branch",
"cpu/ee/branchdelay",
]
class Command(object):
def __init__(self, cmd):
self.cmd = cmd
self.process = None
self.output = None
self.timeout = False
self.thread = None
def start(self, capture=True):
def target():
self.process = subprocess.Popen(self.cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
if capture:
self.process.stdin.close()
self.output = ""
while True:
line = self.process.stdout.readline()
self.output += str(line)
if line == "" or line.rstrip() == "-- TEST END":
break
self.finish()
self.thread = threading.Thread(target=target)
self.thread.start()
def stop(self):
if self.thread.is_alive():
self.timeout = True
self.finish()
self.thread.join()
def finish(self):
try:
self.process.terminate()
except WindowsError:
pass
def run(self, timeout):
self.start()
self.thread.join(timeout)
def prepare_test(test, args):
if not ("-k" in args or "--keep" in args):
olddir = os.getcwd()
os.chdir(TEST_ROOT + os.path.dirname(test))
make_target = "all"
if "-r" in args or "--rebuild" in args:
make_target = "rebuild"
make_result = os.system("%s MAKE=\"%s\" %s" % (MAKE, MAKE, make_target))
os.chdir(olddir)
# Don't run the test if make failed, let them fix it.
if make_result > 0:
sys.exit(make_result)
def gen_test(test, args):
elf_path = TEST_ROOT + test + ".elf"
elf_exists = os.path.exists(elf_path);
irx_path = TEST_ROOT + test + ".irx"
irx_exists = os.path.exists(irx_path)
if not elf_exists and not irx_exists:
print("You must compile the test into a ELF first (" + elf_path + ")")
return False
# Seems like the PS2 can hang if it's not reset, let's just always reset for now.
c = Command([PS2CLIENT, "reset"])
c.run(RECONNECT_TIMEOUT)
c.stop()
# Okay, time to run the command.
if elf_exists:
c = Command([PS2CLIENT, "-t", str(TIMEOUT), "execee", "host:" + elf_path] + args)
else:
# For some reason, it says "invalid IOP module" with less than one extra arg.
c = Command([PS2CLIENT, "-t", str(TIMEOUT), "execiop", "host:" + irx_path, "host:" + irx_path] + args)
c.run(TIMEOUT)
output = c.output
if not re.search(r"^-- TEST END\s*$", output, re.MULTILINE):
print(output)
else:
# Strip out debug output from ps2link, etc.
output = re.sub(r"\A[^\Z]+?-- TEST BEGIN", "-- TEST BEGIN", output, re.MULTILINE)
output = re.sub(r"\n-- TEST END\s*\n[^\Z]+\Z", "\n-- TEST END\n", output, re.MULTILINE)
output = re.sub(r"\r\n", "\n", output)
# IOP seems to give an extra pair of \r\ns on Windows.
output = re.sub(r"\r\n", "\n", output)
return output
return False
def gen_test_expected(test, args):
print("Running test " + test + " on the PS2...")
prepare_test(test, args)
result = gen_test(test, args)
expected_path = TEST_ROOT + test + ".expected"
if result != False:
# Normalize line endings on windows to avoid spurious git warnings.
open(expected_path, "wt").write(result)
print("Expected file written: " + expected_path)
return True
return False
def main():
tests = []
args = []
for arg in sys.argv[1:]:
if arg[0] == "-":
args.append(arg)
else:
tests.append(arg.replace("\\", "/"))
if not tests:
tests = tests_to_generate
if "-h" in args or "--help" in args:
print("Usage: %s [options] cpu/ee/alu cpu/ee/branch...\n" % (os.path.basename(sys.argv[0])))
print("Tests should be found under %s and omit the .elf extension." % (TEST_ROOT))
print("Automatically runs make in the test by default.\n")
print("Options:")
print(" -r, --rebuild run make rebuild for each test")
print(" -k, --keep do not run make before tests")
return
for test in tests:
gen_test_expected(test, args)
main()