2018-03-18 00:19:44 +00:00
|
|
|
import _judger
|
2016-09-02 12:04:29 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
2016-10-15 19:38:45 +00:00
|
|
|
from config import COMPILER_LOG_PATH, COMPILER_USER_UID, COMPILER_GROUP_GID
|
2016-09-02 12:04:29 +00:00
|
|
|
from exception import CompileError
|
2021-05-08 12:39:49 +00:00
|
|
|
import shlex
|
2016-09-02 12:04:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
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")
|
2021-05-08 12:39:49 +00:00
|
|
|
_command = shlex.split(command)
|
2016-09-02 12:04:29 +00:00
|
|
|
|
2018-05-18 23:16:37 +00:00
|
|
|
os.chdir(output_dir)
|
2020-07-08 01:30:49 +00:00
|
|
|
env = compile_config.get("env", [])
|
|
|
|
env.append("PATH=" + os.getenv("PATH"))
|
2016-09-02 12:04:29 +00:00
|
|
|
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"],
|
2017-05-07 09:25:54 +00:00
|
|
|
max_stack=128 * 1024 * 1024,
|
2020-07-08 01:30:49 +00:00
|
|
|
max_output_size=20 * 1024 * 1024,
|
2016-10-09 12:05:04 +00:00
|
|
|
max_process_number=_judger.UNLIMITED,
|
2018-03-18 00:19:44 +00:00
|
|
|
exe_path=_command[0],
|
2016-09-02 12:04:29 +00:00
|
|
|
# /dev/null is best, but in some system, this will call ioctl system call
|
2018-03-18 00:19:44 +00:00
|
|
|
input_path=src_path,
|
|
|
|
output_path=compiler_out,
|
|
|
|
error_path=compiler_out,
|
|
|
|
args=_command[1::],
|
2020-07-08 01:30:49 +00:00
|
|
|
env=env,
|
2016-09-02 12:04:29 +00:00
|
|
|
log_path=COMPILER_LOG_PATH,
|
2016-10-07 09:22:51 +00:00
|
|
|
seccomp_rule_name=None,
|
2016-10-15 19:38:45 +00:00
|
|
|
uid=COMPILER_USER_UID,
|
|
|
|
gid=COMPILER_GROUP_GID)
|
2016-09-02 12:04:29 +00:00
|
|
|
|
|
|
|
if result["result"] != _judger.RESULT_SUCCESS:
|
2016-10-07 05:32:30 +00:00
|
|
|
if os.path.exists(compiler_out):
|
2018-03-25 23:07:36 +00:00
|
|
|
with open(compiler_out, encoding="utf-8") as f:
|
2016-10-07 03:20:09 +00:00
|
|
|
error = f.read().strip()
|
2016-10-07 05:32:30 +00:00
|
|
|
os.remove(compiler_out)
|
|
|
|
if error:
|
|
|
|
raise CompileError(error)
|
2018-03-18 00:19:44 +00:00
|
|
|
raise CompileError("Compiler runtime error, info: %s" % json.dumps(result))
|
2016-10-07 05:32:30 +00:00
|
|
|
else:
|
|
|
|
os.remove(compiler_out)
|
|
|
|
return exe_path
|