Example #1
0
    def __init__(self, module_name: str) -> None:
        """Construct ExecutedActions object."""
        self.module = module_name
        self.new_actions = {}  # type: ignore

        file_data = utils.load_yaml(path=self.path)
        self.old_actions = file_data.setdefault(
            self.module,
            {},
        )
Example #2
0
 def test_killing_processes_when_no_previous_command_has_been_run(self):
     """The first ever invocation of the function should be handled."""
     pidfile = XDG().data('astrality.pid')
     pidfile.unlink()
     kill_old_astrality_processes()
     assert utils.load_yaml(
         path=pidfile,
     ) == psutil.Process().as_dict(
         attrs=['pid', 'create_time', 'username'],
     )
Example #3
0
    def write(self) -> None:
        """Persist all actions that have been checked in object lifetime."""
        if not self.new_actions:
            return

        file_data = utils.load_yaml(path=self.path)
        file_data.setdefault(self.module, {})

        for action_type, action_options in self.new_actions.items():
            file_data[self.module].setdefault(
                action_type,
                [],
            ).extend(action_options)

        utils.dump_yaml(
            path=self.path,
            data=file_data,
        )
Example #4
0
    def reset(self) -> None:
        """Delete all executed module actions."""
        file_data = utils.load_yaml(path=self.path)
        reset_actions = file_data.pop(self.module, None)

        logger = logging.getLogger(__name__)
        if not reset_actions:
            logger.error(
                'No saved executed on_setup actions for module '
                f'"{self.module}"!', )
        else:
            logger.info(
                f'Reset the following actions for module "{self.module}":\n' +
                utils.yaml_str({self.module: reset_actions}), )

        utils.dump_yaml(
            path=self.path,
            data=file_data,
        )
        self.old_actions = {}
Example #5
0
def kill_old_astrality_processes() -> None:
    """
    Kill any previous Astrality process instance.

    This process kills the last process which invoked this function.
    If the process is no longer running, it is owned by another user, or has
    a new create_time, it will *not* be killed.
    """
    # The current process
    new_process = psutil.Process()

    # Fetch info of possible previous process instance
    pidfile = XDG().data('astrality.pid')
    old_process_info = utils.load_yaml(path=pidfile)
    utils.dump_yaml(
        data=new_process.as_dict(attrs=['pid', 'create_time', 'username']),
        path=pidfile,
    )

    if not old_process_info or not psutil.pid_exists(old_process_info['pid']):
        return

    try:
        old_process = psutil.Process(pid=old_process_info['pid'])
    except BaseException:
        return

    if not old_process.as_dict(attrs=['pid', 'create_time', 'username'
                                      ], ) == old_process_info:
        return

    try:
        logger.info(
            'Killing duplicate Astrality process with pid: '
            f'{old_process.pid}.', )
        old_process.terminate()
        old_process.wait()
    except BaseException:
        logger.error(
            f'Could not kill old instance of astrality with pid: '
            f'{old_process.pid}. Continuing anyway...', )
Example #6
0
 def __init__(self) -> None:
     """Constuct CreatedFiles object."""
     self.creations = utils.load_yaml(path=self.path)