예제 #1
0
 def _start_watching_path(self):
     """Schedule handlers for self.path now that it exists."""
     for watch, handlers in self._stopped_handlers.items():
         for handler in handlers:
             self.schedule(event_handler=handler,
                           path=watch.path,
                           recursive=watch.is_recursive)
     # generate any events for files/dirs in self.path that were
     # created before the watch started (since dispatching is stopped,
     # duplicate events will be caught and ignored)
     with self._lock:
         for emitter in self.emitters:
             dirs = [emitter.watch.path]
             for root in dirs:
                 names = os.listdir(root)
                 paths = [os.path.join(root, name) for name in names]
                 paths.sort(key=os.path.getmtime)
                 for path in paths:
                     if os.path.isdir(path):
                         if emitter.watch.is_recursive:
                             dirs.append(path)
                         event = DirCreatedEvent(path)
                     else:
                         event = FileCreatedEvent(path)
                     emitter.queue_event(event)
     # start dispatching events
     self._start_dispatching()
예제 #2
0
파일: polling.py 프로젝트: d-ash/BigBenBox
    def queue_events(self, timeout):
        with self._lock:
            # We don't want to hit the disk continuously.
            # timeout behaves like an interval for polling emitters.
            time.sleep(timeout)

            # Get event diff between fresh snapshot and previous snapshot.
            # Update snapshot.
            new_snapshot = DirectorySnapshot(self.watch.path, self.watch.is_recursive)
            events = DirectorySnapshotDiff(self._snapshot, new_snapshot)
            self._snapshot = new_snapshot

            # Files.
            for src_path in events.files_deleted:
                self.queue_event(FileDeletedEvent(src_path))
            for src_path in events.files_modified:
                self.queue_event(FileModifiedEvent(src_path))
            for src_path in events.files_created:
                self.queue_event(FileCreatedEvent(src_path))
            for src_path, dest_path in events.files_moved:
                self.queue_event(FileMovedEvent(src_path, dest_path))

            # Directories.
            for src_path in events.dirs_deleted:
                self.queue_event(DirDeletedEvent(src_path))
            for src_path in events.dirs_modified:
                self.queue_event(DirModifiedEvent(src_path))
            for src_path in events.dirs_created:
                self.queue_event(DirCreatedEvent(src_path))
            for src_path, dest_path in events.dirs_moved:
                self.queue_event(DirMovedEvent(src_path, dest_path))
예제 #3
0
    def queue_events(self, timeout):
        if self.stopped_event.wait(timeout):
            return
        with self._lock:
            if not self.should_keep_running():
                return
            new_snapshot = self._take_snapshot()
            events = DirectorySnapshotDiff(self._snapshot, new_snapshot)
            self._snapshot = new_snapshot
            for src_path in events.files_deleted:
                self.queue_event(FileDeletedEvent(src_path))

            for src_path in events.files_modified:
                self.queue_event(FileModifiedEvent(src_path))

            for src_path in events.files_created:
                self.queue_event(FileCreatedEvent(src_path))

            for src_path, dest_path in events.files_moved:
                self.queue_event(FileMovedEvent(src_path, dest_path))

            for src_path in events.dirs_deleted:
                self.queue_event(DirDeletedEvent(src_path))

            for src_path in events.dirs_modified:
                self.queue_event(DirModifiedEvent(src_path))

            for src_path in events.dirs_created:
                self.queue_event(DirCreatedEvent(src_path))

            for src_path, dest_path in events.dirs_moved:
                self.queue_event(DirMovedEvent(src_path, dest_path))
예제 #4
0
    def _watch_dir(self, path):
        contents = _get_contents(path)
        if contents is None:
            return

        self._watchlist.add(path)
        for exists in self._loop(path):
            current_contents = _get_contents(path)
            if current_contents is None:
                break

            added_contents = current_contents - contents
            for filename in added_contents:
                filepath = os.path.join(path, filename)
                self._add(filepath)

                if os.path.isdir(filepath):
                    self.queue_event(DirCreatedEvent(filepath))
                else:
                    self.queue_event(FileCreatedEvent(filepath))

            contents = current_contents
            if os.path.exists(path):
                self.queue_event(DirModifiedEvent(path))

        for filename in contents:
            filepath = os.path.join(path, filename)
            self._watchlist.discard(filepath)
        self._watchlist.discard(path)
        self.queue_event(DirDeletedEvent(path))
