2016-09-02 12:04:29 +00:00
|
|
|
# coding=utf-8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
2016-09-11 02:35:08 +00:00
|
|
|
import shutil
|
2016-10-07 05:32:30 +00:00
|
|
|
import uuid
|
2016-09-02 12:04:29 +00:00
|
|
|
|
2016-09-28 12:43:46 +00:00
|
|
|
import web
|
2016-09-02 12:04:29 +00:00
|
|
|
|
|
|
|
from compiler import Compiler
|
2016-11-17 12:08:27 +00:00
|
|
|
from config import JUDGER_WORKSPACE_BASE, SPJ_SRC_DIR, SPJ_EXE_DIR
|
2016-10-01 18:15:49 +00:00
|
|
|
from exception import TokenVerificationFailed, CompileError, SPJCompileError,JudgeClientError
|
2016-09-28 12:43:46 +00:00
|
|
|
from judge_client import JudgeClient
|
2016-11-17 12:08:27 +00:00
|
|
|
from utils import server_info, logger, token
|
2016-10-03 08:13:46 +00:00
|
|
|
|
2016-09-02 12:04:29 +00:00
|
|
|
|
2016-09-28 12:43:46 +00:00
|
|
|
DEBUG = os.environ.get("judger_debug") == "1"
|
|
|
|
|
2016-09-02 12:04:29 +00:00
|
|
|
|
|
|
|
class InitSubmissionEnv(object):
|
|
|
|
def __init__(self, judger_workspace, submission_id):
|
|
|
|
self.path = os.path.join(judger_workspace, submission_id)
|
|
|
|
|
|
|
|
def __enter__(self):
|
2016-10-01 18:15:49 +00:00
|
|
|
try:
|
|
|
|
os.mkdir(self.path)
|
|
|
|
os.chmod(self.path, 0777)
|
|
|
|
except Exception as e:
|
2016-10-04 05:24:50 +00:00
|
|
|
logger.exception(e)
|
2016-10-01 18:15:49 +00:00
|
|
|
raise JudgeClientError("failed to create runtime dir")
|
2016-09-02 12:04:29 +00:00
|
|
|
return self.path
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
2016-09-28 12:43:46 +00:00
|
|
|
if not DEBUG:
|
2016-10-01 18:15:49 +00:00
|
|
|
try:
|
|
|
|
shutil.rmtree(self.path)
|
|
|
|
except Exception as e:
|
2016-10-04 05:24:50 +00:00
|
|
|
logger.exception(e)
|
2016-10-01 18:15:49 +00:00
|
|
|
raise JudgeClientError("failed to clean runtime dir")
|
2016-09-02 12:04:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JudgeServer(object):
|
|
|
|
def pong(self):
|
2016-10-03 08:13:46 +00:00
|
|
|
data = server_info()
|
|
|
|
data["action"] = "pong"
|
|
|
|
return data
|
2016-09-02 12:04:29 +00:00
|
|
|
|
2016-10-07 05:32:30 +00:00
|
|
|
def judge(self, language_config, src, max_cpu_time, max_memory, test_case_id,
|
2016-10-13 13:33:27 +00:00
|
|
|
spj_version=None, spj_config=None, spj_compile_config=None, spj_src=None, output=False):
|
2016-09-02 12:04:29 +00:00
|
|
|
# init
|
2016-10-09 12:05:04 +00:00
|
|
|
compile_config = language_config.get("compile")
|
|
|
|
run_config = language_config["run"]
|
2016-10-07 05:32:30 +00:00
|
|
|
submission_id = str(uuid.uuid4())
|
2016-09-02 12:04:29 +00:00
|
|
|
|
2017-11-25 11:27:08 +00:00
|
|
|
if spj_version and spj_config:
|
|
|
|
spj_exe_path = os.path.join(SPJ_EXE_DIR, spj_config["exe_name"].format(spj_version=spj_version))
|
|
|
|
# spj src has not been compiled
|
|
|
|
if not os.path.isfile(spj_exe_path):
|
|
|
|
logger.warning("%s does not exists, spj src will be recompiled")
|
|
|
|
self.compile_spj(spj_version=spj_version, src=spj_src,
|
|
|
|
spj_compile_config=spj_compile_config)
|
2016-10-09 14:16:30 +00:00
|
|
|
|
2016-10-01 18:05:16 +00:00
|
|
|
with InitSubmissionEnv(JUDGER_WORKSPACE_BASE, submission_id=str(submission_id)) as submission_dir:
|
2016-10-09 12:05:04 +00:00
|
|
|
if compile_config:
|
|
|
|
src_path = os.path.join(submission_dir, compile_config["src_name"])
|
|
|
|
|
|
|
|
# write source code into file
|
|
|
|
with open(src_path, "w") as f:
|
|
|
|
f.write(src.encode("utf-8"))
|
|
|
|
|
|
|
|
# compile source code, return exe file path
|
|
|
|
exe_path = Compiler().compile(compile_config=compile_config,
|
|
|
|
src_path=src_path,
|
|
|
|
output_dir=submission_dir)
|
|
|
|
else:
|
2016-10-09 14:16:30 +00:00
|
|
|
exe_path = os.path.join(submission_dir, run_config["exe_name"])
|
2016-10-09 12:05:04 +00:00
|
|
|
with open(exe_path, "w") as f:
|
|
|
|
f.write(src.encode("utf-8"))
|
2016-09-02 12:04:29 +00:00
|
|
|
|
2016-09-11 02:35:08 +00:00
|
|
|
judge_client = JudgeClient(run_config=language_config["run"],
|
|
|
|
exe_path=exe_path,
|
|
|
|
max_cpu_time=max_cpu_time,
|
|
|
|
max_memory=max_memory,
|
2016-10-01 18:05:16 +00:00
|
|
|
test_case_id=str(test_case_id),
|
|
|
|
submission_dir=submission_dir,
|
2016-10-06 03:43:49 +00:00
|
|
|
spj_version=spj_version,
|
2016-10-13 13:33:27 +00:00
|
|
|
spj_config=spj_config,
|
|
|
|
output=output)
|
2016-09-27 15:30:52 +00:00
|
|
|
run_result = judge_client.run()
|
2016-10-22 13:22:20 +00:00
|
|
|
|
2016-09-27 15:30:52 +00:00
|
|
|
return run_result
|
2016-09-02 12:04:29 +00:00
|
|
|
|
2017-11-25 11:27:08 +00:00
|
|
|
def compile_spj(self, spj_version, src, spj_compile_config):
|
2016-09-28 13:58:00 +00:00
|
|
|
spj_compile_config["src_name"] = spj_compile_config["src_name"].format(spj_version=spj_version)
|
|
|
|
spj_compile_config["exe_name"] = spj_compile_config["exe_name"].format(spj_version=spj_version)
|
2016-09-28 12:43:46 +00:00
|
|
|
|
2016-10-09 14:16:30 +00:00
|
|
|
spj_src_path = os.path.join(SPJ_SRC_DIR, spj_compile_config["src_name"])
|
2016-09-28 12:43:46 +00:00
|
|
|
|
|
|
|
# if spj source code not found, then write it into file
|
|
|
|
if not os.path.exists(spj_src_path):
|
|
|
|
with open(spj_src_path, "w") as f:
|
2016-09-28 13:58:00 +00:00
|
|
|
f.write(src.encode("utf-8"))
|
2016-09-28 12:43:46 +00:00
|
|
|
try:
|
|
|
|
Compiler().compile(compile_config=spj_compile_config,
|
|
|
|
src_path=spj_src_path,
|
2016-10-09 14:16:30 +00:00
|
|
|
output_dir=SPJ_EXE_DIR)
|
2016-09-28 12:43:46 +00:00
|
|
|
# turn common CompileError into SPJCompileError
|
|
|
|
except CompileError as e:
|
|
|
|
raise SPJCompileError(e.message)
|
|
|
|
return "success"
|
|
|
|
|
2016-09-27 15:30:52 +00:00
|
|
|
def POST(self):
|
2016-10-27 10:38:04 +00:00
|
|
|
_token = web.ctx.env.get("HTTP_X_JUDGE_SERVER_TOKEN", None)
|
2016-09-27 15:30:52 +00:00
|
|
|
try:
|
2016-10-27 10:38:04 +00:00
|
|
|
if _token != token:
|
2016-09-29 14:23:08 +00:00
|
|
|
raise TokenVerificationFailed("invalid token")
|
|
|
|
if web.data():
|
2016-10-01 18:15:49 +00:00
|
|
|
try:
|
|
|
|
data = json.loads(web.data())
|
|
|
|
except Exception as e:
|
2016-10-04 05:24:50 +00:00
|
|
|
logger.info(web.data())
|
2016-10-01 18:15:49 +00:00
|
|
|
return {"ret": "ServerError", "data": "invalid json"}
|
2016-09-29 14:23:08 +00:00
|
|
|
else:
|
|
|
|
data = {}
|
|
|
|
|
2016-09-27 15:30:52 +00:00
|
|
|
if web.ctx["path"] == "/judge":
|
2016-09-28 12:43:46 +00:00
|
|
|
callback = self.judge
|
|
|
|
elif web.ctx["path"] == "/ping":
|
|
|
|
callback = self.pong
|
|
|
|
elif web.ctx["path"] == "/compile_spj":
|
|
|
|
callback = self.compile_spj
|
|
|
|
else:
|
2016-10-01 18:15:49 +00:00
|
|
|
return json.dumps({"err": "InvalidMethod", "data": None})
|
2016-09-29 14:23:08 +00:00
|
|
|
return json.dumps({"err": None, "data": callback(**data)})
|
2016-10-04 05:03:54 +00:00
|
|
|
except (CompileError, TokenVerificationFailed, SPJCompileError, JudgeClientError) as e:
|
2016-10-04 05:24:50 +00:00
|
|
|
logger.exception(e)
|
2016-09-27 15:30:52 +00:00
|
|
|
ret = dict()
|
2016-09-29 14:23:08 +00:00
|
|
|
ret["err"] = e.__class__.__name__
|
|
|
|
ret["data"] = e.message
|
2016-09-27 16:45:56 +00:00
|
|
|
return json.dumps(ret)
|
2016-10-04 05:03:54 +00:00
|
|
|
except Exception as e:
|
2016-10-04 05:24:50 +00:00
|
|
|
logger.exception(e)
|
2016-10-04 05:03:54 +00:00
|
|
|
ret = dict()
|
|
|
|
ret["err"] = "JudgeClientError"
|
|
|
|
ret["data"] =e.__class__.__name__ + ":" + e.message
|
|
|
|
return json.dumps(ret)
|
2016-09-02 12:04:29 +00:00
|
|
|
|
|
|
|
|
2016-09-27 15:30:52 +00:00
|
|
|
urls = (
|
|
|
|
"/judge", "JudgeServer",
|
2016-09-28 12:43:46 +00:00
|
|
|
"/ping", "JudgeServer",
|
2016-09-28 13:58:00 +00:00
|
|
|
"/compile_spj", "JudgeServer"
|
2016-09-27 15:30:52 +00:00
|
|
|
)
|
2016-09-02 12:04:29 +00:00
|
|
|
|
2016-09-28 05:38:12 +00:00
|
|
|
|
2016-10-01 18:15:49 +00:00
|
|
|
if DEBUG:
|
2016-10-04 05:24:50 +00:00
|
|
|
logger.info("DEBUG=ON")
|
2016-10-01 18:15:49 +00:00
|
|
|
|
2016-09-27 15:30:52 +00:00
|
|
|
app = web.application(urls, globals())
|
|
|
|
wsgiapp = app.wsgifunc()
|
2016-09-25 06:52:20 +00:00
|
|
|
|
2016-09-27 15:30:52 +00:00
|
|
|
# gunicorn -w 4 -b 0.0.0.0:8080 server:wsgiapp
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run()
|