Example #1
0
    def get_context_data(self, form, **kwargs):
        context = super(CreateTestcaseWizard, self).get_context_data(form=form, **kwargs)

        program = Program.objects.get(pk=self.kwargs['programID'])
        compiler_command = get_compilation_command(program)
        execution_command = get_execution_command(program)
        context.update({'program': program, 'compiler_command': compiler_command, 'execution_command':execution_command})
        return context
Example #2
0
    def get_context_data(self, form, **kwargs):
        context = super(EditTestcaseWizard, self).get_context_data(form=form, **kwargs)

        testcase = Testcase.objects.get(pk=self.kwargs['testcaseID'])
        program = testcase.program
        compiler_command = get_compilation_command(program)
        execution_command = get_execution_command(program)
        context.update({'testcase': testcase, 'compiler_command': compiler_command, 'execution_command':execution_command})
        return context
Example #3
0
        def __init__(self, program_result, program_type):
            self.program_result = program_result # Used in template.
            self.compiler_command = get_compilation_command(program_result.program)
            self.execution_command = get_execution_command(program_result.program)
            all_obj = TestcaseResult.objects.filter(program_result=program_result)
            test_type_obj = [a for a in all_obj]
            self.testResults = [Results.TestcaseResultSaved(obj) for obj in test_type_obj]

            self.marks = reduce(lambda x,y: x + y.marks, self.testResults, 0)
Example #4
0
    def get_context_data(self, form, **kwargs):
        context = super(CreateTestcaseWizard, self).get_context_data(form=form,
                                                                     **kwargs)

        program = Program.objects.get(pk=self.kwargs['programID'])
        compiler_command = get_compilation_command(program)
        execution_command = get_execution_command(program)
        context.update({
            'program': program,
            'compiler_command': compiler_command,
            'execution_command': execution_command
        })
        return context
Example #5
0
        def __init__(self, program_result, program_type):
            self.program_result = program_result  # Used in template.
            self.compiler_command = get_compilation_command(
                program_result.program)
            self.execution_command = get_execution_command(
                program_result.program)
            all_obj = TestcaseResult.objects.filter(
                program_result=program_result)
            test_type_obj = [a for a in all_obj]
            self.testResults = [
                Results.TestcaseResultSaved(obj) for obj in test_type_obj
            ]

            self.marks = reduce(lambda x, y: x + y.marks, self.testResults, 0)
Example #6
0
    def get_context_data(self, form, **kwargs):
        context = super(EditTestcaseWizard, self).get_context_data(form=form,
                                                                   **kwargs)

        testcase = Testcase.objects.get(pk=self.kwargs['testcaseID'])
        program = testcase.program
        compiler_command = get_compilation_command(program)
        execution_command = get_execution_command(program)
        context.update({
            'testcase': testcase,
            'compiler_command': compiler_command,
            'execution_command': execution_command
        })
        return context
Example #7
0
def detailProgram(request, programID):
    program = get_object_or_404(Program, pk=programID)
    testcases = Testcase.objects.filter(program=program)
    assignment = program.assignment
    is_due = (timezone.now() >= assignment.deadline)
    course = assignment.course
    has_submitted = Upload.objects.filter(owner=request.user,
                                          assignment=assignment)
    all_assignments = Assignment.objects.filter(
        course=course).order_by('-serial_number')

    is_moderator = isCourseModerator(course, request.user)
    if is_moderator:
        assignments = all_assignments
    else:
        assignments = [
            a for a in all_assignments if a.publish_on <= timezone.now()
        ]
    compiler_command = get_compilation_command(program)
    execution_command = get_execution_command(program)
    program_errors = None
    if not program.is_sane:
        try:
            program_errors = ProgramErrors.objects.get(program=program)
        except ProgramErrors.DoesNotExist:
            program_errors = None
    testcase_errors = []
    terror_ctype = ContentType.objects.get_for_model(TestcaseErrors)
    for error in AssignmentErrors.objects.filter(assignment=program.assignment,
                                                 content_type=terror_ctype):
        testcase_errors.extend(
            TestcaseErrors.objects.filter(pk=error.object_id))
    get_params = {'source': 'section', 'id': programID}
    return render_to_response('assignments/detailsProgram.html', {
        'program': program,
        'testcases': testcases,
        'assignment': assignment,
        'assignments': assignments,
        'date_time': timezone.now(),
        'program_errors': program_errors,
        'compiler_command': compiler_command,
        'execution_command': execution_command,
        'course': course,
        'is_moderator': is_moderator,
        'is_due': is_due,
        'has_submitted': has_submitted,
        'get_params': get_params,
        'testcase_errors': testcase_errors
    },
                              context_instance=RequestContext(request))
