使用 judger 运行编译器
This commit is contained in:
@@ -1,42 +1,39 @@
|
|||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
import judger
|
||||||
import commands
|
import commands
|
||||||
|
|
||||||
from settings import lrun_uid, lrun_gid
|
|
||||||
from judge_exceptions import CompileError, JudgeClientError
|
from judge_exceptions import CompileError, JudgeClientError
|
||||||
from utils import parse_lrun_output
|
|
||||||
from logger import logger
|
from logger import logger
|
||||||
|
from settings import judger_workspace
|
||||||
|
|
||||||
|
|
||||||
def compile_(language_item, src_path, exe_path):
|
def compile_(language_item, src_path, exe_path):
|
||||||
compile_command = language_item["compile_command"].format(src_path=src_path, exe_path=exe_path)
|
compile_command = language_item["compile_command"].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(judger_workspace, str(time.time()) + ".out")
|
||||||
|
|
||||||
# 防止编译器卡死 或者 include </dev/random>等
|
compile_result = judger.run(exe_path=compiler,
|
||||||
execute_command = "lrun" + \
|
in_file="/dev/null",
|
||||||
" --max-real-time 5" + \
|
out_file=compiler_output_file,
|
||||||
" --uid " + str(lrun_uid) + \
|
max_cpu_time=2000,
|
||||||
" --gid " + str(lrun_gid) + \
|
max_memory=200000000,
|
||||||
" " + \
|
args=compile_args,
|
||||||
compile_command + \
|
env=["PATH=" + os.environ["PATH"]],
|
||||||
" 3>&2"
|
use_sandbox=False)
|
||||||
status, output = commands.getstatusoutput(execute_command)
|
|
||||||
|
|
||||||
output_start = output.rfind("MEMORY")
|
compile_output_handler = open(compiler_output_file)
|
||||||
|
compile_output = compile_output_handler.read()
|
||||||
|
compile_output_handler.close()
|
||||||
|
os.remove(compiler_output_file)
|
||||||
|
|
||||||
if output_start == -1:
|
if compile_result["flag"] != 0:
|
||||||
logger.error("Compiler error")
|
logger.error("Compiler error")
|
||||||
logger.error(output)
|
logger.error(compile_output)
|
||||||
raise JudgeClientError("Error running compiler in lrun")
|
logger.error(str(compile_result))
|
||||||
|
|
||||||
# 返回值不为 0 或者 stderr 中 lrun 的输出之前有 erro r字符串
|
|
||||||
# 判断 error 字符串的原因是链接的时候可能会有一些不推荐使用的函数的的警告,
|
|
||||||
# 但是 -w 参数并不能关闭链接时的警告
|
|
||||||
if status or "error" in output[0:output_start]:
|
|
||||||
raise CompileError(output[0:output_start])
|
|
||||||
|
|
||||||
parse_result = parse_lrun_output(output[output_start:])
|
|
||||||
|
|
||||||
if parse_result["exit_code"] or parse_result["term_sig"] or parse_result["siginaled"] or parse_result["exceed"]:
|
|
||||||
logger.error("Compiler error")
|
|
||||||
logger.error(output)
|
|
||||||
raise CompileError("Compile error")
|
raise CompileError("Compile error")
|
||||||
return exe_path
|
else:
|
||||||
|
if "error" in compile_output:
|
||||||
|
raise CompileError(compile_output)
|
||||||
|
return exe_path
|
||||||
|
|||||||
@@ -14,11 +14,3 @@ lrun_gid = 1002
|
|||||||
|
|
||||||
# judger工作目录
|
# judger工作目录
|
||||||
judger_workspace = "/var/judger/"
|
judger_workspace = "/var/judger/"
|
||||||
|
|
||||||
submission_db = {
|
|
||||||
"host": os.environ.get("MYSQL_PORT_3306_TCP_ADDR", "127.0.0.1"),
|
|
||||||
"port": 3306,
|
|
||||||
"db": "oj_submission",
|
|
||||||
"user": "root",
|
|
||||||
"password": os.environ.get("MYSQL_ENV_MYSQL_ROOT_PASSWORD", "root")
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user