-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
157 lines (139 loc) · 6.33 KB
/
test.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
import os
import sys
import shutil
import functools
import subprocess
sys.dont_write_bytecode = True
sys.path.append('../misc/common/')
import common # noqa
open_safe = common.open_safe('Test')
debug_mode = False
def pipe(filename):
with open_safe(filename, 'rb') as file:
if debug_mode:
print(f'Test: Pipe \'{filename}\'')
sys.stdout.flush()
sys.stdout.buffer.write(file.read())
def run(*args):
if debug_mode:
print(f'Test: Running `{" ".join(args)}`')
sys.stdout.flush()
subprocess.run([*args], check=True)
def rel_path(*args):
# from path relative to this file to path relative to cwd
return os.path.relpath(os.path.join(os.path.dirname(__file__), *args), os.getcwd())
run_python = functools.partial(run, 'python3')
run_cargo = functools.partial(run, 'cargo', *(['--quiet'] if not debug_mode else []),
'run', *(['--release'] if not debug_mode else []), '--bin')
if len(sys.argv) <= 1:
print('Test: Usage: test <filenames|operations>')
sys.exit(1)
target = 'target'
input = sys.argv[1:][::-1]
shutil.rmtree(rel_path(target), ignore_errors=True)
shutil.copytree(rel_path('../lib/'), rel_path(target, 'lib/'), dirs_exist_ok=True)
shutil.copytree(rel_path('../libc/'), rel_path(target, 'libc/'), dirs_exist_ok=True)
shutil.copytree(rel_path('../misc/'), rel_path(target, 'misc/'), dirs_exist_ok=True)
shutil.copytree(rel_path('../test/musts/'), rel_path(target), dirs_exist_ok=True)
shutil.copytree(rel_path('../test/utils/'), rel_path(target), dirs_exist_ok=True)
shutil.copytree(rel_path('../test/games/'), rel_path(target), dirs_exist_ok=True)
shutil.copytree(rel_path('../test/other/'), rel_path(target), dirs_exist_ok=True)
shutil.copytree(rel_path('../test/tests/'), rel_path(target), dirs_exist_ok=True)
shutil.copytree(rel_path('../libc/incl/'), rel_path(target), dirs_exist_ok=True)
shutil.copytree(rel_path('../circ/impl/'), rel_path(target), dirs_exist_ok=True)
shutil.copytree(rel_path('../bf/test/'), rel_path(target), dirs_exist_ok=True)
filenames = []
operations = []
while input:
operation = '' # make type checker happy
try:
operation = input.pop()
match operation:
case 'cc':
(c_source_files, filenames) = (filenames, []) # consume all
assembly_output_file = c_source_files[0] + '.asm'
filenames.append(assembly_output_file)
operations.append((operation, functools.partial(
run_cargo, f'{operation}', *c_source_files, assembly_output_file)))
case 'enc':
hex_source_file = filenames.pop()
memory_image_file = hex_source_file + '.mem'
filenames.append(memory_image_file)
operations.append((operation, functools.partial(
run_python, rel_path(f'../{operation}/{operation}.py'), hex_source_file, memory_image_file)))
case 'dec':
memory_image_file = filenames.pop()
hex_output_file = memory_image_file + '.hex'
filenames.append(hex_output_file)
operations.append((operation, functools.partial(
run_python, rel_path(f'../{operation}/{operation}.py'), memory_image_file, hex_output_file)))
case 'asm':
assembly_source_file = filenames.pop()
memory_image_file = assembly_source_file + '.mem'
filenames.append(memory_image_file)
operations.append((operation, functools.partial(
run_cargo, f'{operation}', assembly_source_file, memory_image_file)))
case 'dasm':
memory_image_file = filenames.pop()
disassembly_output_file = memory_image_file + '.asm'
filenames.append(disassembly_output_file)
operations.append((operation, functools.partial(
run_cargo, f'{operation}', memory_image_file, disassembly_output_file)))
case 'emu':
memory_image_file = filenames.pop()
operations.append((operation, functools.partial(run_cargo, f'{operation}', memory_image_file)))
case 'cemu':
memory_image_file = filenames.pop()
operations.append((operation, functools.partial(run, 'make', '--directory',
f'../{operation}', '--silent' if not debug_mode else '')))
operations.append((operation, functools.partial(run, f'../{operation}/{operation}', memory_image_file)))
case 'mic':
microcode_image_file = rel_path(target, 'microcode.mic')
filenames.append(microcode_image_file)
operations.append((operation, functools.partial(run_cargo, f'{operation}', microcode_image_file)))
case 'sim':
microcode_image_file = filenames.pop()
memory_image_file = filenames.pop()
operations.append((operation, functools.partial(
run_cargo, f'{operation}', memory_image_file, microcode_image_file)))
case 'circ':
circuit_file = filenames.pop()
microcode_image_file = filenames.pop()
memory_image_file = filenames.pop()
operations.append((operation, functools.partial(run_python, rel_path(
f'../{operation}/{operation}.py'), memory_image_file, microcode_image_file, circuit_file)))
case 'bf':
brainfuck_source_file = filenames.pop()
memory_image_file = brainfuck_source_file + '.mem'
filenames.append(memory_image_file)
operations.append((operation, functools.partial(run_python, rel_path(
f'../{operation}/{operation}-pad.py'), brainfuck_source_file, memory_image_file)))
microcode_image_file = rel_path(target, 'brainfuck.mic')
filenames.append(microcode_image_file)
operations.append((operation, functools.partial(run_cargo, f'{operation}-mic', microcode_image_file)))
case 'pop':
filenames.pop()
case 'dup':
filenames.append(filenames[-1])
case 'pipe':
filename = filenames.pop()
operations.append((operation, functools.partial(pipe, filename)))
case file:
filenames.append(rel_path(target, file))
except IndexError:
print(f'Test: Error: Missing argument for operation \'{operation}\'')
sys.exit(1)
if filenames:
for filename in filenames:
print(f'Test: Error: Unused argument \'{filename}\'')
sys.exit(1)
try:
for (name, func) in operations:
try:
func()
except subprocess.CalledProcessError as e:
if debug_mode:
print(f'Test: Warning: Operation subprocess \'{name}\' exited with code {e.returncode}')
except KeyboardInterrupt:
print('Test: Interrupted')
sys.exit(1)