-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrader.py
64 lines (53 loc) · 1.74 KB
/
grader.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
import sys
from timeit import timeit
import subprocess
import platform
from zipfile import ZipFile
import time
import resource
def bad():
print("USAGE: {} <test data zip> <source file>".format(sys.argv[0]))
print("Supported languages: C++11, Java")
sys.exit(1)
if platform.system() == "Windows":
print("Currently, Windows is unsupported.")
if len(sys.argv) != 3:
bad()
zipf = sys.argv[1]
runf = sys.argv[2]
ftype = runf.split('.')[-1]
c_command = None
r_command = None
if ftype == "cpp" or ftype == "cxx" or ftype == "cc" or ftype == "C":
c_command = ["g++", "-std=c++11", "-lm", "-O2", "-fsanitize=undefined", runf]
r_command = ["./a.out"]
elif ftype == "java":
c_command = ["javac", runf]
r_command = ["java", runf[:-5]]
else:
bad()
print(c_command)
print(r_command)
c_time = round(timeit(stmt="subprocess.Popen({}).communicate()".format(c_command), globals=globals(), number=1)*1000, 3)
time.sleep(0.01)
print("Compile time: {}ms".format(c_time))
with ZipFile(zipf) as z:
tc = len(z.infolist()) // 2
for i in range(tc):
with z.open("{}.in".format(i+1)) as f:
with open("grader.in", "wb") as wf:
wf.write(f.read())
r_time = round(timeit(stmt="subprocess.Popen({}).communicate()".format(r_command), globals=globals(), number=1)*1000, 3)
time.sleep(0.01)
c_bytes = None
u_bytes = None
with z.open("{}.out".format(i+1)) as cf:
c_bytes = cf.read()
with open("grader.out", "rb") as uf:
u_bytes = uf.read()
if c_bytes == u_bytes:
print("AC")
else:
print("WA")
sys.exit(0)
print("Time: {}ms".format(r_time))