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 save_case(session, input_binary, output_binary, raw_fingerprint=None, **kwargs): fingerprint = case_hash(session.problem_id, input_binary, output_binary) new_input_path, new_output_path = get_test_file_path(session, fingerprint) config = load_config(session) if raw_fingerprint: config['case'][fingerprint] = config['case'].pop(raw_fingerprint) old_input_path, old_output_path = get_test_file_path( session, raw_fingerprint) replace(old_input_path, new_input_path) replace(old_output_path, new_output_path) else: # This is a new case, should have point, order kwargs.setdefault('point', 10) try: already_have = max( map(lambda d: d['order'], config['case'].values())) except ValueError: already_have = 0 kwargs.setdefault('order', already_have + 1) with open(new_input_path, 'wb') as fs, open(new_output_path, 'wb') as gs: fs.write(input_binary) gs.write(output_binary) config['case'].setdefault(fingerprint, dict()) config['case'][fingerprint].update(**kwargs) dump_config(session, config)
def save_fingerprint(self, problem_id): self.input_file.seek(0) self.output_file.seek(0) self.fingerprint = case_hash(problem_id, self.input_file.read(), self.output_file.read())