def runone(p_path, in_path, out_path): fin = file(in_path) ftemp = file('temp.out', 'w') runcfg = { 'args': ['cd', '/home/ma6174/111/', '&&', 'java', 'Main'], 'fd_in': fin.fileno(), 'fd_out': ftemp.fileno(), 'timelimit': 2000, #in MS 'memorylimit': 65536, #in KB } rst = lorun.run(runcfg) fin.close() ftemp.close() if rst['result'] == 0: ftemp = file('temp.out') fout = file(out_path) crst = lorun.check(fout.fileno(), ftemp.fileno()) fout.close() ftemp.close() # os.remove('temp.out') if crst != 0: return {'result': crst} return rst
def run_one(exe_id, lang, in_path, out_path, time, memory): out = exe_id + ".out" fin = open(in_path) ftemp = open(out, 'w') run_map = {1: "java -cp " + return_exe_path(exe_id) + " Main", 2: "./" + exe_id} main_exe = shlex.split(run_map[lang]) print(main_exe) run_config = { 'args': main_exe, 'fd_in': fin.fileno(), 'fd_out': ftemp.fileno(), 'timelimit': time, # in MS 'memorylimit': memory, # in KB } rst = lorun.run(run_config) fin.close() ftemp.close() if rst['result'] == 0: ftemp = open(out) fout = open(out_path) crst = lorun.check(fout.fileno(), ftemp.fileno()) fout.close() ftemp.close() os.remove(out) if crst != 0: return {'result': crst} return rst
def runone(p_path, in_path, out_path): fin = open(in_path) ftemp = open("temp.out", "w") argsList = p_path.split(" ") runcfg = { "args": argsList, "fd_in": fin.fileno(), "fd_out": ftemp.fileno(), "timelimit": 2000, # in MS "memorylimit": 200000, # in KB } rst = lorun.run(runcfg) fin.close() ftemp.close() if rst["result"] == 0: ftemp = open("temp.out") fout = open(out_path) crst = lorun.check(fout.fileno(), ftemp.fileno()) fout.close() ftemp.close() os.remove("temp.out") if crst != 0: return {"result": crst} return rst
def judge(self, judger, srcPath, outPath, inFile, ansFile, memlimit, timelimit): cmd = config.langRun[judger.lang] % {'src': srcPath, 'target': outPath} fout_path = "".join([sys.path[0], "/", "%s/%d.out" % (config.dataPath["tempPath"], random.randint(0, 65536))]) if os.path.exists(fout_path): os.remove(fout_path) fin = open(inFile, 'rU') fout = open(fout_path, 'w') runcfg = { 'args': [cmd], 'fd_in': fin.fileno(), 'fd_out': fout.fileno(), 'timelimit': int(timelimit), 'memorylimit': int(memlimit) } rst = lorun.run(runcfg) fin.close() fout.close() if rst['result'] == 0: fans = open(ansFile, 'rU') fout = open(fout_path, 'rU') crst = lorun.check(fans.fileno(), fout.fileno()) fout.close() fans.close() return (RESULT_MAP[crst], int(rst['memoryused']), int(rst['timeused'])) return (RESULT_MAP[rst['result']], 0, 0)
def runone(p_path, in_path, out_path, mem_limit, tim_limit): fin = open(in_path) ftemp = open('temp.out', 'w') runcfg = { 'args':[p_path], 'fd_in':fin.fileno(), 'fd_out':ftemp.fileno(), 'timelimit':tim_limit, #in MS 'memorylimit':mem_limit*1024, #in KB } rst = lorun.run(runcfg) fin.close() ftemp.close() if rst['result'] == 0: ftemp = open('temp.out') fout = open(out_path) crst = lorun.check(fout.fileno(), ftemp.fileno()) fout.close() ftemp.close() os.remove('temp.out') if crst != 0: rst['result']=crst return rst return rst
def run(workPath, index, language, testdata_path, standout_path, limitTime, limitMemory, exec_file_name): language = language.lower() cmd = '' if language == 'java': cmd = 'java -classpath %s %s ' % (workPath, exec_file_name) elif language == 'python': cmd = 'python %s ' % (os.path.join(workPath, exec_file_name)) else: cmd = '%s ' % (os.path.join(workPath, exec_file_name)) fin = open(testdata_path) ftemppath = os.path.join(workPath, str(index) + ".out") ftemp = open(ftemppath, 'w') runcfg = { 'args': shlex.split(cmd), 'fd_in': fin.fileno(), 'fd_out': ftemp.fileno(), 'timelimit': int(limitTime), 'memorylimit': int(limitMemory) } res = lorun.run(runcfg) fin.close() ftemp.close() if res['result'] == 0: ftemp = open(ftemppath) fout = open(standout_path) res['result'] = lorun.check(fout.fileno(), ftemp.fileno()) fout.close() ftemp.close() return res
def judge(cmd, file_in, file_out, time_limit, memory_limit, kind, spj): fin = open(file_in) fout = open(file_out, 'w+') ferr = open('/bin/null') runcfg['args'] = cmd runcfg['fd_err'] = ferr.fileno() runcfg['fd_in'] = fin.fileno() runcfg['fd_out'] = fout.fileno() runcfg['timelimit'] = time_limit runcfg['memorylimit'] = memory_limit result = lorun.run(runcfg) fin.close() fout.close() if spj == 1: result = SPJudge(file_in, result) elif result['result'] == 0: file_ans = file_in.replace(".in", ".out") fans = open(file_ans) fout = open(file_out) flag = lorun.check(fans.fileno(), fout.fileno()) fout.close() fans.close() result['result'] = flag #os.remove(file_out) return result
def run(language, testdata_path, standout_path, limitTime, limitMemory): language = language.lower() cmd = '' if language == 'java': cmd = 'java -classpath %s %s ' % (cfg.tempdata_path, cfg.java_file_name_no_ext) elif language == 'python': cmd = 'python %s ' % (os.path.join( cfg.source_path, cfg.python_file_name_no_ext + ".pyc")) else: cmd = './%s ' % (cfg.exec_name) fin = open(testdata_path) ftemp = open(cfg.temp_file_name, 'w') ftemp = open(os.path.join(cfg.tempdata_path, cfg.temp_file_name), 'w') runcfg = { 'args': shlex.split(cmd), 'fd_in': fin.fileno(), 'fd_out': ftemp.fileno(), 'timelimit': int(limitTime), 'memorylimit': int(limitMemory), } res = lorun.run(runcfg) fin.close() ftemp.close() if res['result'] == 0: ftemp = open(os.path.join(cfg.tempdata_path, cfg.temp_file_name)) fout = open(standout_path) res['result'] = lorun.check(fout.fileno(), ftemp.fileno()) fout.close() ftemp.close() return res
def runone(p_path, in_path, out_path): fin = open(in_path) ftemp = open('temp.out', 'w') runcfg = { 'args': ['./m'], 'fd_in': fin.fileno(), 'fd_out': ftemp.fileno(), 'timelimit': 1000, # in MS 'memorylimit': 20000, # in KB } rst = lorun.run(runcfg) fin.close() ftemp.close() if rst['result'] == 0: ftemp = open('temp.out') fout = open(out_path) crst = lorun.check(fout.fileno(), ftemp.fileno()) fout.close() ftemp.close() os.remove('temp.out') if crst != 0: return {'result': crst} return rst
def runone(p_path, in_path, out_path): fin = open(in_path) ftemp = open('temp.out', 'w') runcfg = { 'args':['./m'], 'fd_in':fin.fileno(), 'fd_out':ftemp.fileno(), 'timelimit':1000, #in MS 'memorylimit':20000, #in KB } rst = lorun.run(runcfg) fin.close() ftemp.close() if rst['result'] == 0: ftemp = open('temp.out') fout = open(out_path) crst = lorun.check(fout.fileno(), ftemp.fileno()) fout.close() ftemp.close() os.remove('temp.out') if crst != 0: return {'result':crst} return rst
def runone(p_path, in_path, out_path): fin = file(in_path) ftemp = file('temp.out', 'w') runcfg = { 'args':['cd','/home/ma6174/111/','&&','java','Main'], 'fd_in':fin.fileno(), 'fd_out':ftemp.fileno(), 'timelimit':2000, #in MS 'memorylimit':65536, #in KB } rst = lorun.run(runcfg) fin.close() ftemp.close() if rst['result'] == 0: ftemp = file('temp.out') fout = file(out_path) crst = lorun.check(fout.fileno(), ftemp.fileno()) fout.close() ftemp.close() # os.remove('temp.out') if crst != 0: return {'result':crst} return rst
def judge(runid, pid, language): '''评测题目''' file_name = { 'C': 'main.c', 'C++': 'main.cpp', 'Python2.7': 'main.py' } get_code(runid, file_name[language]) input_file = file(os.path.join(DATA_FOLDER, ''.join([str(pid), '.in']))) output_file = file(os.path.join(DATA_FOLDER, ''.join([str(pid), '.out']))) tmp_file = file(os.path.join(TMP_FOLDER, str(runid)), 'w') time_limit = Problem.query.filter_by(pid = pid).first().time_limit memory_limit = Problem.query.filter_by(pid = pid).first().memory_limit if not compile(runid, language): return 'Compile Error', None if language == 'Python2.7': time_limit *= PYTHON_TIME_LIMIT_TIMES memory_limit *= PYTHON_MEMORY_LIMIT_TIMES cmd = 'python2.7 %s' % (os.path.join(TMP_FOLDER, 'main.pyc')) main_exe = shlex.split(cmd) else: main_exe = [os.path.join(TMP_FOLDER, 'main'), ] runcfg = { 'args': main_exe, 'fd_in': input_file.fileno(), 'fd_out': tmp_file.fileno(), 'timelimit': time_limit, 'memorylimit': memory_limit } rst = lorun.run(runcfg) tmp_file = file(os.path.join(TMP_FOLDER, str(runid))) return JUDGE_RESULT[lorun.check(output_file.fileno(), tmp_file.fileno())], rst
def judge(runid, pid, language): filename = {'C': 'main.c', 'C++': 'main.cpp', 'Python2.7': 'main.py'} #form the src into main.py/main.c get_code(runid, filename[language]) input_file = open(os.path.join(UPLOAD_FOLDER, ''.join([str(pid), '.in']))) output_file = open(os.path.join(UPLOAD_FOLDER, ''.join([str(pid), '.out']))) tmp_file = open(os.path.join(TMP_FOLDER, str(runid)), 'w') time_limit = Problem.query.get(pid).time_limit memory_limit = Problem.query.get(pid).memory_limit if not compile(runid, language): return ('Compile Error', None) if language == 'Python2.7': time_limit *= PYTHON_TIME_LIMIT_TIMES memory_limit *= PYTHON_MEMORY_LIMIT_TIMES cmd = 'python2.7 %s' % (os.path.join(TMP_FOLDER, 'main.pyc')) main_exe = shlex.split(cmd) else: main_exe = [ os.path.join(TMP_FOLDER, 'main'), ] runcfg = { 'args': main_exe, 'fd_in': input_file.fileno(), 'fd_out': tmp_file.fileno(), 'timelimit': time_limit, 'memorylimit': memory_limit } #lorun.run() returns a dict = {'memoryused': kb, 'timeused': ms, 'results': '0 if run properly'} #otherwise non-zero maybe Runtime Error rst = lorun.run(runcfg) print rst input_file.close() tmp_file.close() tmp_file = open(os.path.join(TMP_FOLDER, str(runid))) if rst['result'] == 0: #lorun.check() returns a number which means the final result crst = lorun.check(output_file.fileno(), tmp_file.fileno()) output_file.close() tmp_file.close() rst['result'] = crst print crst return JUDGE_RESULT[rst['result']], rst
def runone(in_path, out_path, ques_info, cur_name): temp_name = cur_name + ".out" fin = open(in_path) cin_text = fin.read() fin.close() fin = open(in_path) ftemp = open(temp_name, "w") runcfg = { "args" : ["./" + cur_name], "fd_in" : fin.fileno(), "fd_out" : ftemp.fileno(), "timelimit" : ques_info["t_l"], "memorylimit": ques_info["m_l"] } print("runone", runcfg["args"]) rst = lorun.run(runcfg) fin.close() ftemp.close() if rst["result"] == 0: ftemp = open(temp_name) fout = open(out_path) crst = lorun.check(fout.fileno(), ftemp.fileno()) ftemp.close() if crst != 0: ftemp = open(temp_name) _t = ftemp.read() out = fout.read() i = len(_t) - 1 while i >= 0 and ( _t[i] == '\n' or _t[i] == ' '): i -= 1 t = _t[0 : i + 1] if(t == out): crst == 0 else: rst["result"] = crst rst["cout"] = t rst["true_cout"] = out ftemp.close() fout.close() rst["cin_text"] = cin_text os.remove(temp_name) print("run_one:" , rst) return rst
def RunOne(runcfg, in_path, out_path, temp_path): fin = file(in_path) ftemp = file(temp_path, 'w') runcfg['fd_in'] = fin.fileno() runcfg['fd_out'] = ftemp.fileno() rst = lorun.run(runcfg) fin.close() ftemp.close() if rst['result'] == 0: ftemp = file(temp_path) fout = file(out_path) crst = lorun.check(fout.fileno(), ftemp.fileno()) fout.close() ftemp.close() os.remove(temp_path) if crst != 0: return {'result': crst} return rst
def judge(cmd,file_in,file_out,time_limit,memory_limit): fin = open(file_in) fout = open(file_out,'w+') runcfg['args'] = cmd.split(' ') runcfg['fd_in'] = fin.fileno() runcfg['fd_out'] = fout.fileno() runcfg['timelimit'] = time_limit runcfg['memorylimit'] = memory_limit result = lorun.run(runcfg) fin.close() fout.close() if result['result'] == 0: file_ans = file_in.split('.') file_ans = file_ans[0] + '.out' fans = open(file_ans) fout = open(file_out) flag = lorun.check(fans.fileno(), fout.fileno()) fout.close() fans.close() #os.remove(file_out) result['result'] = flag return result
def run(cmd, stdIn, stdOut, userOut, timeLimit, memoryLimit): result, fileIn, fileOut = None, None, None try: fileIn = open(stdIn, 'r') fileOut = open(userOut, 'w') runcfg = { 'args': shlex.split(cmd), 'fd_in': fileIn.fileno(), 'fd_out': fileOut.fileno(), 'timelimit': timeLimit, 'memorylimit': memoryLimit, } result = lorun.run(runcfg) except Exception as e: result = {'memoryused': 0, 'timeused': 0, 'result': 8,'errormessage': str(e)} finally: if fileIn is not None: fileIn.close() if fileOut is not None: fileOut.close() if result['result'] == 0: file1, file2 = None, None try: file1 = open(userOut, 'r') file2 = open(stdOut, 'r') rst = lorun.check(file2.fileno(), file1.fileno()) if rst != 0: result = {'memoryused': 0, 'timeused': 0, 'result': rst} except Exception as e: result = {'memoryused': 0, 'timeused': 0, 'result': 8, 'errormessage': str(e)} finally: if file1 is not None: file1.close() if file2 is not None: file2.close() else: result['memoryused'], result['timeused'] = 0, 0 return result
def runone(process, in_path, out_path, user_path, time, memory): fin = open(in_path) tmp = os.path.join(user_path, 'temp.out') ftemp = open(tmp, 'w') runcfg = { 'args': [process], 'fd_in': fin.fileno(), 'fd_out': ftemp.fileno(), 'timelimit': time, # in MS 'memorylimit': memory, # in KB } rst = lorun.run(runcfg) fin.close() ftemp.close() if rst['result'] == 0: ftemp = open(tmp) fout = open(out_path) crst = lorun.check(fout.fileno(), ftemp.fileno()) fout.close() ftemp.close() os.remove(tmp) rst['result'] = crst return rst
def execRunOnce(session_path, handel, config, td_in_path, td_out_path): my_out_filename = "%s.outdata" % handel # 获取程序运行结果输出文件的绝对路径 tMP_PATH = os.path.join(session_path, my_out_filename) # 只读打开测试数据输入样例文件 fin = open(td_in_path) # 清理遗留文件 if os.path.exists(tMP_PATH): os.remove(tMP_PATH) # 只写打开运行结果文件 ftemp = open(tMP_PATH, 'w+') # 创建评测配置信息 # java -classpath <session_path> Main if str(config.get('lang')) == 'java': args = [ 'java', '-client', '-Dfile.encoding=utf-8', '-classpath', session_path, "Main" ] else: args = [os.path.join(session_path, "m")] time_limit = config.get('time_limit', 1000) memory_limit = config.get('memory_limit', 32768) runcfg = { 'args': args, # 运行程序文件 'fd_in': fin.fileno(), 'fd_out': ftemp.fileno(), 'timelimit': time_limit, 'memorylimit': memory_limit } Base.log('[JudgeService]运行源程序!(JUDGE_PROCESS_START)') # 执行Lorun模块,运行程序 rst = lorun.run(runcfg) Base.log('[JudgeService]程序运行结束!(JUDGE_PROCESS_FINISHED)') # 释放文件 fin.close() ftemp.close() # 读取运行结果,0表示运行正常结束 if int(rst.get('result', 0)) == 0: # 只读打开运行结果文件 ftemp = open(tMP_PATH, 'r') # 只读打开测试数据输出样例文件 fout = open(td_out_path) Base.log('[JudgeService]正在检查输出!(JUDGE_DATA_CHECK)') # 调用Lorun的检查模块检查答案错误 crst = lorun.check(fout.fileno(), ftemp.fileno()) Base.log('[JudgeService]输出检查完毕!(JUDGE_DATA_CHECK_FINISHED)') # 释放文件 fout.close() ftemp.close() # 如果数据检查出错,返回结果 if crst != 0: return { 'result': crst, 'memoryused': rst.get('memoryused', 0), 'timeused': rst.get('timeused', 0) }, my_out_filename # 未通过,或者数据检查通过,都会运行到这里 return rst, my_out_filename
def test_contestsubmission(contestsubmission_id): contestsubmission = ContestSubmission.objects.get(pk=contestsubmission_id) if contestsubmission.language == 'C': f = open('temp.c','w') elif contestsubmission.language == 'C++': f = open('temp.cpp','w') elif contestsubmission.language == 'Java': f = open('Main.java','w') elif contestsubmission.language == 'Python3': f = open('temp.py','w') else: contestsubmission.result = '系统错误' contestsubmission.save() return True f.write(str(contestsubmission.code)) f.close() if contestsubmission.language == 'C': result = subprocess.getstatusoutput('gcc temp.c -o temp -lm -O2 -DONLINE_JUDGE') if result[0] != 0: contestsubmission.result = '编译错误' contestsubmission.info = result[1] contestsubmission.save() return True argv = ['./temp'] elif contestsubmission.language == 'C++': result = subprocess.getstatusoutput('g++ temp.cpp -o temp -lm -O2 -DONLINE_JUDGE') if result[0] != 0: contestsubmission.result = '编译错误' contestsubmission.info = result[1] contestsubmission.save() return True argv = ['./temp'] elif contestsubmission.language == 'Java': result = subprocess.getstatusoutput('javac Main.java') if result[0] != 0: contestsubmission.result = '编译错误' contestsubmission.info = result[1] contestsubmission.save() return True argv = ['java','Main'] elif contestsubmission.language == 'Python3': result = subprocess.getstatusoutput('python3 -m py_compile temp.py') if result[0] != 0: contestsubmission.result = '编译错误' contestsubmission.info = result[1] contestsubmission.save() return True argv = ['python3','__pycache__/temp.cpython-35.pyc'] nowpath = os.path.join(BASE_DIR,'problem','problems',str(contestsubmission.problem.id)) if not os.path.exists(nowpath): os.makedirs(nowpath) contestsubmission.result = '评测中' contestsubmission.save() maxtime = 0 maxmemory = 0 ok = 1 for file in os.listdir(nowpath): if os.path.splitext(file)[1] == '.in': file_in = os.path.join(nowpath,os.path.splitext(file)[0]+'.in') file_out = os.path.join(nowpath,os.path.splitext(file)[0]+'.out') fin = open(file_in) ftemp = open('temp.out','w') runcfg = { 'args':argv, 'fd_in':fin.fileno(), 'fd_out':ftemp.fileno(), 'timelimit':contestsubmission.problem.time_limit*1000, 'memorylimit':contestsubmission.problem.memory_limit*1024, } rst = lorun.run(runcfg) fin.close() ftemp.close() maxtime = max(maxtime,rst['timeused']) maxmemory = max(maxmemory,rst['memoryused']) if rst['result'] == 0: ftemp = open('temp.out') fout = open(file_out) crst = lorun.check(fout.fileno(), ftemp.fileno()) fout.close() ftemp.close() #os.remove('temp.out') rst['result'] = crst if rst['result'] != 0: ok = 0 contestsubmission.result = RESULT_STR[rst['result']] break if ok: contestsubmission.result = "答案正确" contestsubmission.problem.save() print(contestsubmission.contestuser.ac_num) if contestsubmission.problem not in contestsubmission.contestuser.ac_problems.all(): contestsubmission.contestuser.ac_problems.add(contestsubmission.problem) contestsubmission.contestuser.ac_num += 1 contestsubmission.contestuser.save() contestsubmission.time = maxtime+15 contestsubmission.memory = maxmemory contestsubmission.save() return True
# 运行特殊评测检查器 rst = lorun.run_checker(checkcfg) rel = rst.get("result", -1) # 如果无需运行通用检查器 if rel != system.WEJUDGE_JUDGE_EXITCODE.SPJFIN: result.judge_result = rel return result # 只读打开运行结果文件 ftemp = self.output_storage.open_file("%s.out" % case.handle, "r") # 只读打开测试数据输出样例文件 fout = tc_storage.open_file("%s.out" % case.handle, "r") _log("RUN TEXT CHECKER") # 调用Lorun的检查模块检查答案错误 rst = lorun.check(fout.fileno(), ftemp.fileno()) # 释放文件 fout.close() ftemp.close() result.judge_result = rst.get('result', -1) result.same_lines = rst.get('same_lines') result.total_lines = rst.get('total_lines') else: result.judge_result = rst.get('result', -1) return result # 返回资源限制内容 def __get_time_mem_limit(self): """
def hack_submission(hack_id): hack = Hack.objects.get(pk=hack_id) submission = hack.submission hacker = hack.myuser owner = hack.submission.myuser hack.result = "评测中" hack.save() if submission.result != "答案正确": hack.result = "系统错误" hack.save() return True nowpath = os.path.join(BASE_DIR,'problem','problems',str(submission.problem.id)) if not os.path.exists(nowpath): os.makedirs(nowpath) if not os.path.exists(nowpath+'/stand.cpp') or not os.path.exists(nowpath+'/check.cpp'): hack.result = "系统错误" hack.save() result = subprocess.getstatusoutput('g++ '+nowpath+'/check.cpp'+' -o temp -lm -O2 -DONLINE_JUDGE') if result[0] != 0: hack.result = "系统错误" hack.save() return True f = open('temp.in','w') f.write(str(hack.input_data)) f.close() fin = open('temp.in') ftemp = open('temp.out','w') runcfg = { 'args':['./temp'], 'fd_in':fin.fileno(), 'fd_out':ftemp.fileno(), 'timelimit':5000, 'memorylimit':512*1024, } rst = lorun.run(runcfg) fin.close() ftemp.close() f = open('temp.out','r') if rst['result'] != 0: hack.result = "系统错误" hack.save() return True if f.readline().split()[0] != "YES": hack.result = "数据有误" hack.save() return True result = subprocess.getstatusoutput('g++ '+nowpath+'/stand.cpp'+' -o temp -lm -O2 -DONLINE_JUDGE') if result[0] != 0: hack.result = "系统错误" hack.save() return True fin = open('temp.in') ftemp = open('temp.out','w') runcfg = { 'args':['./temp'], 'fd_in':fin.fileno(), 'fd_out':ftemp.fileno(), 'timelimit':submission.problem.time_limit*1000, 'memorylimit':submission.problem.memory_limit*1024, } rst = lorun.run(runcfg) fin.close() ftemp.close() if rst['result'] != 0: hack.result = "系统错误" hack.save() return True if submission.language == 'C': f = open('temp.c','w') elif submission.language == 'C++': f = open('temp.cpp','w') elif submission.language == 'Java': f = open('Main.java','w') elif submission.language == 'Python3': f = open('temp.py','w') f.write(str(submission.code)) f.close() if submission.language == 'C': result = subprocess.getstatusoutput('gcc temp.c -o temp -lm -O2 -DONLINE_JUDGE') argv = ['./temp'] elif submission.language == 'C++': result = subprocess.getstatusoutput('g++ temp.cpp -o temp -lm -O2 -DONLINE_JUDGE') argv = ['./temp'] elif submission.language == 'Java': result = subprocess.getstatusoutput('javac Main.java') argv = ['java','Main'] elif submission.language == 'Python3': result = subprocess.getstatusoutput('python3 -m py_compile temp.py') argv = ['python3','__pycache__/temp.cpython-35.pyc'] fin = open('temp.in') ftemp = open('temp2.out','w') runcfg = { 'args':argv, 'fd_in':fin.fileno(), 'fd_out':ftemp.fileno(), 'timelimit':submission.problem.time_limit*1000, 'memorylimit':submission.problem.memory_limit*1024, } rst = lorun.run(runcfg) fin.close() ftemp.close() ftemp1 = open('temp.out') ftemp2 = open('temp2.out') crst = lorun.check(ftemp1.fileno(),ftemp2.fileno()) ftemp1.close() ftemp2.close() os.remove('temp2.out') if rst['result'] == 0 and crst == 0: hack.result = "失败" hack.save() return True hack.result = "成功" hack.save() hacker.hack_num += 1 hacker.save() submission.problem.ac_num -= 1 submission.problem.save() submission.result = "被Hack" submission.save() for i in range(1,10000): if not os.path.exists(nowpath+'/'+str(i)+'.in'): os.rename('temp.in',str(i)+'.in') os.rename('temp.out',str(i)+'.out') shutil.move(str(i)+'.in',nowpath) shutil.move(str(i)+'.out',nowpath) break submissions = Submission.objects.filter(myuser=owner,problem=submission.problem,result="答案正确") if len(submissions) == 0: owner.ac_num -= 1 owner.ac_problems.remove(submission.problem) owner.save() return True
def judge(): try: random_prefix = uuid.uuid1().hex random_src = random_prefix + '.c' random_prog = random_prefix + '_prog' random_output = random_prefix + '.out' if 'code' not in request.form: return 'code not exists!' #write into file with open(random_src, 'w') as f: f.write(request.form['code']) #compile process = multiprocessing.Process(target=compile_binary, args=(random_prefix, )) process.start() process.join(1) if process.is_alive(): process.terminate() return 'compile error!' if not os.path.exists(random_prefix + '_prog'): os.remove(random_src) return 'compile error!' fin = open('a+b.in', 'r') ftemp = open(random_output, 'w') runcfg = { 'args': ['./' + random_prog], 'fd_in': fin.fileno(), 'fd_out': ftemp.fileno(), 'timelimit': 1000, 'memorylimit': 200000, 'trace': True, 'calls': [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 21, 25, 56, 63, 78, 79, 87, 89, 97, 102, 158, 186, 202, 218, 219, 231, 234, 273 ], 'files': { "/etc/ld.so.cache": 524288, "/lib/x86_64-linux-gnu/libc.so.6": 524288, "/lib/x86_64-linux-gnu/libm.so.6": 524288, "/usr/lib/x86_64-linux-gnu/libstdc++.so.6": 524288, "/lib/x86_64-linux-gnu/libgcc_s.so.1": 524288, "/lib/x86_64-linux-gnu/libpthread.so.0": 524288, "/etc/localtime": 524288 } } rst = lorun.run(runcfg) fin.close() ftemp.close() os.remove(random_prog) os.remove(random_src) if rst['result'] == 0: ftemp = open(random_output, 'r') fout = open('a+b.out', 'r') crst = lorun.check(fout.fileno(), ftemp.fileno()) fout.seek(0) ftemp.seek(0) standard_output = fout.read() test_output = ftemp.read() fout.close() ftemp.close() if crst != 0: msg = RESULT_STR[crst] + '<br/>' msg += 'standard output:<br/>' msg += standard_output + '<br/>' msg += 'your output:<br/>' msg += test_output os.remove(random_output) return msg os.remove(random_output) return RESULT_STR[rst['result']] except Exception as e: if os.path.exists(random_prog): os.remove(random_prog) if os.path.exists(random_src): os.remove(random_src) return 'ERROR! ' + str(e) return 'ERROR!'