def run(self): """Run the watcher""" # TODO: wait on condition/event when dir list is empty # instead of looping per frequency interval. while self._continue: deleted = list() added = list() modified = list() # Suspend processing if requested if self._suspend: with self._suspendcond: self._suspendcond.wait() with self._lock: for dobj in self._dirs: if not self._continue: return # Check if a watched directory has been deleted if not os.path.exists(dobj.Path): deleted.append(dobj) self._dirs.remove(dobj) continue snapshot = fileutil.GetDirectoryObject(dobj.Path, False, True) # Check for deletions for tobj in dobj.Files: if not self._continue: return if tobj not in snapshot.Files: deleted.append(tobj) dobj.Files.remove(tobj) # Check for additions and modifications for tobj in snapshot.Files: if not self._continue: return if tobj not in dobj.Files: # new object was added added.append(tobj) dobj.Files.append(tobj) else: idx = dobj.Files.index(tobj) existing = dobj.Files[idx] # object was modified if existing.ModTime < tobj.ModTime: modified.append(tobj) existing.ModTime = tobj.ModTime # Call Notifier if anything changed if any((added, deleted, modified)): self._notifier(added, deleted, modified) # Wait till next check time.sleep(self._freq / 1000.0)
def AddWatchDirectory(self, dpath): """Add a directory to the watch list @param dpath: directory path (unicode) """ assert os.path.isdir(dpath) dobj = fileutil.Directory(dpath) with self._lock: if dobj not in self._dirs: # Get current snapshot of the directory dobj = fileutil.GetDirectoryObject(dpath, False, True) self._dirs.append(dobj)
def AddWatchDirectory(self, dpath): """Add a directory to the watch list @param dpath: directory path (unicode) @return: bool - True means watch was added, False means unable to list directory """ assert os.path.isdir(dpath) dobj = fileutil.Directory(dpath) self._changePending = True with self._lock: if dobj not in self._dirs and os.access(dobj.Path, os.R_OK): # Get current snapshot of the directory try: dobj = fileutil.GetDirectoryObject(dpath, False, True) except OSError: self._changePending = False return False self._dirs.append(dobj) with self._listEmptyCond: self._listEmptyCond.notify() self._changePending = False return True
def run(self): """Run the watcher""" while self._continue: deleted = list() added = list() modified = list() # Watch is empty so wait on things to monitor before continuing if not self._dirs: with self._listEmptyCond: self._listEmptyCond.wait() # Suspend processing if requested if self._suspend: with self._suspendcond: self._suspendcond.wait() with self._lock: for dobj in self._PendingRefresh: if not self._continue: return elif self._changePending: break # Check if a watched directory has been deleted if not os.path.exists(dobj.Path): deleted.append(dobj) self._dirs.remove(dobj) continue snapshot = fileutil.GetDirectoryObject( dobj.Path, False, True) # Check for deletions dobjFiles = dobj.Files # optimization dobjIndex = dobjFiles.index # optimization snapFiles = snapshot.Files # optimization for tobj in dobjFiles: if not self._continue: return elif self._changePending: break if tobj not in snapFiles: deleted.append(tobj) dobjFiles.remove(tobj) # Check for additions and modifications for tobj in snapFiles: if not self._continue: return elif self._changePending: break if tobj not in dobjFiles: # new object was added added.append(tobj) dobjFiles.append(tobj) else: idx = dobjIndex(tobj) existing = dobjFiles[idx] # object was modified if existing.ModTime < tobj.ModTime: modified.append(tobj) existing.ModTime = tobj.ModTime # Call Notifier if anything changed if any((added, deleted, modified)): self._notifier(added, deleted, modified) # Wait till next check if self._freq > 0: # Automatic updates time.sleep(self._freq / 1000.0) else: # Manually controlled updates with self._refreshCond: self._refreshDirs = None self._refreshCond.wait()