Example #8
0
    def __init__(self, program, assignment_result):
        self.assignment_result = assignment_result
        self.program = program

        # Extracting the list of files relevant to only this section.
        if program.program_files:
            prgrm_files = get_file_name_list(name=program.program_files.file.name)
            prgrm_files = " ".join(prgrm_files)
        else:
            prgrm_files = ""

        # Adding the list of files in the assignment model solution to the above list.
        self.programFiles = program.assignment.student_program_files + " " + prgrm_files
        self.language = program.assignment.program_language
        self.compiler_command = get_compilation_command(program)
        self.execution_command = get_execution_command(program)
        self.commandExecutor = CommandExecutor()
Example #9
0
    def __init__(self, program, assignment_result):
        self.assignment_result = assignment_result
        self.program = program

        # Extracting the list of files relevant to only this section.
        if program.program_files:
            prgrm_files = get_file_name_list(
                name=program.program_files.file.name)
            prgrm_files = " ".join(prgrm_files)
        else:
            prgrm_files = ""

        # Adding the list of files in the assignment model solution to the above list.
        self.programFiles = program.assignment.student_program_files + " " + prgrm_files
        self.language = program.assignment.program_language
        self.compiler_command = get_compilation_command(program)
        self.execution_command = get_execution_command(program)
        self.commandExecutor = CommandExecutor()
Example #10
0
def detailProgram(request, programID):
    program = get_object_or_404(Program, pk=programID)
    testcases = Testcase.objects.filter(program=program)
    assignment = program.assignment
    is_due = (timezone.now() >= assignment.deadline)
    course = assignment.course
    has_submitted = Upload.objects.filter(owner=request.user, assignment=assignment)
    all_assignments = Assignment.objects.filter(course=course).order_by('-serial_number')
    
    is_moderator = isCourseModerator(course, request.user)
    if is_moderator:
        assignments = all_assignments
    else:
        assignments = [a for a in all_assignments if a.publish_on <= timezone.now()]
    compiler_command = get_compilation_command(program)
    execution_command = get_execution_command(program)
    program_errors = None
    if not program.is_sane:
        try:
            program_errors = ProgramErrors.objects.get(program=program)
        except ProgramErrors.DoesNotExist:
            program_errors = None
    testcase_errors = []
    terror_ctype = ContentType.objects.get_for_model(TestcaseErrors)
    for error in AssignmentErrors.objects.filter(assignment=program.assignment, content_type=terror_ctype):
        testcase_errors.extend(TestcaseErrors.objects.filter(pk=error.object_id))
    get_params = {'source': 'section', 'id': programID}
    return render_to_response(
                'assignments/detailsProgram.html',
                {'program':program, 'testcases':testcases, 'assignment':assignment,
                 'assignments':assignments, 'date_time': timezone.now(),
                 'program_errors':program_errors, 'compiler_command':compiler_command, 'execution_command':execution_command, 'course':course, 'is_moderator':is_moderator,
                 'is_due': is_due, 'has_submitted':has_submitted, 'get_params': get_params,
                 'testcase_errors': testcase_errors},
                context_instance=RequestContext(request)
            )
