def main(): vm = WatchManager() vm.add_watch(monitor_dirs,ALL_EVENTS,rec = True) en = MyEvent() notifier = Notifier(vm,en) notifier.loop()
def directory(directory, patterns): """ This method monitors a directory given by the "*directory*" parameter. It creates a notifier object. The notifier is registered to await the "*CLOSE_WRITE*" event on a new file that matches the "*pattern*" parameter. If there is no such event, it yields control on timeout, defaulted to 1 second. It returns the created notifier. Parameters ---------- file : str File Name including path patterns : list A list of strings representing file extension Returns ------- None """ class EventHandler(pyinotify.ProcessEvent): def process_IN_CLOSE_WRITE(self, event): for pattern in patterns: file = event.pathname if file.endswith(pattern): files.put(event.pathname) break wm = WatchManager() mask = pyinotify.IN_CLOSE_WRITE handler = EventHandler() notifier = pyinotify.Notifier(wm, handler, timeout=1) wdd = wm.add_watch(directory, mask, rec=False) return notifier
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 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
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 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 _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 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 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 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 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 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()
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()
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()
def __init__(self): self.wm=WatchManager() self.notifier = Notifier(self.wm, EventHandler()) #监视所有已经完成备份的文件的修改操作 fileList = xmlFile.getClientFileListByAttr('state', 'completed') for path in fileList: self.add_watch(path)
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 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
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 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!")
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 __init__(self, configuration, builder, watch_index): self._builder = builder self._root = configuration.GetExpandedDir("projects", "root_dir") self._batch_timeout = float( configuration.Get("file_watcher", "event_batch_timeout_ms")) / 1000 self._moddef_filename = configuration.Get( "general", "module_definition_filename") self.wm = WatchManager() self.watch_index = watch_index self.watched_module_definitions = collections.defaultdict(dict) mask = (EventsCodes.ALL_FLAGS['IN_DELETE'] | EventsCodes.ALL_FLAGS['IN_CREATE'] | EventsCodes.ALL_FLAGS['IN_MODIFY']) handler = functools.partial(TargetWatcher.ProcessEvent, self) self.events_queue = queue.Queue() self.acc_thread = threading.Thread(target=functools.partial( TargetWatcher.AccumulationThreadProc, self), daemon=True) self.acc_thread.start() self.notifier = ThreadedNotifier(self.wm, handler) self.notifier.start() self.watch = self.wm.add_watch(self._root, mask, rec=True, auto_add=True) self.modification_handlers = []
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 monitor_loop(): """ Main loop, create everything needed for the notification and waits for a notification. Loop again when notification happens. """ if nfs_flag: scheduler = BackgroundScheduler() scheduler.add_job(verify_all, 'interval', seconds=60 * 1) scheduler.start() try: # This is here to simulate application activity (which keeps the main thread alive). while True: time.sleep(10) logger.info('sleep!') except (KeyboardInterrupt, SystemExit): # Not strictly necessary if daemonic mode is enabled but should be done if possible scheduler.shutdown() logger.info('Exit The Job!') else: wm = WatchManager() mask = IN_CLOSE_WRITE | IN_CREATE | IN_DELETE process = Process(wm, mask) notifier = Notifier(wm, process) for folder in folders: logger.info("Monitor loop---folder:%s", folder) wdd = wm.add_watch(folder, mask, rec=True) try: while True: notifier.process_events() if notifier.check_events(): notifier.read_events() except (KeyboardInterrupt, SystemExit): notifier.stop()
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
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 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 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 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 start_watching(): class OnEvent(ProcessEvent): def __init__(self): ProcessEvent(self) self.timer = None def process_default(self, event): accepted = [ pyinotify.IN_MODIFY, pyinotify.IN_MOVE_SELF, pyinotify.IN_MOVED_FROM, pyinotify.IN_MOVED_TO ] x = [x for x in accepted if event.mask & x == event.mask] if len(x) > 0: print "default: %-20s %s" % (os.path.join( event.path, event.name), event.maskname) if self.timer: self.timer.cancel() self.timer = None self.timer = threading.Timer(0.2, do_post) self.timer.start() wm = WatchManager() mask = pyinotify.ALL_EVENTS proc = OnEvent() notifier = ThreadedNotifier(wm, proc) notifier.start() wdd = wm.add_watch('.git', mask, rec=True, auto_add=True)
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 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 tailwatch(dir): FLAGS = EventsCodes.ALL_FLAGS mask = FLAGS['IN_CREATE'] |FLAGS['IN_DELETE'] | FLAGS['IN_MODIFY'] wm = WatchManager() p = PTmp() notifier = Notifier(wm, p) wdd = wm.add_watch(dir, mask, rec=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 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 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 __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 __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 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 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 __watch_thread(self, root_lst, sync_list, cond, eventq): """ 初始化客户端监控文件变化的同步线程,根据同步的根目录列表和 需要同步的文件目录白名单,获取需要监控的目录列表以及监控排除的文件列表添加到INotifier中 @param root_lst: 监控的根目录列表 @type root_lst: tuple @param sync_list: 需要同步的文件和目录的列表 @type sync_list: tuple @param cond: 线程同步条件变量 @type cond: threading.Condition @param eventq: 保存文件变化的事件队列 @type eventq: pyinotify.Event @return: 初始化后的监控线程 @rtype: pyinotify.ThreadedNotifier """ wm = WatchManager() mask = IN_DELETE | IN_CLOSE_WRITE | IN_CREATE | IN_MOVED_FROM | IN_MOVED_TO thread_notifier = ThreadedNotifier(wm, EventHandler(cond=cond, eventq=eventq, sync_list=sync_list), read_freq=10, timeout=9) thread_notifier.coalesce_events() # Enable coalescing of events watch_lst = [] # INotifier watch direcory list exclude_lst = [] # INotifier exclude directory list LOGGER.debug('root:%s', str(root_lst)) LOGGER.debug('sublist:%s', str(sync_list)) for root_path in root_lst: # add root directory to watch list watch_lst.append(root_path['name']) if not root_path['is_all']: # get exclude sub direcory list for dirpath, _, _ in os.walk(root_path['name']): if dirpath != root_path['name']: for file_path in sync_list: is_exclude = True if file_path.startswith(dirpath) \ or dirpath.startswith(file_path): # 遍历的目录为同步列表文件的父目录, # 或者同步文件列表下的子目录,都不添加到排除目录列表 LOGGER.debug('dirpath:%s', dirpath) LOGGER.debug('file_path:%s', file_path) is_exclude = False break if is_exclude: exclude_lst.append(dirpath) LOGGER.debug('watchlist:%s', str(watch_lst)) LOGGER.debug('excludelist:%s', str(exclude_lst)) excl = ExcludeFilter(exclude_lst) # 设置受监视的事件,(rec=True, auto_add=True)为递归处理 wm_dict = wm.add_watch(watch_lst, mask, rec=True, auto_add=True, exclude_filter=excl) LOGGER.debug('client monitor lst:%s', str(wm_dict)) return thread_notifier
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()
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 create_monitor(to_watch, name): "Create and start a new directory monitor." messenger = NetworkSender(name) p = Monitor(messenger) wm = WatchManager() # Watch Manager notifier = Notifier(wm, p) # Notifier try: wdd = wm.add_watch(to_watch, IN_DELETE | IN_CREATE | IN_MODIFY) notifier.loop() except WatchManagerError, err: print err, err.wmd
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
def main(args): """ args : [-h] [-r REGEX | -p REGEX] directory script example : react.py /local/data -p '*.hdf' 'echo $f' positional arguments : - directory: the directory which is recursively monitored - script: the script that is executed upon reaction optional arguments : -h, --help show this help message and exit -r REGEX, --regex REGEX files only trigger the reaction if their name matches this regular expression -p REGEX, --pattern REGEX files only trigger the reaction if their name matches this shell pattern """ parser = argparse.ArgumentParser(description='Launch a script if specified files change.') parser.add_argument('directory', help='the directory which is recursively monitored') group = parser.add_mutually_exclusive_group() group.add_argument('-r', '--regex', required=False, default=".*", help='files only trigger the reaction if their name matches this regular expression') group.add_argument('-p', '--pattern', required=False, dest="regex", action=PatternAction, help='files only trigger the reaction if their name matches this shell pattern') parser.add_argument("script", help="the script that is executed upon reaction") options = Options() args = parser.parse_args(namespace=options) #print sys.argv while True: wm = WatchManager() process = Process(options) notifier = Notifier(wm, process) mask = IN_DELETE | IN_CREATE | IN_CLOSE_WRITE wdd = wm.add_watch(options.directory, mask, rec=True) try: while True: notifier.process_events() if notifier.check_events(): notifier.read_events() except Reload: pass except KeyboardInterrupt: notifier.stop() break