Ejemplo n.º 1
0
def judge(solution_id, problem_id, data_count, time_limit, mem_limit,
          program_info, result_code, language):
    low_level()
    max_mem = 0
    max_time = 0
    if language in ['java', 'python2', 'python3']:
        time_limit = time_limit * 10
        mem_limit = mem_limit * 10
        print range(data_count)
    for i in range(data_count):
        ret = judge_one(solution_id, problem_id, i + 1, time_limit + 10,
                        mem_limit, language)
        if ret == False:
            continue
        if ret['result'] == 5:
            program_info['result'] = result_code["Runtime Error"]
            return program_info
        if ret['result'] == 2:
            program_info['result'] = result_code["Time Limit Exceeded"]
            program_info['take_time'] = time_limit + 10
            return program_info
        if ret['result'] == 3:
            program_info['result'] = result_code["Memory Limit Exceeded"]
            program_info['take_memory'] = mem_limit
            return program_info

        if max_time < ret['timeused']:
            max_time = ret['timeused']
        if max_mem < ret['memoryused']:
            max_mem = ret['memoryused']

        result = judge_result(problem_id, solution_id, i + 1)
        if result == False:
            continue
        if result == "Wrong Answer" or result == "Output limit":
            program_info['result'] = result_code[result]
            break
        elif result == "Presentation Error":
            program_info[result] = result_code[result]
        # 终于是走到这一步了
        elif result == "Accepted":
            if program_info['result'] != "Presentation Error":
                program_info['result'] = result_code[result]
                # 在这里修改用户ac和题目ac

        else:
            logging.error("judge did not get result")
    program_info['take_time'] = max_time
    program_info['take_memory'] = max_mem
    return program_info
Ejemplo n.º 2
0
def compile_code(solution_id, language):
    low_level()
    language = language
    dir_work = os.path.join(config.work_dir, str(solution_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()   # 获取编译错误信息
    err_text_path = os.path.join(config.work_dir, str(solution_id), 'error.txt')

    # codecs模块帮我们在读文件时自动转换编码,直接读出unicode
    f = codecs.open(err_text_path, 'w')
    f.write(err)
    f.write(out)
    f.close()
    # 成功
    if p.returncode == 0:
        return True

    # 数据库加锁问题,未解决
    dblock.acquire()
    update_compile_info(solution_id, 7)  # 编译失败,更新题目的编译错误信息
    dblock.release()
    return False
Ejemplo n.º 3
0
def judge_one(solution_id, problem_id, data_num, time_limit, mem_limit,
              language):
    low_level()
    input_path = os.path.join(config.data_dir, str(problem_id),
                              'data%s.in' % data_num)
    try:
        input_data = open(input_path, 'r')
    except:
        return False

    output_path = os.path.join(config.work_dir, str(solution_id),
                               'out%s.txt' % data_num)
    temp_out_data = open(output_path, 'w')

    if language == 'java':
        cmd = 'java -cp %s Main' % (os.path.join(config.work_dir,
                                                 str(solution_id)))
        main_exe = shlex.split(cmd)

    elif language == 'python2':
        cmd = 'python2 %s' % (os.path.join(config.work_dir, str(solution_id),
                                           'main.pyc'))
        main_exe = shlex.split(cmd)

    elif language == 'python3':
        cmd = 'python3 %s' % (os.path.join(config.work_dir, str(solution_id),
                                           '__pycache__/main.cpython-33.pyc'))
        main_exe = shlex.split(cmd)

    else:
        main_exe = [
            os.path.join(config.work_dir, str(solution_id), 'main'),
        ]

    runfig = {
        'args': main_exe,
        'fd_in': input_data.fileno(),
        'fd_out': temp_out_data.fileno(),
        'timelimit': time_limit,
        'memorylimit': mem_limit,
    }

    low_level()
    rst = lorun.run(runfig)
    input_data.close()
    temp_out_data.close()
    logging.debug(rst)
    return rst
Ejemplo n.º 4
0
def run(problem_id, solution_id, language, data_count, user_id):
    low_level()
    dblock.acquire()
    time_limit, mem_limit = get_problem_limit(problem_id)
    dblock.release()

    program_info = {
        "solution_id": solution_id,
        "problem_id": problem_id,
        "take_time": 0,
        "take_memory": 0,
        "user_id": user_id,
        "result": 0,
    }

    result_code = {
        "Waiting": 0,
        "Accepted": 1,
        "Time Limit Exceeded": 2,
        "Memory Limit Exceeded": 3,
        "Wrong Answer": 4,
        "Runtime Error": 5,
        "Output limit": 6,
        "Compile Error": 7,
        "Presentation Error": 8,
        "System Error": 11,
        "Judging": 12,
    }

    # if check_danger_code(solution_id, language):
    #     program_info['result'] = result_code["Runtime Error"]
    #     return program_info

    # 编译
    compile_result = compile_code(solution_id, language)
    if compile_result is False:
        program_info['result'] = result_code["Compile Error"]
        return program_info

    if data_count == 0:  # 没有测试数据
        program_info['result'] = result_code["System Error"]
        return program_info

    result = judge(solution_id, problem_id, data_count, time_limit, mem_limit,
                   program_info, result_code, language)
    logging.debug(result)
    return result
Ejemplo n.º 5
0
def judge_result(program_id, solution_id, data_num):
    low_level()
    logging.debug("Judging result")
    correct_result = os.path.join(config.data_dir, str(program_id),
                                  'data%s.out' % data_num)

    user_result = os.path.join(config.work_dir, str(solution_id),
                               'out%s.txt' % data_num)

    try:
        correct = file(correct_result).read().replace('\r', '').rstrip()
        user = file(user_result).read().replace('\r', '').rstrip()
    except:
        return False
    if correct == user:
        return "Accepted"
    if correct.split() == user.split():
        return "Presentation Error"
    if correct in user:
        return "Output limit"
    return "Wrong Answer"
Ejemplo n.º 6
0
def get_code(solution_id, pro_language):

    select_code_sql = "SELECT code FROM app_problem_submit WHERE id = %s" % solution_id
    feh = run_sql(select_code_sql)
    code = feh[0][0]
    try:
        work_path = os.path.join(config.work_dir, str(solution_id))
        low_level()
        os.makedirs(work_path)
    except OSError as e:
        if (str(e).find('exist')) > 0:  # 文件夹已存在
            pass
        else:
            logging.error(e)
            return False

    try:
        real_path = os.path.join(config.work_dir, str(solution_id),
                                 config.file_name[pro_language])
    except KeyError as e:
        logging.error(e)
        return False
    print real_path
    try:
        low_level()
        f = codecs.open(real_path, 'w', 'utf-8')
        try:
            f.write(code)
        except Exception as e:
            print e.message
            logging.error("%s not write code to file" % solution_id)
            f.close()
            return False
    except OSError as e:
        logging.error(e)
        return False
    return True