예제 #5
0
    def queue_events(self, timeout):

        if self.stopped_event.wait(timeout):
            return

        with self._lock:
            if not self.should_keep_running():
                return

            # Get event diff between fresh snapshot and previous snapshot.
            # Update snapshot.
            new_snapshot = self._take_snapshot()
            events = DirectorySnapshotDiff(self._snapshot, new_snapshot)
            self._snapshot = new_snapshot

            # Files.
            for src_path in events.files_deleted:
                self.queue_event(FileDeletedEvent(src_path))
            for src_path in events.files_modified:
                self.queue_event(FileModifiedEvent(src_path))
            for src_path in events.files_created:
                self.queue_event(FileCreatedEvent(src_path))
            for src_path, dest_path in events.files_moved:
                self.queue_event(FileMovedEvent(src_path, dest_path))

            # Directories.
            for src_path in events.dirs_deleted:
                self.queue_event(DirDeletedEvent(src_path))
            for src_path in events.dirs_modified:
                self.queue_event(DirModifiedEvent(src_path))
            for src_path in events.dirs_created:
                self.queue_event(DirCreatedEvent(src_path))
            for src_path, dest_path in events.dirs_moved:
                self.queue_event(DirMovedEvent(src_path, dest_path))
예제 #6
0
 def test_on_created_folder(self, _remote_create_folder, _os):
     _remote_create_folder.return_value = True
     _os.path.abspath.return_value = "/a/test1.py"
     _os.path.join.return_value = "/b/a/test1.py"
     _event = DirCreatedEvent(src_path="/a/test1.py")
     self.handler.on_created(_event)
     _remote_create_folder.assert_called_once_with(
         dst_ssh=self.handler.dst_ssh, dst_path="/b/a/test1.py")
예제 #7
0
    def _gen_renamed_events(self,
                            src_path,
                            is_directory,
                            ref_snapshot,
                            new_snapshot):
        """
        Compares information from two directory snapshots (one taken before
        the rename operation and another taken right after) to determine the
        destination path of the file system object renamed, and yields
        the appropriate events to be queued.
        """
        try:
            f_inode = ref_snapshot.inode(src_path)
        except KeyError:
            # Probably caught a temporary file/directory that was renamed
            # and deleted. Fires a sequence of created and deleted events
            # for the path.
            if is_directory:
                yield DirCreatedEvent(src_path)
                yield DirDeletedEvent(src_path)
            else:
                yield FileCreatedEvent(src_path)
                yield FileDeletedEvent(src_path)
                # We don't process any further and bail out assuming
            # the event represents deletion/creation instead of movement.
            return

        dest_path = new_snapshot.path(f_inode)
        if dest_path is not None:
            dest_path = absolute_path(dest_path)
            if is_directory:
                event = DirMovedEvent(src_path, dest_path)
                yield event
            else:
                yield FileMovedEvent(src_path, dest_path)
            yield self._parent_dir_modified(src_path)
            yield self._parent_dir_modified(dest_path)
            if is_directory:
                # TODO: Do we need to fire moved events for the items
                # inside the directory tree? Does kqueue does this
                # all by itself? Check this and then enable this code
                # only if it doesn't already.
                # A: It doesn't. So I've enabled this block.
                if self.watch.is_recursive:
                    for sub_event in generate_sub_moved_events(src_path, dest_path):
                        yield sub_event
        else:
            # If the new snapshot does not have an inode for the
            # old path, we haven't found the new name. Therefore,
            # we mark it as deleted and remove unregister the path.
            if is_directory:
                yield DirDeletedEvent(src_path)
            else:
                yield FileDeletedEvent(src_path)
            yield self._parent_dir_modified(src_path)
예제 #8
0
def test_ignore_tree_creation(sync):

    new_dir = Path(sync.dropbox_path) / "parent"

    with sync.fs_events.ignore(DirCreatedEvent(str(new_dir))):
        new_dir.mkdir()
        for i in range(10):
            file = new_dir / f"test_{i}"
            file.touch()

    sync.wait_for_local_changes()
    sync_events, _ = sync.list_local_changes()
    assert len(sync_events) == 0
예제 #9
0
def test_catching_non_ignored_events(sync):

    new_dir = Path(sync.dropbox_path) / "parent"

    with sync.fs_events.ignore(DirCreatedEvent(str(new_dir)), recursive=False):
        new_dir.mkdir()
        for i in range(10):
            # may trigger FileCreatedEvent and FileModifiedVent
            file = new_dir / f"test_{i}"
            file.touch()

    sync.wait_for_local_changes()
    sync_events, _ = sync.list_local_changes()
    assert all(not si.is_directory for si in sync_events)
예제 #10
0
 def test___repr__(self):
   event = DirCreatedEvent(path_1)
   self.assertEqual("<DirCreatedEvent: src_path=%s>" % path_1,
                    event.__repr__())