Exemplo n.º 1
0
def create_evaluation_result(failed=False, evalres=None, message=''):
    if failed:
        return EvaluationResult(success=False,
                                output_file=None,
                                execution_time=0,
                                execution_memory=0,
                                verdict=JudgeVerdict.judge_failed,
                                message=message)

    success, verdict = \
        get_judge_verdict_from_cms(evalres['evalres'], evalres['compiled'], evalres['output'])

    output_file = None
    execution_memory = 0
    if success:
        output_body = base64.b64decode(evalres['output']).decode('utf-8')
        output_file = FileModel()
        output_file.file.save('output', ContentFile(output_body))
        output_file.save()

        execution_memory = float(evalres['memory']) / 1024 / 1024

    return EvaluationResult(
        success=success,
        output_file=output_file,
        execution_time=evalres['time'],
        execution_memory=execution_memory,
        verdict=verdict,
        message=evalres['message'],
    )
Exemplo n.º 2
0
def get_resource_as_file_model(*path):

    current_dir = os.path.dirname(os.path.abspath(__file__))
    file_path = os.path.join(current_dir, *path)
    file = open(file_path, 'r')
    file_model = FileModel(file=File(file))
    file_model.save()
    return file_model
Exemplo n.º 3
0
def put_files(files) -> List[FileModel]:
    fms = []
    for name, text in files:
        with tempfile.TemporaryFile(mode="w+") as tf:
            tf.write(text)
            fm = FileModel(file=File(file=tf, name=name))
            fm.save()
            fms += [fm]
    return fms
Exemplo n.º 4
0
    def test_compile(self):
        source = FileModel(file=ContentFile(self.TEST_CODE, name="code.cpp"))
        source.save()

        job = JobModel(command=["/usr/bin/g++", "code.cpp", "-o", "a.out"], compile_job=True)
        job.save()

        job.add_file(source, JobFile.READONLY, "code.cpp")
        job.mark_file_for_extraction("a.out")

        job.run()
        self.assertEqual(job.exit_status, SandboxBase.EXIT_OK)
        self.assertEqual(job.exit_code, 0)
        return job
Exemplo n.º 5
0
 def post(self, request, problem_code, revision_slug, **kwargs):
     instance_pk = kwargs.get(self.instance_slug)
     instance = get_git_object_or_404(self.model, pk=instance_pk, problem=self.revision)
     code_file = getattr(instance, self.code_field_name)
     if "source_code" in request.POST:
         new_file = FileModel()
         new_file.file.save(code_file.name, ContentFile(request.POST["source_code"]))
         setattr(instance, self.code_field_name, new_file)
         instance.save()
         if isinstance(self, SourceFile):
             self.invalidate_compilation()
         messages.success(request, _("Saved successfully"))
     return HttpResponseRedirect(request.get_full_path())
Exemplo n.º 6
0
    def setUp(self):
        self.problem = mommy.make(Problem, )
        file = tempfile.NamedTemporaryFile()
        self.file_model = FileModel(file=File(file), name="keyvan")
        self.file_model.save()
        self.base_revision = ProblemRevision.objects.create(
            problem=self.problem, author=self.problem.creator)

        self.problem_revision1 = ProblemRevision.objects.create(
            problem=self.problem, author=self.problem.creator)
        self.problem_revision1.parent_revisions.add(self.base_revision)

        self.problem_revision2 = ProblemRevision.objects.create(
            problem=self.problem, author=self.problem.creator)
        self.problem_revision2.parent_revisions.add(self.base_revision)

        self.problem_branch1 = mommy.make(
            ProblemBranch,
            name="b1",
            problem=self.problem,
            head=self.problem_revision1,
            creator=self.problem.creator,
        )
        self.problem_branch2 = mommy.make(
            ProblemBranch,
            name="b2",
            problem=self.problem,
            head=self.problem_revision2,
            creator=self.problem.creator,
        )

        self.base_revision.commit("Base commit")
        self.problem_revision1.commit("Commit 1")
        self.problem_revision2.commit("Commit 2")

        mommy.make(ProblemData, problem=self.base_revision)
        mommy.make(ProblemData, problem=self.problem_revision1)
        mommy.make(ProblemData, problem=self.problem_revision2)
Exemplo n.º 7
0
 def post(self, request, problem_code, revision_slug, **kwargs):
     instance_pk = kwargs.get("file_id")
     try:
         instance = self.problem.files.get(pk=instance_pk)
     except FileModel.DoesNotExist:
         raise Http404
     if "source_code" in request.POST:
         new_file = FileModel()
         new_file.file.save(instance.name,
                            ContentFile(request.POST["source_code"]))
         new_file.save()
         self.problem.files.add(new_file)
         self.problem.files.remove(instance)
         messages.success(request, _("Saved successfully"))
         return HttpResponseRedirect(
             reverse("problems:file_source",
                     kwargs={
                         "problem_code": problem_code,
                         "revision_slug": revision_slug,
                         "file_id": new_file.pk
                     }))
     else:
         return HttpResponseRedirect(request.get_full_path())
