Example #1
0
def sync_and_update_status(server, problem):
  status, _ = ServerProblemStatus.objects.get_or_create(server=server, problem=problem)
  try:
    upload_problem_to_judge_server(problem, server)
    status.last_status = ''
  except:
    status.last_status = traceback.format_exc()
  status.save()
Example #2
0
def push_session(session):
    """
    :type session: EditSession
    :return:
    """
    problem = session.problem
    config = load_config(session)
    for type in ['checker', 'validator', 'interactor']:
        file = config[type]
        if file:
            file_config = config['program'][file]
            if not SpecialProgram.objects.filter(
                    fingerprint=file_config['fingerprint']).exists():
                SpecialProgram.objects.create(
                    fingerprint=file_config['fingerprint'],
                    lang=file_config['lang'],
                    filename=file,
                    category='checker',
                    code=read_program_file(session, file))
            setattr(problem, type, file_config['fingerprint'])
        else:
            setattr(problem, type, '')
    case_order = {}
    case_list, sample_list, pretest_list = [], [], []
    for k, v in config['case'].items():
        session_case_input, session_case_output = get_test_file_path(
            session, k)
        problem_case_input, problem_case_output = get_input_path(
            k), get_output_path(k)
        if not path.exists(problem_case_input):
            copyfile(session_case_input, problem_case_input)
            copyfile(session_case_output, problem_case_output)
        if v['order']:
            case_list.append((k, v['point']))
        if v.get('pretest'):
            pretest_list.append(k)
        if v.get('sample'):
            sample_list.append(k)
        case_order[k] = v['order']
    if case_list:
        cases, points = zip(*sorted(case_list, key=lambda x: case_order[x[0]]))
    else:
        cases, points = [], []
    pretest_list.sort(key=lambda x: case_order[x])
    sample_list.sort(key=lambda x: case_order[x])
    problem.cases = ','.join(cases)
    problem.points = ','.join(map(str, points))
    problem.pretests = ','.join(pretest_list)
    problem.sample = ','.join(sample_list)

    for server in Server.objects.filter(enabled=True).all():
        if not upload_problem_to_judge_server(problem, server):
            raise ValueError("Upload failed. Please recheck your programs.")
        server.last_synchronize_time = datetime.now()
        server.save(update_fields=['last_synchronize_time'])

    problem.save()  # update finally
    pull_session(session)
Example #3
0
    def update(self, update_statement):
        with transaction.atomic():
            path_to_problemxml = os.path.join(self.directory, 'problem.xml')
            if os.path.isfile(path_to_problemxml):
                problem_node = ElementTree.parse(path_to_problemxml)
            else:
                raise RuntimeError(
                    "problem.xml not found or couldn't be opened")

            self.problem.alias = problem_node.getroot().attrib["short-name"]

            if update_statement:
                for statement_node in problem_node.find('statements').findall(
                        'statement'):
                    if self.save_statement(statement_node):
                        break

            self.save_judging_node(problem_node.find("judging"))

            files_node = problem_node.find('files')
            if files_node is not None:
                resources_node = files_node.find('resources')
                if resources_node is not None:
                    # TODO: process resource
                    pass
                attachments_node = files_node.find('attachments')
                if attachments_node is not None:
                    # TODO: process attachments
                    for attachment_node in attachments_node.findall('file'):
                        pass
                executables_node = files_node.find('executables')
                if executables_node is not None:
                    for executable_node in executables_node.findall(
                            'executable'):
                        # TODO: process executables
                        pass

            assets_node = problem_node.find('assets')
            for checker_node in assets_node.findall('checker'):
                source_node = checker_node.find('source')
                self.set_utility_file(source_node.attrib['path'],
                                      source_node.attrib['type'], 'checker')
            validators_node = assets_node.find('validators')
            if validators_node is not None:
                for validator_node in validators_node.findall('validator'):
                    source_node = validator_node.find('source')
                    self.set_utility_file(source_node.attrib['path'],
                                          source_node.attrib['type'],
                                          'validator')

            for testset_node in problem_node.find('judging').findall(
                    'testset'):
                testset_name = testset_node.attrib['name']
                if testset_name != "tests":
                    # EOJ doesn't support multiple testset
                    continue
                self.save_testset(testset_node)
                break

            for server in Server.objects.filter(enabled=True).all():
                upload_problem_to_judge_server(self.problem, server)
                status, _ = ServerProblemStatus.objects.get_or_create(
                    problem=self.problem, server=server)
                status.save()
            self.problem.save()
Example #4
0
    def post(self, request, *args, **kwargs):
        if self.errors:
            return HttpResponse()
        for attr in ("title", "description", "input", "output", "hint"):
            setattr(self.problem, attr,
                    getattr(self.revision.active_statement, attr))
        for t in ("checker", "validator", "interactor"):
            program = getattr(self.revision, "active_%s" % t)
            if program is None:
                setattr(self.problem, t, '')
            else:
                try:
                    SpecialProgram.objects.get(fingerprint=program.fingerprint)
                except SpecialProgram.DoesNotExist:
                    SpecialProgram.objects.create(
                        fingerprint=program.fingerprint,
                        lang=program.lang,
                        filename=program.name,
                        category=program.tag,
                        code=program.code)
                setattr(self.problem, t, program.fingerprint)

        samples, pretests, tests, points = [], [], [], []
        case_prep = [[] for i in range(self.revision.group_count)]
        for case in self.revision.cases.all().order_by("case_number"):
            if case.in_samples: samples.append(case.fingerprint)
            if case.in_pretests: pretests.append(case.fingerprint)
            if case.activated:
                tests.append(case.fingerprint)
                points.append(case.points)
            problem_case_input, problem_case_output = get_input_path(
                case.fingerprint), get_output_path(case.fingerprint)
            if not os.path.exists(problem_case_input):
                copyfile(case.input_file.path, problem_case_input)
                copyfile(case.output_file.path, problem_case_output)
            if self.revision.enable_group:
                case_prep[case.group - 1].append(case.fingerprint)

        if self.revision.enable_group:
            self.problem.cases = ';'.join(map(lambda x: ','.join(x),
                                              case_prep))
            self.problem.points = self.revision.group_points
            self.problem.group_config = self.revision.group_dependencies
        else:
            self.problem.cases = ','.join(tests)
            self.problem.points = ','.join(map(str, points))
            self.problem.group_config = '~'  # ~ means nothing here, since nothing is already taken...
        self.problem.pretests = ','.join(pretests)
        self.problem.sample = ','.join(samples)

        template_dict = {
            template.language: {
                "template": template.template_code,
                "grader": template.grader_code
            }
            for template in self.revision.templates.all()
        }
        self.problem.template_config = json.dumps(template_dict)

        self.problem.time_limit = self.revision.time_limit
        self.problem.memory_limit = self.revision.memory_limit

        try:
            for server in Server.objects.filter(enabled=True).all():
                upload_problem_to_judge_server(self.problem, server)
                status, _ = ServerProblemStatus.objects.get_or_create(
                    problem=self.problem, server=server)
                status.save()
                server.last_synchronize_time = datetime.now()
                server.save(update_fields=['last_synchronize_time'])
            self.problem.save()
            self.revision.status = 1
            self.revision.save(update_fields=["status"])
        except Exception as e:
            messages.error(self.request, "Upload failed: " + str(e))
        return redirect(reverse('polygon:revision_update', kwargs=self.kwargs))