# coding=utf-8 from __future__ import unicode_literals import json import os import time 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"]) command = command.format(src_path=src_path, exe_dir=output_dir, exe_path=exe_path) compiler_out = os.path.join(output_dir, str(time.time()) + "-compiler.out") _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() os.remove(compiler_out) if error: raise CompileError(error) raise CompileError("Compiler runtime error, info: %s" % json.dumps(result).decode("utf-8")) os.remove(compiler_out) return exe_path