Judger/demo/demo.py
spxcds 7fa58f5d72 修复了不能同时写入多个log文件的问题
- 全局变量只会被初始化一次,所以下次使用的时候log文件处于打开状态
- 去掉全部的全局变量,使用传入文件指针的方式打开文件
- flock函数可以防止多线程和多进程对文件的并发修改
2016-04-26 16:18:01 +08:00

50 lines
1.7 KiB
Python

# coding=utf-8
import os
import judger
base_path = os.path.dirname(os.path.abspath(__file__))
def _compile():
return judger.run(path="/usr/bin/gcc",
in_file=os.path.join(base_path, "in"),
out_file=os.path.join(base_path, "gcc_out"),
max_cpu_time=judger.CPU_TIME_UNLIMITED,
max_memory=judger.MEMORY_UNLIMITED,
args=[os.path.join(base_path, "demo.c"), "-o", os.path.join(base_path, "demo")],
env=["PATH=" + os.environ["PATH"]],
log_path="compile.log",
use_sandbox=False,
use_nobody=False)
def run(use_sandbox, use_nobody):
print "compile result: ", _compile()
path = os.path.join(base_path, "demo")
return judger.run(path=path,
in_file=os.path.join(base_path, "in"),
out_file=os.path.join(base_path, "out"),
# ms
max_cpu_time=2000,
# Byte
max_memory=200000000,
# args env and log_path are optional
args=["1", "2", "####"],
env=["aaa=123"],
log_path="run.log",
# default is True
use_sandbox=use_sandbox,
use_nobody=use_nobody)
print "sandbox and nobody"
print run(use_sandbox=True, use_nobody=True)
print '\n\nno sandbox and root'
print run(use_sandbox=False, use_nobody=False)
print "\n\nout: ", open("out").read()
print "\n\nno sandbox and nobody"
print run(use_sandbox=False, use_nobody=True)
print "\n\nout: ", open("out").read()