-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestforces.py
131 lines (115 loc) · 3.71 KB
/
testforces.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
import sys
import os
import re
import string
import urllib
from subprocess import Popen, PIPE
from HTMLParser import HTMLParser
from collections import defaultdict
class TaskHTMLParser(HTMLParser):
def __init__(self):
self.inBox = False
self.collecting = False
self.input = []
self.output = []
self.tmp = []
# HTMLParser is an old style class.
# writing this down in case it changes in future python version and breaks my script
HTMLParser.__init__(self)
def handle_starttag(self, tag, attrs):
attrs = defaultdict(str, attrs)
if attrs['class'] == 'input':
self.inBox = True
self.destination = self.input
elif attrs['class'] == 'output':
self.inBox = True
self.destination = self.output
if tag == 'pre' and self.inBox:
self.collecting = True
def handle_endtag(self, tag):
if tag == 'pre':
self.destination.append('\n'.join(self.tmp))
self.tmp = []
self.collecting = False
self.inBox = False
def handle_data(self, data):
if self.collecting:
self.tmp.append(data)
def extractName(taskName):
pattern = re.compile('([0-9]*)([a-zA-Z])')
if pattern.search(taskName):
return pattern.search(taskName).groups()
else:
print "Cannot parse problem name"
raise ValueError
def runCommand(cmd, inputData=None, verbose=False, stderr=True):
if verbose:
print "Running:", ' '.join(cmd)
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = p.communicate(inputData)
if p.returncode == 0:
if err and stderr:
print "WARNING:\n", err
print "Status: [OK]"
return out
else:
print "Status: [FAIL]"
print "Exit code:", p.returncode
print "Out:", out
print "Errors:", err
exit(1)
def findAndCompile(number, letter, sourceDirectory='.'):
allFiles = os.listdir(sourceDirectory)
fullName = number + letter
for f in allFiles:
if f == (fullName + '.cpp'):
runCommand(['g++', '-Wall', '-O2', fullName + '.cpp', '-o', fullName + '.out'], verbose=True)
return ['./{0}.out'.format(fullName)]
elif f == (fullName + '.c'):
runCommand(['gcc', '-O2', fullName + '.c', '-o', fullName + '.out'], verbose=True)
return ['./{0}.out'.format(fullName)]
elif f == (fullName + '.py'):
return ['python', (fullName + '.py')]
elif f == (fullName + '.hs'):
runCommand(['ghc', '--make', fullName + '.hs', '-o', fullName + '.out'], verbose=True)
return ['./{0}.out'.format(fullName)]
def checkTask(taskName):
problemNumber, problemLetter = extractName(taskName)
solutionBinary = findAndCompile(problemNumber, problemLetter)
runAllTests(solutionBinary, problemNumber, problemLetter)
def downloadTests(number, letter):
# todo: potential different URL for contests outside training?
u = urllib.urlopen('http://codeforces.com/problemset/problem/{0}/{1}'.format(number, letter))
site = ''.join([line for line in u])
p = TaskHTMLParser()
p.feed(site)
inp = p.input
outp = p.output
return zip(inp, outp)
def runTest(binary, t):
(case, expected) = t
inp, expected = t
output = runCommand(binary, inputData=inp)
output = output.strip()
expected = expected.strip()
if output != expected:
print "WRONG ANSWER!\nInput:\n{0}\nExpected:\n{1}\nGot:\n{2}\n".format(case, expected, output)
exit(0)
def runAllTests(binary, number, letter):
tests = downloadTests(number, letter)
for i, t in enumerate(tests):
print "Running test", i+1
runTest(binary, t)
print
print "All tests OK"
def showHelp():
print 'usage: python testforces.py 123a'
print 'supported languages: python, c, c++, haskell'
pass
def main():
if len(sys.argv) < 2:
showHelp()
else:
checkTask(sys.argv[1])
if __name__ == '__main__':
main()