def run(self): """Runs the corresponding script for every Delivery which has not yet been run.""" results = { AutomaticCorrectionRunner.SUCCESSFULL_RESULTS_KEY: 0, AutomaticCorrectionRunner.FAILED_RESULTS_KEY: 0 } pending_automatic_corrections = self.selection_strategy.get_automatic_corrections( ) for pending_automatic_correction in pending_automatic_corrections: try: self.setup_enviroment.run(pending_automatic_correction, AutomaticCorrectionRunner.TMP_DIR) self.run_script_command.set_script( os.path.join( AutomaticCorrectionRunner.TMP_DIR, os.path.basename(pending_automatic_correction.script))) script_result = self.run_script_command.execute() except Exception, e: script_result = ScriptResult() script_result.captured_stdout = "An error has occurred when running the automatic correction process. Error information: " + str( e) script_result.exit_value = 2 script_result.automatic_correction = pending_automatic_correction for visitor in self.publish_result_visitors: script_result.accept(visitor) self.clean_up_tmp_dir() if (script_result.exit_value == 0): results[AutomaticCorrectionRunner.SUCCESSFULL_RESULTS_KEY] += 1 else: results[AutomaticCorrectionRunner.FAILED_RESULTS_KEY] += 1
def testEjecutarDeberiaInvocarAlCorregirCuandoHayCorreccionesPendientes( self): corrector = AutomaticCorrectionRunner() automatic_correction = Mock() return_value = Mock() automatic_correction.delivery.practice.get_script.return_value = return_value automatic_correction.script = "test_script.sh" selection_strategy_mock = Mock() selection_strategy_mock.get_automatic_corrections.return_value = ( automatic_correction, ) setup_enviroment_mock = Mock() script_result = ScriptResult() script_result.exit_value = 0 script_result.captured_stdout = "some stdout" execution_command_mock = Mock() execution_command_mock.execute.return_value = script_result publication_visitor = Mock() corrector.selection_strategy = selection_strategy_mock corrector.setup_enviroment = setup_enviroment_mock corrector.run_script_command = execution_command_mock corrector.publish_result_visitors = (publication_visitor, ) corrector.run() selection_strategy_mock.get_automatic_corrections.assert_called() setup_enviroment_mock.setup_enviroment.assert_called() execution_command_mock.execute.assert_called() publication_visitor.visit.assert_called()
def execute(self): if (self.script is None): self.log.error( "attempt to run the correction process but the script to be run is not defined." ) raise IllegalStateException( reason="In order to execute the script, you must set it first." ) # now we may call the script self.log.debug("launching correction process...") # FIXME: this is plain shit. We should try to use 'with' automatic_correction_tmp_dir = os.path.dirname( self.script ) # managepath.get_instance().get_automatic_correction_tmp_dir() current_dir = os.getcwd() self.log.debug("current dir: %s", current_dir) self.log.debug("script dir : %s", automatic_correction_tmp_dir) os.chdir(automatic_correction_tmp_dir) process = subprocess.Popen([self.script], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) os.chdir(current_dir) # fin FIXME!!! # must ensure that the process won't run forever process_timer = ProcessTimeout(process, RunScriptCommand.TIME_OUT) process_timer.start_timer() self.log.debug("Process timeout timer launched") # finally, we must capture all results so the can be published script_result = ScriptResult() self.log.debug("waiting for process to finish...") script_result.exit_value = process.wait() self.log.debug("process finished with exit value %d", script_result.exit_value) #if the result has been obtained, the is no point on keeping the timer alive if process_timer.ran: self.log.info( "Execution has been terminated for exceding the timeout limit." ) else: process_timer.cancel_timer() self.log.debug( "Process finished correctly without exceding timeout limit.") output = process.communicate() script_result.captured_stdout = output[0] self.log.debug("stdout captured.") self.log.debug("excecution completed.") return script_result
def setUp(self): self.automatic_correction = Mock(spec=AutomaticCorrection) self.script_result = ScriptResult() self.script_result.automatic_correction = self.automatic_correction self.script_result.exit_value = self.exit_value self.script_result.captured_stdout = self.stdout