def __init__(self, input_str=None):
     """
     Initializes the json string that should be turn into a local entity
     """
     self.log = LoggerManager().get_new_logger(
         "auto correction-json translator")
     self.json = input_str
class JSONToAutoCorrectionTranslator():
    """
    This class' responsability is limited to generate automatic correction entities out of the json strings recieved from the rest api
    """
    def __init__(self, input_str=None):
        """
        Initializes the json string that should be turn into a local entity
        """
        self.log = LoggerManager().get_new_logger(
            "auto correction-json translator")
        self.json = input_str

    def get_automatic_corrections(self):
        self.log.debug("parsing: %s", self.json)
        automatic_correction_data_list = json.loads(self.json)['results']
        automatic_correction_list = []
        for automatic_correction_data in automatic_correction_data_list:
            automatic_correction = AutomaticCorrection(
                automatic_correction_data['id'])
            automatic_correction.captured_stdout = automatic_correction_data[
                'captured_stdout']
            automatic_correction.exit_value = automatic_correction_data[
                'exit_value']
            automatic_correction.status = automatic_correction_data['status']
            automatic_correction.delivery_id = automatic_correction_data[
                'delivery']
            automatic_correction.delivery = automatic_correction_data[
                'get_delivery_file']
            automatic_correction.script = automatic_correction_data[
                'get_correction_script']
            automatic_correction.user_mail = automatic_correction_data[
                'user_mail']
            automatic_correction_list.append(automatic_correction)
        return automatic_correction_list
Esempio n. 3
0
    def run(self):
        self.log = LoggerManager().get_new_logger("daemon control")
        self.log.info("Daemon's game loop started.")
        while True:
            start_timestamp = datetime.today()

            try:
                result = self.automatic_correction_runner.run()
                self.log.info(
                    "Automatic correction process completed.\nResult summary: \n\tsuccessfull: %d\n\tfailed: %d",
                    result[AutomaticCorrectionRunner.SUCCESSFULL_RESULTS_KEY],
                    result[AutomaticCorrectionRunner.FAILED_RESULTS_KEY])
            except:
                self.log.exception(
                    "The automatic correction process failed. Have in mind that is required that the web service must be online."
                )

            try:
                self.administrator_mail.send_mails()
            except:
                self.log.exception(
                    "The mail sending process failed. Have in mind that is required that the web service must be online."
                )

            finish_timestamp = datetime.today()
            self.stall_loop(start_timestamp, finish_timestamp)
class JSONToAutoCorrectionTranslator():
    """
    This class' responsability is limited to generate automatic correction entities out of the json strings recieved from the rest api
    """


    def __init__(self, input_str=None):
        """
        Initializes the json string that should be turn into a local entity
        """
        self.log = LoggerManager().get_new_logger("auto correction-json translator")
        self.json = input_str
    
    def get_automatic_corrections(self):
        self.log.debug("parsing: %s", self.json)
        automatic_correction_data_list = json.loads(self.json)['results']
        automatic_correction_list = []
        for automatic_correction_data in automatic_correction_data_list:
            automatic_correction = AutomaticCorrection(automatic_correction_data['id'])
            automatic_correction.captured_stdout = automatic_correction_data['captured_stdout']
            automatic_correction.exit_value = automatic_correction_data['exit_value']
            automatic_correction.status = automatic_correction_data['status']
            automatic_correction.delivery_id = automatic_correction_data['delivery']
            automatic_correction.delivery = automatic_correction_data['get_delivery_file']
            automatic_correction.script = automatic_correction_data['get_correction_script']
            automatic_correction.user_mail = automatic_correction_data['user_mail']
            automatic_correction_list.append(automatic_correction);
        return automatic_correction_list
Esempio n. 5
0
 def __init__(self, json_str=None):
     """
     Constructor
     """
     self.json = json_str
     logger_manager = LoggerManager()
     self.log = logger_manager.get_new_logger("json translator")
