Example #1
0
 def is_program_file(self, fname):
     filename = fname.split("/")[-1]
     if self.program.program_files and filename in get_file_name_list(self.program.program_files.file.name):
         return True
     elif self.program.assignment.model_solution and filename in get_file_name_list(self.program.assignment.model_solution.file.name):
         return True
     elif is_intermediate_files(filename,self.program,self.program.language):
         return True
     else:
         return False
Example #2
0
 def is_program_file(self, fname):
     filename = fname.split("/")[-1]
     if self.program.program_files and filename in get_file_name_list(
             self.program.program_files.file.name):
         return True
     elif self.program.assignment.model_solution and filename in get_file_name_list(
             self.program.assignment.model_solution.file.name):
         return True
     elif is_intermediate_files(filename, self.program,
                                self.program.language):
         return True
     else:
         return False
Example #3
0
    def clean_docfile(self):
        # Get all files that should be submitted by student from all programs of given AssignmentID.
        data = self.cleaned_data
        submitted_files = get_file_name_list(fileobj=data['docfile'])
        progrm_files_name = self.assignment_model_obj.student_program_files.split(
        )

        # Check if tarfile contains all of these.
        missing_files = get_missing_files(progrm_files_name,
                                          fileobj=data['docfile'])

        # If there are missing files raise error and display all missing files.
        if missing_files:
            self.fields['docfile'].error_messages[
                "docfile"] = "There were missing files."
            if set(submitted_files) >= set(progrm_files_name):
                error_msg = "Directory structure is incorrect! Put all source files in single directory only.\
                Nested directories are not allowed."

            else:
                error_msg = 'Missing files!. Your SUBMISSION contains "{0}" and we expect you to submit "{1}"\
                            Please upload again with missing files "{2}"'                                                                         .format(" ".join(submitted_files),\
                            " ".join(progrm_files_name), " ".join(missing_files))
            raise forms.ValidationError(error_msg)
        return data['docfile']
Example #4
0
    def setup(self, submission, program_type):
        # Create a temporary directory inside the evaluation directory. Copy the submission files into this directory.
        src = submission.filePath.file.name # gives absolute path
        self.temp_dir = tempfile.mkdtemp(prefix="grader", dir=self.eval_dir)
        os.chdir(self.temp_dir)
        extract_or_copy(src=src, dest=self.temp_dir)

        # Update or create the assignment result for the submission. Update the submitted files for existing assignment results.
        self.submittedFiles = get_file_name_list(name=src)
        self.assignment_result, _ = AssignmentResults.objects.get_or_create(
                                            submission=submission,
                                            defaults={'submitted_files': "\n".join(self.submittedFiles)}
                                        )
        self.assignment_result.is_stale = False
        self.assignment_result.save()

        # Assuming only 1 directory level, cd to that directory. If the student submitted only 1 file, then dont do anything.
        directories = [a for a in os.listdir('./') if os.path.isdir(a)]
        if directories:
            os.chdir(directories[0])
        currentDir = os.getcwd()

        # Copy the program files (of all sections) to this directory. Note that the solution file is still student's submission.
        programs = ProgramModel.objects.filter(assignment=submission.assignment)
        testcaseList = []
        for program in programs:
            testcaseList.append(Testcase.objects.filter(program=program))
            if program.program_files:
                extract_or_copy(src=program.program_files.file.name, dest=currentDir)

        # Copy input-output files for all testcases. Now we are ready for the evaluation process.
        for testcase in testcaseList:
            for test in testcase:
                if test.input_files:
                    extract_or_copy(src=test.input_files.file.name, dest=currentDir)
Example #5
0
    def setup(self):
        # This is the directory where the student program is copied to.
        self.temp_dir = tempfile.mkdtemp(prefix="grader", dir=self.eval_dir)
        os.chdir(self.temp_dir)

        # Copy student solution files to tmp directory.
        extract_or_copy(src=self.submission.filePath.file.name,
                        dest=self.temp_dir)

        # Another temp directory for running solution program. This is the directory where the solution program is copied to.
        self.solution_temp_dir = tempfile.mkdtemp(prefix="solution",
                                                  dir=self.eval_dir)

        directories = [a for a in os.listdir('./') if os.path.isdir(a)]
        if directories:
            os.chdir(directories[0])
        currentDir = os.getcwd()

        # Extract the program file and the input file to the student directory, and the input file to the program directory as well.
        extract_or_copy(src=self.program.program_files.file.name,
                        dest=currentDir)
        self.submittedFiles = get_file_name_list(
            name=self.submission.filePath.file.name)
        input_file = self.store_input_file(self.inputFile, currentDir)
        shutil.copy(src=input_file, dst=self.solution_temp_dir)
