-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathx.py
More file actions
executable file
·193 lines (144 loc) · 5.39 KB
/
x.py
File metadata and controls
executable file
·193 lines (144 loc) · 5.39 KB
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
#!/usr/bin/env python
import subprocess
import sys
import os
import platform
import argparse
import json
from typing import Callable
import re
CC = os.environ.get("CC", default="gcc")
CC_PPC = os.environ.get("CC_PPC", default="powerpc-linux-gnu-gcc")
PYTHON = os.environ.get("PYTHON", default=sys.executable)
CFLAGS = [
"-std=c11",
"-Og",
"-g",
"-Wall",
"-Wextra",
"-pedantic",
"-fanalyzer",
"-Wno-analyzer-infinite-loop",
"-I.",
"-Imock",
"-DCOMPOST_DEBUG",
]
CFLAGS_SANITIZED = [
"-fsanitize=undefined",
"-fsanitize=address",
]
CFLAGS_POWERPC = [
"-static",
]
already_run: set[str] = set()
targets: dict[str, Callable] = dict()
def target(msg: str = None, dependencies: set[str] = frozenset()):
def decorator(func):
def wrapper():
if func.__name__ in already_run:
return
for dep in dependencies:
dep()
if msg:
print(f"\n>\t{msg}\n")
func()
already_run.add(func.__name__)
targets[func.__name__] = wrapper
return wrapper
return decorator
def run(args: list[str], **kwargs):
if "check" not in kwargs:
kwargs["check"] = True
print(f"Running command: {' '.join(args)}")
try:
return subprocess.run(args, **kwargs)
except subprocess.CalledProcessError as e:
print(f"Command failed with error: \n{e.stderr}")
sys.exit(1)
except FileNotFoundError as e:
print(f"Command {e.filename} does not exist!")
sys.exit(1)
@target("Generating version from Git")
def version():
ver = json.loads(run(["dotnet-gitversion"], capture_output=True, text=True).stdout)
if ver['PreReleaseLabel']:
prerelease = f".{ver['PreReleaseLabel']}{ver['PreReleaseNumber']}"
else:
prerelease = ""
python_ver = f"{ver['MajorMinorPatch']}{prerelease}"
with open("../compost_rpc/compost_rpc.py", "r") as file:
content = file.read()
content = re.sub(r'^__version__\s*=.*$', f"__version__ = \"{python_ver}\"", content, flags=re.MULTILINE)
with open("../compost_rpc/compost_rpc.py", "w") as file:
file.write(content)
run(["uv", "version", python_ver])
print(f"Detected version {python_ver} from Git repository.")
@target("Generating code")
def codegen():
run([PYTHON, "protocol_def.py"])
@target("Testing slices", {codegen})
def slices_test():
run([CC, *CFLAGS, "test_slice.c", "compost.c", "protocol_impl.c", "-o", "test_slice"])
run(["./test_slice"])
@target("Testing slices (sanitized)", {codegen})
def slices_sanitized_test():
run([CC, *CFLAGS, *CFLAGS_SANITIZED, "test_slice.c", "compost.c", "protocol_impl.c", "-o", "test_slice"])
run(["./test_slice"])
@target("Testing slices (PowerPC)", {codegen})
def slices_powerpc_test():
run([CC_PPC, *CFLAGS, *CFLAGS_POWERPC, "test_slice.c", "compost.c", "protocol_impl.c", "-o", "test_slice"])
run(["qemu-ppc", "./test_slice"])
@target("Building mock", {codegen})
def mock():
run([CC, *CFLAGS, "-o", "mock/compost_mock", "mock/main.c", "compost.c", "protocol_impl.c"])
@target("Building mock (sanitized)", {codegen})
def mock_sanitized():
run([CC, *CFLAGS, *CFLAGS_SANITIZED, "-o", "mock/compost_mock", "mock/main.c", "compost.c", "protocol_impl.c"])
@target("Checking mock", {mock})
def mock_check():
run(["echo", '"00 01 02 03" | xxd -r -p | ./mock/compost_mock > /dev/null"'], shell=True)
@target("Testing Python with mock", {mock, mock_check})
def mock_test():
run([PYTHON, "test_compost.py", "--mock", "./mock/compost_mock", "--log-cli-level", "DEBUG"])
@target("Building mock (PowerPC)", {codegen})
def mock_powerpc():
run([CC_PPC, *CFLAGS, *CFLAGS_POWERPC, "-o", "mock/compost_mock_ppc", "mock/main.c", "compost.c", "protocol_impl.c"])
@target("Checking mock (PowerPC)", {mock_powerpc})
def mock_powerpc_check():
run(["echo", '"00 01 02 03" | xxd -r -p | qemu-ppc ./mock/compost_mock_ppc > /dev/null"'], shell=True)
@target("Testing Python with mock (PowerPC)", {mock_powerpc, mock_powerpc_check})
def mock_powerpc_test():
run([PYTHON, "test_compost.py", "--mock", "qemu-ppc ./mock/compost_mock_ppc", "--log-cli-level", "DEBUG"])
@target("Checking mock (sanitized)", {mock_sanitized})
def mock_sanitized_check():
run(["echo", '"00 01 02 03" | xxd -r -p | ./mock/compost_mock > /dev/null"'], shell=True)
@target("Testing Python with mock (sanitized)", {mock_sanitized, mock_sanitized_check})
def mock_sanitized_test():
run([PYTHON, "test_compost.py", "--mock", "./mock/compost_mock", "--log-cli-level", "DEBUG"])
@target()
def test():
if platform.system() == "Linux":
slices_sanitized_test()
mock_test()
slices_powerpc_test()
mock_powerpc_test()
else:
slices_test()
mock_test()
print("Not running on Linux - skipping advanced tests")
@target()
def test_native():
slices_test()
mock_sanitized_test()
@target()
def test_powerpc():
slices_powerpc_test()
mock_powerpc_test()
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="test.py", description="Compost test runner")
parser.add_argument("target", nargs="?", default="test", choices=targets.keys(), help="Target to run")
args = parser.parse_args()
# Change current working directory to the script directory
os.chdir(sys.path[0] + "/test")
targets[args.target]()
print("Finished successfully")