class AutomaticCorrectionSelectionStrategyThroughRestApi(
        AutomaticCorrectionSelectionStrategy):
    """
    Implementation of the selection strategy that brings the automatic correction information from the rest api
    """

    HTTP_AUTOMATIC_CORRECTION_SERIALIZER = REST_BASE_URL + '/richautomaticcorrectionserializer/'

    def __init__(self, auth_user, auth_pass):
        """
        Constructor
        """
        self.log = LoggerManager().get_new_logger("auto correction selection")
        self.rest_api_helper = RestApiHelper(
            auth_user, auth_pass,
            AutomaticCorrectionSelectionStrategyThroughRestApi.
            HTTP_AUTOMATIC_CORRECTION_SERIALIZER)

    def get_automatic_corrections(self):
        self.log.debug("searching for deliveries with status pending...")
        pending_automatic_corrections = self.rest_api_helper.get_automatic_corrections(
        )
        count = len(pending_automatic_corrections)
        if count > 0:
            self.log.debug("%d deliveries obtained.", count)
        return pending_automatic_corrections
Esempio n. 7
0
 def __init__(self, json_str=None):
     """
     Constructor
     """
     self.json = json_str
     logger_manager = LoggerManager()
     self.log = logger_manager.get_new_logger("json translator")
Esempio n. 8
0
 def __init__(self, auth_user, auth_pass):
     self.log = LoggerManager().get_new_logger("result publication mail")
     self.rest_api_helper = RestApiHelper(
         auth_user,
         auth_pass,
         http_mail_serializer=PublishResultsVisitorMail.HTTP_MAIL_SERIALIZER
     )
 def __init__(self, auth_user, auth_pass):
     """
     Constructor
     """
     self.log = LoggerManager().get_new_logger("auto correction selection")
     self.rest_api_helper = RestApiHelper(
         auth_user, auth_pass,
         AutomaticCorrectionSelectionStrategyThroughRestApi.
         HTTP_AUTOMATIC_CORRECTION_SERIALIZER)
Esempio n. 10
0
 def __init__(self):
     logger_manager = LoggerManager()
     self.log = logger_manager.get_new_logger("administrator mail")
     self.mail_handle_strategy = MailHandleRESTAPIStrategy(
                                                       http_serializer=HTTP_SERIALIZER, 
                                                       auth_user=settings.SERIALIZER_AUTH_USER, 
                                                       auth_pass=settings.SERIALIZER_AUTH_PASS)
     self.session_helper = MailSessionHelper(email_host=settings.EMAIL_HOST, 
                                                      email_port=settings.EMAIL_PORT, 
                                                      email_host_user=settings.EMAIL_HOST_USER, 
                                                      email_host_password=settings.EMAIL_HOST_PASSWORD)
Esempio n. 11
0
 def __init__(self):
     logger_manager = LoggerManager()
     self.log = logger_manager.get_new_logger("loop runner")
     self.loop_interval = LoopRunner.LOOP_INTERVAL  # in seconds
     self.stdin_path = '/dev/null'
     self.stdout_path = '/dev/tty'
     self.stderr_path = '/dev/tty'
     self.pidfile_path = '/tmp/foo.pid'
     self.pidfile_timeout = 5
     self.automatic_correction_runner = AutomaticCorrectionRunner()
     self.administrator_mail = AdministratorMail()
Esempio n. 12
0
 def __init__(self):
     logger_manager = LoggerManager()
     self.log = logger_manager.get_new_logger("loop runner")
     self.loop_interval = LoopRunner.LOOP_INTERVAL # in seconds
     self.stdin_path = '/dev/null'
     self.stdout_path = '/dev/tty'
     self.stderr_path = '/dev/tty'
     self.pidfile_path =  '/tmp/foo.pid'
     self.pidfile_timeout = 5
     self.automatic_correction_runner = AutomaticCorrectionRunner()
     self.administrator_mail = AdministratorMail()
 def __init__(self, http_serializer=None, auth_user=None, auth_pass=None):
     """
     Constructor
     """
     logger_manager = LoggerManager()
     self.log = logger_manager.get_new_logger("administrator mail")
     self.http_serializer = http_serializer
     self.auth_user = auth_user
     self.auth_pass = auth_pass
     
     self.json_translator = JSONToMailTranslator()
     self.requests = requests
Esempio n. 14
0
    def __init__(self, http_serializer=None, auth_user=None, auth_pass=None):
        """
        Constructor
        """
        logger_manager = LoggerManager()
        self.log = logger_manager.get_new_logger("administrator mail")
        self.http_serializer = http_serializer
        self.auth_user = auth_user
        self.auth_pass = auth_pass

        self.json_translator = JSONToMailTranslator()
        self.requests = requests
