Exemple #1
0
def main(answer, database_id):
    config_parser = configparser.ConfigParser()
    if len(config_parser.read(CONFIG_FILE_PATH)) == 0:
        return construct_json_response(RESPONSE_CODE['FAIL'], None, 'Can not load config.ini.')
    MYSQL_JUDGE_DB_HOST, MYSQL_JUDGE_DB_PORT, MYSQL_JUDGE_DB_USERNAME, MYSQL_JUDGE_DB_PASSWORD, MYSQL_JUDGE_DB_CHARSET, DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_CHARSET = init_config(
        config_parser)
    main_db_conn = pymysql.connect(
        host=DB_HOST,
        user=DB_USERNAME,
        password=DB_PASSWORD,
        database=DB_DATABASE,
        charset=DB_CHARSET
    )
    main_db_cursor = main_db_conn.cursor()
    create_table, test_data = get_database_detail(main_db_cursor, database_id)
    if create_table is None or test_data is None:
        main_db_cursor.close()
        main_db_conn.close()
        return construct_json_response(RESPONSE_CODE['FAIL'], None, '无法找到该题目对应数据库信息')

    main_db_cursor.close()
    main_db_conn.close()
    true_result, run_exception = get_true_result(MYSQL_JUDGE_DB_HOST, MYSQL_JUDGE_DB_PORT, MYSQL_JUDGE_DB_USERNAME,
                                                 MYSQL_JUDGE_DB_PASSWORD,
                                                 MYSQL_JUDGE_DB_CHARSET, answer, create_table, test_data)

    if true_result is None:
        return construct_json_response(RESPONSE_CODE['FAIL'], None, run_exception)
    else:
        return construct_json_response(RESPONSE_CODE['OK'], {
            'trueResult': true_result
        }, None)
Exemple #2
0
def judge(SQLITE_DIR, SQLITE_TEMP_DIR, cursor, solution_id):
    problem_id, source_code = get_solution_detail_by_solution_id(cursor, solution_id)
    # logging.info('Start judging solution {}'.format(solution_id))
    database_id, answer = get_problem_detail_by_problem_id(cursor, problem_id)
    answer = answer.strip()
    sqlite_db_file_path = os.path.join(SQLITE_DIR, '{}.db'.format(database_id))
    if not os.path.exists(sqlite_db_file_path):
        return construct_json_response(RESPONSE_CODE['NO_DB_FILE'], None, '无法找到该题目对应数据库文件')
    temp_sqlite_db_file_path = os.path.join(SQLITE_TEMP_DIR, '{}_{}_temp.db'.format(database_id, solution_id))
    shutil.copyfile(sqlite_db_file_path, temp_sqlite_db_file_path)
    true_result = get_true_result_by_problem_id(cursor, problem_id)
    if not is_select_problem(answer):
        last_select_code = get_last_select_code_in_answer(answer)
        exec_script_result, run_exception = exec_script(temp_sqlite_db_file_path, source_code)
        if exec_script_result:
            exec_result, exec_exception = exec_code(temp_sqlite_db_file_path, last_select_code)
        else:
            exec_result = None
            exec_exception = run_exception
    else:
        exec_result, exec_exception = exec_code(temp_sqlite_db_file_path, source_code)
    if exec_result is None:
        judge_result_index = SOLUTION_RESULT['Compile Error']
    else:
        exec_result = hashlib.md5(exec_result.encode(encoding='UTF-8')).hexdigest().upper()
        if exec_result == true_result:
            judge_result_index = SOLUTION_RESULT['Accepted']
        else:
            judge_result_index = SOLUTION_RESULT['Wrong Answer']
    os.remove(temp_sqlite_db_file_path)
    return construct_json_response(RESPONSE_CODE['OK'], {
        'solutionId': solution_id,
        'result': judge_result_index,
        'runError': exec_exception,
    }, None)
def main(database_id, create_table, test_data):
    config_parser = configparser.ConfigParser()
    if len(config_parser.read(CONFIG_FILE_PATH)) == 0:
        return construct_json_response(RESPONSE_CODE['FAIL'], None,
                                       'Can not load config.ini.')
    SQLITE_DIR, SQLITE_TEMP_DIR, DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_CHARSET = init_config(
        config_parser)
    init_work_directory(SQLITE_DIR)
    create_result, create_exception = create_database(SQLITE_DIR, database_id,
                                                      create_table, test_data)
    if create_result:
        return construct_json_response(RESPONSE_CODE['OK'], None, 'OK')
    else:
        return construct_json_response(RESPONSE_CODE['FAIL'], None,
                                       create_exception)
Exemple #4
0
def main(answer, database_id):
    config_parser = configparser.ConfigParser()
    if len(config_parser.read(CONFIG_FILE_PATH)) == 0:
        return construct_json_response(RESPONSE_CODE['FAIL'], None,
                                       'Can not load config.ini.')
    SQLITE_DIR, SQLITE_TEMP_DIR, DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_CHARSET = init_config(
        config_parser)
    init_work_directory(SQLITE_TEMP_DIR)
    sqlite_db_file_path = os.path.join(SQLITE_DIR, '{}.db'.format(database_id))
    if not os.path.exists(sqlite_db_file_path):
        return construct_json_response(RESPONSE_CODE['NO_DB_FILE'], None,
                                       '无法找到该题目对应数据库文件')
    true_result, run_exception = get_true_result(SQLITE_DIR, SQLITE_TEMP_DIR,
                                                 answer, database_id)
    if true_result is None:
        return construct_json_response(RESPONSE_CODE['FAIL'], None,
                                       run_exception)
    else:
        return construct_json_response(RESPONSE_CODE['OK'],
                                       {'trueResult': true_result}, None)
Exemple #5
0
def main(solution_id):
    config_parser = configparser.ConfigParser()
    if len(config_parser.read(CONFIG_FILE_PATH)) == 0:
        return construct_json_response(RESPONSE_CODE['FAIL'], None, 'Can not load config.ini.')
    SQLITE_DIR, SQLITE_TEMP_DIR, DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_CHARSET = init_config(config_parser)
    init_work_directory(SQLITE_TEMP_DIR)
    conn = pymysql.connect(
        host=DB_HOST,
        user=DB_USERNAME,
        password=DB_PASSWORD,
        database=DB_DATABASE,
        charset=DB_CHARSET
    )
    cursor = conn.cursor()
    result = judge(SQLITE_DIR, SQLITE_TEMP_DIR, cursor, solution_id)
    cursor.close()
    conn.close()
    return result