Anonymous avatar Anonymous committed 397cd7a Draft

Ensure that moved directories are correctly tracked

Tentative fix for issue #1

Comments (0)

Files changed (1)

inotify/watcher.py

 
         return list(self._add_iter(path, mask, onerror))
 
+    def remove_all(self, path):
+        '''Remove watches over path and its subdirectories.'''
+
+        path = os.path.normpath(path)
+        prefix = path + '/'
+        watched_paths = self._paths.keys()
+        for wpath in watched_paths:
+            if wpath.startswith(prefix) or wpath == path:
+                self.remove_path(wpath)
+        del watched_paths[:]
+
 
 class AutoWatcher(Watcher):
     '''Watcher class that automatically watches newly created directories.'''
 
     def read(self, bufsize=None):
         events = super(AutoWatcher, self).read(bufsize)
+
+        addmask = inotify.IN_CREATE | inotify.IN_MOVED_TO
+
         for evt in events:
-            if evt.mask & inotify.IN_ISDIR and evt.mask & inotify.IN_CREATE:
-                if self.addfilter is None or self.addfilter(evt):
+            if evt.mask & inotify.IN_ISDIR:
+                if self.addfilter and not self.addfilter(evt):
+                    continue
+                if evt.mask & addmask:
                     # See note about race avoidance via IN_ONLYDIR above.
                     mask = evt.watch.mask | inotify.IN_ONLYDIR
                     try:
                     except EnvironmentError as err:
                         if err.errno not in self.ignored_errors:
                             raise
+                elif evt.mask & inotify.IN_MOVED_FROM:
+                    # Cleanup old watch entries.
+                    self.remove_all(evt.fullpath)
+
         return events
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.