def compile(submission_id, language_id): secure.low_level() '''将程序编译成可执行文件''' language = language.get_lang(language_id).lower() dir_work = os.path.join(config.work_dir, str(submission_id)) build_cmd = { "gcc": "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", "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() # 获取编译错误信息 if p.returncode == 0: # 返回值为0,编译成功 return True init.dblock.acquire() update.update_compile_info(submission_id, err + out) # 编译失败,更新题目的编译错误信息 init.dblock.release() return False
def get_input(submission_id): '''从数据库获取input并写入data目录下对应的文件''' sql= 'SELECT input FROM ' + config.db_table_name + ' WHERE submission_id = ' + str(submission_id) + ';' data = db.run_sql(sql) input_data = data[0][0] file_name = str(submission_id) + '.in' try: data_path = os.path.join(config.data_dir, str(submission_id)) secure.low_level() os.mkdir(data_path) except OSError as e: if str(e).find('exist') > 0: # 文件夹已经存在 pass else: return False try: real_path = os.path.join(config.data_dir, str(submission_id), file_name) except KeyError as e: return False try: log.log_info('Importing input, %d' % submission_id) secure.low_level() f = open(real_path, 'w') try: f.write(input_data) except: f.close() log.log_error('Importing input failed, %d' % submission_id) return False f.close() except OSError as e: return False return True
def judge(submission_id, time_limit, mem_limit, language_id): secure.low_level() '''评测编译类型语言''' max_mem = 0 max_time = 0 ret = run.before_run(submission_id, time_limit + 10, mem_limit, language_id) if ret == False: return false program_info['take_time'] = ret['timeused'] program_info['take_memory'] = ret['memoryused'] program_info['result'] = status.DONE if ret['result'] == 5: program_info['result'] = status.RUNTIME_ERROR elif ret['result'] == 2: program_info['take_time'] = time_limit program_info['result'] = status.TIME_LIMIT_EXCEEDE elif ret['result'] == 3: program_info['result'] = status.MEMORY_LIMIT_EXCEEDE program_info['take_memory'] = mem_limit return program_info
def get_code(submission_id): '''从数据库获取代码并写入work目录下对应的文件''' sql = 'SELECT code, language FROM ' + config.db_table_name + ' WHERE submission_id = ' + str(submission_id) + ';' data = db.run_sql(sql) if data != False: code = data[0][0] language_id = data[0][1] file_name = language.get_filename(language_id) else: return False try: work_path = os.path.join(config.work_dir, str(submission_id)) secure.low_level() os.mkdir(work_path) except OSError as e: if str(e).find('exist') > 0: # 文件夹已经存在 pass else: return False try: real_path = os.path.join(config.work_dir, str(submission_id), file_name) except KeyError as e: # print e return False try: log.log_info('Importing code, %d' % submission_id) secure.low_level() f = open(real_path, 'w') try: f.write(code) except: f.close() log.log_error('Importing code failed, %d' % submission_id) return False f.close() except OSError as e: return False return True
def before_run(submission_id, time, memory, language_id): secure.low_level() '''获取程序执行时间和内存''' if secure.check_dangerous_code(submission_id, language_id) is False: update.update_status(submission_id, status.DANGEROUS_CODE) log.log_error('Has DANGEROUS code, %d' % submission_id) return False update.update_status(submission_id, status.COMPILEING) compile_result = compiler.compile(submission_id, language_id) if compile_result is False: # 编译错误 update.update_status(submission_id, status.COMPILATION_ERROR) log.log_error('Compile failed, %d' % submission_id) return False log.log_info('Compile completed, %d' % submission_id) result = run( solution_id, time_limit, mem_limit, program_info, language_id) return result