21
|
1 #! /usr/bin/env python
|
16
|
2 # Copyright (c) 2010 Chortos-2 <chortos@inbox.lv>
|
|
3
|
21
|
4 from __future__ import division, with_statement
|
|
5
|
|
6 try:
|
|
7 from compat import *
|
|
8 import files, problem, config
|
|
9 except ImportError:
|
|
10 import __main__
|
|
11 __main__.import_error(sys.exc_info()[1])
|
|
12 else:
|
|
13 from __main__ import clock, options
|
|
14
|
|
15 import glob, re, sys, tempfile, time
|
|
16 from subprocess import Popen, PIPE, STDOUT
|
|
17
|
|
18 import os
|
|
19 devnull = open(os.path.devnull, 'w+')
|
|
20
|
|
21 try:
|
|
22 from signal import SIGTERM, SIGKILL
|
|
23 except ImportError:
|
|
24 SIGTERM = 15
|
|
25 SIGKILL = 9
|
|
26
|
16
|
27 try:
|
21
|
28 from _subprocess import TerminateProcess
|
|
29 except ImportError:
|
|
30 # CPython 2.5 does define _subprocess.TerminateProcess even though it is
|
|
31 # not used in the subprocess module, but maybe something else does not
|
|
32 try:
|
|
33 import ctypes
|
|
34 TerminateProcess = ctypes.windll.kernel32.TerminateProcess
|
|
35 except (ImportError, AttributeError):
|
|
36 TerminateProcess = None
|
|
37
|
22
|
38
|
|
39 # Do the hacky-wacky dark magic needed to catch presses of the Escape button.
|
|
40 # If only Python supported forcible termination of threads...
|
|
41 if not sys.stdin.isatty():
|
|
42 canceled = init_canceled = lambda: False
|
|
43 pause = None
|
|
44 else:
|
|
45 try:
|
|
46 # Windows has select() too, but it is not the select() we want
|
|
47 import msvcrt
|
|
48 except ImportError:
|
|
49 try:
|
|
50 import select, termios, tty, atexit
|
|
51 except ImportError:
|
|
52 # It cannot be helped!
|
|
53 # Silently disable support for killing the program being tested
|
|
54 canceled = init_canceled = lambda: False
|
|
55 pause = None
|
|
56 else:
|
|
57 def cleanup(old=termios.tcgetattr(sys.stdin.fileno())):
|
|
58 termios.tcsetattr(sys.stdin.fileno(), termios.TCSAFLUSH, old)
|
|
59 atexit.register(cleanup)
|
|
60 del cleanup
|
|
61 tty.setcbreak(sys.stdin.fileno())
|
|
62 def canceled():
|
|
63 while select.select((sys.stdin,), (), (), 0)[0]:
|
|
64 if sys.stdin.read(1) == '\33':
|
|
65 return True
|
|
66 return False
|
|
67 def init_canceled():
|
|
68 while select.select((sys.stdin,), (), (), 0)[0]:
|
|
69 sys.stdin.read(1)
|
|
70 def pause():
|
|
71 sys.stdin.read(1)
|
|
72 else:
|
|
73 def canceled():
|
|
74 while msvcrt.kbhit():
|
|
75 c = msvcrt.getch()
|
|
76 if c == '\33':
|
|
77 return True
|
|
78 elif c == '\0':
|
|
79 # Let's hope no-one is fiddling with this
|
|
80 msvcrt.getch()
|
|
81 return False
|
|
82 def init_canceled():
|
|
83 while msvcrt.kbhit():
|
|
84 msvcrt.getch()
|
|
85 def pause():
|
|
86 msvcrt.getch()
|
|
87
|
|
88
|
21
|
89 __all__ = ('TestCase', 'load_problem', 'TestCaseNotPassed',
|
22
|
90 'TimeLimitExceeded', 'CanceledByUser', 'WrongAnswer',
|
|
91 'NonZeroExitCode', 'CannotStartTestee',
|
|
92 'CannotStartValidator', 'CannotReadOutputFile',
|
|
93 'CannotReadInputFile', 'CannotReadAnswerFile')
|
21
|
94
|
|
95
|
|
96
|
|
97 # Exceptions
|
|
98
|
|
99 class TestCaseNotPassed(Exception): __slots__ = ()
|
|
100 class TimeLimitExceeded(TestCaseNotPassed): __slots__ = ()
|
22
|
101 class CanceledByUser(TestCaseNotPassed): __slots__ = ()
|
21
|
102
|
|
103 class WrongAnswer(TestCaseNotPassed):
|
|
104 __slots__ = 'comment'
|
|
105 def __init__(self, comment=''):
|
|
106 self.comment = comment
|
|
107
|
|
108 class NonZeroExitCode(TestCaseNotPassed):
|
|
109 __slots__ = 'exitcode'
|
|
110 def __init__(self, exitcode):
|
|
111 self.exitcode = exitcode
|
|
112
|
|
113 class ExceptionWrapper(TestCaseNotPassed):
|
|
114 __slots__ = 'upstream'
|
|
115 def __init__(self, upstream):
|
|
116 self.upstream = upstream
|
|
117
|
|
118 class CannotStartTestee(ExceptionWrapper): __slots__ = ()
|
|
119 class CannotStartValidator(ExceptionWrapper): __slots__ = ()
|
|
120 class CannotReadOutputFile(ExceptionWrapper): __slots__ = ()
|
|
121 class CannotReadInputFile(ExceptionWrapper): __slots__ = ()
|
|
122 class CannotReadAnswerFile(ExceptionWrapper): __slots__ = ()
|
|
123
|
|
124
|
|
125
|
22
|
126 # Helper context managers
|
|
127
|
|
128 class CopyDeleting(object):
|
|
129 __slots__ = 'case', 'file', 'name'
|
|
130
|
|
131 def __init__(self, case, file, name):
|
|
132 self.case = case
|
|
133 self.file = file
|
|
134 self.name = name
|
|
135
|
|
136 def __enter__(self):
|
|
137 if self.name:
|
|
138 try:
|
|
139 self.file.copy(self.name)
|
|
140 except:
|
|
141 try:
|
|
142 self.__exit__(None, None, None)
|
|
143 except:
|
|
144 pass
|
|
145 raise
|
|
146
|
|
147 def __exit__(self, exc_type, exc_val, exc_tb):
|
|
148 if self.name:
|
|
149 self.case.files_to_delete.append(self.name)
|
|
150
|
|
151
|
|
152 class Copying(object):
|
|
153 __slots__ = 'file', 'name'
|
|
154
|
|
155 def __init__(self, file, name):
|
|
156 self.file = file
|
|
157 self.name = name
|
|
158
|
|
159 def __enter__(self):
|
|
160 if self.name:
|
|
161 self.file.copy(self.name)
|
|
162
|
|
163 def __exit__(self, exc_type, exc_val, exc_tb):
|
|
164 pass
|
|
165
|
|
166
|
|
167
|
21
|
168 # Test case types
|
16
|
169
|
|
170 class TestCase(object):
|
21
|
171 __slots__ = ('problem', 'id', 'isdummy', 'infile', 'outfile', 'points',
|
|
172 'process', 'time_started', 'time_stopped', 'time_limit_string',
|
22
|
173 'realinname', 'realoutname', 'maxtime', 'maxmemory',
|
|
174 'has_called_back', 'files_to_delete')
|
21
|
175
|
|
176 if ABCMeta:
|
|
177 __metaclass__ = ABCMeta
|
16
|
178
|
21
|
179 def __init__(case, prob, id, isdummy, points):
|
16
|
180 case.problem = prob
|
21
|
181 case.id = id
|
|
182 case.isdummy = isdummy
|
|
183 case.points = points
|
|
184 case.maxtime = case.problem.config.maxtime
|
|
185 case.maxmemory = case.problem.config.maxmemory
|
|
186 if case.maxtime:
|
|
187 case.time_limit_string = '/%.3f' % case.maxtime
|
|
188 else:
|
|
189 case.time_limit_string = ''
|
|
190 if not isdummy:
|
|
191 case.realinname = case.problem.config.testcaseinname
|
|
192 case.realoutname = case.problem.config.testcaseoutname
|
|
193 else:
|
|
194 case.realinname = case.problem.config.dummyinname
|
|
195 case.realoutname = case.problem.config.dummyoutname
|
|
196
|
|
197 @abstractmethod
|
|
198 def test(case): raise NotImplementedError
|
16
|
199
|
22
|
200 def __call__(case, callback):
|
|
201 case.has_called_back = False
|
|
202 case.files_to_delete = []
|
21
|
203 try:
|
22
|
204 return case.test(callback)
|
21
|
205 finally:
|
22
|
206 now = clock()
|
|
207 if not getattr(case, 'time_started', None):
|
|
208 case.time_started = case.time_stopped = now
|
|
209 elif not getattr(case, 'time_stopped', None):
|
|
210 case.time_stopped = now
|
|
211 if not case.has_called_back:
|
|
212 callback()
|
21
|
213 case.cleanup()
|
|
214
|
|
215 def cleanup(case):
|
|
216 #if getattr(case, 'infile', None):
|
|
217 # case.infile.close()
|
|
218 #if getattr(case, 'outfile', None):
|
|
219 # case.outfile.close()
|
|
220 if getattr(case, 'process', None):
|
|
221 # Try killing after three unsuccessful TERM attempts in a row
|
|
222 # (except on Windows, where TERMing is killing)
|
|
223 for i in range(3):
|
|
224 try:
|
|
225 try:
|
|
226 case.process.terminate()
|
|
227 except AttributeError:
|
|
228 # Python 2.5
|
|
229 if TerminateProcess and hasattr(proc, '_handle'):
|
|
230 # Windows API
|
|
231 TerminateProcess(proc._handle, 1)
|
|
232 else:
|
|
233 # POSIX
|
|
234 os.kill(proc.pid, SIGTERM)
|
|
235 except Exception:
|
|
236 time.sleep(0)
|
|
237 case.process.poll()
|
|
238 else:
|
22
|
239 case.process.wait()
|
21
|
240 break
|
|
241 else:
|
|
242 # If killing the process is unsuccessful three times in a row,
|
|
243 # just silently stop trying
|
|
244 for i in range(3):
|
|
245 try:
|
|
246 try:
|
|
247 case.process.kill()
|
|
248 except AttributeError:
|
|
249 # Python 2.5
|
|
250 if TerminateProcess and hasattr(proc, '_handle'):
|
|
251 # Windows API
|
|
252 TerminateProcess(proc._handle, 1)
|
|
253 else:
|
|
254 # POSIX
|
|
255 os.kill(proc.pid, SIGKILL)
|
|
256 except Exception:
|
|
257 time.sleep(0)
|
|
258 case.process.poll()
|
|
259 else:
|
22
|
260 case.process.wait()
|
21
|
261 break
|
22
|
262 if case.files_to_delete:
|
|
263 for name in case.files_to_delete:
|
|
264 try:
|
|
265 os.remove(name)
|
|
266 except Exception:
|
|
267 # It can't be helped
|
|
268 pass
|
21
|
269
|
|
270 def open_infile(case):
|
|
271 try:
|
|
272 case.infile = files.File('/'.join((case.problem.name, case.realinname.replace('$', case.id))))
|
|
273 except IOError:
|
|
274 e = sys.exc_info()[1]
|
|
275 raise CannotReadInputFile(e)
|
|
276
|
|
277 def open_outfile(case):
|
|
278 try:
|
|
279 case.outfile = files.File('/'.join((case.problem.name, case.realoutname.replace('$', case.id))))
|
|
280 except IOError:
|
|
281 e = sys.exc_info()[1]
|
|
282 raise CannotReadAnswerFile(e)
|
|
283
|
16
|
284
|
21
|
285 class ValidatedTestCase(TestCase):
|
|
286 __slots__ = 'validator'
|
|
287
|
|
288 def __init__(case, *args):
|
|
289 TestCase.__init__(case, *args)
|
|
290 if not case.problem.config.tester:
|
|
291 case.validator = None
|
|
292 else:
|
|
293 case.validator = case.problem.config.tester
|
|
294
|
|
295 # TODO
|
|
296 def validate(case, output):
|
|
297 if not case.validator:
|
|
298 # Compare the output with the reference output
|
|
299 case.open_outfile()
|
|
300 with case.outfile.open() as refoutput:
|
|
301 for line, refline in zip(output, refoutput):
|
|
302 if not isinstance(refline, basestring):
|
|
303 line = bytes(line, sys.getdefaultencoding())
|
|
304 if line != refline:
|
22
|
305 raise WrongAnswer
|
21
|
306 try:
|
|
307 try:
|
|
308 next(output)
|
|
309 except NameError:
|
|
310 output.next()
|
|
311 except StopIteration:
|
|
312 pass
|
|
313 else:
|
22
|
314 raise WrongAnswer
|
21
|
315 try:
|
|
316 try:
|
|
317 next(refoutput)
|
|
318 except NameError:
|
|
319 refoutput.next()
|
|
320 except StopIteration:
|
|
321 pass
|
|
322 else:
|
22
|
323 raise WrongAnswer
|
21
|
324 return case.points
|
|
325 elif callable(case.validator):
|
|
326 return case.validator(output)
|
|
327 else:
|
|
328 # Call the validator program
|
|
329 output.close()
|
|
330 case.open_outfile()
|
22
|
331 case.outfile.copy(case.problem.config.ansname)
|
21
|
332 case.process = Popen(case.validator, stdin=devnull, stdout=PIPE, stderr=STDOUT, universal_newlines=True, bufsize=-1)
|
|
333 comment = case.process.communicate()[0].strip()
|
|
334 lower = comment.lower()
|
|
335 match = re.match(r'(ok|correct|wrong(?:(?:\s|_)*answer)?)(?:$|\s+|[.,!:]+\s*)', lower)
|
|
336 if match:
|
|
337 comment = comment[match.end():]
|
|
338 if not case.problem.config.maxexitcode:
|
|
339 if case.process.returncode:
|
|
340 raise WrongAnswer(comment)
|
|
341 else:
|
|
342 return case.points, comment
|
|
343 else:
|
|
344 return case.points * case.process.returncode / case.problem.config.maxexitcode, comment
|
|
345
|
|
346
|
|
347 class BatchTestCase(ValidatedTestCase):
|
|
348 __slots__ = ()
|
|
349
|
22
|
350 def test(case, callback):
|
|
351 init_canceled()
|
21
|
352 if sys.platform == 'win32' or not case.maxmemory:
|
|
353 preexec_fn = None
|
|
354 else:
|
|
355 def preexec_fn():
|
|
356 try:
|
|
357 import resource
|
|
358 maxmemory = int(case.maxmemory * 1048576)
|
|
359 resource.setrlimit(resource.RLIMIT_AS, (maxmemory, maxmemory))
|
|
360 # I would also set a CPU time limit but I do not want the time
|
|
361 # that passes between the calls to fork and exec to be counted in
|
|
362 except MemoryError:
|
|
363 # We do not have enough memory for ourselves;
|
|
364 # let the parent know about this
|
|
365 raise
|
|
366 except Exception:
|
|
367 # Well, at least we tried
|
|
368 pass
|
|
369 case.open_infile()
|
|
370 case.time_started = None
|
|
371 if case.problem.config.stdio:
|
|
372 if options.erase and not case.validator:
|
22
|
373 # TODO: re-use the same file name if possible
|
21
|
374 # FIXME: 2.5 lacks the delete parameter
|
|
375 with tempfile.NamedTemporaryFile(delete=False) as f:
|
22
|
376 inputdatafname = f.name
|
|
377 context = CopyDeleting(case, case.infile, inputdatafname)
|
21
|
378 else:
|
|
379 inputdatafname = case.problem.config.inname
|
22
|
380 context = Copying(case.infile, inputdatafname)
|
|
381 with context:
|
|
382 with open(inputdatafname, 'rU') as infile:
|
|
383 with tempfile.TemporaryFile('w+') if options.erase and not case.validator else open(case.problem.config.outname, 'w+') as outfile:
|
21
|
384 try:
|
22
|
385 try:
|
|
386 case.process = Popen(case.problem.config.path, stdin=infile, stdout=outfile, stderr=devnull, universal_newlines=True, bufsize=-1, preexec_fn=preexec_fn)
|
|
387 except MemoryError:
|
|
388 # If there is not enough memory for the forked test.py,
|
|
389 # opt for silent dropping of the limit
|
|
390 case.process = Popen(case.problem.config.path, stdin=infile, stdout=outfile, stderr=devnull, universal_newlines=True, bufsize=-1)
|
|
391 except OSError:
|
|
392 raise CannotStartTestee(sys.exc_info()[1])
|
|
393 case.time_started = clock()
|
|
394 if not case.maxtime:
|
|
395 while True:
|
|
396 exitcode, now = case.process.poll(), clock()
|
|
397 if exitcode is not None:
|
|
398 case.time_stopped = now
|
|
399 break
|
|
400 elif canceled():
|
|
401 raise CanceledByUser
|
|
402 else:
|
|
403 time_end = case.time_started + case.maxtime
|
|
404 while True:
|
|
405 exitcode, now = case.process.poll(), clock()
|
|
406 if exitcode is not None:
|
|
407 case.time_stopped = now
|
|
408 break
|
|
409 elif now >= time_end:
|
|
410 raise TimeLimitExceeded
|
|
411 elif canceled():
|
|
412 raise CanceledByUser
|
|
413 if config.globalconf.force_zero_exitcode and case.process.returncode:
|
|
414 raise NonZeroExitCode(case.process.returncode)
|
|
415 callback()
|
|
416 case.has_called_back = True
|
|
417 outfile.seek(0)
|
|
418 return case.validate(outfile)
|
21
|
419 else:
|
22
|
420 case.infile.copy(case.problem.config.inname)
|
21
|
421 try:
|
|
422 try:
|
|
423 case.process = Popen(case.problem.config.path, stdin=devnull, stdout=devnull, stderr=STDOUT, preexec_fn=preexec_fn)
|
|
424 except MemoryError:
|
|
425 # If there is not enough memory for the forked test.py,
|
|
426 # opt for silent dropping of the limit
|
|
427 case.process = Popen(case.problem.config.path, stdin=devnull, stdout=devnull, stderr=STDOUT)
|
|
428 except OSError:
|
|
429 raise CannotStartTestee(sys.exc_info()[1])
|
|
430 case.time_started = clock()
|
|
431 if not case.maxtime:
|
22
|
432 while True:
|
|
433 exitcode, now = case.process.poll(), clock()
|
|
434 if exitcode is not None:
|
|
435 case.time_stopped = now
|
|
436 break
|
|
437 elif canceled():
|
|
438 raise CanceledByUser
|
21
|
439 else:
|
|
440 time_end = case.time_started + case.maxtime
|
|
441 while True:
|
22
|
442 exitcode, now = case.process.poll(), clock()
|
21
|
443 if exitcode is not None:
|
|
444 case.time_stopped = now
|
|
445 break
|
|
446 elif now >= time_end:
|
22
|
447 raise TimeLimitExceeded
|
|
448 elif canceled():
|
|
449 raise CanceledByUser
|
21
|
450 if config.globalconf.force_zero_exitcode and case.process.returncode:
|
|
451 raise NonZeroExitCode(case.process.returncode)
|
22
|
452 callback()
|
|
453 case.has_called_back = True
|
21
|
454 with open(case.problem.config.outname, 'rU') as output:
|
|
455 return case.validate(output)
|
|
456
|
|
457
|
|
458 # This is the only test case type not executing any programs to be tested
|
|
459 class OutputOnlyTestCase(ValidatedTestCase):
|
|
460 __slots__ = ()
|
|
461 def cleanup(case): pass
|
|
462
|
|
463 class BestOutputTestCase(ValidatedTestCase):
|
|
464 __slots__ = ()
|
|
465
|
|
466 # This is the only test case type executing two programs simultaneously
|
|
467 class ReactiveTestCase(TestCase):
|
|
468 __slots__ = ()
|
|
469 # The basic idea is to launch the program to be tested and the grader
|
|
470 # and to pipe their standard I/O from and to each other,
|
|
471 # and then to capture the grader's exit code and use it
|
|
472 # like the exit code of a test validator is used.
|
|
473
|
|
474
|
|
475 def load_problem(prob, _types={'batch' : BatchTestCase,
|
|
476 'outonly' : OutputOnlyTestCase,
|
|
477 'bestout' : BestOutputTestCase,
|
|
478 'reactive': ReactiveTestCase}):
|
16
|
479 if prob.config.usegroups:
|
22
|
480 # FIXME: test groups should again be supported!
|
16
|
481 pass
|
|
482 else:
|
21
|
483 # We will need to iterate over these configuration variables twice
|
|
484 try:
|
|
485 len(prob.config.dummies)
|
|
486 except Exception:
|
|
487 prob.config.dummies = tuple(prob.config.dummies)
|
|
488 try:
|
|
489 len(prob.config.tests)
|
|
490 except Exception:
|
|
491 prob.config.dummies = tuple(prob.config.tests)
|
|
492 # First get prob.cache.padoutput right
|
|
493 for i in prob.config.dummies:
|
|
494 s = 'sample ' + str(i).zfill(prob.config.paddummies)
|
|
495 prob.cache.padoutput = max(prob.cache.padoutput, len(s))
|
16
|
496 for i in prob.config.tests:
|
21
|
497 s = str(i).zfill(prob.config.padtests)
|
|
498 prob.cache.padoutput = max(prob.cache.padoutput, len(s))
|
|
499 # Now yield the actual test cases
|
|
500 for i in prob.config.dummies:
|
|
501 s = str(i).zfill(prob.config.paddummies)
|
|
502 yield _types[prob.config.kind](prob, s, True, 0)
|
|
503 for i in prob.config.tests:
|
|
504 s = str(i).zfill(prob.config.padtests)
|
|
505 yield _types[prob.config.kind](prob, s, False, prob.config.pointmap.get(i, prob.config.pointmap.get(None, prob.config.maxexitcode if prob.config.maxexitcode else 1))) |