Commits

Christopher Nilsson committed c53117e

Base class records snapshots of monitored directories, to help in error recovery.

  • Participants
  • Parent commits d257d7b

Comments (0)

Files changed (1)

 import os.path
 import threading
 import logging
+import time
 
 LOG_NAME = "dirwatch"
 LOG_STANDARD_FMT = "%(asctime)s p=%(process)d t=%(thread)d [%(levelname)s] %(message)s"
         self.monitoring = False      
         self.init_logging() 
         self._worker_thread = None
+        self.snapshot = {}
         
     def init_logging(self, verbosity=logging.DEBUG):
         self.log = logging.getLogger(LOG_NAME)
                 with self._notify_lock:
                     self.events.notify(event_id, path=fullpath, timestamp=timestamp)
             except Exception, details:
-                self.log.error("_process_incoming_queue: Exception: %s", details)                                
+                self.log.error("_process_incoming_queue: Exception: %s", details)   
+            
+            self.monitor_events.task_done()                             
         
     def add_watch(self, path):
         """ Adds the given path to the watch list. 
         """
         if (path not in self.paths) and os.path.exists(path) and os.path.isdir(path):
             self.paths.append(path)
+            
+            self.log.debug("add_watch: getting initial snapshot of %s", path)
+            self.snapshot[path] = self.get_dir_snapshot(path)            
+            self.log.debug("add_watch: done.")
+            
             with self._notify_lock:
                 self.events.notify(eventtype.EV_WATCH_ADDED, path=path)
             return True
             self.monitoring = False
             self._worker_thread.join()   
             with self._notify_lock:
-                self.events.notify(eventtype.EV_MONITOR_STOPPED)         
+                self.events.notify(eventtype.EV_MONITOR_STOPPED) 
+                
+    def get_dir_snapshot(self, directory, include_subdirs=False):
+        """ Builds a set of filepaths in the given directory.
+        
+        Intension is to be able to find before/after differences, when
+        there are too many events to process.
+        """
+        def _iter_paths():
+            for path, subdirs, filenames in os.walk(directory):
+                if (not include_subdirs) and (path != directory):
+                    break
+                for filename in filenames:
+                    yield os.path.join(path, filename)
+        return set(_iter_paths())
+    
+    def generate_dir_snapshot_diff_events(self, before, after):
+        """ Given two dir snapshot sets, send the add/delete events for the file lists. """
+        additions = after - before
+        for filepath in additions:
+            with self._notify_lock:
+                self.events.notify(eventtype.EV_FILE_CREATED, path=filepath, timestamp=time.time())
+        additions = None 
+        
+        removals = before - after
+        for filepath in removals:
+            with self._notify_lock:
+                self.events.notify(eventtype.EV_FILE_DELETED, path=filepath, timestamp=time.time())
+        removals = None
+        
+        
+        
+