JudgeServer/compiler.py

50 lines
2.1 KiB
Python
Raw Normal View History

2016-09-02 12:04:29 +00:00
# coding=utf-8
from __future__ import unicode_literals
import json
import os
import time
2016-09-02 12:04:29 +00:00
import _judger
from config import COMPILER_LOG_PATH, LOW_PRIVILEDGE_UID, LOW_PRIVILEDGE_GID
from exception import CompileError
class Compiler(object):
def compile(self, compile_config, src_path, output_dir):
command = compile_config["compile_command"]
exe_path = os.path.join(output_dir, compile_config["exe_name"])
2016-09-11 02:35:08 +00:00
command = command.format(src_path=src_path, exe_dir=output_dir, exe_path=exe_path)
2016-10-07 05:32:30 +00:00
compiler_out = os.path.join(output_dir, "compiler.out")
2016-09-02 12:04:29 +00:00
_command = command.split(" ")
result = _judger.run(max_cpu_time=compile_config["max_cpu_time"],
max_real_time=compile_config["max_real_time"],
max_memory=compile_config["max_memory"],
max_output_size=1024 * 1024,
2016-10-09 12:05:04 +00:00
max_process_number=_judger.UNLIMITED,
2016-09-02 12:04:29 +00:00
exe_path=_command[0],
# /dev/null is best, but in some system, this will call ioctl system call
input_path=src_path,
output_path=compiler_out,
error_path=compiler_out,
args=[item.encode("utf-8") for item in _command[1::]],
env=[("PATH=" + os.getenv("PATH")).encode("utf-8")],
log_path=COMPILER_LOG_PATH,
2016-10-07 09:22:51 +00:00
seccomp_rule_name=None,
2016-09-02 12:04:29 +00:00
uid=LOW_PRIVILEDGE_UID,
gid=LOW_PRIVILEDGE_GID)
if result["result"] != _judger.RESULT_SUCCESS:
2016-10-07 05:32:30 +00:00
if os.path.exists(compiler_out):
2016-10-07 03:20:09 +00:00
with open(compiler_out) as f:
error = f.read().strip()
2016-10-07 05:32:30 +00:00
os.remove(compiler_out)
if error:
raise CompileError(error)
raise CompileError("Compiler runtime error, info: %s" % json.dumps(result).decode("utf-8"))
else:
os.remove(compiler_out)
return exe_path