Exemplo n.º 8
0
class VersionControlTests(TestCase):
    def setUp(self):
        self.problem = mommy.make(Problem, )
        file = tempfile.NamedTemporaryFile()
        self.file_model = FileModel(file=File(file), name="keyvan")
        self.file_model.save()
        self.base_revision = ProblemRevision.objects.create(
            problem=self.problem, author=self.problem.creator)

        self.problem_revision1 = ProblemRevision.objects.create(
            problem=self.problem, author=self.problem.creator)
        self.problem_revision1.parent_revisions.add(self.base_revision)

        self.problem_revision2 = ProblemRevision.objects.create(
            problem=self.problem, author=self.problem.creator)
        self.problem_revision2.parent_revisions.add(self.base_revision)

        self.problem_branch1 = mommy.make(
            ProblemBranch,
            name="b1",
            problem=self.problem,
            head=self.problem_revision1,
            creator=self.problem.creator,
        )
        self.problem_branch2 = mommy.make(
            ProblemBranch,
            name="b2",
            problem=self.problem,
            head=self.problem_revision2,
            creator=self.problem.creator,
        )

        self.base_revision.commit("Base commit")
        self.problem_revision1.commit("Commit 1")
        self.problem_revision2.commit("Commit 2")

        mommy.make(ProblemData, problem=self.base_revision)
        mommy.make(ProblemData, problem=self.problem_revision1)
        mommy.make(ProblemData, problem=self.problem_revision2)

    @mock.patch("problems.models.file.Resource.diverged_from",
                return_value=True)
    @mock.patch("problems.models.problem.ProblemData.diverged_from",
                return_value=False)
    def test_conflicts(self, *args, **kwargs):
        self.create_resource(self.problem_revision1, name="t1")
        self.create_resource(self.problem_revision2, name="t1")
        self.problem_revision1.merge(self.problem_revision2)
        new_revision = self.problem.revisions.get(
            revision_id__isnull=True, parent_revisions=self.problem_revision1)
        self.assertEqual(len(new_revision.merge_result.conflicts.all()), 1)
        self.assertEqual(len(new_revision.resource_set.all()), 1)

    @mock.patch("problems.models.file.Resource.diverged_from",
                return_value=True)
    @mock.patch("problems.models.problem.ProblemData.diverged_from",
                return_value=False)
    def test_auto_merge(self, *args, **kwargs):
        self.create_resource(self.problem_revision1, name="t1")
        self.create_resource(self.problem_revision2, name="t2")
        self.problem_revision1.merge(self.problem_revision2)
        new_revision = self.problem.revisions.get(
            revision_id__isnull=True, parent_revisions=self.problem_revision1)
        self.assertEqual(len(new_revision.merge_result.conflicts.all()), 0)
        self.assertEqual(len(new_revision.resource_set.all()), 2)

    @mock.patch("problems.models.file.Resource.diverged_from",
                return_value=False)
    @mock.patch("problems.models.problem.ProblemData.diverged_from",
                return_value=False)
    def test_delete(self, *args, **kwargs):
        self.create_resource(self.base_revision, name="t1")
        self.create_resource(self.problem_revision1, name="t1")
        self.problem_revision1.merge(self.problem_revision2)
        new_revision = self.problem.revisions.get(
            revision_id__isnull=True, parent_revisions=self.problem_revision1)
        self.assertEqual(len(new_revision.merge_result.conflicts.all()), 0)
        self.assertEqual(len(new_revision.resource_set.all()), 0)

    @mock.patch("problems.models.file.Resource.diverged_from",
                return_value=True)
    @mock.patch("problems.models.problem.ProblemData.diverged_from",
                return_value=False)
    def test_conflict_with_deleted_ours(self, *args, **kwargs):
        self.create_resource(self.base_revision, name="t1")
        self.create_resource(self.problem_revision2, name="t1")
        self.problem_revision1.merge(self.problem_revision2)
        new_revision = self.problem.revisions.get(
            revision_id__isnull=True, parent_revisions=self.problem_revision1)
        self.assertEqual(len(new_revision.merge_result.conflicts.all()), 1)
        self.assertEqual(len(new_revision.resource_set.all()), 0)

    @mock.patch("problems.models.file.Resource.diverged_from",
                return_value=True)
    @mock.patch("problems.models.problem.ProblemData.diverged_from",
                return_value=False)
    def test_conflict_with_deleted_theirs(self, *args, **kwargs):
        self.create_resource(self.base_revision, name="t1")
        self.create_resource(self.problem_revision1, name="t1")
        self.problem_revision1.merge(self.problem_revision2)
        new_revision = self.problem.revisions.get(
            revision_id__isnull=True, parent_revisions=self.problem_revision1)
        self.assertEqual(len(new_revision.merge_result.conflicts.all()), 1)
        self.assertEqual(len(new_revision.resource_set.all()), 1)

    @mock.patch("problems.models.file.Resource.diverged_from",
                return_value=False)
    @mock.patch("problems.models.problem.ProblemData.diverged_from",
                return_value=False)
    def test_fork_merge_with_revision(self, *args, **kwargs):
        fork1_current_head_pk = self.problem_branch1.head.pk
        self.problem_branch1.merge(self.problem_revision2)
        self.assertListEqual(
            list(self.problem_branch1.working_copy.parent_revisions.all()),
            [self.problem_revision1, self.problem_revision2])

    @mock.patch("problems.models.file.Resource.diverged_from",
                return_value=False)
    @mock.patch("problems.models.problem.ProblemData.diverged_from",
                return_value=False)
    def test_fork_merge_with_fork(self, *args, **kwargs):
        fork1_current_head_pk = self.problem_branch1.head.pk
        fork2_current_head_pk = self.problem_branch2.head.pk
        self.problem_branch1.merge(self.problem_branch2.head)
        self.assertIsNotNone(self.problem_branch1.working_copy)
        self.assertEqual(fork1_current_head_pk, self.problem_branch1.head.pk)
        self.assertEqual(fork2_current_head_pk, self.problem_branch2.head.pk)

    def create_resource(self, revision, name):
        return Resource.objects.create(problem=revision,
                                       name=name,
                                       file=self.file_model)

    def test_delete_and_pull(self, *args, **kwargs):
        branch3 = mommy.make(
            ProblemBranch,
            name="b3",
            problem=self.problem,
            head=self.problem_branch1.head,
            creator=self.problem.creator,
        )
        self.problem_branch2.set_as_head(self.problem_branch1.head)
        self.create_resource(self.problem_branch1.head, name="t1")
        working_copy = self.problem_branch2.get_or_create_working_copy(
            self.problem.creator)
        self.assertEqual(working_copy.resource_set.all().count(), 1)
        working_copy.resource_set.all().delete()
        self.assertEqual(working_copy.resource_set.all().count(), 0)
        working_copy.commit("Deleted")
        self.problem_branch2.set_working_copy_as_head()
        self.assertEqual(self.problem_branch2.head.resource_set.all().count(),
                         0)
        self.problem_branch1.pull_from_branch(self.problem_branch2)
        self.assertEqual(self.problem_branch1.head.resource_set.all().count(),
                         0)
        branch3.pull_from_branch(self.problem_branch1)
        self.assertEqual(branch3.head.resource_set.all().count(), 0)

    def test_statement_conflict(self, *args, **kwargs):
        p1 = self.problem_revision1.problem_data
        p1.statement = "P1"
        p1.save()
        p2 = self.problem_revision2.problem_data
        p2.statement = "P2"
        p2.save()
        self.problem_branch1.pull_from_branch(self.problem_branch2)
        revision = self.problem_branch1.working_copy
        self.assertEqual(len(revision.merge_result.conflicts.all()), 1)
        conflict = revision.merge_result.conflicts.get()
        conflict.resolved = True
        conflict.save()
        revision.commit("Resolved")
        self.problem_branch1.set_working_copy_as_head()
        self.problem_branch1.pull_from_branch(self.problem_branch2)

        self.assertIsNone(self.problem_branch1.working_copy)

    def test_validator_subtask_merge1(self, *args, **kwargs):
        self.problem_branch2.set_as_head(self.problem_branch1.head)
        r = self.problem_branch1.head
        s = mommy.make(Subtask, problem=r, name="t")
        v = mommy.make(Validator, problem=r, global_validator=False)
        v._subtasks.add(s)
        r2 = self.problem_branch2.get_or_create_working_copy(
            self.problem.creator)
        r2.validator_set.all()[0]._subtasks.clear()
        r2.commit("Committed")
        self.problem_branch2.set_working_copy_as_head()
        self.problem_branch2.pull_from_branch(self.problem_branch1)
        self.assertEqual(
            len(self.problem_branch2.head.validator_set.all()[0].subtasks), 0)

    def test_validator_subtask_merge2(self, *args, **kwargs):
        self.problem_branch2.set_as_head(self.problem_branch1.head)
        r = self.problem_branch1.head
        s = mommy.make(Subtask, problem=r, name="t")
        v = mommy.make(Validator, problem=r, global_validator=False)
        r2 = self.problem_branch2.get_or_create_working_copy(
            self.problem.creator)
        r2.validator_set.all()[0]._subtasks.add(r2.subtasks.all()[0])
        r2.commit("Committed")
        self.problem_branch2.set_working_copy_as_head()
        self.problem_branch1.pull_from_branch(self.problem_branch2)
        self.assertEqual(
            len(self.problem_branch1.head.validator_set.all()[0].subtasks), 1)

    def test_validator_subtask_merge3(self, *args, **kwargs):
        self.problem_branch2.set_as_head(self.problem_branch1.head)
        r = self.problem_branch1.head
        s = mommy.make(Subtask, problem=r, name="t")
        v = mommy.make(Validator, problem=r, global_validator=False)
        v._subtasks.add(s)
        r2 = self.problem_branch2.get_or_create_working_copy(
            self.problem.creator)
        r2.validator_set.all()[0]._subtasks.clear()
        r2.commit("Committed")
        self.problem_branch2.set_working_copy_as_head()
        self.problem_branch1.pull_from_branch(self.problem_branch2)
        self.assertEqual(
            len(self.problem_branch1.head.validator_set.all()[0].subtasks), 0)