2016-09-02 12:04:29 +00:00
|
|
|
# coding=utf-8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
2016-10-04 05:33:26 +00:00
|
|
|
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-04 05:33:26 +00:00
|
|
|
compiler_out = os.path.join(output_dir, str(time.time()) + "-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,
|
|
|
|
max_process_number=20,
|
|
|
|
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,
|
|
|
|
seccomp_rule_so_path=None,
|
|
|
|
uid=LOW_PRIVILEDGE_UID,
|
|
|
|
gid=LOW_PRIVILEDGE_GID)
|
|
|
|
|
|
|
|
if result["result"] != _judger.RESULT_SUCCESS:
|
|
|
|
with open(compiler_out) as f:
|
|
|
|
error = f.read().strip()
|
2016-10-01 18:05:16 +00:00
|
|
|
os.remove(compiler_out)
|
2016-09-02 12:04:29 +00:00
|
|
|
if error:
|
|
|
|
raise CompileError(error)
|
|
|
|
raise CompileError("Compiler runtime error, info: %s" % json.dumps(result).decode("utf-8"))
|
|
|
|
|
2016-10-04 05:33:26 +00:00
|
|
|
os.remove(compiler_out)
|
2016-09-02 12:04:29 +00:00
|
|
|
return exe_path
|