Example #6
0
    def get_form_kwargs(self, step=None):
        if step == '0':
            return {'solution_ready' : self.solution_ready}
        if step == '1':
            choice_dict = {}
            if self.storage.get_step_files('0'):
                if self.storage.get_step_files('0').get('0-input_files', ""):
                    f_in_obj = self.storage.get_step_files('0').get('0-input_files')
                    f_in_obj.open()
                    choice_dict['in_file_choices'] = [(a, a) for a in get_file_name_list(fileobj=f_in_obj)]

                if self.storage.get_step_files('0').get('0-output_files', ""):
                    f_out_obj = self.storage.get_step_files('0').get('0-output_files')
                    f_out_obj.open()
                    choice_dict['out_file_choices'] = [(b, b) for b in get_file_name_list(fileobj=f_out_obj)]
            return choice_dict
        else:
            return super(CreateTestcaseWizard, self).get_form_kwargs(step)
Example #7
0
    def get_form_kwargs(self, step=None):
        if step == '0':
            return {'solution_ready' : self.solution_ready}
        if step == '1':
            choice_dict = {}
            testcase = Testcase.objects.get(pk=self.kwargs['testcaseID'])
            if self.storage.get_step_files('0'): # if there is at least one file.
                if self.storage.get_step_files('0').get('0-input_files', ""): # if input_file is uploaded.
                    f_in_obj = self.storage.get_step_files('0').get('0-input_files')
                    f_in_obj.open()
                    choice_dict['in_file_choices'] = [(a, a) for a in get_file_name_list(fileobj=f_in_obj)]
                elif testcase.input_files: # provide options from older file.
                    choice_dict['in_file_choices'] = [(b, b) for b in get_file_name_list(fileobj=testcase.input_files.file)]

                if self.storage.get_step_files('0').get('0-output_files', ""): # if output_file is uploaded.
                    f_out_obj = self.storage.get_step_files('0').get('0-output_files')
                    f_out_obj.open()
                    choice_dict['out_file_choices'] = [(b, b) for b in get_file_name_list(fileobj=f_out_obj)]
                elif testcase.output_files: # provide options from older file.
                    choice_dict['out_file_choices'] = [(b, b) for b in get_file_name_list(fileobj=testcase.output_files.file)]

            else: # No file uploaded in step 0
                if '0-input_files-clear' not in self.storage.get_step_data('0') and testcase.input_files:
                    choice_dict['in_file_choices'] = [(b, b) for b in get_file_name_list(fileobj=testcase.input_files.file)]
                else:
                    pass
                if '0-output_files-clear' not in self.storage.get_step_data('0') and testcase.output_files:
                    choice_dict['out_file_choices'] = [(b, b) for b in get_file_name_list(fileobj=testcase.output_files.file)]
            return choice_dict
        else:
            return super(EditTestcaseWizard, self).get_form_kwargs(step)
Example #8
0
    def get_form_kwargs(self, step=None):
        if step == '0':
            return {'solution_ready': self.solution_ready}
        if step == '1':
            choice_dict = {}
            if self.storage.get_step_files('0'):
                if self.storage.get_step_files('0').get('0-input_files', ""):
                    f_in_obj = self.storage.get_step_files('0').get(
                        '0-input_files')
                    f_in_obj.open()
                    choice_dict['in_file_choices'] = [
                        (a, a) for a in get_file_name_list(fileobj=f_in_obj)
                    ]

                if self.storage.get_step_files('0').get('0-output_files', ""):
                    f_out_obj = self.storage.get_step_files('0').get(
                        '0-output_files')
                    f_out_obj.open()
                    choice_dict['out_file_choices'] = [
                        (b, b) for b in get_file_name_list(fileobj=f_out_obj)
                    ]
            return choice_dict
        else:
            return super(CreateTestcaseWizard, self).get_form_kwargs(step)
Example #9
0
    def get_form_kwargs(self, step=None):
        if step == '0':
            return {'solution_ready': self.solution_ready}
        if step == '1':
            choice_dict = {}
            testcase = Testcase.objects.get(pk=self.kwargs['testcaseID'])
            if self.storage.get_step_files(
                    '0'):  # if there is at least one file.
                if self.storage.get_step_files('0').get(
                        '0-input_files', ""):  # if input_file is uploaded.
                    f_in_obj = self.storage.get_step_files('0').get(
                        '0-input_files')
                    f_in_obj.open()
                    choice_dict['in_file_choices'] = [
                        (a, a) for a in get_file_name_list(fileobj=f_in_obj)
                    ]
                elif testcase.input_files:  # provide options from older file.
                    choice_dict['in_file_choices'] = [
                        (b, b) for b in get_file_name_list(
                            fileobj=testcase.input_files.file)
                    ]

                if self.storage.get_step_files('0').get(
                        '0-output_files', ""):  # if output_file is uploaded.
                    f_out_obj = self.storage.get_step_files('0').get(
                        '0-output_files')
                    f_out_obj.open()
                    choice_dict['out_file_choices'] = [
                        (b, b) for b in get_file_name_list(fileobj=f_out_obj)
                    ]
                elif testcase.output_files:  # provide options from older file.
                    choice_dict['out_file_choices'] = [
                        (b, b) for b in get_file_name_list(
                            fileobj=testcase.output_files.file)
                    ]

            else:  # No file uploaded in step 0
                if '0-input_files-clear' not in self.storage.get_step_data(
                        '0') and testcase.input_files:
                    choice_dict['in_file_choices'] = [
                        (b, b) for b in get_file_name_list(
                            fileobj=testcase.input_files.file)
                    ]
                else:
                    pass
                if '0-output_files-clear' not in self.storage.get_step_data(
                        '0') and testcase.output_files:
                    choice_dict['out_file_choices'] = [
                        (b, b) for b in get_file_name_list(
                            fileobj=testcase.output_files.file)
                    ]
            return choice_dict
        else:
            return super(EditTestcaseWizard, self).get_form_kwargs(step)
