-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdaemon.py
executable file
·197 lines (177 loc) · 6.17 KB
/
daemon.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
#!/usr/bin/python
#coding=utf-8
#
#Author: TT_last
#
#This is a daemon for linux judge
#
#
#
import codecs
import sys, os, time, atexit
import subprocess
import fcntl
from pymongo import MongoClient
from signal import SIGTERM
import ConfigParser
#This is the class for judge
#
daemondir = "Path of mine"
cfgfile = "/daemon.ini" # Don't change it!!!!
host = "127.0.0.1"
dbname = "test_db"
cefile = "ce.txt"
dadir = "./data"
tmdir = "./temp"
lockerpath = "/home/kidx/"
langf = {1:"Main.c",2:"Main.cpp",3:"Main.java",4:"Main.cpp",5:"Main.cs",6:"Main.vb"}
#This is a daemon module
class Daemon:
'''
A daemon for gzhu judge
'''
def __init__(self,stdin='/dev/null',stdout='/dev/null',stderr='/dev/null'):
self.stdin = stdin
self.stdout = stdout
self.stderr = stderr
def start(self):
'''
'''
os.chdir(daemondir + "/judge");
#os.umask(0)
#redirect standard io
'''
sys.stdout.flush()
sys.stderr.flush()
sin = file(self.stdin,"r")
sout = file(self.stdout,"a+")
serr = file(self.stderr,"a+",0)
os.dup2(sin.fileno(),sys.stdin.fileno())
os.dup2(sout.fileno(),sys.stdout.fileno())
os.dup2(serr.fileno(),sys.stderr.fileno())
'''
self.run()
def run(slef):
pass
def makefile(indir,lang,val):
try:
sfile = indir + "/" + langf[lang]
fd = codecs.open(sfile,"wb",'utf-8')
fd.write(val)
fd.close()
return True
except:
return False
class judge:
'''
'''
def __init__(self,lang,datadir = "./data",tmpdir = "./temp",\
timelimit = 1000,memlimit = 65535,outlimit = 8192,spj = False,tc = False):
self.lang = lang
self.datadir = datadir
self.tmpdir = tmpdir
self.timelimit = timelimit
self.memlimit = memlimit
self.outlimit = outlimit
self.spj = spj
self.tc = tc
def setlimit(self,timelimit = 1000,memlimit = 65535,outlimit = 8192):
self.timelimit = timelimit
self.memlimit = memlimit
self.outlimit = outlimit
def run(self):
try:
#os.chdir("./Judge")
self.result,self.mem,self.time = (0,0,0)
if self.spj and self.tc:
p = subprocess.Popen("./Judge -l "+str(self.lang)+" -D "+self.datadir\
+" -d "+self.tmpdir+" -t "+str(self.timelimit)+" -m "+str(self.memlimit)+" -o "+str(self.outlimit) + " -S dd -T",shell=True,stdout=subprocess.PIPE)
elif self.spj:
p = subprocess.Popen("./Judge -l "+str(self.lang)+" -D "+self.datadir\
+" -d "+self.tmpdir+" -t "+str(self.timelimit)+" -m "+str(self.memlimit)+" -o "+str(self.outlimit) + " -S dd",shell=True,stdout=subprocess.PIPE)
elif self.tc:
p = subprocess.Popen("./Judge -l "+str(self.lang)+" -D "+self.datadir\
+" -d "+self.tmpdir+" -t "+str(self.timelimit)+" -m "+str(self.memlimit)+" -o "+str(self.outlimit) + " -T",shell=True,stdout=subprocess.PIPE)
else:
p = subprocess.Popen("./Judge -l "+str(self.lang)+" -D "+self.datadir\
+" -d "+self.tmpdir+" -t "+str(self.timelimit)+" -m "+str(self.memlimit)+" -o "+str(self.outlimit),shell=True,stdout=subprocess.PIPE)
for l in p.stdout:
(self.result,self.mem,self.time) = l.split()
self.result = int(self.result)
self.mem = int(self.mem)
self.time = int(self.time)
except :
exit(1)
OJ_WAIT = 0
OJ_RUN = 1
OJ_AC = 2
OJ_PE = 3
OJ_TLE = 4
OJ_MLE = 5
OJ_WA = 6
OJ_OLE = 7
OJ_CE = 8
#this is the judgeDaemon
class JudgeDaemon(Daemon):
def run(self):
cfg = ConfigParser.ConfigParser()
cfg.readfp(open(daemondir+cfgfile))
host = cfg.get('daemon','Host')
dbname = cfg.get('daemon','DataBase')
cefile = cfg.get('daemon','CE_File')
dadir = cfg.get('daemon','DataFolder')
tmdir = cfg.get('daemon','TempFolder')
lockerpath = cfg.get('daemon','LockerPath')
while True:
try:
con = MongoClient(host)
db = con[dbname]
users = db.users
problems = db.problems
solutions = db.solutions
one_solution = solutions.find_and_modify({'result':OJ_WAIT}, {"$set":{"result":OJ_RUN}})
if one_solution != None:
user = users.find_one({'name':one_solution['userName']})
if makefile(tmdir,int(one_solution["language"]),one_solution["code"]):
one_problem = problems.find_one({'problemID':int(one_solution['problemID'])})
if int(one_problem['spj']) == 1 and int(one_problem['TC']) == 1:
gzhujudge = judge(one_solution["language"],datadir = dadir +"/" + str(one_solution['problemID']),tmpdir=tmdir,spj=True,tc=True);
elif int(one_problem['spj']) == 1:
gzhujudge = judge(one_solution["language"],datadir = dadir +"/" + str(one_solution['problemID']),tmpdir=tmdir,spj=True);
elif int(one_problem['TC']) == 1:
gzhujudge = judge(one_solution["language"],datadir = dadir +"/" + str(one_solution['problemID']),tmpdir=tmdir,tc=True);
else:
gzhujudge = judge(one_solution["language"],datadir = dadir +"/" + str(one_solution['problemID']),tmpdir=tmdir);
gzhujudge.setlimit(int(one_problem['timeLimit']),int(one_problem['memoryLimit']))
gzhujudge.run();
if gzhujudge.result == OJ_CE:
ce_file = open(tmdir + '/' + cefile)
try:
all_ce_text = ce_file.read()
ce_file.close()
solutions.update({"_id":one_solution["_id"]},{"$set":{"CE":all_ce_text}})
except:
ce_file.close()
solutions.update({"_id":one_solution["_id"]},{"$set":{"CE":"错误:编译信息无法获取!(可能存在乱码)\n"}})
if gzhujudge.result == OJ_AC: #AC
problems.update({"problemID":int(one_solution['problemID'])},{"$inc":{"AC":1}})
userfile = lockerpath + str(user['name']) + ".lock"
userlocker = open(userfile, 'w')
fcntl.flock(userlocker, fcntl.LOCK_EX)
is_ac = solutions.find_one({'problemID':int(one_solution['problemID']),'userName':one_solution['userName'],'result':OJ_AC})
if is_ac == None:
users.update({'name':user['name']},{"$inc":{"solved":1}})
solutions.update({"_id":one_solution["_id"]},{"$set":{"result":gzhujudge.result,"time":gzhujudge.time,"memory":gzhujudge.mem}})
fcntl.flock(userlocker, fcntl.LOCK_UN)
userlocker.close()
else:
solutions.update({"_id":one_solution["_id"]},{"$set":{"result":gzhujudge.result,"time":gzhujudge.time,"memory":gzhujudge.mem}})
else:
pass
except:
pass
time.sleep(1)
if __name__ == "__main__":
daemon = JudgeDaemon(stdout="/dev/stdout")
daemondir = os.getcwd()
daemon.start()