2015-08-12 02:24:06 +00:00
|
|
|
# coding=utf-8
|
|
|
|
import sys
|
2015-08-15 06:50:22 +00:00
|
|
|
import os
|
2015-08-12 06:01:34 +00:00
|
|
|
import pymongo
|
|
|
|
|
|
|
|
from bson.objectid import ObjectId
|
|
|
|
|
2015-08-12 02:24:06 +00:00
|
|
|
from client import JudgeClient
|
|
|
|
from language import languages
|
|
|
|
from compiler import compile_
|
|
|
|
from result import result
|
2015-08-15 06:50:22 +00:00
|
|
|
from settings import judger_workspace
|
|
|
|
|
|
|
|
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '..'))
|
|
|
|
|
2015-08-15 09:00:55 +00:00
|
|
|
from judger_controller.settings import celery_mongodb_config, docker_mongodb_config
|
2015-08-12 02:24:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 简单的解析命令行参数
|
2015-08-12 06:56:18 +00:00
|
|
|
# 参数有 -solution_id -time_limit -memory_limit -test_case_id
|
|
|
|
# 获取到的值是['xxx.py', '-solution_id', '1111', '-time_limit', '1000', '-memory_limit', '100', '-test_case_id', 'aaaa']
|
2015-08-12 02:24:06 +00:00
|
|
|
args = sys.argv
|
2015-08-13 10:15:00 +00:00
|
|
|
submission_id = args[2]
|
2015-08-12 06:56:18 +00:00
|
|
|
time_limit = args[4]
|
|
|
|
memory_limit = args[6]
|
2015-08-12 02:24:06 +00:00
|
|
|
test_case_id = args[8]
|
|
|
|
|
2015-08-15 09:00:55 +00:00
|
|
|
connection = pymongo.MongoClient(host=docker_mongodb_config["host"], port=docker_mongodb_config["port"])
|
2015-08-12 06:01:34 +00:00
|
|
|
collection = connection["oj"]["oj_submission"]
|
|
|
|
|
2015-08-13 10:15:00 +00:00
|
|
|
submission = collection.find_one({"_id": ObjectId(submission_id)})
|
2015-08-12 06:01:34 +00:00
|
|
|
if not submission:
|
|
|
|
exit()
|
2015-08-13 10:15:00 +00:00
|
|
|
connection.close()
|
2015-08-12 06:01:34 +00:00
|
|
|
|
|
|
|
# 将代码写入文件
|
|
|
|
language = languages[submission["language"]]
|
|
|
|
src_path = judger_workspace + "run/" + language["src_name"]
|
2015-08-12 02:24:06 +00:00
|
|
|
f = open(src_path, "w")
|
2015-08-13 10:15:00 +00:00
|
|
|
f.write(submission["code"].encode("utf8"))
|
2015-08-12 02:24:06 +00:00
|
|
|
f.close()
|
|
|
|
|
2015-08-12 06:01:34 +00:00
|
|
|
# 编译
|
2015-08-12 02:24:06 +00:00
|
|
|
try:
|
2015-08-12 06:01:34 +00:00
|
|
|
exe_path = compile_(language, src_path, judger_workspace + "run/")
|
2015-08-12 02:24:06 +00:00
|
|
|
except Exception as e:
|
2015-08-13 10:15:00 +00:00
|
|
|
print e
|
2015-08-15 09:00:55 +00:00
|
|
|
connection = pymongo.MongoClient(host=docker_mongodb_config["host"], port=docker_mongodb_config["port"])
|
2015-08-13 10:15:00 +00:00
|
|
|
collection = connection["oj"]["oj_submission"]
|
|
|
|
data = {"result": result["compile_error"], "info": str(e)}
|
|
|
|
collection.find_one_and_update({"_id": ObjectId(submission_id)}, {"$set": data})
|
|
|
|
connection.close()
|
2015-08-12 02:24:06 +00:00
|
|
|
exit()
|
2015-08-13 10:15:00 +00:00
|
|
|
|
|
|
|
print "Compile successfully"
|
|
|
|
# 运行
|
2015-08-12 08:49:25 +00:00
|
|
|
try:
|
2015-08-12 11:55:41 +00:00
|
|
|
client = JudgeClient(language_code=submission["language"],
|
2015-08-12 08:49:25 +00:00
|
|
|
exe_path=exe_path,
|
|
|
|
max_cpu_time=int(time_limit),
|
|
|
|
max_real_time=int(time_limit) * 2,
|
|
|
|
max_memory=int(memory_limit),
|
2015-08-13 10:15:00 +00:00
|
|
|
test_case_dir=judger_workspace + "test_case/" + test_case_id + "/")
|
|
|
|
judge_result = {"result": result["accepted"], "info": client.run()}
|
|
|
|
|
|
|
|
for item in judge_result["info"]:
|
|
|
|
if item["result"]:
|
|
|
|
judge_result["result"] = item["result"]
|
|
|
|
break
|
2015-08-13 12:28:23 +00:00
|
|
|
else:
|
|
|
|
l = sorted(judge_result["info"], key=lambda k: k["cpu_time"])
|
|
|
|
judge_result["accepted_answer_info"] = {"time": l[-1]["cpu_time"]}
|
2015-08-13 10:15:00 +00:00
|
|
|
|
2015-08-12 08:49:25 +00:00
|
|
|
except Exception as e:
|
|
|
|
print e
|
2015-08-13 10:15:00 +00:00
|
|
|
judge_result = {"result": result["system_error"], "info": str(e)}
|
|
|
|
|
|
|
|
print "Run successfully"
|
|
|
|
print judge_result
|
2015-08-15 09:00:55 +00:00
|
|
|
connection = pymongo.MongoClient(host=docker_mongodb_config["host"], port=docker_mongodb_config["port"])
|
2015-08-13 10:15:00 +00:00
|
|
|
collection = connection["oj"]["oj_submission"]
|
|
|
|
collection.find_one_and_update({"_id": ObjectId(submission_id)}, {"$set": judge_result})
|
|
|
|
connection.close()
|