Esempio n. 15
0
 def __init__(self):
     logger_manager = LoggerManager()
     self.log = logger_manager.get_new_logger("administrator mail")
     self.mail_handle_strategy = MailHandleRESTAPIStrategy(
         http_serializer=HTTP_SERIALIZER,
         auth_user=settings.SERIALIZER_AUTH_USER,
         auth_pass=settings.SERIALIZER_AUTH_PASS)
     self.session_helper = MailSessionHelper(
         email_host=settings.EMAIL_HOST,
         email_port=settings.EMAIL_PORT,
         email_host_user=settings.EMAIL_HOST_USER,
         email_host_password=settings.EMAIL_HOST_PASSWORD)
 def __init__(self, auth_user, auth_pass):
     """
     Constructor
     """
     self.log = LoggerManager().get_new_logger("auto correction selection")
     self.rest_api_helper = RestApiHelper(auth_user, auth_pass, 
                                          AutomaticCorrectionSelectionStrategyThroughRestApi.HTTP_AUTOMATIC_CORRECTION_SERIALIZER)
Esempio n. 17
0
class LoopRunner():

    LOOP_INTERVAL = 65

    def __init__(self):
        logger_manager = LoggerManager()
        self.log = logger_manager.get_new_logger("loop runner")
        self.loop_interval = LoopRunner.LOOP_INTERVAL  # in seconds
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path = '/tmp/foo.pid'
        self.pidfile_timeout = 5
        self.automatic_correction_runner = AutomaticCorrectionRunner()
        self.administrator_mail = AdministratorMail()

    def stall_loop(self, start_timestamp, finish_timestamp):
        delta = finish_timestamp - start_timestamp
        time_to_wait = self.loop_interval - delta.seconds  # if the process took less than 30 seconds, we will wait
        if (time_to_wait > 0):
            time.sleep(time_to_wait)
        else:
            print("not sleeping... delta: " + str(delta))
        start_timestamp = datetime.today()

    def print_result(self, result):
        # TODO: pass to a log entry
        print str(datetime.today()) + " | " + str(result)

    def run(self):
        self.log = LoggerManager().get_new_logger("daemon control")
        self.log.info("Daemon's game loop started.")
        while True:
            start_timestamp = datetime.today()

            try:
                result = self.automatic_correction_runner.run()
                self.log.info(
                    "Automatic correction process completed.\nResult summary: \n\tsuccessfull: %d\n\tfailed: %d",
                    result[AutomaticCorrectionRunner.SUCCESSFULL_RESULTS_KEY],
                    result[AutomaticCorrectionRunner.FAILED_RESULTS_KEY])
            except:
                self.log.exception(
                    "The automatic correction process failed. Have in mind that is required that the web service must be online."
                )

            try:
                self.administrator_mail.send_mails()
            except:
                self.log.exception(
                    "The mail sending process failed. Have in mind that is required that the web service must be online."
                )

            finish_timestamp = datetime.today()
            self.stall_loop(start_timestamp, finish_timestamp)
class PublishResultsVisitorWeb(PublishResultsVisitor):
    """
    
    This is the visitor in charge of publishing the results to the web.
    
    """
    
    HTTP_MAIL_SERIALIZER = REST_BASE_URL + '/automaticcorrectionserializer/'
    
    def __init__(self, auth_user, auth_pass):
        self.log = LoggerManager().get_new_logger("result publication")
        self.rest_api_helper = RestApiHelper(auth_user, auth_pass, PublishResultsVisitorWeb.HTTP_MAIL_SERIALIZER)
    
    def visit(self, visitable):
        self.log.debug("publishing results...")
        visitable.automatic_correction.exit_value = visitable.exit_value
        visitable.automatic_correction.captured_stdout = visitable.captured_stdout
        visitable.automatic_correction.status = 1 + (-2 * visitable.exit_value)
        self.rest_api_helper.save_automatic_correction(visitable.automatic_correction)
        self.log.debug("results published in through the web interface.")
