def test_gutils_binary_to_ascii_watch(self): wm = WatchManager() mask = IN_MOVED_TO | IN_CLOSE_WRITE # Convert binary data to ASCII processor = Slocum2AsciiProcessor( deployments_path=resource('slocum'), ) notifier = ThreadedNotifier(wm, processor) notifier.coalesce_events() notifier.start() wdd = wm.add_watch( binary_path, mask, rec=True, auto_add=True ) # Wait 5 seconds for the watch to start time.sleep(5) gpath = os.path.join(original_binary, '*.*bd') # Sort the files so the .cac files are generated in the right order for g in sorted(glob(gpath)): shutil.copy2(g, binary_path) wait_for_files(ascii_path, 32) wm.rm_watch(wdd.values(), rec=True) notifier.stop()
class Notify(): def __init__(self): self.wm = WatchManager() self.pe = ProcessNotifyEvents() self.notifier = ThreadedNotifier(self.wm, self.pe) self.notifier.start() self.path = None #thread.start_new_thread(self.jobTask, (self,)) def setNotify(self, path, cbfun): #print 'setnotify ' + path if self.path: self.wm.rm_watch(list(self.wdd.values())) self.path = path self.pe.cbfun = cbfun # ugly... #print sys.getfilesystemencoding() self.wdd = self.wm.add_watch(self.path, pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MOVED_TO | pyinotify.IN_MOVED_FROM | pyinotify.IN_MODIFY) def stop(self): if self.path: self.wm.rm_watch(list(self.wdd.values())) self.notifier.stop() def notifyThread(self): while 1: notifier.process_events() if notifier.check_events(): notifier.read_events()
def test_gutils_ascii_to_netcdf_watch(self): wm = WatchManager() mask = IN_MOVED_TO | IN_CLOSE_WRITE # Convert ASCII data to NetCDF processor = Slocum2NetcdfProcessor(deployments_path=resource('slocum'), subset=False, template='trajectory', profile_id_type=2, tsint=10, filter_distance=1, filter_points=5, filter_time=10, filter_z=1) notifier = ThreadedNotifier(wm, processor) notifier.coalesce_events() notifier.start() wdd = wm.add_watch(ascii_path, mask, rec=True, auto_add=True) # Wait 5 seconds for the watch to start time.sleep(5) # Make the ASCII we are watching for merger = SlocumMerger(original_binary, ascii_path, globs=['*.tbd', '*.sbd']) merger.convert() wait_for_files(netcdf_path, 230) wm.rm_watch(wdd.values(), rec=True) notifier.stop()
def test_gutils_netcdf_to_erddap_watch(self): wm = WatchManager() mask = IN_MOVED_TO | IN_CLOSE_WRITE # Convert ASCII data to NetCDF processor = Netcdf2ErddapProcessor( deployments_path=resource('slocum'), erddap_content_path=erddap_content_path, erddap_flag_path=erddap_flag_path ) notifier = ThreadedNotifier(wm, processor, read_freq=5) notifier.coalesce_events() notifier.start() wdd = wm.add_watch( netcdf_path, mask, rec=True, auto_add=True ) # Wait 5 seconds for the watch to start time.sleep(5) orig_netcdf = resource('profile.nc') dummy_netcdf = os.path.join(netcdf_path, 'profile.nc') shutil.copy(orig_netcdf, dummy_netcdf) wait_for_files(erddap_content_path, 1) wait_for_files(erddap_flag_path, 1) wm.rm_watch(wdd.values(), rec=True) notifier.stop()
class INotifyDriver(Component): channel = "inotify" def __init__(self, freq=1, timeout=1, channel=channel): super(INotifyDriver, self).__init__(channel=channel) self._freq = freq self._wm = WatchManager() self._notifier = Notifier(self._wm, self._process, timeout=timeout) def _sleep(self, rtime): # Only consider sleeping if _freq is > 0 if self._freq > 0: ctime = time.time() s = self._freq - (ctime - rtime) if s > 0: time.sleep(s) def __tick__(self): self._notifier.process_events() rtime = time.time() if self._notifier.check_events(): self._sleep(rtime) self._notifier.read_events() def _process(self, event): dir = event.dir mask = event.mask path = event.path name = event.name pathname = event.pathname #print dir, mask, path, name, pathname for k, v in EVENT_MAP.iteritems(): if mask & k: e = v(name, path, pathname, dir) c = e.name.lower() self.push(e, c) def add(self, path, mask=None, recursive=True): mask = mask or MASK self._wm.add_watch(path, mask, rec=recursive) def remove(self, path, recursive=False): wd = self._wm.get_wd(path) if wd: self._wm.rm_watch(wd, rec=recursive)
class INotifyDriver(Component): channel = "inotify" def __init__(self, freq=1, timeout=1, channel=channel): super(INotifyDriver, self).__init__(channel=channel) self._freq = freq self._wm = WatchManager() self._notifier = Notifier(self._wm, self._process, timeout=timeout) def _sleep(self, rtime): # Only consider sleeping if _freq is > 0 if self._freq > 0: ctime = time.time() s = self._freq - (ctime - rtime) if s > 0: time.sleep(s) def __tick__(self): self._notifier.process_events() rtime = time.time() if self._notifier.check_events(): self._sleep(rtime) self._notifier.read_events() def _process(self, event): dir = event.dir mask = event.mask path = event.path name = event.name pathname = event.pathname for k, v in EVENT_MAP.iteritems(): if mask & k: e = v(name, path, pathname, dir) c = e.name.lower() self.push(e, c) def add(self, path, mask=None, recursive=False): mask = mask or MASK self._wm.add_watch(path, mask, rec=recursive) def remove(self, path, recursive=False): wd = self._wm.get_wd(path) if wd: self._wm.rm_watch(wd, rec=recursive)
class InotifyObserver(Thread): def __init__(self, interval=1,): Thread.__init__(self) self.wm = WatchManager() self.stopped = ThreadedEvent() self.notifiers = set() self.name_to_rule = dict() self.setDaemon(True) def stop(self): self.stopped.set() for notifier in self.notifiers: notifier.stop() @synchronized() def schedule(self, name, event_handler, *paths): """Schedules monitoring.""" #from pyinotify import PrintAllEvents #dispatcher = PrintAllEvents() dispatcher = _ProcessEventDispatcher(event_handler=event_handler) notifier = ThreadedNotifier(self.wm, dispatcher) self.notifiers.add(notifier) for path in paths: if not isinstance(path, str): raise TypeError( "Path must be string, not '%s'." % type(path).__name__) descriptors = self.wm.add_watch(path, ALL_EVENTS, rec=True, auto_add=True) self.name_to_rule[name] = _Rule(name, notifier, descriptors) notifier.start() @synchronized() def unschedule(self, *names): for name in names: try: rule = self.name_to_rule[name] self.wm.rm_watch(rule.descriptors.values()) except KeyError: raise def run(self): import time while not self.stopped.is_set(): time.sleep(1)
class _PathWatcher(Queue): # iNotify watcher for directory ''' iNotify watcher object for monitor of changes in directory. ''' FLAGS = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVED_FROM | IN_MOVED_TO | IN_ATTRIB def __init__(self, path, exclude=None): class _EH(ProcessEvent): def process_default(self, event): _handleEvent(event) Queue.__init__(self) self._path = path self.exclude = exclude or [] _handleEvent = self.put self._wm = WatchManager() self._iNotifier = ThreadedNotifier(self._wm, _EH(), timeout=10) self._iNotifier.start() self.started = False self._watch = [] def start(self, exclude=None): if not self.started: # Add watch and start watching # Update exclude filter if it provided in call of start method self.exclude = exclude or self.exclude self._watch = self._wm.add_watch(self._path, self.FLAGS, exclude_filter=ExcludeFilter( self.exclude), auto_add=True, rec=True, do_glob=False) self.started = True def stop(self): if self.started: # Remove watch and stop watching self._wm.rm_watch(self._watch[self._path], rec=True) self.started = False def exit(self): self.stop() self._iNotifier.stop()
class FileEvent: def __init__(self, eventHandler): self.logger = logging.getLogger('FileEvent') self.wm = WatchManager() self.watches = dict() # Set the flags of the events that are to be listened to FLAGS = EventsCodes.ALL_FLAGS self.mask = FLAGS['IN_CREATE'] | FLAGS['IN_DELETE'] | FLAGS['IN_MODIFY'] | FLAGS['IN_DELETE_SELF'] # Set-up notifier self.notifier = ThreadedNotifier(self.wm, EventProcessor(eventHandler)) def startNotifyLoop(self): self.notifier.start() def stopNotifyLoop(self): self.notifier.stop() def addWatches(self, paths, mask=None): added_watches = dict() for path in paths: added_watches.update(self.addWatch(path, mask)) return added_watches # Also monitors all sub-directories of the given directory and automatically adds newly # created directories to watch. # TODO should be able to add files as well, but doesn't work atm def addWatch(self, path, mask=None): if mask is None: mask = self.mask added_watches = self.wm.add_watch(path, mask, rec=True, auto_add=True) self.watches.update(added_watches) return added_watches def removeWatch(self, path): watch_descriptor = self.wm.get_wd(path) if watch_descriptor is not None: result = self.wm.rm_watch(watch_descriptor, rec=True) # Remove the no longer active watches from the current watches dictionary for key, value in self.watches.items(): if value in result: del self.watches[key] else: result = None return result def getWatches(self): return self.watches
class LinuxFileSysMonitor(UFSMonitor): """File system monitor thread""" def __init__(self, name=None): super(LinuxFileSysMonitor, self).__init__(name) self.defaultMask = IN_DELETE | IN_CREATE | IN_MODIFY | IN_MOVED_TO | IN_MOVED_FROM self.wm = WatchManager() self.__lock = threading.Lock() def addWatch(self, path, mask=0): """Add watch for path""" if mask == 0: mask = self.defaultMask super(LinuxFileSysMonitor, self).addWatch(path, mask) self.wm.add_watch(path, mask, auto_add=True, rec=True) def rmWatch(self, path): """Remove watch for path""" super(LinuxFileSysMonitor, self).rmWatch(path) wd = self.wm.get_wd(path) self.wm.rm_watch(wd, rec=True) def run(self): """Thread entry""" super(LinuxFileSysMonitor, self).run() self.notifier = Notifier(self.wm, EventHandler(None, fsMonitor = self)) while self.isRunning: self.processMsg(1) if self.notifier.check_events(1000): self.notifier.read_events() self.notifier.process_events() def stop(self): """Stop watch""" super(LinuxFileSysMonitor, self).stop() self.notifier.stop()
class LinuxFileSysMonitor(UFSMonitor): """File system monitor thread""" def __init__(self, name=None): super(LinuxFileSysMonitor, self).__init__(name) self.defaultMask = IN_DELETE | IN_CREATE | IN_MODIFY | IN_MOVED_TO | IN_MOVED_FROM self.wm = WatchManager() self.__lock = threading.Lock() def addWatch(self, path, mask=0): """Add watch for path""" if mask == 0: mask = self.defaultMask super(LinuxFileSysMonitor, self).addWatch(path, mask) self.wm.add_watch(path, mask, auto_add=True, rec=True) def rmWatch(self, path): """Remove watch for path""" super(LinuxFileSysMonitor, self).rmWatch(path) wd = self.wm.get_wd(path) self.wm.rm_watch(wd, rec=True) def run(self): """Thread entry""" super(LinuxFileSysMonitor, self).run() self.notifier = Notifier(self.wm, EventHandler(None, fsMonitor=self)) while self.isRunning: self.processMsg(1) if self.notifier.check_events(1000): self.notifier.read_events() self.notifier.process_events() def stop(self): """Stop watch""" super(LinuxFileSysMonitor, self).stop() self.notifier.stop()
class Notify(BaseComponent): channel = "notify" def __init__(self, channel=channel): super(Notify, self).__init__(channel=channel) self._poller = None self._wm = WatchManager() self._notifier = Notifier(self._wm, self._on_process_events) def _on_process_events(self, event): dir = event.dir mask = event.mask path = event.path name = event.name pathname = event.pathname for k, v in EVENT_MAP.items(): if mask & k: self.fire(v(name, path, pathname, dir)) def add_path(self, path, mask=None, recursive=False): mask = mask or MASK self._wm.add_watch(path, mask, rec=recursive) def remove_path(self, path, recursive=False): wd = self._wm.get_wd(path) if wd: self._wm.rm_watch(wd, rec=recursive) @handler("ready") def _on_ready(self, component): self._poller.addReader(self, self._notifier._fd) @handler("registered", channel="*") def _on_registered(self, component, manager): if self._poller is None: if isinstance(component, BasePoller): self._poller = component self.fire(ready(self)) else: if component is not self: return component = findcmp(self.root, BasePoller) if component is not None: self._poller = component self.fire(ready(self)) else: self._poller = Poller().register(self) self.fire(ready(self)) @handler("started", channel="*", priority=1) def _on_started(self, event, component): if self._poller is None: self._poller = Poller().register(self) self.fire(ready(self)) event.stop() @handler("_read", priority=1) def __on_read(self, fd): self._notifier.read_events() self._notifier.process_events()
class FileWatcher(ProcessEvent): """ FileWatcher -> Starts an INotify thread to watch a directory for file changes. """ def __init__(self): """ FileWatcher(directory) -> Watch the directory for changes. """ if not pyinotify: raise Exception("pyinotify is not loaded.") super(FileWatcher, self).__init__() self._file_callback_dict = {} self._watch_manager = WatchManager() self._events_mask = EventsCodes.ALL_FLAGS['IN_MODIFY'] self._notifier = ThreadedNotifier(self._watch_manager, self) self._notifier.setDaemon(True) @classmethod def check(cls): """ Returns true if pyinotify is loaded otherwise false. """ return pyinotify def is_running(self): """ Returns a boolean indecating the state of the notifier. """ return self._notifier.isAlive() def start(self): """ Start the notifier thread. """ self._notifier.start() def stop(self): """ Stop the notifier thread. """ self._notifier.stop() def add_directory(self, directory): """ add_directory(directory) -> Add a directory to watch. """ dir_watch = self._watch_manager.add_watch(directory, self._events_mask, rec=True) def remove_directory(self, directory): """ remove_directory(directory) -> Remove a directory from the watch. """ self._watch_manager.rm_watch(directory, rec=True) def has_file(self, filename): """ Returns a boolean indecating if the file is being watched. """ return filename in self._file_callback_dict def add_file(self, filename, callback, *user_data): """ add_file(filename, callback, *user_data) -> Add a file to watch with its callback and optional user_data. """ self._file_callback_dict[filename] = (callback, user_data) def remove_file(self, filename): """ remove_file(filename) -> Remove the file from the watch list. """ return self._file_callback_dict.pop(filename, None) def process_IN_MODIFY(self, event): """ Process modify events. """ filename = os.path.join(event.path, event.name) callback_data = self._file_callback_dict.get(filename, ()) if callback_data: callback = callback_data[0] glib.idle_add(callback, callback_data[1:])
class FilesystemMonitor(object): """ FileMonitor Class keeps track of all files down a tree starting at the root """ def __init__(self, searcher): self.searcher = searcher self._thread_pool = ThreadPool(THREAD_POOL_WORKS) # Add a watch to the root of the dir self.watch_manager = WatchManager() self.notifier = ThreadedNotifier(self.watch_manager, FileProcessEvent(self)) self.notifier.start() self._build_exclude_list() def _build_exclude_list(self): log.info("[FileMonitor] Set Regexs for Ignore List") self._exclude_regexs = [] # Complie Ignore list in to a list of regexs for ignore in self.searcher.configuration.get_value("EXCLUDE_LIST"): ignore = ignore.strip() ignore = ignore.replace(".", "\.") ignore = ignore.replace("*", ".*") ignore = "^"+ignore+"$" log.debug("[FileMonitor] Ignore Regex = %s" % ignore) self._exclude_regexs.append(re.compile(ignore)) def change_root(self, previous_root): self._thread_pool.clearTasks() wd = self.watch_manager.get_wd(previous_root) if wd: self.watch_manager.rm_watch(wd, rec=True) self.searcher.clear_database() self.add_directory(self.searcher.current_root) def add_directory(self, path): """ Starts a WalkDirectoryThread to add the directory """ basename = os.path.basename(path) if self.validate(basename): self.watch_manager.add_watch(path, EVENT_MASK) self._thread_pool.queueTask(self.walk_directory, path) def add_file(self, path, name): """ Add a single file to the databse """ if self.validate(name): self.searcher.add_file(path, name) def remove_file(self, path, name): self.searcher.remove_file(path, name) def remove_directory(self, path): self.searcher.remove_directory(path) def walk_directory(self, root): """ From a give root of a tree this method will walk through ever branch and return a generator. """ if os.path.isdir(root): names = os.listdir(root) for name in names: try: file_stat = os.lstat(os.path.join(root, name)) except os.error: continue if stat.S_ISDIR(file_stat.st_mode): self.add_directory(os.path.join(root, name)) else: if not stat.S_ISLNK(file_stat.st_mode): self.add_file(root, name) def finish(self): wd = self.watch_manager.get_wd(self.searcher.current_root) self.watch_manager.rm_watch(wd, rec=True) self.notifier.stop() self._thread_pool.joinAll(waitForTasks=False) def validate(self, name): # Check to make sure the file not in the ignore list for ignore_re in self._exclude_regexs: if ignore_re.match(name): log.debug("[WalkDirectoryThread] ##### Ignored %s #####", name) return False log.debug("[WalkDirectoryThread] # Passed %s", name) return True
def process_IN_IGNORED(self, event): if settings.debug: print event else: pass if __name__ == '__main__': mask = EventsCodes.IN_DELETE | EventsCodes.IN_CREATE | EventsCodes.IN_MODIFY | \ EventsCodes.IN_MOVED_FROM | EventsCodes.IN_MOVED_TO repo = subversion.Manager(settings.repo_url) wm = WatchManager() notifier = ThreadedNotifier(wm, EventProcessor()) notifier.start() wm.add_watch(settings.root, mask, rec=True, auto_add=True) wm.rm_watch(wm.get_wd(settings.root + ".svn"), rec=True) print 'start monitoring %s' % settings.root while True: try: time.sleep(0.01) except KeyboardInterrupt: # ...until c^c signal print 'stop monitoring...' notifier.stop() break except Exception, err: print err
class FilesystemMonitor(object): """ FileMonitor Class keeps track of all files down a tree starting at the root """ def __init__(self, searcher): self.searcher = searcher self._thread_pool = ThreadPool(THREAD_POOL_WORKS) # Add a watch to the root of the dir self.watch_manager = WatchManager() self.notifier = ThreadedNotifier(self.watch_manager, FileProcessEvent(self)) self.notifier.start() self._build_exclude_list() def _build_exclude_list(self): log.info("[FileMonitor] Set Regexs for Ignore List") self._exclude_regexs = [] # Complie Ignore list in to a list of regexs for ignore in self.searcher.configuration.exclude_list: ignore = ignore.strip() ignore = ignore.replace(".", "\.") ignore = ignore.replace("*", ".*") ignore = "^" + ignore + "$" log.debug("[FileMonitor] Ignore Regex = %s" % ignore) self._exclude_regexs.append(re.compile(ignore)) def change_root(self, previous_root): self._thread_pool.clearTasks() wd = self.watch_manager.get_wd(previous_root) if wd: self.watch_manager.rm_watch(wd, rec=True) self.searcher.clear_database() self.add_directory(self.searcher.current_root) def add_directory(self, path): """ Starts a WalkDirectoryThread to add the directory """ basename = os.path.basename(path) if self.validate(basename): self.watch_manager.add_watch(path, EVENT_MASK) self._thread_pool.queueTask(self.walk_directory, path) def add_file(self, path, name): """ Add a single file to the databse """ if self.validate(name): self.searcher.add_file(path, name) def remove_file(self, path, name): self.searcher.remove_file(path, name) def remove_directory(self, path): self.searcher.remove_directory(path) def walk_directory(self, root): """ From a give root of a tree this method will walk through ever branch and return a generator. """ if os.path.isdir(root): names = os.listdir(root) for name in names: try: file_stat = os.lstat(os.path.join(root, name)) except os.error: continue if stat.S_ISDIR(file_stat.st_mode): self.add_directory(os.path.join(root, name)) else: if not stat.S_ISLNK(file_stat.st_mode): self.add_file(root, name) def finish(self): wd = self.watch_manager.get_wd(self.searcher.current_root) self.watch_manager.rm_watch(wd, rec=True) self.notifier.stop() self._thread_pool.joinAll(waitForTasks=False) def validate(self, name): # Check to make sure the file not in the ignore list for ignore_re in self._exclude_regexs: if ignore_re.match(name): log.debug("[WalkDirectoryThread] ##### Ignored %s #####", name) return False log.debug("[WalkDirectoryThread] # Passed %s", name) return True
print "Create:%s" % os.path.join(event.path, event.name) def process_IN_DELETE(self, event): print "Remove:%s" % os.path.join(event.path, event.name) notifier = Notifier(wm, PTmp()) wdd = wm.add_watch("/tmp", mask, rec=True) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() #todo print '.', except KeyboardInterrupt: notifier.stop() break notifier = ThreadedNotitier(wm, PTmp()) notifier.start() wdd = wm.add_watch('/tmp', mask, rec=True) if wdd['/tmp']>0: wm.rm_watch(wdd['/tmp']) #wm.rm_watch(wdd['/tmp'], rec=True) wm.rm_watch(wdd.values()) notifier.stop()
class FileMonitor(threading.Thread): def __init__(self, listener): threading.Thread.__init__(self) self.__p = Processor(self, listener) self.manager = WatchManager() self.notifier = Notifier(self.manager, self.__p) self.event = threading.Event() self.setDaemon(True) self.watches = [] self.__isSuspended = False def suspend(self): self.__isSuspended = True def unsuspend(self): t = threading.Thread(target=self.__unsuspend) t.start() def __unsuspend(self): time.sleep(1.5) self.__isSuspended = False for watch in self.watches: if not os.path.exists(watch): _logger.debug("Removed stale watch on %s", watch) self.watches.remove(watch) def is_suspended(self): return self.__isSuspended def has_watch(self, path): return path in self.watches def add_watch(self, path): _logger.debug("Adding watch for %s", path) self.manager.add_watch(path, MASK, self.__p) self.watches.append(path) def remove_watch(self, path): _logger.debug("Removing watch for %s", path) wd = self.manager.get_wd(path) self.manager.rm_watch(wd, True) self.watches.remove(path) for i in range(len(self.watches)): try: if self.watches[i].startswith(path): self.watches.remove(self.watches[i]) except IndexError: break def run(self): while not self.event.isSet(): self.notifier.process_events() if self.notifier.check_events(1000): self.notifier.read_events() _logger.info("Shutting down file monitor") self.notifier.stop() def stop(self): self.event.set() self.join()
class EventWatcherClass(object): FLAGS = EventsCodes.ALL_FLAGS mask = ( FLAGS["IN_DELETE"] | FLAGS["IN_CREATE"] | FLAGS["IN_MOVED_FROM"] | FLAGS["IN_MODIFY"] | FLAGS["IN_MOVED_TO"] | FLAGS["IN_ATTRIB"] | FLAGS["IN_IGNORED"] | FLAGS["IN_MOVE_SELF"] ) def __init__(self, config, fs): self.__config = config self.__listeners = [] self.__fs = fs self.__watchManager = WatchManager() self.configFile = self.__config.configFile self.cp = CustomProcess(self, self.__listener, self.configFile) self.__daemonize = self.__config.daemon self.dirToBeWatched = [] self.wm = None def __getDirToBeWatched(self): self.dirToBeWatched = self.__config.watchDirs if self.dirToBeWatched == []: self.dirToBeWatched.append(os.path.expanduser("/tmp")) # watching /tmp directory by default # also watch the config file self.configFile = self.__config.configFile if self.configFile not in self.dirToBeWatched: self.dirToBeWatched.append(self.configFile) def __processWatchedDir(self, stopWatchDir=[]): self.__getDirToBeWatched() for dir in self.dirToBeWatched: if dir.rstrip("/") != self.configFile.rstrip("/"): if self.__fs.isDirectory(dir): # modifiedDate = os.stat(dir)[ST_MTIME] #when a file start being watched, use the current system time modifiedDate = time.time() detail = ("file://" + dir, int(modifiedDate), "start", True) self.__listener(eventDetail=detail, initialization=True) else: print "fail to add dir to watched, the dir %s might not exist" % dir for dir in stopWatchDir: if dir.rstrip("/") != self.configFile.rstrip("/"): if self.__fs.isDirectory(dir): # modifiedDate = os.stat(dir)[ST_MTIME] #when a file start being watched, use the current system time modifiedDate = time.time() detail = ("file://" + dir, int(modifiedDate), "stop", True) self.__listener(eventDetail=detail, initialization=True) else: print "fail to add dir to watched, the dir %s might not exist" % dir def __startWatcher(self): self.notifier = Notifier(self.__watchManager, default_proc_fun=self.cp) self.resetDirToBeWatched() self.notifier.loop( daemonize=self.__daemonize, pid_file="/tmp/pyinotify.pid", force_kill=True, stdout="/tmp/stdout.txt" ) def resetDirToBeWatched(self, configChanged=False): if configChanged: oldDirToBeWatched = self.dirToBeWatched oldDirToBeWatched.sort() # reload the config file self.__config.reload() self.__getDirToBeWatched() stopWatchDir = [] if oldDirToBeWatched != self.dirToBeWatched.sort(): stopWatchDir = [item for item in oldDirToBeWatched if not item in self.dirToBeWatched] self.__processWatchedDir(stopWatchDir) if self.wm and self.wm.values(): self.wm = self.__watchManager.rm_watch(self.wm.values()) self.wm = self.__watchManager.add_watch(self.dirToBeWatched, self.mask, rec=True, auto_add=True) # self.wm = self.__watchManager.add_watch(self.__dirToBeWatched, self.mask, rec=True, # auto_add=True, quiet=False, exclude_filter=self.excl) def addListener(self, listener): self.__listeners.append(listener) self.__processWatchedDir() self.__startWatcher() def removeListener(self, listener): if self.__listeners.count(listener) > 0: self.__listeners.remove(listener) return True return False def __listener(self, *args, **kwargs): for listener in self.__listeners: try: listener(*args, **kwargs) except Exception, e: print str(e) pass