-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjudgeDemon.py
201 lines (160 loc) · 5.09 KB
/
judgeDemon.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
197
198
199
200
201
#!/usr/bin/python
import filecmp
import signal
import sqlite3
import subprocess
import threading
import time
from sqlite3 import Error
import os
BDPATH = 'db.sqlite3'
PATH_STATIC = './static/'
veredict_choices = ['QUE', 'JUD', 'AC', 'WA', 'TL', 'RTE', 'CE']
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by the db_file
:param db_file: database file
:return: Connection object or None
"""
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return None
def get_submission_queue(conn):
cur = conn.cursor()
cur.execute("SELECT * FROM App_queuesubmission LIMIT 1")
row = cur.fetchall()
return row
def get_submission(id, conn):
cur = conn.cursor()
cur.execute("SELECT * FROM App_submission WHERE id = " + str(id))
row = cur.fetchall()
return row[0]
def get_source_code(id, conn):
cur = conn.cursor()
cur.execute("SELECT source_code FROM App_submission WHERE id = " + str(id))
row = cur.fetchall()
return row[0]
def get_problem_id(id, conn):
cur = conn.cursor()
cur.execute("SELECT problem_id FROM App_submission WHERE id = " + str(id))
row = cur.fetchall()
return row[0]
def get_problem(id, conn):
cur = conn.cursor()
cur.execute("SELECT * FROM App_problem WHERE id = " + str(id))
row = cur.fetchall()
return row[0]
def get_time_limit(id, conn):
cur = conn.cursor()
cur.execute("SELECT time_limit FROM App_problem WHERE id = " + str(id))
row = cur.fetchall()
return row[0]
def get_test_cases(id, conn):
cur = conn.cursor()
cur.execute("SELECT * FROM App_testcase WHERE description_id = " + str(id))
rows = cur.fetchall()
return rows
def get_description_id(id, conn):
cur = conn.cursor()
cur.execute("SELECT description_id FROM App_problem WHERE id = " + str(id))
rows = cur.fetchall()
return rows
class Command(object):
def __init__(self, cmd):
self.cmd = cmd
self.process = None
def run(self, timeout):
def target():
print('Thread started')
self.process = subprocess.Popen(self.cmd, shell=True,
preexec_fn=os.setsid)
self.process.communicate()
print('Thread finished')
thread = threading.Thread(target=target)
thread.start()
thread.join(timeout)
if thread.is_alive():
print('Terminating process')
os.killpg(self.process.pid, signal.SIGTERM)
thread.join()
return (self.process.returncode, 4)
return (self.process.returncode, 5)
def changue_state(id, conn, veredict):
cur = conn.cursor()
consulta = "UPDATE App_submission SET veredict = \'" + veredict_choices[
veredict] + "\' WHERE id = " + str(id) + ";"
cur.execute(consulta)
cur.fetchall()
conn.commit()
'''
status
2 - AC
3 - WA
4 - TL
5 - RTE
6 - CE
'''
def judge(id, conn):
changue_state(id, conn, 1)
# submission = get_submission(id, conn)
# print(submission)
source_code = get_source_code(id, conn)[0]
problem_id = get_problem_id(id, conn)[0]
# problem = get_problem(problem_id, conn)
time_limit = get_time_limit(problem_id, conn)[0]
description_id = get_description_id(problem_id, conn)[0][0]
test_cases = get_test_cases(description_id, conn)
status = 2
for case in test_cases:
fileIn = case[2]
fileOut = case[3]
os.system("echo --.-JOHAN--$ > " + PATH_STATIC + "out.out")
# print(source_code)
os.system('chmod +x ' + source_code)
command = "python " + source_code + ' < ' + fileIn
command += ' > ' + PATH_STATIC + 'out.out'
command_judge = Command(cmd=command)
# print(command)
res = command_judge.run(timeout=time_limit)
if res[0]:
status = res[1]
break
with open(fileOut) as fileAc:
with open(PATH_STATIC + 'out.out') as fileVer:
lineAc = fileAc.read()
lineVer = fileVer.read()
while lineAc and lineVer:
if lineAc.strip() != lineVer.strip():
status = 3
break
lineAc = fileAc.read()
lineVer = fileVer.read()
if status != 2:
break
changue_state(id, conn, status)
def delete(id, conn):
cur = conn.cursor()
consulta = "DELETE FROM App_queuesubmission WHERE id = " + str(id) + ";"
cur.execute(consulta)
cur.fetchall()
conn.commit()
def main():
database = BDPATH
# create a database connection
conn = create_connection(database)
with conn:
while True:
queue = get_submission_queue(conn)
if not queue:
print("Not submission in queue I go to sleep... ")
time.sleep(5)
continue
id = queue[0][1]
queue_id = queue[0][0]
judge(id, conn)
delete(queue_id, conn)
if __name__ == '__main__':
main()