Esempio n. 19
0
 def __init__(self, auth_user, auth_pass, 
              http_automatic_correction_serializer=None, 
              http_delivery_serializer=None,
              http_practice_serializer=None,
              http_script_serializer=None,
              http_mail_serializer=None):
     """
     Constructor
     """
     logger_manager = LoggerManager()
     self.log = logger_manager.get_new_logger("auto-correction-strategy")
     self.http_automatic_correction_serializer = http_automatic_correction_serializer
     self.http_delivery_serializer = http_delivery_serializer
     self.http_practice_serializer = http_practice_serializer
     self.http_script_serializer = http_script_serializer
     self.http_mail_serializer = http_mail_serializer
     self.auth_user = auth_user
     self.auth_pass = auth_pass
     
     self.requests = requests
     self.json_translator = JSONToAutoCorrectionTranslator()
Esempio n. 20
0
class LoopRunner():
    
    LOOP_INTERVAL = 65
    
    def __init__(self):
        logger_manager = LoggerManager()
        self.log = logger_manager.get_new_logger("loop runner")
        self.loop_interval = LoopRunner.LOOP_INTERVAL # in seconds
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/foo.pid'
        self.pidfile_timeout = 5
        self.automatic_correction_runner = AutomaticCorrectionRunner()
        self.administrator_mail = AdministratorMail()
    
    def stall_loop(self, start_timestamp, finish_timestamp):
        delta = finish_timestamp - start_timestamp
        time_to_wait = self.loop_interval - delta.seconds # if the process took less than 30 seconds, we will wait
        if (time_to_wait > 0):
            time.sleep(time_to_wait)
        else:
            print("not sleeping... delta: " + str(delta))
        start_timestamp = datetime.today()
    
    def print_result(self, result):
        # TODO: pass to a log entry
        print str(datetime.today()) + " | " + str(result)
    
    def run(self):
        self.log = LoggerManager().get_new_logger("daemon control")
        self.log.info("Daemon's game loop started.")
        while True:
            start_timestamp = datetime.today()
            
            try:
                result = self.automatic_correction_runner.run()
                self.log.info("Automatic correction process completed.\nResult summary: \n\tsuccessfull: %d\n\tfailed: %d", 
                              result[AutomaticCorrectionRunner.SUCCESSFULL_RESULTS_KEY],
                              result[AutomaticCorrectionRunner.FAILED_RESULTS_KEY])
            except:
                self.log.exception("The automatic correction process failed. Have in mind that is required that the web service must be online.")
            
            try:
                self.administrator_mail.send_mails()
            except:
                self.log.exception("The mail sending process failed. Have in mind that is required that the web service must be online.")
            
            finish_timestamp = datetime.today()
            self.stall_loop(start_timestamp, finish_timestamp)
class AutomaticCorrectionSelectionStrategyThroughRestApi(AutomaticCorrectionSelectionStrategy):
    """
    Implementation of the selection strategy that brings the automatic correction information from the rest api
    """
    
    HTTP_AUTOMATIC_CORRECTION_SERIALIZER = REST_BASE_URL + '/richautomaticcorrectionserializer/'
    
    def __init__(self, auth_user, auth_pass):
        """
        Constructor
        """
        self.log = LoggerManager().get_new_logger("auto correction selection")
        self.rest_api_helper = RestApiHelper(auth_user, auth_pass, 
                                             AutomaticCorrectionSelectionStrategyThroughRestApi.HTTP_AUTOMATIC_CORRECTION_SERIALIZER)
    
    def get_automatic_corrections(self):
        self.log.debug("searching for deliveries with status pending...")
        pending_automatic_corrections = self.rest_api_helper.get_automatic_corrections()
        count = len(pending_automatic_corrections)
        if count > 0:
            self.log.debug("%d deliveries obtained.", count)
        return pending_automatic_corrections
