def save_testset(self, testset_node): input_pattern = testset_node.find('input-path-pattern').text answer_pattern = testset_node.find('answer-path-pattern').text # TODO: group support test_id = 0 tests, samples = [], [] for test_node in testset_node.find('tests').findall('test'): test_id += 1 with open(os.path.join(self.directory, input_pattern % test_id), 'rb') as test_input: # process CRLF input_text = test_input.read().replace(b"\r\n", b"\n") with open(os.path.join(self.directory, answer_pattern % test_id), 'rb') as test_answer: answer_text = test_answer.read().replace(b"\r\n", b"\n") hash_str = case_hash(self.problem.id, input_text, answer_text) input_path, answer_path = get_input_path( hash_str), get_output_path(hash_str) with open(input_path, "wb") as test_input, open(answer_path, "wb") as test_answer: test_input.write(input_text) test_answer.write(answer_text) if 'sample' in test_node.attrib and test_node.attrib[ "sample"] == "true": samples.append(hash_str) tests.append(hash_str) self.problem.cases = ",".join(tests) self.problem.points = ",".join(["10"] * len(tests)) self.problem.sample = ",".join(samples)
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)
def post(self, *args, **kwargs): if self.problem.revisions.all().count(): revision_num = self.problem.revisions.all().aggregate( Max("revision"))["revision__max"] + 1 else: revision_num = 1 revision = Revision.objects.create( problem=self.problem, user=self.request.user, revision=revision_num, time_limit=self.problem.time_limit, memory_limit=self.problem.memory_limit) if self.problem.title or self.problem.description or self.problem.input or \ self.problem.output or self.problem.hint: statement = revision.statements.create( description=self.problem.description, input=self.problem.input, output=self.problem.output, hint=self.problem.hint, title=self.problem.title, create_time=datetime.now()) revision.active_statement = statement for t in ("checker", "validator", "interactor"): fingerprint = getattr(self.problem, t) if not fingerprint: continue program = SpecialProgram.objects.get(fingerprint=fingerprint) created_program = revision.programs.create( name=program.filename, lang=program.lang, code=program.code, tag=program.category, create_time=datetime.now()) setattr(revision, 'active_%s' % t, created_program) asset_base_dir = os.path.join(settings.UPLOAD_DIR, str(self.problem.id)) if os.path.exists(asset_base_dir): asset_files = os.listdir(asset_base_dir) for file in asset_files: file_path = os.path.join(asset_base_dir, file) with open(file_path, 'rb') as file_reader: ref_name = file[:file.find('.')] if '.' in file else file asset = revision.assets.create(name=ref_name, real_path='/upload/%d/%s' % (self.problem.id, file), create_time=datetime.now()) asset.file.save(file, File(file_reader)) cases_to_copy = self.keep_first_occurrence(self.problem.sample_list + self.problem.pretest_list + self.problem.case_list) if self.problem.group_enabled: case_to_pts = dict() group_list = self.problem.group_list revision.enable_group = True revision.group_points = self.problem.points revision.group_count = max(group_list) revision.group_dependencies = self.problem.group_config else: case_to_pts = dict( zip(self.problem.case_list, self.problem.point_list)) group_list = [0] * len(self.problem.case_list) loc = 0 for idx, fingerprint in enumerate(cases_to_copy, start=1): input_path, output_path = get_input_path( fingerprint), get_output_path(fingerprint) with open(input_path, "rb") as inf, open(output_path, "rb") as ouf: case = revision.cases.create( fingerprint=fingerprint, in_samples=fingerprint in self.problem.sample_list, in_pretests=fingerprint in self.problem.pretest_list, activated=fingerprint in self.problem.case_list, points=int(case_to_pts.get(fingerprint, 0)), case_number=idx, create_time=datetime.now(), group=group_list[loc]) case.input_file.save("input", File(inf)) case.output_file.save("output", File(ouf)) if fingerprint in self.problem.case_list: loc += 1