Exemple #1
0
class InotifyBuffer(BaseThread):
    """A wrapper for `Inotify` that holds events for `delay` seconds. During
    this time, IN_MOVED_FROM and IN_MOVED_TO events are paired.
    """

    delay = 0.5

    def __init__(self, path, recursive=False, delay=delay):
        BaseThread.__init__(self)
        self._queue = DelayedQueue(self.delay)
        self._inotify = None
        self._path = path
        self._recursive = recursive

    def on_thread_start(self):
        """
        Start reading inotify events.
        """
        self._inotify = Inotify(self._path, self._recursive)

    def read_event(self):
        """Returns a single event or a tuple of from/to events in case of a
        paired move event. If this buffer has been closed, immediately return
        None.
        """
        return self._queue.get()

    def on_thread_stop(self):
        if self._inotify:
            self._inotify.close()
        self._queue.close()

    def close(self):
        self.stop()
        try:
            self.join()
        except RuntimeError:
            pass

    def run(self):
        """Read event from `inotify` and add them to `queue`. When reading a
        IN_MOVE_TO event, remove the previous added matching IN_MOVE_FROM event
        and add them back to the queue as a tuple.
        """
        while self.should_keep_running():
            inotify_events = self._inotify.read_events()
            for inotify_event in inotify_events:
                if inotify_event.is_moved_to:

                    def matching_from_event(event):
                        return (not isinstance(event, tuple) and event.is_moved_from
                                and event.cookie == inotify_event.cookie)

                    from_event = self._queue.remove(matching_from_event)
                    if from_event is not None:
                        self._queue.put((from_event, inotify_event))
                    else:
                        self._queue.put(inotify_event)
                else:
                    self._queue.put(inotify_event)
Exemple #2
0
class InotifyBuffer(BaseThread):
    """A wrapper for `Inotify` that holds events for `delay` seconds. During
    this time, IN_MOVED_FROM and IN_MOVED_TO events are paired.
    """

    delay = 0.5

    def __init__(self, path, recursive=False):
        BaseThread.__init__(self)
        self._queue = DelayedQueue(self.delay)
        self._inotify = Inotify(path, recursive)
        self.start()

    def read_event(self):
        """Returns a single event or a tuple of from/to events in case of a
        paired move event. If this buffer has been closed, immediately return
        None.
        """
        return self._queue.get()

    def on_thread_stop(self):
        self._inotify.close()
        self._queue.close()

    def close(self):
        self.stop()
        self.join()

    def run(self):
        """Read event from `inotify` and add them to `queue`. When reading a
        IN_MOVE_TO event, remove the previous added matching IN_MOVE_FROM event
        and add them back to the queue as a tuple.
        """
        deleted_self = False
        while self.should_keep_running() and not deleted_self:
            inotify_events = self._inotify.read_events()
            for inotify_event in inotify_events:
                logger.debug("in-event %s", inotify_event)
                if inotify_event.is_moved_to:

                    def matching_from_event(event):
                        return (not isinstance(event, tuple)
                                and event.is_moved_from
                                and event.cookie == inotify_event.cookie)

                    from_event = self._queue.remove(matching_from_event)
                    if from_event is not None:
                        self._queue.put((from_event, inotify_event))
                    else:
                        logger.debug("could not find matching move_from event")
                        self._queue.put(inotify_event)
                else:
                    self._queue.put(inotify_event)

                if inotify_event.is_delete_self and \
                        inotify_event.src_path == self._inotify.path:
                    # Deleted the watched directory, stop watching for events
                    deleted_self = True
