def main(): vm = WatchManager() vm.add_watch(monitor_dirs,ALL_EVENTS,rec = True) en = MyEvent() notifier = Notifier(vm,en) notifier.loop()
def run(self): self.pclose = PClose(self.path) PC = self.pclose # only watch these events mask = IN_CLOSE_WRITE | IN_CLOSE_NOWRITE # watch manager instance wm = WatchManager() notifier = Notifier(wm, PC) print 'monitoring of %s started' % self.path added_flag = False # read and process events while True: try: if not added_flag: # on first iteration, add a watch on path: # watch path for events handled by mask. wm.add_watch(self.path, mask) added_flag = True notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: # ...until c^c signal print 'stop monitoring...' # stop monitoring notifier.stop() break except Exception, err: # otherwise keep on watching print err
def enabled(self): if not self.running: wm = WatchManager() self.event_handler = LibraryEvent(app.library) # Choose event types to watch for # FIXME: watch for IN_CREATE or for some reason folder copies # are missed, --nickb FLAGS = ['IN_DELETE', 'IN_CLOSE_WRITE',# 'IN_MODIFY', 'IN_MOVED_FROM', 'IN_MOVED_TO', 'IN_CREATE'] mask = reduce(lambda x, s: x | EventsCodes.ALL_FLAGS[s], FLAGS, 0) if self.USE_THREADS: print_d("Using threaded notifier") self.notifier = ThreadedNotifier(wm, self.event_handler) # Daemonize to ensure thread dies on exit self.notifier.daemon = True self.notifier.start() else: self.notifier = Notifier(wm, self.event_handler, timeout=100) GLib.timeout_add(1000, self.unthreaded_callback) for path in get_scan_dirs(): print_d('Watching directory %s for %s' % (path, FLAGS)) # See https://github.com/seb-m/pyinotify/wiki/ # Frequently-Asked-Questions wm.add_watch(path, mask, rec=True, auto_add=True) self.running = True
def watch_files(paths, mask): """ Vigila los ficheros de path y encola en queue los eventos producidos. """ watcher = WatchManager() mask = (EventsCodes.ALL_FLAGS.get('IN_MODIFY', 0)) @asyncio.coroutine def send_event(event): """Encola un evento en la cola.""" yield from event_queue.put(event) notifier = ThreadedNotifier( watcher, lambda e: asyncio.get_event_loop().call_soon_threadsafe( asyncio. async, send_event(e))) for path in paths: watcher.add_watch(path, mask, rec=True) while True: notifier.process_events() event_present = yield from asyncio.get_event_loop().run_in_executor( None, notifier.check_events) if event_present: notifier.read_events()
def fsMonitor(path="/data"): wm = WatchManager() mask = IN_DELETE | IN_MODIFY | IN_CREATE notifier = Notifier(wm, EventHandler(), read_freq=10) notifier.coalesce_events() wm.add_watch(path, mask, rec=True, auto_add=True) notifier.loop()
class Monitor(object): ALL = ALL_EVENTS CREATE = IN_CREATE DELETE = IN_DELETE MODIFY = IN_MODIFY ISDIR = IN_ISDIR def __init__(self, config): self._config = config self._logger = Logger() self._watchManager = WatchManager() self._notifier = Notifier(self._watchManager) def watch_config(self): for d in self._config.get_directories(): self.watch(d) def watch(self, directory): self._logger.info("watching directory %s" % directory.get_path()) self._watchManager.add_watch(directory.get_path(), directory.get_eventmask(), proc_fun=directory) def run(self): self._logger.info("start monitor loop") self._notifier.loop()
def watch_files(paths, mask): """ Vigila los ficheros de path y encola en queue los eventos producidos. """ watcher = WatchManager() mask = (EventsCodes.ALL_FLAGS.get('IN_MODIFY', 0)) @asyncio.coroutine def send_event(event): """Encola un evento en la cola.""" yield from event_queue.put(event) notifier = ThreadedNotifier( watcher, lambda e: asyncio.get_event_loop().call_soon_threadsafe( asyncio.async, send_event(e))) for path in paths: watcher.add_watch(path, mask, rec=True) while True: notifier.process_events() event_present = yield from asyncio.get_event_loop().run_in_executor( None, notifier.check_events) if event_present: notifier.read_events()
def Monitor(path): class PClose(ProcessEvent): def process_default(self, event): print('default processing event: {}'.format(event)) def process_IN_CREATE(self, event): print('process_IN_CREATE: processing event: {}'.format(event)) def process_IN_DELETE(self, event): print('process_IN_DELETE: processing event: {}'.format(event)) wm = WatchManager() notifier = Notifier(wm, PClose()) wm.add_watch(path, pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_ISDIR ) # | pyinotify.IN_UNMOUNT ) #|pyinotify.IN_CLOSE_NOWRITE) #wm.add_watch( path, pyinotify.ALL_EVENTS ) #, proc_fun=PClose() ) try: while 1: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() return
def _reloader_inotify(fnames, interval=None): #: Mutated by inotify loop when changes occur. changed = [False] # Setup inotify watches from pyinotify import WatchManager, EventsCodes, Notifier wm = WatchManager() mask = "IN_DELETE_SELF IN_MOVE_SELF IN_MODIFY IN_ATTRIB".split() mask = reduce(lambda m, a: m | getattr(EventsCodes, a), mask, 0) def signal_changed(event): if changed[0]: return _log('info', ' * Detected change in %r, reloading' % event.path) changed[:] = [True] for fname in fnames: wm.add_watch(fname, mask, signal_changed) # ... And now we wait... notif = Notifier(wm) try: while not changed[0]: notif.process_events() if notif.check_events(timeout=interval): notif.read_events() # TODO Set timeout to something small and check parent liveliness finally: notif.stop() sys.exit(3)
def __init__(self, paths, callback): class EventHandler(ProcessEvent): def process_IN_CREATE(self, event): # noqa callback() def process_IN_DELETE(self, event): # noqa callback() manager = WatchManager() # Watch Manager mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE # watched events self.async_notifier = AsyncNotifier(manager, EventHandler()) # aggregate inotify events try: self.async_notifier.coalesce_events() except AttributeError as inst: LOG.warn('Can not coalesce events, pyinotify does not seem to ' 'support it (maybe too old): %s' % inst) for path in paths: if os.path.exists(path): manager.add_watch(path, mask, rec=False) else: LOG.warn("%s folder doesn't exist yet." % path) wait_dir(path) manager.add_watch(path, mask, rec=False) LOG.info("%s has just been created." % path)
def enabled(self): if not self.running: wm = WatchManager() self.event_handler = LibraryEvent(library=app.library) FLAGS = ['IN_DELETE', 'IN_CLOSE_WRITE', # 'IN_MODIFY', 'IN_MOVED_FROM', 'IN_MOVED_TO', 'IN_CREATE'] masks = [EventsCodes.FLAG_COLLECTIONS['OP_FLAGS'][s] for s in FLAGS] mask = reduce(operator.or_, masks, 0) if self.USE_THREADS: print_d("Using threaded notifier") self.notifier = ThreadedNotifier(wm, self.event_handler) # Daemonize to ensure thread dies on exit self.notifier.daemon = True self.notifier.start() else: self.notifier = Notifier(wm, self.event_handler, timeout=100) GLib.timeout_add(1000, self.unthreaded_callback) for path in get_scan_dirs(): real_path = os.path.realpath(path) print_d('Watching directory %s for %s (mask: %x)' % (real_path, FLAGS, mask)) # See https://github.com/seb-m/pyinotify/wiki/ # Frequently-Asked-Questions wm.add_watch(real_path, mask, rec=True, auto_add=True) self.running = True
def run(self): self.pclose = PClose(self.path) PC = self.pclose # only watch these events mask = EventsCodes.IN_CLOSE_WRITE | EventsCodes.IN_CLOSE_NOWRITE # watch manager instance wm = WatchManager() notifier = Notifier(wm, PC) print 'monitoring of %s started' % self.path added_flag = False # read and process events while True: try: if not added_flag: # on first iteration, add a watch on path: # watch path for events handled by mask. wm.add_watch(self.path, mask) added_flag = True notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: # ...until c^c signal print 'stop monitoring...' # stop monitoring notifier.stop() break except Exception, err: # otherwise keep on watching print err
def main_to_ascii(): setup_cli_logger(logging.INFO) parser = create_ascii_arg_parser() args = parser.parse_args() if not args.deployments_path: L.error("Please provide a --deployments_path agrument or set the " "GUTILS_DEPLOYMENTS_DIRECTORY environmental variable") sys.exit(parser.print_usage()) wm = WatchManager() mask = IN_MOVED_TO | IN_CLOSE_WRITE wm.add_watch(args.deployments_path, mask, rec=True, auto_add=True) # Convert binary data to ASCII if args.type == 'slocum': processor = Slocum2AsciiProcessor( deployments_path=args.deployments_path) notifier = Notifier(wm, processor, read_freq=10) # Read every 10 seconds # Enable coalescing of events. This merges event types of the same type on the same file # together over the `read_freq` specified in the Notifier. notifier.coalesce_events() try: L.info(f"Watching {args.deployments_path} for new binary files") notifier.loop(daemonize=args.daemonize) except NotifierError: L.exception('Unable to start notifier loop') return 1 L.info("GUTILS binary_to_ascii Exited Successfully") return 0
def main(): argparser = argparse.ArgumentParser(description='%(prog)s watches directory for auction/* files to attach esnipers.') argparser.add_argument('--version', action='version', version='%(prog)s 0.3') argparser.add_argument('-d', '--debug', action='store_true', help='print simple debug statements') argparser.add_argument('directory', help='Directory to watch for auctions.') args = argparser.parse_args() if args.debug: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO) os.chdir(args.directory) snipers = Snipers() wm = WatchManager() mask = EventsCodes.ALL_FLAGS['IN_CLOSE_WRITE']|EventsCodes.ALL_FLAGS['IN_MOVED_TO']| \ EventsCodes.ALL_FLAGS['IN_MOVED_FROM']|EventsCodes.ALL_FLAGS['IN_DELETE'] notifier = Notifier(wm, ProcessFiles(snipers)) wm.add_watch('auction/', mask) auctions = filter(filefilter, os.listdir('auction/')) for a in auctions: snipers.restart(a) while True: logging.debug('cycle') if notifier.check_events(None): # "None" necessary for endless select() notifier.read_events() notifier.process_events()
def FSMonitor(path): wm = WatchManager() mask = IN_DELETE | IN_CREATE | IN_MODIFY notifier = Notifier(wm, EventHandler()) wm.add_watch(path, mask, auto_add=True, rec=True) print('now starting monitor % s' % (path)) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
def persy_start(self): """ initializes the worker thread and notifier """ self.log.info("start working") self.log.setStart() self.running = True FLAGS=EventsCodes.ALL_FLAGS mask = FLAGS['IN_MODIFY'] | FLAGS['IN_DELETE_SELF']|FLAGS['IN_DELETE'] | FLAGS['IN_CREATE'] | FLAGS['IN_CLOSE_WRITE'] | FLAGS['IN_MOVE_SELF'] | FLAGS['IN_MOVED_TO'] | FLAGS['IN_MOVED_FROM'] # watched events wm = WatchManager() #addin the watched directories for watch in self.config['local']['watched']: wdd = wm.add_watch("%s"%(watch), mask, rec=True, auto_add=True) #watch for changes of the configurationfile if self.config['general']['autoshare']: wdd = wm.add_watch(self.config.getAttribute('CONFIGFILE'), mask, rec=True, auto_add=True) self.log.debug("init the syncer") self.worker = TheSyncer(self, self.config, self.log, self.config['remote']['sleep'], self.config['local']['sleep']) self.log.debug("init the filesystem notifier") self.notifier = ThreadedNotifier(wm, FileChangeHandler(self.log, self.worker.newEvent)) self.log.resetError() self.log.debug("starting syncer") self.worker.start() self.notifier.start()
def main(argv): if len(argv) == 1: print "Usage: %s [file]" % argv[0] return 1 path = argv[1] logging.basicConfig( level=logging.DEBUG, format="%(asctime)s,%(msecs)03d %(levelname)-5.5s [%(name)s] %(message)s", datefmt="%H:%M:%S", filename=None # filename = "/tmp/filemon.log" ) logging.debug("Init Watcher (%s)" % path) class PClose(ProcessEvent): def process_default(self, event): logging.info("%s: %s: %s" % (getusers(event.pathname), event.pathname, str(event.maskname))) wm = WatchManager() notifier = Notifier(wm, PClose()) wm.add_watch(path, EventsCodes.ALL_FLAGS["ALL_EVENTS"], rec=True) logging.info("Waiting for updates") while True: notifier.process_events() if notifier.check_events(): notifier.read_events() return 0
def main(): vm = WatchManager() vm.add_watch(monitor_dirs, ALL_EVENTS, rec=True) en = MyEvent() notifier = Notifier(vm, en) notifier.loop()
class LinuxFileSysMonitor(FileSysMonitor): """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=None): """Add watch for path""" super(LinuxFileSysMonitor, self).addWatch(path, mask) if not mask: mask = self.defaultMask self.wm.add_watch(path, mask, auto_add=True, rec=True) def run(self): """Thread entry""" super(LinuxFileSysMonitor, self).run() self.notifier = Notifier(self.wm, EventHandler(None, fsMonitor = self)) while not self.threadStop: 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(FileSysMonitor): """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=None): """Add watch for path""" super(LinuxFileSysMonitor, self).addWatch(path, mask) if not mask: mask = self.defaultMask self.wm.add_watch(path, mask, auto_add=True, rec=True) def run(self): """Thread entry""" super(LinuxFileSysMonitor, self).run() self.notifier = Notifier(self.wm, EventHandler(None, fsMonitor=self)) while not self.threadStop: 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()
def enabled(self): if not self.running: wm = WatchManager() self.event_handler = LibraryEvent(app.library) # Choose event types to watch for # FIXME: watch for IN_CREATE or for some reason folder copies # are missed, --nickb FLAGS = [ 'IN_DELETE', 'IN_CLOSE_WRITE', # 'IN_MODIFY', 'IN_MOVED_FROM', 'IN_MOVED_TO', 'IN_CREATE' ] mask = reduce(lambda x, s: x | EventsCodes.ALL_FLAGS[s], FLAGS, 0) if self.USE_THREADS: print_d("Using threaded notifier") self.notifier = ThreadedNotifier(wm, self.event_handler) # Daemonize to ensure thread dies on exit self.notifier.daemon = True self.notifier.start() else: self.notifier = Notifier(wm, self.event_handler, timeout=100) GLib.timeout_add(1000, self.unthreaded_callback) for path in get_scan_dirs(): print_d('Watching directory %s for %s' % (path, FLAGS)) # See https://github.com/seb-m/pyinotify/wiki/ # Frequently-Asked-Questions wm.add_watch(path, mask, rec=True, auto_add=True) self.running = True
def create_notifier(topic, instrument, posttroll_port, filepattern, event_names, monitored_dirs, aliases=None, tbus_orbit=False, history=0, granule_length=0): '''Create new notifier''' # Event handler observes the operations in defined folder manager = WatchManager() # Collect mask for events that are monitored if type(event_names) is not list: event_names = event_names.split(',') event_mask = 0 for event in event_names: try: event_mask |= getattr(pyinotify, event) except AttributeError: LOGGER.warning('Event ' + event + ' not found in pyinotify') event_handler = EventHandler(topic, instrument, posttroll_port=posttroll_port, filepattern=filepattern, aliases=aliases, tbus_orbit=tbus_orbit, history=history, granule_length=granule_length) notifier = NewThreadedNotifier(manager, event_handler) # Add directories and event masks to watch manager for monitored_dir in monitored_dirs: manager.add_watch(monitored_dir, event_mask, rec=True) return notifier
def run(self, location='.'): transport = get_transport(location) root = transport.local_abspath('.') new_dirs = set('.') relpaths = set('.') while relpaths: relpath = relpaths.pop() paths = transport.list_dir(relpath) for path in paths: st = transport.stat(relpath + '/' + path) if S_ISDIR(st.st_mode): if path != '.bzr': new_dirs.add(relpath + '/' + path) relpaths.add(relpath + '/' + path) # gather all dirs wm = WatchManager() added_flag = False handler = ProcessClose() handler._bzr_wm = wm notifier = Notifier(wm, handler) # read and process events try: while True: if new_dirs: for path in new_dirs: wm.add_watch(root + '/' + path, dir_mask) new_dirs = set() notifier.process_events() if notifier.check_events(): notifier.read_events() finally: notifier.stop()
def run(self): self.pclose = PClose(self.path) # PC = self.pclose # mask = EventsCodes.IN_CLOSE_WRITE | EventsCodes.IN_CLOSE_NOWRITE mask = pyinotify.ALL_EVENTS wm = WatchManager() notifier = Notifier(wm,pyinotify.ALL_EVENTS) print(f"mointioring of {self.path} started") added_flag = False while True: try: if not added_flag: wm.add_watch(self.path, mask) added_flag = True notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: print("stop monitoring...") notifier.stop() except Exception as err: pprint.pprint(err) print("error happend!")
class TrackState(State, ProcessEvent): mask = (EventsCodes.OP_FLAGS['IN_DELETE'] | EventsCodes.OP_FLAGS['IN_CLOSE_WRITE']) def __init__(self, *args, **kwargs): State.__init__(self, *args, **kwargs) self.wm = WatchManager() self.notifier = Notifier(self.wm, self) def process_IN_CLOSE_WRITE(self, event): debug("IN_CLOSE_WRITE: %r" % (event,)) path = os.path.join(event.path, event.name) if os.path.isfile(path) and self.acceptable(path): self.upload(self.filename_to_File(path)) def process_IN_DELETE(self, event): debug("IN_DELETE: %r" % (event,)) path = os.path.join(event.path, event.name) if self.acceptable(path): self.delete(self.filename_to_File(path)) def run(self): for f in self.files: f = os.path.abspath(f) self.wm.add_watch(f, self.mask, rec=True, auto_add=True) try: while True: self.notifier.process_events() if self.notifier.check_events(100): self.notifier.read_events() sleep(0) except KeyboardInterrupt: self.notifier.stop()
def inotify(): setproctitle('event-report') print('EventReport %d Process start...' % getpid()) file_event = FileEvent() dir_event = DirEvent() #需要保证,监听前,该路径已存在 #dir_path = '/home/lz/Desktop/test_inotify/' #需要保证,监听前,该文件已存在 #file_path = '/home/lz/Desktop/test_inotify/alarm' #dir_muti_event = EventsCodes.FLAG_COLLECTIONS['OP_FLAGS']['IN_CREATE']\ # | EventsCodes.FLAG_COLLECTIONS['OP_FLAGS']['IN_DELETE']\ # | EventsCodes.FLAG_COLLECTIONS['OP_FLAGS']['IN_MODIFY'] #file_muti_event = EventsCodes.FLAG_COLLECTIONS['OP_FLAGS']['IN_MODIFY'] #file_handler = MyFileEventHandler() #dir_handler = MyDirEventHandler() wm = WatchManager() wm.add_watch(dir_event.get_path(), dir_event.get_muti_event(), dir_event.get_event_handler()) #可以这样使用,同时监听,不同路径/文件的不同操作 wm.add_watch(file_event.get_path(), file_event.get_muti_event(), file_event.get_event_handler()) notifier = Notifier(wm) print('inotify loop...') notifier.loop()
def add_watch(self, path): if path not in self.watching_paths: wm = WatchManager() notifier = QNotifier(wm, self._emit_signal_on_change) notifier.start() wm.add_watch(path, mask, rec=True, auto_add=True) self.watching_paths[path] = notifier
def watch(event): import os clock = event pathname = os.getcwd() mask = (pyinotify.IN_CLOSE_WRITE | pyinotify.IN_CREATE | pyinotify.IN_MOVED_FROM | pyinotify.IN_DELETE | pyinotify.IN_Q_OVERFLOW | pyinotify.IN_MOVED_TO) wm = WatchManager() p = Process() p.add_clock(clock) notifier = Notifier(wm, p) #excl = pyinotify.ExcludeFilter('*.swp') wm.add_watch(pathname, mask, rec=False, auto_add=True) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
def watch_path(path, add_watch_opt=None, watcher_opt=None): """Tail all the files specify by path. path: By default all files under the path, which should be a directory, are tailed. Path could also be list of paths or a glob pattern. The behavior can be modified by add_watch_opt. See pyinotify.WatchManager.add_watch. output: defaults to stdout. can be diverted any callable with: watcher_opt=dict(out=got_log_line) where got_log_line() takes a tuple (log_path, log_line) *_opt: Are pass-through to pyinotify.WatchManager.add_watch and tailall.Monitor. See respective functions for detail. """ wm = WatchManager() notifier = Notifier(wm, default_proc_fun=FsEvent()) #mask=ALL_EVENTS #mask=IN_MOVED_TO|IN_CREATE|IN_MODIFY mask=IN_MODIFY|IN_CLOSE_WRITE kw=dict(rec=True, auto_add=False) kw.update(add_watch_opt or {}) wm.add_watch(path, mask, **kw) monitor=Monitor(watcher_opt=watcher_opt) notifier.loop(callback=monitor.got_event)
def watch(pathes, extensions): manager = WatchManager() handler = Handler(extensions=extensions) notifier = Notifier(manager, default_proc_fun=handler) for path in pathes: manager.add_watch(path, IN_MODIFY, rec=True, auto_add=True) notifier.loop()
def _watch(self): wm = WatchManager() wm2 = WatchManager() clusterNotifier = ThreadedNotifier(wm, \ ClustersDefinitionsChangeHandler(\ masterCallback=self.callback)) suiteNotifier = ThreadedNotifier(wm2, \ SuiteDefinitionsChangeHandler(\ masterCallback=self.callback)) clusterNotifier.start() suiteNotifier.start() local_path = '' if self.config.has_option(self.repo, 'local_path'): local_path = self.config.get(self.repo, 'local_path') else: LOGGER.error('No local path defined for repository %s' % self.repo) if not self.config.has_option(self.repo, 'cluster_defs_path'): clustdir = local_path + os.sep + 'clusters' else: clustdir = local_path + os.sep + self.config.get(self.repo, 'cluster_defs_path') if not self.config.has_option(self.repo, 'suite_defs_path'): suitedir = local_path + os.sep + 'test-suites' else: suitedir = local_path + os.sep + self.config.get(self.repo, 'suite_defs_path') try: wm.add_watch(clustdir, self.mask, rec=True, quiet=False) wm2.add_watch(suitedir, self.mask, rec=True, quiet=False) except WatchManagerError, e: LOGGER.error(e)
def add_watch(self, path): if path not in self.watching_paths: wm = WatchManager() notifier = QNotifier(wm, self._emit_signal_on_change) notifier.start() exclude = ExcludeFilter([os.path.join(path, folder) for folder in self._ignore_hidden]) wm.add_watch(path, mask, rec=True, auto_add=True, exclude_filter=exclude) self.watching_paths[path] = notifier
def FSMonitor(path='/root/wpf'): wm = WatchManager() mask = IN_DELETE | IN_MODIFY | IN_CREATE notifier = Notifier(wm, EventHandler(), read_freq=10) notifier.coalesce_events() # 设置受监视的事件,这里只监视文件创建事件,(rec=True, auto_add=True)为递归处理 wm.add_watch(path, mask, rec=True, auto_add=True) notifier.loop()
def FSMonitor(path='/root/wpf'): wm = WatchManager() mask = IN_DELETE | IN_MODIFY | IN_CREATE notifier = Notifier(wm, EventHandler(),read_freq=10) notifier.coalesce_events() # 设置受监视的事件,这里只监视文件创建事件,(rec=True, auto_add=True)为递归处理 wm.add_watch(path,mask,rec=True, auto_add=True) notifier.loop()
def watch_dir(self): wm = WatchManager() handler = EventHandler(self.set_color) notifier = Notifier(wm, handler) wm.add_watch(self.path_to_leddir, IN_CLOSE_WRITE, rec=True) notifier.loop()
def main_to_netcdf(): setup_cli_logger(logging.INFO) parser = create_netcdf_arg_parser() args = parser.parse_args() filter_args = vars(args) # Remove non-filter args into positional arguments deployments_path = filter_args.pop('deployments_path') subset = filter_args.pop('subset') daemonize = filter_args.pop('daemonize') template = filter_args.pop('template') profile_id_type = int(filter_args.pop('profile_id_type')) # Move reader_class to a class reader_class = filter_args.pop('reader_class') if reader_class == 'slocum': reader_class = SlocumReader if not deployments_path: L.error("Please provide a --deployments_path argument or set the " "GUTILS_DEPLOYMENTS_DIRECTORY environmental variable") sys.exit(parser.print_usage()) # Add inotify watch wm = WatchManager() mask = IN_MOVED_TO | IN_CLOSE_WRITE wm.add_watch( deployments_path, mask, rec=True, auto_add=True ) # Convert ASCII data to NetCDF using a specific reader class if reader_class == SlocumReader: processor = Slocum2NetcdfProcessor( deployments_path=deployments_path, subset=subset, template=template, profile_id_type=profile_id_type, prefer_file_filters=True, **filter_args ) notifier = Notifier(wm, processor, read_freq=10) # Enable coalescing of events. This merges event types of the same type on the same file # together over the `read_freq` specified in the Notifier. notifier.coalesce_events() try: L.info(f"Watching {deployments_path} for new ascii files") notifier.loop(daemonize=daemonize) except NotifierError: L.exception('Unable to start notifier loop') return 1 L.info("GUTILS ascii_to_netcdf Exited Successfully") return 0
def change_check(): wm = WatchManager() mask = IN_CREATE | IN_MODIFY notifier = Notifier(wm, EventHandler()) wm.add_watch(conf_dir, mask, rec=True) try: notifier.loop() except NotifierError, err: print err
def loop(self): wm = WatchManager() handler = EventHandler(self.workq, self.src, self.dst) self.notifier = ThreadedNotifier(wm, handler) self.notifier.start() mask = IN_CREATE | IN_MODIFY wm.add_watch(self.src, mask, rec=self.gdr.rec)
def addWatch(self, asset_dir): wm = WatchManager() mask = pyinotify.ALL_EVENTS notifier = ThreadedNotifier(wm) worker = AssetsCopyWorker(asset_dir) wm.add_watch(self.basepath+asset_dir, mask, proc_fun=AssetsProcessEvent(worker), rec=True) self.__threadpool.append(notifier) self.__threadpool.append(worker)
def __init__( self, path, MountDetectedCallback, UnmountDetectedCallback ): wm = WatchManager() notifier = Notifier( wm, MountEvents( MountDetectedCallback, UnmountDetectedCallback ) ) wm.add_watch(path, pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_ISDIR ) while( True ): notifier.process_events() if notifier.check_events(): notifier.read_events() time.sleep( 0.1 )
def builder_process(): logger.debug(' - Watched static files for changes to rebuild') wm = WatchManager() notifier = Notifier(wm, default_proc_fun=_build) wm.add_watch( watched_dir, IN_MODIFY, # | IN_CREATE | IN_DELETE, rec=True, auto_add=True ) notifier.loop()
def builder_process(): logger.debug(' - Watched static files for changes to rebuild') wm = WatchManager() notifier = Notifier(wm, default_proc_fun=_build) wm.add_watch( watched_dir, IN_MODIFY, # | IN_CREATE | IN_DELETE, rec=True, auto_add=True) notifier.loop()
def main(): """ 文件监听的入口程序 """ check_dir_exist() wm = WatchManager() notifier = Notifier(wm, EventHandler()) wm.add_watch(FILE_DIR, IN_CLOSE_WRITE, rec=True, auto_add=True) log.info('Now starting monitor %s' % (FILE_DIR)) notifier.loop()
class PhotoWatcher(ProcessEvent): MASK = (EventsCodes.ALL_FLAGS['IN_DELETE'] | EventsCodes.ALL_FLAGS['IN_CLOSE_WRITE'] | EventsCodes.ALL_FLAGS['IN_MOVED_FROM'] | EventsCodes.ALL_FLAGS['IN_MOVED_TO']) def __init__(self, db, walker, root): self.root = root self.db = db self.walker = walker self.wm = WatchManager() self.wdds = [] def Watch(self): self.notifier = ThreadedNotifier(self.wm, self) self.notifier.start() self.wdds.append(self.wm.add_watch(self.root, self.MASK, rec=True)) # add soft link sub-folders for dirname, dirnames, _filenames in os.walk(self.root, followlinks=True): for d in dirnames: path = os.path.join(dirname, d) if os.path.islink(path): self.wdds.append( self.wm.add_watch(os.path.realpath(path), self.MASK, rec=True)) def Stop(self): self.notifier.stop() def process_IN_DELETE(self, event): self.db.DeletePhoto(os.path.join(event.path, event.name)) def process_IN_MOVED_FROM(self, event): self.process_IN_DELETE(event) def process_IN_MOVED_TO(self, event): full_path = os.path.join(event.path, event.name) try: meta = self.walker.ReadMetadata(full_path) except Exception: return self.db.StorePhoto(full_path, meta) def process_IN_CLOSE_WRITE(self, event): full_path = os.path.join(event.path, event.name) try: meta = self.walker.ReadMetadata(full_path) except Exception: return if self.db.HasPhoto(full_path): self.db.UpdatePhoto(full_path, meta) else: self.db.StorePhoto(full_path, meta)
def fs_monitor(path=need_remove_disk_file): wm = WatchManager() mask = FLAG['OP_FLAGS']['IN_CLOSE_WRITE'] | FLAG['OP_FLAGS']['IN_CLOSE_NOWRITE'] notifier = Notifier(wm, EventHandler()) wm.add_watch(path, mask, rec=True) # print('now starting monitor %s' % path) while True: notifier.process_events() if notifier.check_events(): notifier.read_events()
def construct(cls, path, timeout=None): handler = TailEventHandler(path) watch_manager = WatchManager() watch_manager.add_watch(path, cls.__IN_EVENTS) notifier_timeout = cls.DEFAULT_TIMEOUT if timeout is None else timeout notifier = TailNotifier(watch_manager, default_proc_fun=handler, timeout=notifier_timeout) return cls(notifier, handler)
def watch_posix_start(self): '''os-specific command to watch''' # test to see if we already have a notifier object, if not, make it, otherwise we are already watching a set of folders if self.notifier1 == None and self.notifier2 == None: try: pyinotify.compatibility_mode() print 'pyinotify running in compatibility mode' except: print 'pyinotify running in standard mode' try: #mask = EventsCodes.IN_CREATE |EventsCodes.IN_MOVED_TO mask = pyinotify.ALL_EVENTS #ECJ20100831 Reason why we have two threads: it never returns control ever to the main loop if only one thread, so no ctrl+c #The second thread is a dummy, so it performs switching/relinquishing control #Eventually, we want to watch many folders, so that is why we are using the ThreadedNotifier, versus the recommended Notifier. #Even then, Notifier is still probably the way to go, but we'll use this as long as it works, because then you don't have to poll/While loop # Thread #1 watch_manager1 = WatchManager() self.notifier1 = ThreadedNotifier(watch_manager1, EventHandler(self.queue)) self.notifier1.start() print 'Starting the threaded notifier on ', self.dir_to_watch watch_manager1.add_watch(self.dir_to_watch, mask) # Thread #2 watch_manager2 = WatchManager() self.notifier2 = ThreadedNotifier(watch_manager2, EventHandlerDummy(self.queue)) self.notifier2.start() #just watch any place, but don't remove this or Ctrl+C will not work watch_manager2.add_watch(settings.BASE_PATH, mask) if settings.DEBUG: print "both notifiers started" except KeyboardInterrupt: print "Keyboard Interrupt in notifier" self.notifier1.stop() self.notifier2.stop() return except NameError: self.notifier1.stop() self.notifier2.stop() return ['POSIX Watch Error'] except: print "General exception caught within notifier while loop, stopping both notifiers now" self.notifier1.stop() self.notifier2.stop() # SBB20090903 Turn on verbose mode self.notifier1.VERBOSE = settings.DEBUG print "returning to calling function" return True
def main(): logger.setLevel(logging.INFO) logger.addHandler(logging.StreamHandler()) parser = argparse.ArgumentParser( description="Monitor a directory for new glider data. " "Processes and uploads new data to a Mongo Database. " "Announce changes via ZMQ.") parser.add_argument("-d", "--data_path", help="Path to Glider data directory", default=os.environ.get('GDB_DATA_DIR')) parser.add_argument("--zmq_url", help='Port to publish ZMQ messages on. ' 'Default is "tcp://127.0.0.1:44444".', default=os.environ.get('ZMQ_URL', 'tcp://127.0.0.1:44444')) parser.add_argument( "--mongo_url", help='Mongo Database URL. Can include authentication parameters. ' 'Default is "mongodb://localhost:27017".', default=os.environ.get('MONGO_URL', 'mongodb://localhost:27017')) parser.add_argument("--daemonize", help="To daemonize or not to daemonize", type=bool, default=False) args = parser.parse_args() if not args.data_path: logger.error("Please provide a --data_path agrument or set the " "GDB_DATA_DIR environmental variable") sys.exit(parser.print_usage()) wm = WatchManager() mask = IN_MOVED_TO | IN_CLOSE_WRITE wm.add_watch(args.data_path, mask, rec=True, auto_add=True) processor = GliderFileProcessor(zmq_url=args.zmq_url, mongo_url=args.mongo_url) notifier = Notifier(wm, processor) try: logger.info("Watching {}\nInserting into {}\nPublishing to {}".format( args.data_path, args.mongo_url, args.zmq_url)) notifier.loop(daemonize=args.daemonize) except NotifierError: logger.exception('Unable to start notifier loop') return 1 logger.info("GDAM Exited Successfully") return 0
def start_watcher(source, new_files, destination, bitrate): wm = WatchManager() process = Process(new_files, destination, bitrate) notifier = Notifier(wm, process) # TODO detect files moving from a dir to another wm.add_watch(source, IN_CLOSE_WRITE, rec=True) try: while True: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop()
def start_queue(): dir_queue = vmcheckerpaths.dir_queue() # register for inotify envents before processing stale jobs wm = WatchManager() notifier = Notifier(wm, _QueueManager()) wm.add_watch(dir_queue, EventsCodes.ALL_FLAGS['IN_CLOSE_WRITE']) process_stale_jobs(dir_queue) # set callback to receive notifications (includes queued jobs after # setting up inotify but before we finished processing stale jobs) notifier.loop(callback=_callback)
def start_watch_loop(self): wm = WatchManager() process = self.Process(self.folderpath, self.event_callback) #options) self.notifier = ThreadedNotifier(wm, process) #Notifier(wm, process) #notifier = Notifier(wm) mask = IN_DELETE | IN_CREATE | IN_CLOSE_WRITE #wdd = wm.add_watch(options.directory, mask, rec=True) #wm.add_watch('./excelsrc', mask, rec=True) wm.add_watch('./' + self.folderpath, mask, rec=True) self.notifier.start() """
def monitor(watch_path, callback): watch_path = os.path.abspath(watch_path) if os.path.isfile(watch_path): path_for_manager = os.path.dirname(watch_path) else: path_for_manager = watch_path manager = WatchManager() notifier = Notifier(manager, AutoRunner(watch_path, callback)) manager.add_watch(path_for_manager, IN_MODIFY) notifier.loop()
def FSMonitor(path='/var/log'): wm = WatchManager() mask = IN_DELETE | IN_CREATE |IN_MODIFY notifier = Notifier(wm, EventHandler()) wm.add_watch(path, mask,rec=True) print 'now starting monitor %s'%(path) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
def FSMonitor(path='.'): wm = WatchManager() mask = IN_DELETE | IN_CREATE | IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE | IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE_SELF | IN_MOVE_SELF notifier = Notifier(wm, EventHandler()) wm.add_watch(path, mask,auto_add=True,rec=True) print 'now starting monitor %s'%(path) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break