Esempio n. 22
0
class PublishResultsVisitorMail(PublishResultsVisitor):
    """
    Implementation of the publishing visitor which posts a mail through the web interface to await sending
    """

    HTTP_MAIL_SERIALIZER = REST_BASE_URL + '/mailserializer/'
    STATUS_STRINGS = {-1: "failed", 0: "pending", 1: "successfull"}
    STATUS_UNKNOWN = 'unknown status'

    def __init__(self, auth_user, auth_pass):
        self.log = LoggerManager().get_new_logger("result publication mail")
        self.rest_api_helper = RestApiHelper(
            auth_user,
            auth_pass,
            http_mail_serializer=PublishResultsVisitorMail.HTTP_MAIL_SERIALIZER
        )

    def get_status(self, status=10):
        """Returns a status raw value as a human readable value"""
        self.log.debug("translating status %d", status)
        try:
            status_string = PublishResultsVisitorMail.STATUS_STRINGS[status]
        except:
            status_string = PublishResultsVisitorMail.STATUS_UNKNOWN
        return status_string

    def build_mail(self, result):
        self.log.debug("building mail...")
        mail = Mail()
        mail.subject = "Resultado de la correccion automatica"
        mail.recipient = result.automatic_correction.user_mail
        exit_value = result.exit_value
        mail.body = "Ejecucion "
        if (exit_value == 0):
            mail.body += "exitosa, trabajo aprobado"
        else:
            mail.body += "fallida, trabajo no aprobado. Puede corregirlo y volver a intentarlo"
        mail.body += ".\n\n"
        return mail

    def visit(self, visitable):
        self.log.debug("publishing results...")
        visitable.automatic_correction.exit_value = visitable.exit_value
        visitable.automatic_correction.captured_stdout = visitable.captured_stdout
        visitable.automatic_correction.status = 1 + (-2 * visitable.exit_value)
        mail = self.build_mail(visitable)
        self.rest_api_helper.save_mail(mail)
        self.log.debug("results published in through mail.")
Esempio n. 23
0
class PublishResultsVisitorWeb(PublishResultsVisitor):
    """
    
    This is the visitor in charge of publishing the results to the web.
    
    """

    HTTP_MAIL_SERIALIZER = REST_BASE_URL + '/automaticcorrectionserializer/'

    def __init__(self, auth_user, auth_pass):
        self.log = LoggerManager().get_new_logger("result publication")
        self.rest_api_helper = RestApiHelper(
            auth_user, auth_pass,
            PublishResultsVisitorWeb.HTTP_MAIL_SERIALIZER)

    def visit(self, visitable):
        self.log.debug("publishing results...")
        visitable.automatic_correction.exit_value = visitable.exit_value
        visitable.automatic_correction.captured_stdout = visitable.captured_stdout
        visitable.automatic_correction.status = 1 + (-2 * visitable.exit_value)
        self.rest_api_helper.save_automatic_correction(
            visitable.automatic_correction)
        self.log.debug("results published in through the web interface.")
class PublishResultsVisitorMail(PublishResultsVisitor):
    """
    Implementation of the publishing visitor which posts a mail through the web interface to await sending
    """
    
    HTTP_MAIL_SERIALIZER = REST_BASE_URL + '/mailserializer/'
    STATUS_STRINGS = {-1:"failed", 0:"pending", 1:"successfull"}
    STATUS_UNKNOWN = 'unknown status'
    
    def __init__(self, auth_user, auth_pass):
        self.log = LoggerManager().get_new_logger("result publication mail")
        self.rest_api_helper = RestApiHelper(auth_user, auth_pass, 
                                             http_mail_serializer=PublishResultsVisitorMail.HTTP_MAIL_SERIALIZER)
    
    def get_status(self, status=10):
        """Returns a status raw value as a human readable value"""
        self.log.debug("translating status %d", status)
        try:
            status_string = PublishResultsVisitorMail.STATUS_STRINGS[status]
        except:
            status_string = PublishResultsVisitorMail.STATUS_UNKNOWN
        return status_string
    
    def build_mail(self, result):
        self.log.debug("building mail...")
        mail = Mail()
        mail.subject = "Resultado de la correccion automatica"
        mail.recipient = result.automatic_correction.user_mail
        exit_value = result.exit_value
        mail.body = "Ejecucion "
        if(exit_value==0):
            mail.body += "exitosa, trabajo aprobado"
        else:
            mail.body += "fallida, trabajo no aprobado. Puede corregirlo y volver a intentarlo"
        mail.body += ".\n\n"
        return mail
    
    def visit(self, visitable):
        self.log.debug("publishing results...")
        visitable.automatic_correction.exit_value = visitable.exit_value
        visitable.automatic_correction.captured_stdout = visitable.captured_stdout
        visitable.automatic_correction.status = 1 + (-2 * visitable.exit_value)
        mail = self.build_mail(visitable)
        self.rest_api_helper.save_mail(mail)
        self.log.debug("results published in through mail.")