Exemple #3
0
class InotifyBuffer(BaseThread):
    """A wrapper for `Inotify` that holds events for `delay` seconds. During
    this time, IN_MOVED_FROM and IN_MOVED_TO events are paired.
    """

    delay = 0.5

    def __init__(self, path, recursive=False):
        BaseThread.__init__(self)
        self._queue = DelayedQueue(self.delay)
        self._inotify = Inotify(path, recursive)
        self.start()

    def read_event(self):
        """Returns a single event or a tuple of from/to events in case of a
        paired move event. If this buffer has been closed, immediately return
        None.
        """
        return self._queue.get()

    def on_thread_stop(self):
        self._inotify.close()
        self._queue.close()

    def close(self):
        self.stop()
        self.join()

    def run(self):
        """Read event from `inotify` and add them to `queue`. When reading a
        IN_MOVE_TO event, remove the previous added matching IN_MOVE_FROM event
        and add them back to the queue as a tuple.
        """
        deleted_self = False
        while self.should_keep_running() and not deleted_self:
            inotify_events = self._inotify.read_events()
            for inotify_event in inotify_events:
                logger.debug("in-event %s", inotify_event)
                if inotify_event.is_moved_to:

                    def matching_from_event(event):
                        return (not isinstance(event, tuple) and event.is_moved_from
                                and event.cookie == inotify_event.cookie)

                    from_event = self._queue.remove(matching_from_event)
                    if from_event is not None:
                        self._queue.put((from_event, inotify_event))
                    else:
                        logger.debug("could not find matching move_from event")
                        self._queue.put(inotify_event)
                else:
                    self._queue.put(inotify_event)

                if inotify_event.is_delete_self and \
                        inotify_event.src_path == self._inotify.path:
                    # Deleted the watched directory, stop watching for events
                    deleted_self = True
class InotifyBuffer(BaseThread):
    """A wrapper for `Inotify` that holds events for `delay` seconds. During
    this time, IN_MOVED_FROM and IN_MOVED_TO events are paired.
    """

    delay = 0.5

    def __init__(self, path, recursive=False):
        BaseThread.__init__(self)
        self._queue = DelayedQueue(self.delay)
        self._inotify = Inotify(path, recursive)
        self.start()

    def read_event(self):
        
        return self._queue.get()

    def on_thread_stop(self):
        self._inotify.close()
        self._queue.close()

    def close(self):
        self.stop()
        self.join()

    def run(self):
        
        while self.should_keep_running():
            inotify_events = self._inotify.read_events()
            for inotify_event in inotify_events:
                logger.debug("in-event %s", inotify_event)
                if inotify_event.is_moved_to:

                    def matching_from_event(event):
                        return (not isinstance(event, tuple) and event.is_moved_from
                                and event.cookie == inotify_event.cookie)

                    from_event = self._queue.remove(matching_from_event)
                    if from_event is not None:
                        self._queue.put((from_event, inotify_event))
                    else:
                        logger.debug("could not find matching move_from event")
                        self._queue.put(inotify_event)
                else:
                    self._queue.put(inotify_event)
Exemple #5
0
class InotifyBuffer(BaseThread):
    delay = 0.5

    def __init__(self, path, recursive=False):
        BaseThread.__init__(self)
        self._queue = DelayedQueue(self.delay)
        self._inotify = Inotify(path, recursive)
        self.start()

    def read_event(self):
        return self._queue.get()

    def on_thread_stop(self):
        self._inotify.close()
        self._queue.close()

    def close(self):
        self.stop()
        self.join()

    def run(self):
        while self.should_keep_running():
            inotify_events = self._inotify.read_events()
            for inotify_event in inotify_events:
                logger.debug('in-event %s', inotify_event)
                if inotify_event.is_moved_to:

                    def matching_from_event(event):
                        return not isinstance(
                            event, tuple
                        ) and event.is_moved_from and event.cookie == inotify_event.cookie

                    from_event = self._queue.remove(matching_from_event)
                    if from_event is not None:
                        self._queue.put((from_event, inotify_event))
                    else:
                        logger.debug('could not find matching move_from event')
                        self._queue.put(inotify_event)
                else:
                    self._queue.put(inotify_event)
class InotifyBuffer(BaseThread):
    delay = 0.5

    def __init__(self, path, recursive = False):
        BaseThread.__init__(self)
        self._queue = DelayedQueue(self.delay)
        self._inotify = Inotify(path, recursive)
        self.start()

    def read_event(self):
        return self._queue.get()

    def on_thread_stop(self):
        self._inotify.close()
        self._queue.close()

    def close(self):
        self.stop()
        self.join()

    def run(self):
        while self.should_keep_running():
            inotify_events = self._inotify.read_events()
            for inotify_event in inotify_events:
                logger.debug('in-event %s', inotify_event)
                if inotify_event.is_moved_to:

                    def matching_from_event(event):
                        return not isinstance(event, tuple) and event.is_moved_from and event.cookie == inotify_event.cookie

                    from_event = self._queue.remove(matching_from_event)
                    if from_event is not None:
                        self._queue.put((from_event, inotify_event))
                    else:
                        logger.debug('could not find matching move_from event')
                        self._queue.put(inotify_event)
                else:
                    self._queue.put(inotify_event)
 def __init__(self, path, recursive=False):
     BaseThread.__init__(self)
     self._queue = DelayedQueue(self.delay)
     self._inotify = Inotify(path, recursive)
     self.start()
