def download_input_file_view(request, test_id): test = get_object_or_404(Test, id=test_id) if not can_admin_instance_of_problem(request, test.problem): raise PermissionDenied return stream_file(test.input_file, strip_num_or_hash(test.input_file.name))
def download_name(self): ext = split_extension(self.package_file.name)[1] if self.problem: return self.problem.short_name + ext else: filename = os.path.split(self.package_file.name)[1] return strip_num_or_hash(filename)
def _assertTestsNotInPackage(self, tests, tp): zipf = zipfile.ZipFile(tp.package, 'r') for t in tests: for f in [t.input_file, t.output_file]: with self.assertRaises(KeyError): file_name = strip_num_or_hash(os.path.basename(f.file.name)) zipf.open(file_name) zipf.close()
def download_checker_exe_view(request, checker_id): checker = get_object_or_404(OutputChecker, id=checker_id) if not can_admin_instance_of_problem(request, checker.problem): raise PermissionDenied if not checker.exe_file: raise Http404 return stream_file(checker.exe_file, strip_num_or_hash(checker.exe_file.name))
def download_checker_exe_view(request, checker_id): checker = get_object_or_404(OutputChecker, id=checker_id) if not test.problem_instance.controller.can_see_checker_exe(request, test): raise PermissionDenied if not checker.exe_file: raise Http404 return stream_file(checker.exe_file, strip_num_or_hash(checker.exe_file.name))
def _assertTestsInPackage(self, tests, tp): zipf = zipfile.ZipFile(tp.package, 'r') for t in tests: for f in [t.input_file, t.output_file]: file_name = strip_num_or_hash(os.path.basename(f.file.name)) content1 = zipf.open(file_name).read() content2 = f.file.file.read() self.assertEqual(content1, content2) zipf.close()
def _assertTestsNotInPackage(self, tests, tp): zipf = zipfile.ZipFile(tp.package, 'r') for t in tests: for f in [t.input_file, t.output_file]: with self.assertRaises(KeyError): file_name = strip_num_or_hash(os.path.basename( f.file.name)) zipf.open(file_name) zipf.close()
def test_strip_num_or_hash(self): cases = { 'abc_1.pdf': 'abc.pdf', 'abc_123.pdf': 'abc.pdf', 'abc_45HKmyT.pdf': 'abc.pdf', 'abc_1.tar.gz': 'abc.tar.gz', 'abc_45HKmyT.tar.gz': 'abc.tar.gz', 'abc.tar_1.gz': 'abc.tar.gz', 'abc.tar.gz': 'abc.tar.gz', '/a/b/abc.tar.gz': '/a/b/abc.tar.gz', 'my_file': 'my_file', 'a_1_2.pdf': 'a_1.pdf', } for (before, after) in cases.iteritems(): self.assertEqual(strip_num_or_hash(before), after)
def test_strip_num_or_hash(self): cases = { 'abc_1.pdf': 'abc.pdf', 'abc_123.pdf': 'abc.pdf', 'abc_45HKmyT.pdf': 'abc.pdf', 'abc_1.tar.gz': 'abc.tar.gz', 'abc_45HKmyT.tar.gz': 'abc.tar.gz', 'abc.tar_1.gz': 'abc.tar.gz', 'abc.tar.gz': 'abc.tar.gz', '/a/b/abc.tar.gz': '/a/b/abc.tar.gz', 'my_file': 'my_file', 'a_1_2.pdf': 'a_1.pdf', } for (before, after) in six.iteritems(cases): self.assertEqual(strip_num_or_hash(before), after)
def _create_tests_package(sender, instance, action, reverse, **kwargs): # This function is called upon changing TestsPackage object. # The package should be repacked only if set of tests changes # but not when, e.g., only the description of package changes. if action == 'pre_clear': instance._old_tests = list(instance.tests.all()) elif action == 'post_add': if instance._old_tests == list(instance.tests.all()): return with tempfile.NamedTemporaryFile(delete=False) as f: zipf = zipfile.ZipFile(f, 'w', zipfile.ZIP_DEFLATED) for test in instance.tests.all(): for test_file in [test.input_file, test.output_file]: arch_path = strip_num_or_hash( os.path.basename(test_file.file.name)) pack_test_file(test_file, arch_path, zipf) zipf.close() instance.package.save('tests.zip', File(f)) os.unlink(f.name)
def download_name(self): return strip_num_or_hash(self.filename)
def download_output_file_view(request, test_id): test = get_object_or_404(Test, id=test_id) if not test.problem_instance.controller.can_see_test(request, test): raise PermissionDenied return stream_file(test.output_file, strip_num_or_hash(test.output_file.name))