-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathojjudger.py
112 lines (92 loc) · 3.73 KB
/
ojjudger.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
import config
import oj
import random
import subprocess
import codecs
import os
import sys
import ojrunnerwin
import ojrunnerlinux
import status
class Judger:
def __init__(self, client, sid, pid, lang):
self.client = client
self.sid = sid
self.pid = pid
self.lang = lang
self.runner = ojrunnerlinux.Runner()
def compile(self, srcPath, outPath):
return self.runner.compile(self, srcPath, outPath)
def judge(self, srcPath, outPath, inFile, ansFile, memlimit, timelimit):
return self.runner.judge(self, srcPath, outPath, inFile, ansFile, memlimit, timelimit)
def run(self):
srcPath = "%s/%s/%s_%d.code" % (sys.path[0], config.dataPath["codePath"], self.sid, random.randint(0, 65536))
outPath = "%s/%s/%s_%d.exe" % (sys.path[0], config.dataPath["execPath"], self.sid, random.randint(0, 65536))
self.saveCode(srcPath)
retdata = "Judged by %s\n=========\n" %(config.ojconfig["judger"])
print(" Compiling...")
retVal, retData = self.compile(srcPath, outPath)
if(retVal != 0):
self.putRet("%s%s" % (retdata, retData.decode()))
self.putStatus(7, 0, 0)
return 0
else:
self.putStatus(8, 0, 0)
print(" Getting Data...")
prob_data = self.getDataList(self.pid)
datalist = prob_data["datalist"]
for key in datalist:
if self.hasData(key, datalist[key], "in") == False:
dat = self.getData(key, "in")
self.saveData(key, datalist[key], "in", dat)
if self.hasData(key, datalist[key], "out") == False:
dat = self.getData(key, "out")
self.saveData(key, datalist[key], "out", dat)
print(" Judging...")
retval = 2
mem = 0
time = 0
jcount = 0
_sorted_data_list = sorted(datalist, key=lambda d:int(d), reverse = False)
for _key in _sorted_data_list:
key = str(_key)
retval, _mem, _time = self.judge(srcPath, outPath, self.__getDataPath(key, datalist[key], "in"),
self.__getDataPath(key, datalist[key], "out"), prob_data["memlimit"], prob_data["timelimit"])
jcount += 1
mem += _mem
time += _time
retdata = retdata + "%s on Test %s | Time %d ms Memory %d KB\n" % (status.langMap[retval], datalist[key].ljust(30), _time, _mem)
self.putRet(retdata)
if(retval != 2):
self.putStatus(retval, mem / jcount, time / jcount)
break
if(retval == 2):
self.putStatus(retval, mem / jcount, time / jcount)
print("Complete")
return 0
def saveCode(self, path):
code = self.client.GetCode(self.sid)
fp = codecs.open(path, 'a', 'utf-8')
fp.write(code.decode('utf-8'))
fp.close()
def putRet(self, ret):
self.client.PutRet(self.sid, ret.encode())
def putStatus(self, retcode, mem, time):
self.client.PutStatus(self.sid, retcode, mem, time)
def getDataList(self, pid):
return self.client.GetDataList(pid)
def getData(self, pdid, ext):
return self.client.GetData(pdid, ext)
def __getDataPath(self, pdid, name, ext):
return "%s/%s/%s_%s.%s" % (sys.path[0], config.dataPath["probPath"], str(pdid), name, ext)
def saveData(self, pdid, name, ext, data):
fp = open(self.__getDataPath(pdid, name, ext), 'w')
fp.write(data.decode())
fp.close()
def hasData(self, pdid, name, ext):
return os.path.exists(self.__getDataPath(pdid, name, ext))
def readData(self, path):
fp = open(path, 'rU')
__dat = fp.read().encode()
fp.close()
return __dat