-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjudge_jobs.py
180 lines (146 loc) · 6.05 KB
/
judge_jobs.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
import os
import conf
import requests
from consts import VerdictResult, RESULT_STR
from exceptions import ExecutorInitException
from executors import get_executor
from checkers import get_checker
from checkers.special_judger import create_special_judge
import json
def check_solution_and_checker(**detail):
solution_code = detail['solution_code']
solution_lang = detail['solution_lang']
problem_id = detail['problem_id']
time_limit = detail['time_limit']
memory_limit = detail['memory_limit']
channel_name = detail['channel_name']
problem_dir = f'problems/{problem_id}'
result = {
'solution': {},
'special_judge': {},
}
try:
try:
solution_executor = get_executor(solution_lang, solution_code, f'{problem_dir}/solution')
result['solution'] = get_solution_answers(problem_dir, solution_executor, time_limit, memory_limit)
except ExecutorInitException as e:
result['solution']['verdict'] = VerdictResult.CE
result['solution']['desc'] = str(e)
sj_code = detail.get('sj_code')
sj_name = detail.get('sj_name')
if sj_code is not None:
log_file_path = f'{conf.LOG_DIR}/sj_{sj_name}.log'
result['special_judge'] = create_special_judge(sj_code, sj_name, log_file_path) # compile here
finally:
result = {'problem_id': problem_id, 'channel_name': channel_name, 'result': json.dumps(result)}
send_result_back(conf.SJ_RESULT_API_URL, result)
def get_solution_answers(problem_dir, solution_executor, time_limit, memory_limit):
result = {
"verdict": VerdictResult.AC,
"desc": "",
"time_usage": 0,
"memory_usage": 0,
"outputs": [],
}
case_no = 1
while True:
input_path = f'{problem_dir}/{case_no}.in'
answer_path = f'{problem_dir}/{case_no}.ans'
log_path = f'{problem_dir}/{case_no}.log'
# processed all test cases
if not os.path.isfile(input_path):
break
solution_res = solution_executor.execute(input_path, answer_path, log_path, time_limit, memory_limit, trace=True)
if solution_res['result'] != VerdictResult.AC:
result['verdict'] = VerdictResult.CE
result['desc'] = (f'Solution has verdict <{RESULT_STR[solution_res["result"]]}> '
f'instead of <{RESULT_STR[VerdictResult.AC]}>\n'
f'{solution_res.get("desc", "")}')
return result
result['time_usage'] = max(result['time_usage'], solution_res['timeused'])
result['memory_usage'] = max(result['memory_usage'], solution_res['memoryused'])
with open(answer_path, 'r') as f:
result['outputs'].append(f.read())
if solution_res['result'] != VerdictResult.AC:
result['verdict'] = solution_res['result']
result['desc'] = solution_res.get('desc', '')
return result
case_no += 1
return result
def judge_submission(**submit_detail):
submitted_code = submit_detail["submitted_code"]
submitted_lang = submit_detail['submitted_lang']
submit_id = submit_detail['submit_id']
result = {
"verdict": VerdictResult.SE,
"desc": "",
"time_usage": 0,
"memory_usage": 0,
"outputs": [],
}
try:
submission_dir = f'{conf.SUBMISSION_DIR}/{submit_id}'
try:
submitted_executor = get_executor(submitted_lang, submitted_code, f'{submission_dir}/submitted')
except ExecutorInitException as e:
result['verdict'] = VerdictResult.CE
result['desc'] = str(e)
return result
result = do_judge(submit_detail, submission_dir, submitted_executor)
except Exception as ex:
result['desc'] = str(ex)
raise
finally:
result = {'submit_id': submit_id, 'result': json.dumps(result)}
send_result_back(conf.RESULT_API_URL, result)
def do_judge(submit_detail, submission_dir, submitted_executor):
problem_id = submit_detail['problem_id']
time_limit = submit_detail['time_limit']
memory_limit = submit_detail['memory_limit']
checker_type = submit_detail['checker_type']
result = {
"verdict": VerdictResult.AC,
"desc": "",
"time_usage": 0,
"memory_usage": 0,
"outputs": [],
}
case_no = 1
while True:
input_path = f'problems/{problem_id}/{case_no}.in'
output_path = f'{submission_dir}/{case_no}.out'
answer_path = f'problems/{problem_id}/{case_no}.ans'
log_path = f'{submission_dir}/{case_no}.log'
# processed all test cases
if not os.path.isfile(input_path):
break
submitted_res = submitted_executor.execute(input_path, output_path, log_path, time_limit, memory_limit, trace=True)
result['time_usage'] = max(result['time_usage'], submitted_res['timeused'])
result['memory_usage'] = max(result['memory_usage'], submitted_res['memoryused'])
with open(output_path, 'r') as f:
result['outputs'].append(f.read())
if submitted_res['result'] != VerdictResult.AC:
result['verdict'] = submitted_res['result']
result['desc'] = submitted_res.get('desc', '')
return result
# check the result
checker = get_checker(checker_type)
result['verdict'], info = checker.check(output_path, answer_path)
if result['verdict'] != VerdictResult.AC:
result['desc'] = info
return result
case_no += 1
return result
def send_result_back(api_url, result):
requests.post(api_url, result, timeout=3)
def check_submissions_and_remove():
import time
import shutil
now = time.time()
with os.scandir(conf.SUBMISSION_DIR) as it:
for entry in it:
stat = entry.stat()
elapsed_hours_since_created = (now - stat.st_mtime) / 60 / 60
if elapsed_hours_since_created > conf.SUBMISSION_EXPIRE_HOURS:
shutil.rmtree(entry.path)
print(f"deleted {entry.path}")