Esempio n. 25
0
class SetupEnviroment():

    def __init__(self):
        self.prepare_files_strategy = PrepareFilesStrategyZip()
        self.log = LoggerManager().get_new_logger("setup enviroment")
    
    def run(self, automatic_correction, working_dir):
        self.log.debug("setting up enviroment...")
        self.log.debug("cleaning up working directory...")
        shutil.rmtree(working_dir, ignore_errors=True)
        self.log.debug("preparing delivery files...")
        self.prepare_files_strategy.zip = automatic_correction.delivery
        os.mkdir(working_dir)
        self.prepare_files_strategy.prepare_files(working_dir)
        shutil.copy(automatic_correction.script, working_dir + "/" + os.path.basename(automatic_correction.script))
        # We must ensure the script is runnable
        process = subprocess.Popen(["chmod", "a+x", working_dir + "/" + os.path.basename(automatic_correction.script)])
        process.wait()
        self.log.debug("enviroment set.")
Esempio n. 26
0
 def run(self):
     self.log = LoggerManager().get_new_logger("daemon control")
     self.log.info("Daemon's game loop started.")
     while True:
         start_timestamp = datetime.today()
         
         try:
             result = self.automatic_correction_runner.run()
             self.log.info("Automatic correction process completed.\nResult summary: \n\tsuccessfull: %d\n\tfailed: %d", 
                           result[AutomaticCorrectionRunner.SUCCESSFULL_RESULTS_KEY],
                           result[AutomaticCorrectionRunner.FAILED_RESULTS_KEY])
         except:
             self.log.exception("The automatic correction process failed. Have in mind that is required that the web service must be online.")
         
         try:
             self.administrator_mail.send_mails()
         except:
             self.log.exception("The mail sending process failed. Have in mind that is required that the web service must be online.")
         
         finish_timestamp = datetime.today()
         self.stall_loop(start_timestamp, finish_timestamp)
Esempio n. 27
0
class SetupEnviroment():
    def __init__(self):
        self.prepare_files_strategy = PrepareFilesStrategyZip()
        self.log = LoggerManager().get_new_logger("setup enviroment")

    def run(self, automatic_correction, working_dir):
        self.log.debug("setting up enviroment...")
        self.log.debug("cleaning up working directory...")
        shutil.rmtree(working_dir, ignore_errors=True)
        self.log.debug("preparing delivery files...")
        self.prepare_files_strategy.zip = automatic_correction.delivery
        os.mkdir(working_dir)
        self.prepare_files_strategy.prepare_files(working_dir)
        shutil.copy(
            automatic_correction.script,
            working_dir + "/" + os.path.basename(automatic_correction.script))
        # We must ensure the script is runnable
        process = subprocess.Popen([
            "chmod", "a+x",
            working_dir + "/" + os.path.basename(automatic_correction.script)
        ])
        process.wait()
        self.log.debug("enviroment set.")
Esempio n. 28
0
class RunScriptCommand():
    """
    
    Top class for the hierarchy of the Command in charge of running the scripts
    of autocorrection for Practices. This should ONLY run the script and 
    capture the results, so that they can be published afterwards when the
    visitors in the 'publication' package are run.
    
    """
    
    TIME_OUT = 60 # seconds
    
    def __init__(self):
        self.script = None
        self.log = LoggerManager().get_new_logger("run script")
    
    def set_script(self, script):
        self.script = script
    
    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
Esempio n. 29
0
 def __init__(self):
     self.script = None
     self.log = LoggerManager().get_new_logger("run script")
Esempio n. 30
0
 def __init__(self):
     self.script = None
     self.log = LoggerManager().get_new_logger("run script")
