OnlineJudge/judger/run.py

56 lines
1.6 KiB
Python
Raw Normal View History

2015-08-12 02:24:06 +00:00
# coding=utf-8
import sys
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
from settings import judger_workspace, 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
solution_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]
connection = pymongo.MongoClient(host=mongodb_config["host"], port=mongodb_config["port"])
collection = connection["oj"]["oj_submission"]
submission = collection.find_one({"_id": ObjectId(solution_id)})
if not submission:
exit()
# 将代码写入文件
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")
f.write(submission["code"])
2015-08-12 02:24:06 +00:00
f.close()
# 编译
2015-08-12 02:24:06 +00:00
try:
exe_path = compile_(language, src_path, judger_workspace + "run/")
2015-08-12 02:24:06 +00:00
except Exception as e:
print {"result": result["compile_error"]}
2015-08-12 02:24:06 +00:00
exit()
2015-08-12 08:49:25 +00:00
try:
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),
test_case_dir= judger_workspace + "test_case/" + test_case_id + "/")
2015-08-12 08:49:25 +00:00
print client.run()
except Exception as e:
print e
print {"result": result["system_error"]}
2015-08-12 02:24:06 +00:00