Example #11
0
    def run(self):
        # Compile the solution. If there are errors then write it to the error file and return
        if compile_required(self.program.language):
            compiler_command = get_compilation_command(self.program)
            print "Compiling model solution. Compilation Command - " + compiler_command
            self.command_output = self.commandExecutor.safe_execute(
                                                            command=compiler_command,
                                                            limits=self.get_resource_limit()
                                                        )
            if self.command_output.getReturnCode():
                # There was some error. Write it in database.
                print "Compilation of model solution failed."
                self.failed = True
                _, self.error_file = tempfile.mkstemp(prefix="error", dir=self.temp_input_d)
                f = open(self.error_file, 'w')

                for a_line in self.command_output.get_stderr():
                    f.write(a_line)
                f.close()
                return

        # No errors and thus can continue. Run the solution
        execution_command = get_execution_command(self.program)
        print "Creating Output. Running following execution command - " + execution_command
        #if self.testcase.command_line_args:
        #    execution_command = execution_command + self.testcase.command_line_args
        if self.testcase.std_in_file_name:
            execution_command = execution_command + " < " + self.testcase.std_in_file_name
        self.command_output = self.commandExecutor.safe_execute(
                                                            command=execution_command,
                                                            limits=self.get_resource_limit()
                                                        )
        self.success = bool(self.command_output.getReturnCode())
        self.command_output.printResult()

        # Delete input files from current directory. And the program files
        dir_content = os.listdir('./')
        for a_file in dir_content:
            if self.is_program_file(a_file):
                os.remove(a_file)

        if self.input:
            for a_file in get_file_name_list(name=self.input): # delete extracted files
                if os.path.isfile(a_file):
                    os.remove(a_file)

        # Write standard output to a file and save it in database.
        directory_content = os.listdir('./')

        _, self.std_out_file = tempfile.mkstemp(prefix='output', dir="./")
        out_file = open(self.std_out_file, 'w')

        for a_line in ["\n".join(self.command_output.get_stdout())]:
            out_file.write(a_line)
        out_file.close()

        # There was some error. Write it in database.
        if self.command_output.getReturnCode():
            self.failed = True
            _, self.error_file = tempfile.mkstemp(prefix="error", dir=self.temp_input_d)
            f = open(self.error_file, 'w')

            for a_line in self.command_output.get_stderr():
                f.write(a_line)
            f.close()

        # If directory has any content left then make a tar, else set the outfile to the output file.
        if directory_content: # make a tar
            self.out_file = self.make_tar(directory_content + [self.std_out_file], self.std_out_file)
        else: # there are no other files standard output only.
            self.out_file = self.std_out_file
Example #12
0
    def run(self):
        # Compile the solution. If there are errors then write it to the error file and return
        if compile_required(self.program.language):
            compiler_command = get_compilation_command(self.program)
            print "Compiling model solution. Compilation Command - " + compiler_command
            self.command_output = self.commandExecutor.safe_execute(
                command=compiler_command, limits=self.get_resource_limit())
            if self.command_output.getReturnCode():
                # There was some error. Write it in database.
                print "Compilation of model solution failed."
                self.failed = True
                _, self.error_file = tempfile.mkstemp(prefix="error",
                                                      dir=self.temp_input_d)
                f = open(self.error_file, 'w')

                for a_line in self.command_output.get_stderr():
                    f.write(a_line)
                f.close()
                return

        # No errors and thus can continue. Run the solution
        execution_command = get_execution_command(self.program)
        print "Creating Output. Running following execution command - " + execution_command
        #if self.testcase.command_line_args:
        #    execution_command = execution_command + self.testcase.command_line_args
        if self.testcase.std_in_file_name:
            execution_command = execution_command + " < " + self.testcase.std_in_file_name
        self.command_output = self.commandExecutor.safe_execute(
            command=execution_command, limits=self.get_resource_limit())
        self.success = bool(self.command_output.getReturnCode())
        self.command_output.printResult()

        # Delete input files from current directory. And the program files
        dir_content = os.listdir('./')
        for a_file in dir_content:
            if self.is_program_file(a_file):
                os.remove(a_file)

        if self.input:
            for a_file in get_file_name_list(
                    name=self.input):  # delete extracted files
                if os.path.isfile(a_file):
                    os.remove(a_file)

        # Write standard output to a file and save it in database.
        directory_content = os.listdir('./')

        _, self.std_out_file = tempfile.mkstemp(prefix='output', dir="./")
        out_file = open(self.std_out_file, 'w')

        for a_line in ["\n".join(self.command_output.get_stdout())]:
            out_file.write(a_line)
        out_file.close()

        # There was some error. Write it in database.
        if self.command_output.getReturnCode():
            self.failed = True
            _, self.error_file = tempfile.mkstemp(prefix="error",
                                                  dir=self.temp_input_d)
            f = open(self.error_file, 'w')

            for a_line in self.command_output.get_stderr():
                f.write(a_line)
            f.close()

        # If directory has any content left then make a tar, else set the outfile to the output file.
        if directory_content:  # make a tar
            self.out_file = self.make_tar(
                directory_content + [self.std_out_file], self.std_out_file)
        else:  # there are no other files standard output only.
            self.out_file = self.std_out_file