Exemple #8
0
 def __init__(self, path, recursive=False):
     BaseThread.__init__(self)
     self._queue = DelayedQueue(self.delay)
     self._inotify = Inotify(path, recursive)
     self.start()
Exemple #9
0
def test_raise_error(error, pattern):
    with patch.object(ctypes, "get_errno", new=lambda: error):
        with pytest.raises(OSError) as exc:
            Inotify._raise_error()
    assert exc.value.errno == error
    assert pattern in str(exc.value)
Exemple #10
0
 def __init__(self, path, recursive=False):
     super().__init__()
     self._queue = DelayedQueue(self.delay)
     self._inotify = Inotify(path, recursive)
     self.start()
Exemple #11
0
class InotifyBuffer(BaseThread):
    """A wrapper for `Inotify` that holds events for `delay` seconds. During
    this time, IN_MOVED_FROM and IN_MOVED_TO events are paired.
    """

    delay = 0.5

    def __init__(self, path, recursive=False):
        super().__init__()
        self._queue = DelayedQueue(self.delay)
        self._inotify = Inotify(path, recursive)
        self.start()

    def read_event(self):
        """Returns a single event or a tuple of from/to events in case of a
        paired move event. If this buffer has been closed, immediately return
        None.
        """
        return self._queue.get()

    def on_thread_stop(self):
        self._inotify.close()
        self._queue.close()

    def close(self):
        self.stop()
        self.join()

    def _group_events(self, event_list):
        """Group any matching move events"""
        grouped = []
        for inotify_event in event_list:
            logger.debug("in-event %s", inotify_event)

            def matching_from_event(event):
                return (not isinstance(event, tuple) and event.is_moved_from
                        and event.cookie == inotify_event.cookie)

            if inotify_event.is_moved_to:
                # Check if move_from is already in the buffer
                for index, event in enumerate(grouped):
                    if matching_from_event(event):
                        grouped[index] = (event, inotify_event)
                        break
                else:
                    # Check if move_from is in delayqueue already
                    from_event = self._queue.remove(matching_from_event)
                    if from_event is not None:
                        grouped.append((from_event, inotify_event))
                    else:
                        logger.debug("could not find matching move_from event")
                        grouped.append(inotify_event)
            else:
                grouped.append(inotify_event)
        return grouped

    def run(self):
        """Read event from `inotify` and add them to `queue`. When reading a
        IN_MOVE_TO event, remove the previous added matching IN_MOVE_FROM event
        and add them back to the queue as a tuple.
        """
        deleted_self = False
        while self.should_keep_running() and not deleted_self:
            inotify_events = self._inotify.read_events()
            grouped_events = self._group_events(inotify_events)
            for inotify_event in grouped_events:
                if not isinstance(inotify_event,
                                  tuple) and inotify_event.is_ignored:
                    if inotify_event.src_path == self._inotify.path:
                        # Watch was removed explicitly (inotify_rm_watch(2)) or automatically (file
                        # was deleted, or filesystem was unmounted), stop watching for events
                        deleted_self = True
                    continue

                # Only add delay for unmatched move_from events
                delay = not isinstance(inotify_event,
                                       tuple) and inotify_event.is_moved_from
                self._queue.put(inotify_event, delay)

                if not isinstance(inotify_event, tuple) and inotify_event.is_delete_self and \
                        inotify_event.src_path == self._inotify.path:
                    # Deleted the watched directory, stop watching for events
                    deleted_self = True
Exemple #12
0
 def on_thread_start(self):
     """
     Start reading inotify events.
     """
     self._inotify = Inotify(self._path, self._recursive)