import lorun import os RESULT_STR = [ 'Accepted', 'Presentation Error', 'Time Limit Exceeded', 'Memory Limit Exceeded', 'Wrong Answer', 'Runtime Error', 'Output Limit Exceeded', 'Compile Error', 'System Error' ] def compile_c(src_path): if os.system('gcc %s -o main' % src_path) != 0: return False return True def compile_cpp(src_path): if os.system('g++ %s -o main' % src_path) != 0: return False return True def compile_java(src_path): if os.system('javac %s' % src_path) != 0: return False return True def compile_python(src_path): if os.system('python3 -m py_compile %s' % src_path) != 0: return False if os.path.exists('py_compile'): os.remove('py_compile') return True def run(args, in_path, out_path, time_limit, memory_limit): fin = open(in_path) ftemp = open('temp.out', 'w') runcfg = { 'args': args, 'fd_in': fin.fileno(), 'fd_out': ftemp.fileno(), 'timelimit': time_limit, # in MS 'memorylimit': memory_limit, # 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(src_path, td_path, td_total, time_limit, memory_limit, language): args = [] src_file = '' rst_list = [] if language == 'c': if not compile_c(src_path): return {'result_list': [{'result': RESULT_STR[7]}]} args = ['./main'] src_file = 'main' elif language == 'cpp': if not compile_cpp(src_path): return {'result_list': [{'result': RESULT_STR[7]}]} args = ['./main'] src_file = 'main' elif language == 'java': if not compile_java(src_path): return {'result_list': [{'result': RESULT_STR[7]}]} args = ['java', 'Main'] src_file = 'Main.class' time_limit *= 3 elif language == 'python': if not compile_python(src_path): return {'result_list': [{'result': RESULT_STR[7]}]} args = ['python3', 'main.py'] src_file = 'main.pyc' for i in range(td_total): in_path = os.path.join(td_path, '%d.in' % i) out_path = os.path.join(td_path, '%d.out' % i) if os.path.isfile(in_path) and os.path.isfile(out_path): rst = run(args, in_path, out_path, time_limit, memory_limit) rst['result'] = RESULT_STR[rst['result']] rst_list.append(rst) else: os.remove(src_file) rst_list.append({'result': RESULT_STR[8]}) return {'result_list': rst_list, 'message': 'test data:%d incompleted' % i} if os.path.exists(src_file): os.remove(src_file) return {'result_list': rst_list}