Esempio n. 31
0
class RunScriptCommand():
    """
    
    Top class for the hierarchy of the Command in charge of running the scripts
    of autocorrection for Practices. This should ONLY run the script and 
    capture the results, so that they can be published afterwards when the
    visitors in the 'publication' package are run.
    
    """

    TIME_OUT = 60  # seconds

    def __init__(self):
        self.script = None
        self.log = LoggerManager().get_new_logger("run script")

    def set_script(self, script):
        self.script = script

    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 __init__(self, auth_user, auth_pass):
     self.log = LoggerManager().get_new_logger("result publication mail")
     self.rest_api_helper = RestApiHelper(auth_user, auth_pass, 
                                          http_mail_serializer=PublishResultsVisitorMail.HTTP_MAIL_SERIALIZER)
Esempio n. 33
0
 def __init__(self):
     self.prepare_files_strategy = PrepareFilesStrategyZip()
     self.log = LoggerManager().get_new_logger("setup enviroment")
Esempio n. 34
0
                self.log.exception(
                    "The mail sending process failed. Have in mind that is required that the web service must be online."
                )

            finish_timestamp = datetime.today()
            self.stall_loop(start_timestamp, finish_timestamp)


parser = argparse.ArgumentParser(
    description='Starts or stops the seal daemon.')
parser.add_argument('command',
                    metavar='start/stop',
                    help='the command that should be executed on the daemon')
args = parser.parse_args()

logger_manager = LoggerManager()
log = logger_manager.getLogger()
log.info("Daemon action recived: '%s'", args.command)

loop_runner = LoopRunner()
daemon_runner = DaemonRunner(loop_runner)  # runner.DaemonRunner(loop_runner)
daemon_runner.daemon_context.files_preserve = [
    logger_manager.get_file_handler().stream
]
daemon_runner.do_action()

if ("start".__eq__(args.command.lower())):
    log.info("Daemon launched.")
else:
    log.info("Daemon stopped.")
Esempio n. 35
0
 def __init__(self):
     self.prepare_files_strategy = PrepareFilesStrategyZip()
     self.log = LoggerManager().get_new_logger("setup enviroment")
Esempio n. 36
0
                self.log.exception("The automatic correction process failed. Have in mind that is required that the web service must be online.")
            
            try:
                self.administrator_mail.send_mails()
            except:
                self.log.exception("The mail sending process failed. Have in mind that is required that the web service must be online.")
            
            finish_timestamp = datetime.today()
            self.stall_loop(start_timestamp, finish_timestamp)
           


parser = argparse.ArgumentParser(description='Starts or stops the seal daemon.')
parser.add_argument('command', metavar='start/stop', 
                    help='the command that should be executed on the daemon')
args = parser.parse_args()

logger_manager = LoggerManager()
log = logger_manager.getLogger()
log.info("Daemon action recived: '%s'", args.command)

loop_runner = LoopRunner()
daemon_runner = DaemonRunner(loop_runner) # runner.DaemonRunner(loop_runner)
daemon_runner.daemon_context.files_preserve=[logger_manager.get_file_handler().stream]
daemon_runner.do_action()

if("start".__eq__(args.command.lower())):
    log.info("Daemon launched.")
else:
    log.info("Daemon stopped.")
Esempio n. 37
0
class ProcessTimeout:
    """
    Handler of the timer for setting a timeout to the automatic correction process
    """
    def __init__(self, process, timeout):
        self.process = process
        self.timeout = timeout
        self.ran = False
        self.timer = Timer(self.timeout, self.kill_proc)
        self.logger = LoggerManager().get_new_logger("process timeout")

    def killtree(self):
        parent = psutil.Process(self.process.pid)
        for child in parent.get_children(recursive=True):
            self.logger.debug("killing process %d", child.pid)
            child.kill()
        self.logger.debug("killing process %d", parent.pid)
        parent.kill()

    def kill_proc(self):
        self.logger.debug("timer expired, killing process...")
        self.killtree()
        self.logger.debug("process terminate invoked.")
        self.ran = True

    def start_timer(self):
        self.logger.debug("timer started")
        self.timer.start()

    def cancel_timer(self):
        self.logger.debug("timer cancelled")
        self.timer.cancel()
Esempio n. 38
0
 def testLoggerManagerMustReturnALoggerAppropiatelyConfiguredWhenProvidedAName(self):
     logger_manager = LoggerManager()
     logger = logger_manager.getLogger("a_name")
     pass
