完成对SPJ的支持

xml rpc不能使用None
This commit is contained in:
virusdefender
2016-04-06 20:33:19 +08:00
parent 3d396cc8e4
commit b3116cc430
11 changed files with 134 additions and 41 deletions

View File

@@ -1,12 +1,13 @@
# coding=utf-8
import os
import json
import commands
import hashlib
import judger
import spj_client
from multiprocessing import Pool
from settings import max_running_number, lrun_gid, lrun_uid, judger_workspace
from settings import max_running_number
from language import languages
from result import result
from judge_exceptions import JudgeClientError
@@ -20,7 +21,7 @@ def _run(instance, test_case_id):
class JudgeClient(object):
def __init__(self, language_code, exe_path, max_cpu_time, max_memory, test_case_dir, judge_base_path):
def __init__(self, language_code, exe_path, max_cpu_time, max_memory, test_case_dir, judge_base_path, spj_path):
"""
:param language_code: 语言编号
:param exe_path: 可执行文件路径
@@ -53,11 +54,12 @@ class JudgeClient(object):
# 测试用例配置项
self._test_case_info = self._load_test_case_info()
self._judge_base_path = judge_base_path
self._spj_path = spj_path
def _load_test_case_info(self):
# 读取测试用例信息 转换为dict
try:
f = open(self._test_case_dir + "info")
f = open(os.path.join(self._test_case_dir, "info"))
return json.loads(f.read())
except IOError:
raise JudgeClientError("Test case config file not found")
@@ -96,11 +98,13 @@ class JudgeClient(object):
return output_md5, output_md5 == test_case_config["striped_output_md5"]
def _judge_one(self, test_case_id):
in_file = os.path.join(self._test_case_dir, str(test_case_id) + ".in")
out_file = os.path.join(self._judge_base_path, str(test_case_id) + ".out")
run_result = judger.run(path=self.execute_command[0],
max_cpu_time=self._max_cpu_time,
max_memory=self._max_memory,
in_file=os.path.join(self._test_case_dir, str(test_case_id) + ".in"),
out_file=os.path.join(self._judge_base_path, str(test_case_id) + ".out"),
in_file=in_file,
out_file=out_file,
args=self.execute_command[1:],
env=["PATH=" + os.environ["PATH"]],
use_sandbox=self._language["use_sandbox"],
@@ -113,12 +117,27 @@ class JudgeClient(object):
# 将judger返回的结果标志转换为本系统中使用的
if run_result["flag"] == 0:
output_md5, r = self._compare_output(test_case_id)
if r:
run_result["result"] = result["accepted"]
if self._spj_path is None:
output_md5, r = self._compare_output(test_case_id)
if r:
run_result["result"] = result["accepted"]
else:
run_result["result"] = result["wrong_answer"]
run_result["output_md5"] = output_md5
else:
run_result["result"] = result["wrong_answer"]
run_result["output_md5"] = output_md5
spj_result = spj_client.spj(path=self._spj_path, max_cpu_time=3 * self._max_cpu_time,
max_memory=3 * self._max_memory,
in_path=in_file,
user_out_path=out_file)
if spj_result["spj_result"] == spj_client.AC:
run_result["result"] = result["accepted"]
elif spj_result["spj_result"] == spj_client.WA:
run_result["result"] = result["wrong_answer"]
else:
run_result["result"] = result["system_error"]
run_result["error"] = "SPJ Crashed, return: %d, signal: %d" % \
(spj_result["spj_result"], spj_result["signal"])
elif run_result["flag"] in [1, 2]:
run_result["result"] = result["time_limit_exceeded"]
elif run_result["flag"] == 3:

View File

@@ -7,8 +7,9 @@ from logger import logger
from settings import judger_workspace
def compile_(language_item, src_path, exe_path, judge_base_path):
compile_command = language_item["compile_command"].format(src_path=src_path, exe_path=exe_path).split(" ")
def compile_(language_item, src_path, exe_path, judge_base_path, compile_spj=False):
command_item = "spj_compile_command" if compile_spj else "compile_command"
compile_command = language_item[command_item].format(src_path=src_path, exe_path=exe_path).split(" ")
compiler = compile_command[0]
compile_args = compile_command[1:]
compiler_output_file = os.path.join(judge_base_path, "compiler.out")

View File

@@ -7,6 +7,7 @@ languages = {
"src_name": "main.c",
"code": 1,
"compile_command": "/usr/bin/gcc -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c99 {src_path} -lm -o {exe_path}/main",
"spj_compile_command": "/usr/bin/gcc -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c99 {src_path} -lm -o {exe_path}",
"execute_command": "{exe_path}/main",
"use_sandbox": True
},
@@ -15,6 +16,7 @@ languages = {
"src_name": "main.cpp",
"code": 2,
"compile_command": "/usr/bin/g++ -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c++11 {src_path} -lm -o {exe_path}/main",
"spj_compile_command": "/usr/bin/g++ -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c++11 {src_path} -lm -o {exe_path}",
"execute_command": "{exe_path}/main",
"use_sandbox": True
},

View File

@@ -12,7 +12,8 @@ from settings import judger_workspace
class JudgeInstanceRunner(object):
def run(self, token, submission_id, language_code, code, time_limit, memory_limit, test_case_id):
def run(self, token, submission_id, language_code, code, time_limit, memory_limit, test_case_id,
spj, spj_language, spj_code, spj_version):
language = languages[language_code]
host_name = socket.gethostname()
judge_base_path = os.path.join(judger_workspace, "run", submission_id)
@@ -35,25 +36,50 @@ class JudgeInstanceRunner(object):
# 编译
try:
exe_path = compile_(language, src_path, judge_base_path, judge_base_path)
exe_path = compile_(language_item=language, src_path=src_path,
exe_path=judge_base_path, judge_base_path=judge_base_path, compile_spj=False)
except Exception as e:
shutil.rmtree(judge_base_path, ignore_errors=True)
return {"code": 1, "data": {"error": str(e), "server": host_name}}
test_case_dir = os.path.join(judger_workspace, "test_case", test_case_id)
# SPJ相关
if spj:
spj_path = os.path.join(test_case_dir, "spj-" + spj_version)
if "spj-" + spj_version not in os.listdir(test_case_dir):
spj_language_item = languages[spj_language]
spj_code_path = os.path.join(test_case_dir, "spj-" + spj_language_item["src_name"])
f = open(spj_code_path, "w")
f.write(spj_code.encode("utf8"))
f.close()
try:
compile_(language_item=languages[spj_language], src_path=spj_code_path,
exe_path=spj_path,
judge_base_path=judge_base_path, compile_spj=True)
except Exception as e:
return {"code": 2, "data": {"error": "SPJ Compile error: " + str(e), "server": host_name}}
else:
spj_path = None
# 运行
try:
client = JudgeClient(language_code=language_code,
exe_path=exe_path,
max_cpu_time=int(time_limit),
max_memory=int(memory_limit) * 1024 * 1024,
test_case_dir=judger_workspace + "test_case/" + test_case_id + "/",
judge_base_path=judge_base_path)
test_case_dir=test_case_dir,
judge_base_path=judge_base_path, spj_path=spj_path)
judge_result = {"result": result["accepted"], "info": client.run(),
"accepted_answer_time": None, "server": host_name}
for item in judge_result["info"]:
if item["result"] != 0:
judge_result["result"] = item["result"]
if item.get("error"):
judge_result["info"] = item["error"]
break
else:
l = sorted(judge_result["info"], key=lambda k: k["cpu_time"])

View File

@@ -1,16 +1,9 @@
# coding=utf-8
import os
# 单个判题端最多同时运行的程序个数因为判题端会同时运行多组测试数据比如一共有5组测试数据
# 如果MAX_RUNNING_NUMBER大于等于5那么这5组数据就会同时进行评测然后返回结果。
# 如果MAX_RUNNING_NUMBER小于5为3那么就会同时运行前三组测试数据然后再运行后两组数据
# 这样可以避免同时运行的程序过多导致的cpu占用太高
max_running_number = 10
# lrun运行用户的uid
lrun_uid = 1001
# lrun用户组gid
lrun_gid = 1002
# judger工作目录
judger_workspace = "/var/judger/"

26
judge/spj_client.py Normal file
View File

@@ -0,0 +1,26 @@
# coding=utf-8
import os
import judger
WA = 1
AC = 0
SPJ_ERROR = -1
def file_exists(path):
return os.path.exists(path)
def spj(path, max_cpu_time, max_memory, in_path, user_out_path):
if file_exists(in_path) and file_exists(user_out_path):
result = judger.run(path=path, in_file="/dev/null", out_file="/dev/null",
max_cpu_time=max_cpu_time, max_memory=max_memory,
args=[in_path, user_out_path], env=["PATH=" + os.environ.get("PATH", "")],
use_sandbox=True, use_nobody=True)
if result["signal"] == 0 and result["exit_status"] in [AC, WA, SPJ_ERROR]:
result["spj_result"] = result["exit_status"]
else:
result["spj_result"] = SPJ_ERROR
return result
else:
raise ValueError("in_path or user_out_path does not exist")