Example #1
0
def execute_compilation(solution: Solution, compile_command: str) -> bool:
    error_file = open(join(solution.get_directory(), COMPILE_ERROR_FILENAME),
                      'w+')
    bash_command = [
        COMPILE_SCRIPT_PATH,
        solution.get_directory(), compile_command
    ]
    subprocess.Popen(bash_command, stderr=error_file).wait()
    if error_occurred(error_file):
        handle_compile_error(solution, error_file)
        return False
    return True
Example #2
0
def add_solution(exercise: Exercise, member: Member, file: FileStorage,
                 ip_address: str, attempt_nr: int, os_info: str):
    filename = secure_filename(file.filename)
    solution = Solution(filename=filename,
                        ip_address=ip_address,
                        send_date=get_current_date(),
                        os_info=os_info,
                        attempt=attempt_nr)
    exercise.solutions.append(solution)
    member.solutions.append(solution)
    solution_directory = solution.get_directory()
    create_directory(solution_directory)
    file.save(join(solution_directory, solution.filename))
    unpack_file(solution.filename, solution_directory)
    db.session.commit()
    solution.enqueue_execution()
Example #3
0
 def patch(self, id_):
     solution = Solution.get_by_id(id_)
     if solution is None:
         raise NotFound()
     status = modify_solution_parser.parse_args()['status']
     solution.modify(
         status='{} modify status to {}'.format(g.user.id, status))
Example #4
0
    def post(self):
        args = create_solution_parser.parse_args()
        problem = Problem.get_by_id(args['problem_id'])
        if args['contest_id']:
            contest = Contest.get_by_id(args['contest_id'])
            if contest.status != 2:
                raise ParameterException('contest not available')
            if problem.id not in [i.id for i in contest.problem_list]:
                raise ParameterException('problem not in contest')

        real_language = Language.search(oj=problem.remote_oj,
                                        key=args['language'])['data']
        if not real_language:
            raise ParameterException('language does not exist')
        real_language = real_language[0].value
        old_language = args['language']
        args['language'] = real_language
        solution = Solution.create(**args,
                                   user_id=g.user.id,
                                   status='Local info: Create solution')
        async_submit_code(solution.id, solution.problem_id, old_language,
                          solution.code)
        return {
            'message': 'create solution success',
            'solution_id': solution.id
        }, 201
Example #5
0
def clear_directory(solution: Solution):
    solution_dir = solution.get_directory()
    files = [f for f in listdir(solution_dir) if isfile(join(solution_dir, f))]
    for file in files:
        if file != solution.filename:
            if not file.endswith(OUTPUT_FILE_SUFFIX) or \
                    (file.endswith(OUTPUT_FILE_SUFFIX) and solution.output_file is None):
                os.remove(join(solution_dir, file))
Example #6
0
def grade(solution: Solution):
    exercise = solution.exercise
    run_command = exercise.run_command
    output_file_name = exercise.program_name + OUTPUT_FILE_SUFFIX
    for test in exercise.get_sorted_tests():
        error_file = open(join(solution.get_directory(), ERROR_TEST_FILENAME),
                          'w+')
        command = [
            RUN_SCRIPT_PATH,
            solution.get_directory(),
            test.get_input_path(),
            test.get_output_path(), run_command, output_file_name
        ]
        process = subprocess.Popen(command,
                                   stderr=error_file,
                                   preexec_fn=limit_memory())
        try:
            process.communicate(timeout=test.timeout)
            if error_occurred(error_file):
                handle_test_error(solution, error_file)
                break
            else:
                if process.returncode == SUCCESS_RETURN_CODE:
                    solution.test_passed(test.points)
                else:
                    solution.output_file = output_file_name
                    break
        except subprocess.TimeoutExpired:
            error_file.close()
            solution.timeout_occurred()
            break
        finally:
            kill_processes(process.pid)
Example #7
0
 def post(self, id_):
     solution = Solution.get_by_id(id_)
     if solution is None:
         raise NotFound()
     if solution.status_canonical != 'OTHER' and g.user.permission != -1:
         raise Forbidden()
     problem = Problem.get_by_id(solution.problem_id)
     old_language = Language.search(oj=problem.remote_oj,
                                    value=solution.language)['data'][0].key
     solution.modify(status='Local info: Start rejudge')
     async_submit_code(solution.id, solution.problem_id, old_language,
                       solution.code)
     return {'message': 'create rejudge success'}, 201
Example #8
0
def submit_code(solution_id, problem_id, language, code):
    solution = Solution.get_by_id(solution_id)
    g.solution = solution
    problem = Problem.get_by_id(problem_id)
    remote_user = get_remote_user(problem.remote_oj)
    solution.modify(status='Local info: Assign remote user: {}'.format(
        remote_user.username),
                    remote_user_id=remote_user.id)
    spider = globals()[remote_user.oj.title() + 'Spider'](remote_user)
    solution.modify(status='Local info: Submitting')
    res = spider.submit(problem.remote_oj, problem.remote_prob, language, code)
    remote_user.modify(status=1)
    if not res.get('success'):
        solution.modify(status='Remote info: {}'.format(res.get('error')),
                        processing=0)
        return
    remote_id = res['remote_id']
    solution.modify(status='Local info: Get remote id: {}'.format(remote_id),
                    remote_id=remote_id)
    check_status(spider, solution)
Example #9
0
def handle_test_error(solution: Solution, error_file):
    solution.status = Solution.Status['TEST_ERROR']
    with open(error_file.name) as f:
        solution.error_msg = clear_error_msg(f.read(), RUN_SCRIPT_NAME)
Example #10
0
def handle_compile_error(solution: Solution, error_file):
    solution.status = Solution.Status['COMPILE_ERROR']
    with open(error_file.name) as f:
        solution.error_msg = clear_error_msg(f.read(), COMPILE_SCRIPT_NAME)
Example #11
0
 def get(self, id_):
     solution = Solution.get_by_id(id_)
     if solution is None:
         raise NotFound()
     return solution
Example #12
0
 def post(self):
     return Solution.search(**search_solution_parser.parse_args())
Example #13
0
def validate_solution_teacher(member: Member, required_role: str,
                              solution: Solution):
    validate_exists(solution)
    validate_course(member, required_role, solution.get_course())
Example #14
0
def validate_solution_student(member: Member, required_role: str,
                              solution: Solution):
    validate_exists(solution)
    validate_course(member, required_role, solution.get_course())
    if solution not in member.solutions:
        abort(404)