-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdriver.py
executable file
·209 lines (189 loc) · 5.99 KB
/
driver.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
#!/usr/bin/env python3
from __future__ import print_function
import subprocess
import sys
import getopt
# Driver program for C programming exercise
class Tracer:
traceDirectory = "./traces"
qtest = "./qtest"
command = qtest
verbLevel = 0
autograde = False
useValgrind = False
colored = False
groupOutput = False
traceDict = {
1: "trace-01-ops",
2: "trace-02-ops",
3: "trace-03-ops",
4: "trace-04-ops",
5: "trace-05-ops",
6: "trace-06-ops",
7: "trace-07-string",
8: "trace-08-robust",
9: "trace-09-robust",
10: "trace-10-robust",
11: "trace-11-malloc",
12: "trace-12-malloc",
13: "trace-13-malloc",
14: "trace-14-perf",
15: "trace-15-perf",
16: "trace-16-perf",
17: "trace-17-complexity"
}
traceProbs = {
1: "Trace-01",
2: "Trace-02",
3: "Trace-03",
4: "Trace-04",
5: "Trace-05",
6: "Trace-06",
7: "Trace-07",
8: "Trace-08",
9: "Trace-09",
10: "Trace-10",
11: "Trace-11",
12: "Trace-12",
13: "Trace-13",
14: "Trace-14",
15: "Trace-15",
16: "Trace-16",
17: "Trace-17"
}
maxScores = [0, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5]
RED = '\033[91m'
GREEN = '\033[92m'
WHITE = '\033[0m'
def __init__(self,
qtest="",
verbLevel=0,
autograde=False,
useValgrind=False,
colored=False,
groupOutput=False):
if qtest != "":
self.qtest = qtest
self.verbLevel = verbLevel
self.autograde = autograde
self.useValgrind = useValgrind
self.colored = colored
self.groupOutput = groupOutput
def printInColor(self, text, color):
if self.colored == False:
color = self.WHITE
print(color, text, self.WHITE, sep = '')
def runTrace(self, tid):
if not tid in self.traceDict:
self.printInColor("ERROR: No trace with id %d" % tid, self.RED)
return False
fname = "%s/%s.cmd" % (self.traceDirectory, self.traceDict[tid])
vname = "%d" % self.verbLevel
clist = self.command + ["-v", vname, "-f", fname]
try:
retcode = subprocess.call(clist)
except Exception as e:
self.printInColor("Call of '%s' failed: %s" % (" ".join(clist), e), self.RED)
return False
return retcode == 0
def run(self, tid=0):
scoreDict = {k: 0 for k in self.traceDict.keys()}
print("---\tTrace\t\tPoints")
if tid == 0:
tidList = self.traceDict.keys()
else:
if not tid in self.traceDict:
self.printInColor("ERROR: Invalid trace ID %d" % tid, self.RED)
return
tidList = [tid]
score = 0
maxscore = 0
if self.useValgrind:
self.command = ['valgrind', self.qtest]
else:
self.command = [self.qtest]
for t in tidList:
tname = self.traceDict[t]
if self.verbLevel > 0:
print("+++ TESTING trace %s:" % tname)
if self.groupOutput:
print("::group::Trace %s" % tname, flush=True)
ok = self.runTrace(t)
maxval = self.maxScores[t]
tval = maxval if ok else 0
if self.groupOutput:
print("::endgroup::", flush=True)
if tval < maxval:
self.printInColor("---\t%s\t%d/%d" % (tname, tval, maxval), self.RED)
else:
self.printInColor("---\t%s\t%d/%d" % (tname, tval, maxval), self.GREEN)
score += tval
maxscore += maxval
scoreDict[t] = tval
if score < maxscore:
self.printInColor("---\tTOTAL\t\t%d/%d" % (score, maxscore), self.RED)
else:
self.printInColor("---\tTOTAL\t\t%d/%d" % (score, maxscore), self.GREEN)
if self.autograde:
# Generate JSON string
jstring = '{"scores": {'
first = True
for k in scoreDict.keys():
if not first:
jstring += ', '
first = False
jstring += '"%s" : %d' % (self.traceProbs[k], scoreDict[k])
jstring += '}}'
print(jstring)
if score < maxscore:
sys.exit(1)
def usage(name):
print("Usage: %s [-h] [-p PROG] [-t TID] [-v LEVEL] [--valgrind] [-c] [--group-output]" % name)
print(" -h Print this message")
print(" -p PROG Program to test")
print(" -t TID Trace ID to test")
print(" -v LEVEL Set verbosity level (0-3)")
print(" -c Enable colored text")
sys.exit(0)
def run(name, args):
prog = ""
tid = 0
vlevel = 1
levelFixed = False
autograde = False
useValgrind = False
colored = False
groupOutput = False
optlist, args = getopt.getopt(args, 'hp:t:v:A:c', ['valgrind', 'group-output'])
for (opt, val) in optlist:
if opt == '-h':
usage(name)
elif opt == '-p':
prog = val
elif opt == '-t':
tid = int(val)
elif opt == '-v':
vlevel = int(val)
levelFixed = True
elif opt == '-A':
autograde = True
elif opt == '--valgrind':
useValgrind = True
elif opt == '-c':
colored = True
elif opt == '--group-output':
groupOutput = True
else:
print("Unrecognized option '%s'" % opt)
usage(name)
if not levelFixed and autograde:
vlevel = 0
t = Tracer(qtest=prog,
verbLevel=vlevel,
autograde=autograde,
useValgrind=useValgrind,
colored=colored,
groupOutput=groupOutput)
t.run(tid)
if __name__ == "__main__":
run(sys.argv[0], sys.argv[1:])