Example #13
0
def compile_solution(sender, instance, **kwargs):

    # If the program language is an interpreted language, then we dont do anything
    if not compile_required(instance.assignment.program_language) and not getattr(instance, 'execute_now', False):
        instance.set_sane()
        instance.delete_error_message()
        return

    if not compile_required(instance.assignment.program_language) :
        old_pwd = os.getcwd()
        instance.set_sane()
        instance.UpdateOutput()
        instance.delete_error_message()
        os.chdir(old_pwd)
        return

    # instance.id would indicate that record was not created.
    if not getattr(instance, 'compile_now', False) or instance.id is None: 
        return

    # If solution is given then set the path, else do nothing
    programFilePath = instance.program_files.name
    if hasattr(instance.assignment.model_solution, 'file'):
        ModelSolutionPath = instance.assignment.model_solution.file.name
    else:
        instance.set_sane()
        instance.delete_error_message()
        return 

    # Model Solution was not provided hence do nothing.
    if not os.path.isfile(ModelSolutionPath):
        return

    # Copying module solution to a temp directory for compilation
    tmp_dir = tempfile.mkdtemp(prefix='grader')
    old_pwd = os.getcwd()
    os.chdir(tmp_dir)
    currentDir = os.getcwd()
    try:
        # Copy model solution to temp directory.
        with Archive(fileobj=instance.assignment.model_solution.file) as archive:
            if archive.is_archive():
                archive.extract(dest=currentDir)
            else:
                shutil.copy(src=os.path.join(MEDIA_ROOT, ModelSolutionPath), dst=currentDir)

        # Change directory if solution tar contains a directory.
        directories = [a for a in os.listdir('./') if os.path.isdir(a)]
        if directories:
            os.chdir(directories[0])
            currentDir = os.getcwd()

        # Copying the program files to the current directory
        if instance.program_files:
            with Archive(name=instance.program_files.file.name) as archive:
                if archive.is_archive():
                    archive.extract(dest=currentDir)
                else:
                    shutil.copy(src=os.path.join(MEDIA_ROOT, programFilePath), dst=currentDir)

        # Setting up compilation process and calling the CommandExecutor with appropriate command
        executor = CommandExecutor(timeout=100)
        compiler_command = get_compilation_command(instance)
        compileResult = executor.executeCommand(command=compiler_command)

        # Compilation successful => Set the program to sane and delete errors, else report the errors.
        if compileResult.returnCode == 0: # save exe file on success.
            instance.is_sane = True
            instance.UpdateOutput()
            instance.delete_error_message()
        else:
            message = "Compilation failed.\nReturn code = {0}.\nError message={1}".format(compileResult.returnCode, "".join(compileResult.stderr))
            print "Instructor solution not sane - " + message
            instance.save_error(message)
        shutil.rmtree(tmp_dir)
    finally:
        os.chdir(old_pwd)