Example #10
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 #11
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 #12
0
    def setup(self):
        # This is the directory where the student program is copied to.
        self.temp_dir = tempfile.mkdtemp(prefix="grader", dir=self.eval_dir)
        os.chdir(self.temp_dir)

        # Copy student solution files to tmp directory.
        extract_or_copy(src=self.submission.filePath.file.name, dest=self.temp_dir)

        # Another temp directory for running solution program. This is the directory where the solution program is copied to.
        self.solution_temp_dir = tempfile.mkdtemp(prefix="solution", dir=self.eval_dir)
        
        directories = [a for a in os.listdir('./') if os.path.isdir(a)]
        if directories:
            os.chdir(directories[0])
        currentDir = os.getcwd()

        # Extract the program file and the input file to the student directory, and the input file to the program directory as well.
        extract_or_copy(src=self.program.program_files.file.name, dest=currentDir)
        self.submittedFiles = get_file_name_list(name=self.submission.filePath.file.name)
        input_file = self.store_input_file(self.inputFile, currentDir)
        shutil.copy(src=input_file, dst=self.solution_temp_dir)
Example #13
0
    def clean_docfile(self):
        # Get all files that should be submitted by student from all programs of given AssignmentID.
        data = self.cleaned_data
        submitted_files = get_file_name_list(fileobj=data['docfile'])
        progrm_files_name = self.assignment_model_obj.student_program_files.split()

        # Check if tarfile contains all of these.
        missing_files = get_missing_files(progrm_files_name, fileobj=data['docfile'])

        # If there are missing files raise error and display all missing files.
        if missing_files:
            self.fields['docfile'].error_messages["docfile"] = "There were missing files."
            if set(submitted_files) >= set(progrm_files_name):
                error_msg = "Directory structure is incorrect! Put all source files in single directory only.\
                Nested directories are not allowed."
            else:
                error_msg = 'Missing files!. Your SUBMISSION contains "{0}" and we expect you to submit "{1}"\
                            Please upload again with missing files "{2}"'.format(" ".join(submitted_files),\
                            " ".join(progrm_files_name), " ".join(missing_files))
            raise forms.ValidationError(error_msg)
        return data['docfile']
Example #14
0
    def setup(self, submission, program_type):
        # Create a temporary directory inside the evaluation directory. Copy the submission files into this directory.
        src = submission.filePath.file.name  # gives absolute path
        self.temp_dir = tempfile.mkdtemp(prefix="grader", dir=self.eval_dir)
        os.chdir(self.temp_dir)
        extract_or_copy(src=src, dest=self.temp_dir)

        # Update or create the assignment result for the submission. Update the submitted files for existing assignment results.
        self.submittedFiles = get_file_name_list(name=src)
        self.assignment_result, _ = AssignmentResults.objects.get_or_create(
            submission=submission,
            defaults={'submitted_files': "\n".join(self.submittedFiles)})
        self.assignment_result.is_stale = False
        self.assignment_result.save()

        # Assuming only 1 directory level, cd to that directory. If the student submitted only 1 file, then dont do anything.
        directories = [a for a in os.listdir('./') if os.path.isdir(a)]
        if directories:
            os.chdir(directories[0])
        currentDir = os.getcwd()

        # Copy the program files (of all sections) to this directory. Note that the solution file is still student's submission.
        programs = ProgramModel.objects.filter(
            assignment=submission.assignment)
        testcaseList = []
        for program in programs:
            testcaseList.append(Testcase.objects.filter(program=program))
            if program.program_files:
                extract_or_copy(src=program.program_files.file.name,
                                dest=currentDir)

        # Copy input-output files for all testcases. Now we are ready for the evaluation process.
        for testcase in testcaseList:
            for test in testcase:
                if test.input_files:
                    extract_or_copy(src=test.input_files.file.name,
                                    dest=currentDir)
Example #15
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 #16
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