-
Notifications
You must be signed in to change notification settings - Fork 1
/
measure.py
executable file
·310 lines (293 loc) · 10.8 KB
/
measure.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env python3
# Copyright (C) 2013 Jussi Pakkanen
#
# Authors:
# Jussi Pakkanen <jpakkane@gmail.com>
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of version 3 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from glob import glob
import os
import subprocess
import re
import random
import Levenshtein
import math
random.seed()
precision_target = int(math.pi*1024*1024)
def precisionchecker(infile):
if len(open(infile, 'rb').read()) > 256:
print('Source file', infile, 'too long.')
return False
return True
def plainchecker(infile):
permitted = {'vector' : True,
'map' : True,
'iostream' : True,
'functional' : True,
'memory' : True,
'utility' : True,
'stdexcept' : True,
'string' : True,
'set' : True,
'unordered_map' : True,
'unordered_set' : True,
'regex' : True,
'array' : True,
'stack' : True,
'queue' : True,
'algorithm' : True,
'iterator' : True,
'complex' : True,
'atomic' : True,
'thread' : True,
'mutex' : True,
'future' : True,
'typeinfo' : True,
'tuple' : True,
'initializer_list' : True
}
if len(open(infile, 'rb').read()) > 512:
print('Source file', infile, 'too long.')
includere = re.compile('''^\s*#\s*include\s*[<"](.*?)[>"]''')
for line in open(infile):
m = re.search(includere, line)
if m:
include = m.group(1)
if include not in permitted:
print("Invalid include", include, "in", infile)
return False
elif '#' in line or '??=' in line or '%:' in line or line.strip().endswith('%\\'):
print('Invalid use of preprocessor in', infile)
return False
return True
def oneshotchecker(infile):
if len(open(infile, 'rb').read()) > 256:
print('Source file', infile, 'too long.')
return False
return True
def create_testdata():
res = []
for i in range(random.randint(1000, 10000)):
res.append(chr(ord('a') + random.randint(0, 26)))
if random.random() < 0.1:
res.append('\n')
return ''.join(res)
def levenshteinchecker(infile):
(base, suf) = os.path.splitext(infile)
okfile = base + '_ok' + suf
if not os.path.exists(okfile):
print('Passing file', okfile, 'not found.')
return False
entry_src = open(infile, 'rb').read()
if len(entry_src) > 256:
print('Source file too big.')
return False
dist = Levenshtein.distance(entry_src, open(okfile, 'rb').read())
if dist != 1:
print('Levenshtein distance', dist, 'not equal to one.')
return False
binname = os.path.join(os.path.curdir, 'levbinary')
cmd = ['g++', '-std=c++11', '-Wall', '-Wextra', '-Wpedantic', '-o', binname, okfile]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if b'//' in entry_src or b'/*' in entry_src:
print("Comments are forbidden in this category. Nice try, though.")
return False
(stdo, stde) = p.communicate()
stdo = stdo.decode()
stde = stde.decode()
if p.returncode != 0:
print("Compilation failed.")
print(stdo)
print(stde)
return False
if len(stdo) != 0:
print("Fail, stdout has text:")
print(stdo)
return False
if len(stde) != 0:
print('Fail, stderr has text:')
print(stde)
return False
testdata = create_testdata()
testifname = 'test.dat'
testofname = 'output.dat'
open(testifname, 'w').write(testdata)
p = subprocess.Popen([binname, testifname, testofname], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
p.communicate()
if p.returncode != 0:
print("Running test binary failed.")
os.unlink(binname)
os.unlink(testifname)
return False
if not os.path.exists(testofname):
print('Output file not created.')
os.unlink(binname)
os.unlink(testifname)
return False
testoutput = open(testofname, 'r').read()
os.unlink(binname)
os.unlink(testifname)
os.unlink(testofname)
if testoutput[::-1] != testdata:
print("Output is incorrect.")
return False
return True
def packages_installed(packagefile):
if not os.path.isfile(packagefile):
print('Package file missing in ', packagefile)
for line in open(packagefile).readlines():
line = line.strip()
if line == '':
continue
cmd = ['aptitude', 'show', line]
pc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
so = pc.communicate()[0].decode()
if not 'State: installed' in so:
print('Package', line, 'not installed in', packagefile)
return False
return True
def info_ok(infofile):
if not os.path.isfile(infofile):
print('Info file missing for', infofile)
return False
lines = open(infofile).readlines()
if len(lines) != 3:
print('Incorrect number of lines in info file in', infofile)
return False
elems = lines[0].strip().split()
if len(elems) != 2:
print('Malformed email line in', infofile)
return False
if elems[0] != 'email' or '@' not in elems[1]:
print('Malformed email line in', infofile)
return False
elems = lines[1].strip().split()
if len(elems) < 2 or elems[0] != 'title':
print('Malformed title line in', infofile)
return False
elems = lines[2].strip().split()
if len(elems) < 2 or elems[0] != 'author':
print('Malformed author line in', infofile)
return False
return True
def has_extra_files(d, basename):
allowed = {'info.txt' : True,
'includes.txt' : True,
'packages.txt' : True,
basename + '.cpp': True
}
if os.path.split(d)[0] == 'levenshtein':
allowed[basename + '_ok.cpp'] = True
for d in glob(os.path.join(d, '*')):
base = os.path.split(d)[-1]
if base not in allowed:
print(basename, 'has an extra file', base)
return True
return False
def measure(subdir):
compiler = '/usr/bin/g++'
basic_flags = ['-std=c++11', '-c', '-o', '/dev/null']
buildtype_flags = {'oneshot': ['-fmax-errors=1'],
'levenshtein' : [],
'precision' : []}
results = []
include_re = re.compile('[^a-zA-Z0-9/-_.]')
dirname_re = re.compile('[^a-z0-9]')
for d in glob(os.path.join(subdir, '*')):
basename = os.path.split(d)[-1]
if dirname_re.search(basename) is not None:
print("Only lowercase letters and numbers allowed in entry name.")
continue
sourcename = basename + '.cpp'
fullsrc = os.path.join(d, sourcename)
if has_extra_files(d, basename):
continue
if not os.path.isfile(fullsrc):
print('Missing source file', fullsrc)
continue
infofile = os.path.join(d, 'info.txt')
packagefile = os.path.join(d, 'packages.txt')
if subdir == 'anything':
if not packages_installed(packagefile):
continue
else:
if os.path.isfile(packagefile):
print('Package file exists in non-anything dir', basename)
continue
if not info_ok(infofile):
continue
if subdir == 'oneshot':
checker = oneshotchecker
elif subdir == 'levenshtein':
checker = levenshteinchecker
else:
checker = precisionchecker
if not checker(fullsrc):
continue
if not os.path.isfile(fullsrc):
print("Bad file in subdir", d)
continue
cmd_arr = ['(', 'ulimit', '-t', '300', ';',\
'ulimit', '-v', '16252928', ';', compiler, "'%s'" % fullsrc] + basic_flags
faulty = False
if subdir == 'oneshot':
includefile = os.path.join(d, 'includes.txt')
for line in open(includefile):
line = line.strip()
if include_re.search(line) is not None:
print('Invalid include dir', line, 'in', d)
faulty = True
break
cmd_arr.append('-I' + line)
if faulty:
continue
cmd_arr += buildtype_flags[subdir]
cmd_arr += [')', '2>&1', '>', '/dev/null', '|', 'wc', '-c']
cmd = ' '.join(cmd_arr)
# Remember kids, you should not use shell=True unless you
# have a very good reason. We need it to use wc and ulimit.
pc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
res = pc.communicate()
stdout = res[0].decode()
input_size = len(open(fullsrc, 'rb').read())
output_size = int(stdout)
if input_size == 0:
print('Empty input file in subdir', d)
continue
ratio = output_size / input_size
results.append((ratio, input_size, output_size, basename))
results.sort(reverse=True)
return results
def run():
print('The Grand C++ Error Explosion Competition\n')
print('This program will measure entries and print the results (not necessarily in order).\n')
print('The output contains four elements:')
print('ratio, source code size, error message size, name\n')
print('Starting measurements for type oneshot.')
plain_times = measure('oneshot')
print('Table for category oneshot:')
for i in plain_times:
print('%.2f' % i[0], i[1], i[2], i[3])
print('\nStarting measurements for type levenshtein.')
lev_times = measure('levenshtein')
print('\nTable for category levenshtein:')
for i in lev_times:
print('%.2f' % i[0], i[1], i[2], i[3])
print('\nStarting measurements for type precision.')
print('source code size, delta, name')
prec_times = measure('precision')
print('\nTable for category precision:')
for i in prec_times:
print(i[1], '%d' % abs(precision_target - i[2]), i[3])
if __name__ == '__main__':
run()