Esempio n. 39
0
 def __init__(self, auth_user, auth_pass):
     self.log = LoggerManager().get_new_logger("result publication")
     self.rest_api_helper = RestApiHelper(
         auth_user, auth_pass,
         PublishResultsVisitorWeb.HTTP_MAIL_SERIALIZER)
 def __init__(self, input_str=None):
     """
     Initializes the json string that should be turn into a local entity
     """
     self.log = LoggerManager().get_new_logger("auto correction-json translator")
     self.json = input_str
Esempio n. 41
0
 def __init__(self):
     """Initializes the zip file path to None"""
     self.zip = None
     self.log = LoggerManager().get_new_logger("preparation")
 def __init__(self, auth_user, auth_pass):
     self.log = LoggerManager().get_new_logger("result publication")
     self.rest_api_helper = RestApiHelper(auth_user, auth_pass, PublishResultsVisitorWeb.HTTP_MAIL_SERIALIZER)
Esempio n. 43
0
class PrepareFilesStrategyZip(PrepareFilesStrategy):
    """
    
    Strategy meant to obtain the files to be "auto-checked" from a zip file uploaded to the app.
    
    """


    def __init__(self):
        """Initializes the zip file path to None"""
        self.zip = None
        self.log = LoggerManager().get_new_logger("preparation")
    
    def prepare_files(self, destination_path):
        self.log.debug("preparing files for automatic correction process...")
        if (self.zip is None):
            raise IllegalStateException(reason="In order to prepare the files by unzipping them, you must set the zip file as source.\
                                                Try setting the zip attribute for this object.")
        zipfile = ZipFile(self.zip)
        zipfile.extractall(destination_path)
        
        self.log.debug("\n\nWalking destination path...")
        for path, directories, files in os.walk(destination_path):
            self.log.debug("path: %s", path)
            for directory in directories:
                self.log.debug("directory: %s", directory)
            for a_file in files:
                self.log.debug("file: %s", a_file)
        self.log.debug("\n\n")
        
        self.log.debug("zip file extracted to %s.", destination_path)
Esempio n. 44
0
 def __init__(self, process, timeout):
     self.process = process
     self.timeout = timeout
     self.ran = False
     self.timer = Timer(self.timeout, self.kill_proc)
     self.logger = LoggerManager().get_new_logger("process timeout")
Esempio n. 45
0
 def testLoggerManagerMustReturnALoggerAppropiatelyConfiguredWhenProvidedAName(
         self):
     logger_manager = LoggerManager()
     logger = logger_manager.getLogger("a_name")
     pass
Esempio n. 46
0
 def testLoggerManagerMustReturnALoggerAppropiatelyConfiguredWhenANameIsNotProvided(self):
     logger_manager = LoggerManager()
     logger = logger_manager.getLogger()
     pass
Esempio n. 47
0
class ProcessTimeout:
    """
    Handler of the timer for setting a timeout to the automatic correction process
    """


    def __init__(self, process, timeout):
        self.process = process
        self.timeout = timeout
        self.ran = False
        self.timer = Timer(self.timeout, self.kill_proc)
        self.logger = LoggerManager().get_new_logger("process timeout")
    
    def killtree(self):    
        parent = psutil.Process(self.process.pid)
        for child in parent.get_children(recursive=True):
            self.logger.debug("killing process %d", child.pid)
            child.kill()
        self.logger.debug("killing process %d", parent.pid)
        parent.kill()
    
    def kill_proc(self):
        self.logger.debug("timer expired, killing process...")
        self.killtree()
        self.logger.debug("process terminate invoked.")
        self.ran = True
    
    def start_timer(self):
        self.logger.debug("timer started")
        self.timer.start()
    
    def cancel_timer(self):
        self.logger.debug("timer cancelled")
        self.timer.cancel()
Esempio n. 48
0
 def testLoggerManagerMustReturnALoggerAppropiatelyConfiguredWhenANameIsNotProvided(
         self):
     logger_manager = LoggerManager()
     logger = logger_manager.getLogger()
     pass
Esempio n. 49
0
 def __init__(self, process, timeout):
     self.process = process
     self.timeout = timeout
     self.ran = False
     self.timer = Timer(self.timeout, self.kill_proc)
     self.logger = LoggerManager().get_new_logger("process timeout")