-
Notifications
You must be signed in to change notification settings - Fork 14
/
run.py
executable file
·132 lines (94 loc) · 3.43 KB
/
run.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
#!/usr/bin/env python3
'''
DICE - script to execute a unit test of DICE with a MIPS emulator
------------------------------------------------------
Copyright (C) 2019-2020 RiS3 Lab
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
'''
import configparser
import argparse
from argparse import Namespace
from subprocess import STDOUT, check_output
import subprocess,sys,os,re,signal,atexit,shutil,logging,time,csv
import struct
def color_print(s, color="green"):
if color == "green":
print("\033[92m%s\033[0m" % s)
elif color == "blue":
print("\033[94m%s\033[0m" % s)
elif color == "yellow":
print("\033[93m%s\033[0m" % s)
elif color == "red":
print("\033[91m%s\033[0m" % s)
else:
print(s)
def sig_handler(signo, stack_frame):
global oldstdout
global oldstderr
subprocess.call(["killall", cfg.qemu_bin])
sys.stdout = oldstdout
sys.stderr =oldstderr
sys.exit("execution finished!")
# fix terminal after killing the emulator
cmd = ["stty", "sane"]
subprocess.call(cmd)
cmd = ["tput", "rs1"]
subprocess.call(cmd)
def read_config(cfg_f):
if not os.path.isfile(cfg_f):
sys.exit("Cannot find the specified configuration file: %s" % cfg_f)
parser = configparser.SafeConfigParser()
parser.read(cfg_f)
return Namespace(
qemu_bin = os.path.abspath(parser.get("qemu", "bin")),
machine = parser.get("test", "machine"),
monitor = parser.get("test", "monitor"),
serial = parser.get("test", "serial").split(','),
bios = os.path.abspath(parser.get("test", "bios")),
kernel = os.path.abspath(parser.get("test", "kernel")),
sd = os.path.abspath(parser.get("test", "sd")),
)
if __name__ == "__main__":
global oldstdout
oldstdout = sys.stdout
global oldstderr
oldstderr = sys.stderr
start_time = time.time()
parser = argparse.ArgumentParser(description="MIPS emulator unit tester")
parser.add_argument("-c", "--config", dest="config", required=True, help="configuration file for unit test")
parser.add_argument("-o", "--output", dest="out", default="./out.txt", help="output file")
args = parser.parse_args()
try:
cfg = read_config(args.config)
except:
sys.exit("error parsing config file")
signal.signal(signal.SIGALRM , sig_handler)
cmd = []
cmd.append(cfg.qemu_bin)
cmd.extend(['-machine',cfg.machine])
cmd.append("-nographic")
cmd.extend(["-monitor", cfg.monitor])
for s in cfg.serial:
if not 'none' in s :
cmd.extend(["-serial", s])
if not 'none' in cfg.bios :
cmd.extend(["-bios", cfg.bios])
if not 'none' in cfg.kernel :
cmd.extend(["-kernel", cfg.kernel])
if not 'none' in cfg.sd:
print(type(cfg.sd))
print(cfg.sd)
cmd.extend(["-sd", cfg.sd])
color_print( 'Executing: ' + ' '.join(cmd))
try:
with open(args.out, "w") as f:
subprocess.call(cmd, stdout=f, stderr=f, timeout= 10)
except:
cmd = ["stty", "sane"]
subprocess.call(cmd)
cmd = ["tput", "rs1"]
subprocess.call(cmd)
print("execution finished!")