不同的语言执行不同的代码编译命令。
代码编译成功后进行子程序的判断运行。
def compile(solution_id, language, program_info, dblock):
low_level()
'''将程序编译成可执行文件'''
language = language.lower()
language_to_cmd={
"c": "gcc",
"c++": "g++",
"python": "python2",
}
if language in language_to_cmd.keys():
language = language_to_cmd[language]
dir_work = os.path.join(config.work_dir, str(solution_id))
build_cmd = {
"gcc":
"gcc main.c -o main",
#"gcc main.c -o main -Wall -lm -O2 -std=c99 --static -DONLINE_JUDGE",
"g++":
#"g++ main.cpp -O2 -Wall -lm --static -DONLINE_JUDGE -o main",
"g++ main.cpp -o main",
"java": "javac Main.java",
"ruby": "reek main.rb",
"perl": "perl -c main.pl",
"pascal": 'fpc main.pas -O2 -Co -Ct -Ci',
"go": '/opt/golang/bin/go build -ldflags "-s -w" main.go',
"lua": 'luac -o main main.lua',
"python2": 'python2 -m py_compile main.py',
"python3": 'python3 -m py_compile main.py',
"haskell": "ghc -o main main.hs",
}
if language not in build_cmd.keys():
return False
p = subprocess.Popen(
build_cmd[language],
shell=True,
cwd=dir_work,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate() # 获取编译错误信息
logging.info("complie info (empty means OK): " + out+err)
err_txt_path = os.path.join(config.work_dir, str(solution_id), 'error.txt')
f = file(err_txt_path, 'w')
f.write(err)
f.write(out)
f.close()
if p.returncode == 0: # 返回值为0,编译成功
return True
program_info["compile_info"] = err + out
#dblock.acquire()
#update_compile_info(solution_id, err + out) # 编译失败,更新题目的编译错误信息
#dblock.release()
return False