def __init__(self, inputFile, program, submission): # Setting section of the testcase, submission to use, the input file given by the student and the command executor that runs the commands. self.program = program self.submission = submission self.inputFile = inputFile self.commandExecutor = CommandExecutor() self.eval_dir = os.path.join(PROJECT_ROOT, '../evaluate/safeexec')
def __init__(self, instance): self.program = instance.program self.testcase = instance self.input_files = instance.input_files self.commandExecutor = CommandExecutor() self.failed = False self.eval_dir = os.path.join(PROJECT_ROOT, '../evaluate/safeexec')
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()
def compile_model_solution(sender, instance, **kwargs): # If source file was not changed no need to re-compile. if not hasattr(instance, 'source_changed'): return if not kwargs.get('created'): print "Not created!" programFilePath = instance.program_files.name ModelSolutionPath = instance.model_solution.name # Model Solution was not provided hence do nothing. if os.path.isdir(ModelSolutionPath): return os.chdir(tmpDir) extractFile(os.path.join(MEDIA_ROOT, ModelSolutionPath), tmpDir) # Change directory if solution tar contains a directory. if os.path.isdir(os.listdir('./')[0]): os.chdir(os.listdir('./')[0]) currentDir = os.getcwd() extractFile(os.path.join(MEDIA_ROOT, programFilePath), currentDir) # Setting up compilation and creating CommandExecutor executor = CommandExecutor(timeout=5) command = get_compilation_command(instance) ''' To Remove: COMPILER_FLAGS exeName = "exefile_" + str(instance.id) compilerFlags = instance.compiler_flags if instance.compiler_flags else "" command = instance.compiler_name + " " + compilerFlags\ + " " + instance.file_names_to_compile + " " + " -o " + exeName ''' compileResult = executor.executeCommand(command=command) # Update output files on success if compileResult.returnCode == 0: instance.UpdateOutput() cleanUp()
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)
def __init__(self, submission): self.commandExecutor = CommandExecutor() self.submission = submission self.eval_dir = os.path.join(PROJECT_ROOT, '../evaluate/safeexec')
def __init__(self, testcase, program_result): self.testcase = testcase self.program_result = program_result self.name = testcase.name self.stdInFile = str(testcase.std_in_file_name) self.commandExecutor = CommandExecutor()