def main(): vm = WatchManager() vm.add_watch(monitor_dirs,ALL_EVENTS,rec = True) en = MyEvent() notifier = Notifier(vm,en) notifier.loop()
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 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 startSentry(sentry_paths): """ Sentry Runner """ notifier = Notifier(wm, do_event()) for path in sentry_paths: wdd = wm.add_watch(sentry_paths[path].path, mask, rec=True) print 'watching %s ' % sentry_paths[path].path #wdd = wm.add_watch('/home/cmdln/sandbox/permissionminder/test', mask, rec=True) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break except ConfigChange: notifier.stop() raise ConfigChange('Config Changed') break except: #from pdb import set_trace;set_trace() pass
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 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()
def test_update_conf(self, default_config, show_notification): conf_time_1 = path.getmtime(self.tmp.conf.join("config.py")) out_file = self.tmp_output.join("out.log") command_args = [ "-c", self.config_file, "-r", "bash -c 'echo a | tee -a {}'".format(out_file), "-d", unicode(self.tmp.src), ] events = [ (self.tmp.conf, "config.py", "# some new data"), (self.tmp.conf, "config.py", "# some new data"), ] self._copy_default_config(default_config) default_config.RUNNER_DELAY = -1 wm = WatchManager() config = Config(watch_manager=wm, command_args=command_args) handler = FileChangeHandler(config=config) notifier = Notifier(wm, handler, timeout=1000) notifier.loop(callback=partial(self._event_generator, events)) # There are some stupid race conditions (possibly due to the callbacks) # Sleep time allows to execute all needed code sleep(0.2) conf_time_2 = path.getmtime(self.tmp.conf.join("config.py")) self.assertNotEqual(conf_time_1, conf_time_2) self.assertTrue(path.exists(out_file)) self.assertEqual(show_notification.call_count, 2)
def __init__( self, watch_manager, default_proc_fun=None, read_freq=0, threshold=0, timeout=None, airtime_config=None, api_client=None, bootstrap=None, mmc=None, ): Notifier.__init__(self, watch_manager, default_proc_fun, read_freq, threshold, timeout) self.logger = logging.getLogger() self.config = airtime_config self.api_client = api_client self.bootstrap = bootstrap self.md_manager = AirtimeMetadata() self.import_processes = {} self.watched_folders = [] self.mmc = mmc self.wm = watch_manager self.mask = pyinotify.ALL_EVENTS while not self.init_rabbit_mq(): self.logger.error("Error connecting to RabbitMQ Server. Trying again in few seconds") time.sleep(5)
def run(self): p = PTmp() notifier = Notifier(Settings.wm, p) # TODO: Not necessary to watch the directories that already have # a procedures.xml Settings.wdd = Settings.wm.add_watch(Settings.monitor_path, Settings.mask, rec=True) notifier.loop()
def run(self): """Create the inotify WatchManager and generate FileSysEvents""" if utils.is_win32(): self.run_win32() return # Only capture events that git cares about self._wmgr = WatchManager() if self._is_pyinotify_08x(): notifier = Notifier(self._wmgr, FileSysEvent(), timeout=self._timeout) else: notifier = Notifier(self._wmgr, FileSysEvent()) self._watch_directory(self._path) # Register files/directories known to git for filename in core.decode(self._git.ls_files()).splitlines(): filename = os.path.realpath(filename) directory = os.path.dirname(filename) self._watch_directory(directory) # self._running signals app termination. The timeout is a tradeoff # between fast notification response and waiting too long to exit. while self._running: if self._is_pyinotify_08x(): check = notifier.check_events() else: check = notifier.check_events(timeout=self._timeout) if not self._running: break if check: notifier.read_events() notifier.process_events() notifier.stop()
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 test_update_filtered(self, default_config, show_notification): out_file = self.tmp_output.join("out.log") command_args = [ "-c", self.config_file, "-r", "bash -c 'echo a | tee -a {}'".format(out_file), "-d", unicode(self.tmp.src), ] events = [ (self.tmp.src, "filtered_1.pyc", "some new data"), (self.tmp.src, "filtered_2.tmp", "some new data"), (self.tmp.src, ".hidden", "some new data"), ] self._copy_default_config(default_config) default_config.RUNNER_DELAY = -1 wm = WatchManager() config = Config(watch_manager=wm, command_args=command_args) handler = FileChangeHandler(config=config) notifier = Notifier(wm, handler) notifier.loop(callback=partial(self._event_generator, events)) # There are some stupid race conditions (possibly due to the callbacks) # Sleep time allows to execute all needed code sleep(0.2) self.assertTrue(path.exists(self.tmp.src.join("filtered_1.pyc"))) self.assertTrue(path.exists(self.tmp.src.join("filtered_2.tmp"))) self.assertTrue(path.exists(self.tmp.src.join(".hidden"))) self.assertFalse(path.exists(out_file)) self.assertFalse(show_notification.called)
def run(self): """Create the inotify WatchManager and generate FileSysEvents""" # Only capture events that git cares about self._wmgr = WatchManager() if self._is_pyinotify_08x(): notifier = Notifier(self._wmgr, FileSysEvent(self), timeout=self._timeout) else: notifier = Notifier(self._wmgr, FileSysEvent(self)) dirs_seen = set() added_flag = False # self._abort signals app termination. The timeout is a tradeoff # between fast notification response and waiting too long to exit. while not self._abort: if not added_flag: self._watch_directory(self._path) # Register files/directories known to git for filename in self._git.ls_files().splitlines(): directory = os.path.dirname(filename) self._watch_directory(directory) added_flag = True notifier.process_events() if self._is_pyinotify_08x(): check = notifier.check_events() else: check = notifier.check_events(timeout=self._timeout) if check: notifier.read_events() notifier.stop()
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 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()
class QNotifier(QThread): def __init__(self, wm, processor): self.event_queue = list() self._processor = processor self.notifier = Notifier(wm, NinjaProcessEvent(self.event_queue.append)) self.notifier.coalesce_events(True) self.keep_running = True QThread.__init__(self) def run(self): while self.keep_running: try: self.notifier.process_events() except OSError: pass # OSError: [Errno 2] No such file or directory happens e_dict = {} while len(self.event_queue): e_type, e_path = self.event_queue.pop(0) e_dict.setdefault(e_path, []).append(e_type) keys = e_dict.keys() while len(keys): key = keys.pop(0) event = e_dict.pop(key) if (ADDED in event) and (DELETED in event): event = [e for e in event if e not in (ADDED, DELETED)] for each_event in event: self._processor(each_event, key) if self.notifier.check_events(): self.notifier.read_events() self.notifier.stop()
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
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 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 monitor_photos(directory='/mnt/photos'): wm = WatchManager() # watched events mask = IN_DELETE | IN_CREATE |IN_MODIFY | IN_CLOSE_WRITE | IN_MOVED_FROM | IN_MOVED_TO emails = ["[email protected]@163.com"] class PFilePath(ProcessEvent): def process_IN_CREATE(self, event): print(datetime.now().strftime('%Y%m%d %H%M%S: ')) print("***Create file: %s" % os.path.join(event.path, event.name)) print("%s sending email"%event.name) subject = "%s uploaded."%event.name body_text = "<a href=http://7xrst7.com1.z0.glb.clouddn.com/%s>%s</a>"%(event.name, event.name) #send_email(subject, body_text, emails) p = Process(target=send_email, args=(subject, body_text, emails)) p.start() p.join() def process_IN_MODIFY(self, event): #print(datetime.now().strftime('%Y%m%d %H%M%S: ')) #print("***Modify file: %s" % os.path.join(event.path, event.name)) pass def process_IN_CLOSE_WRITE(self, event): print(datetime.now().strftime('%Y%m%d %H%M%S: ')) print("***Close file: %s" % os.path.join(event.path, event.name)) update_list(directory, 'add', event.name) def process_IN_MOVED_TO(self, event): print(datetime.now().strftime('%Y%m%d %H%M%S: ')) print("***Movedto file: %s" % os.path.join(event.path, event.name)) update_list(directory, 'add', event.name) def process_IN_MOVED_FROM(self, event): print(datetime.now().strftime('%Y%m%d %H%M%S: ')) print("***Movedfrom file: %s" % os.path.join(event.path, event.name)) update_list(directory, 'delete', event.name) def process_IN_DELETE(self, event): print(datetime.now().strftime('%Y%m%d %H%M%S: ')) print("***Delete file: %s" % os.path.join(event.path, event.name)) update_list(directory, 'delete', event.name) notifier = Notifier(wm, PFilePath()) wdd = wm.add_watch(directory, mask) print(datetime.now().strftime('%Y%m%d %H%M%S: ')) print('***Start watching***') notifier.loop()
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 _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 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
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)
def __init__(self, input_path, output_path): threading.Thread.__init__(self) self.input_path = input_path self.output_path = output_path self.thread_stop = False self.wm = WatchManager() self.notifier = Notifier(self.wm, EventHandler())
def main(argv): server_config_file = _parseCommandArguments(argv) [static_path, minify_enabled] = configure_server_and_app(server_config_file) print "server_config_file = " + server_config_file print "static_path = " + static_path print "minify_enabled = " + str(minify_enabled) store = RedisStore(redis.StrictRedis()) jso = jsOptimizer(minify_enabled) jso.watch(static_path, store, force=True) try: wm = WatchManager() notifier = Notifier(wm, StaticsChangesProcessor(jso, store)) wm.add_watch(static_path, IN_CREATE | IN_MODIFY | IN_DELETE | IN_MOVED_TO, rec=True) notifier.loop() finally: pass
def __init__(self, wm, processor): self.event_queue = list() self._processor = processor self.notifier = Notifier(wm, NinjaProcessEvent(self.event_queue.append)) self.notifier.coalesce_events(True) self.keep_running = True QThread.__init__(self)
def __init__(self, file, should_read=True, callback=None, timeout=None): super(InoTailer, self).__init__(file, callback, timeout) if self.timeout: self.timeout *= 1000 self._should_read_myself = should_read if not hasattr(self.file, 'fileno') and self._should_read_myself: if not os.path.exists(self.file): raise DoesNotExist("Can't find %s. It does not seem to exist" % self.file) self.file = open(self.file) class Watcher(ProcessEvent): pass if callable(self.callback): setattr(Watcher, 'process_IN_MODIFY', self.callback) mask = IN_MODIFY self.wm = WatchManager() self.notifier = Notifier(self.wm, Watcher()) self.wm.add_watch(self.file.name, mask,) self._initial_lines = None if not self.at_end(): if self._should_read_myself: self._initial_lines = self.file.read().rstrip('\n').split('\n') else: self._initial_lines = True
def __init__(self, watch_manager, path, handler): GLib.Source.__init__(self) self._wm = watch_manager self._handler = handler # set timeout to 0 -> don't wait self._notifier = Notifier(self._wm, handler, timeout=0) self._wdd = self._wm.add_watch(path, handler.MASK, rec=True)
def watch(watch_file): ''' Watch the given file for changes ''' wm = WatchManager() with TailHandler(watch_file) as th: notifier = Notifier(wm, th) wdd = wm.add_watch(watch_file, TailHandler.MASK) notifier.loop() # flush queue before exiting th.publish_queue.publish() print 'Exiting'
def Monitor(path_list): wm = WatchManager() notifier = Notifier(wm, MyProcessEvent()) for path in path_list: print '增加监控路径: %s' % path wm.add_watch(os.path.abspath(path), MASK, rec=True, auto_add=True) try: while 1: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() return
def fsMonitor(path="."): wm = WatchManager() # 创建监控组 mask = IN_CREATE | IN_DELETE | IN_MODIFY wm.add_watch(path, mask, auto_add=True, rec=True) # 将具体路径的监控加入监视组 notifier = Notifier(wm, EventHandler()) # 创建事件处理器,参数为监视组和对应的事件处理函数 print("now starting monitor %s." % path) while True: try: notifier.process_events() # 对事件队列中的事件逐个调用事件处理函数 if notifier.check_events(): # 等待 检查是否有新事件到来 print("check event true") notifier.read_events() # 将新事件读入事件队列 except KeyboardInterrupt: print("keyboard interrupt") notifier.stop() break
def FSMonitor(): back_sql() wm = WatchManager() mask = IN_CREATE | IN_MODIFY notifier = Notifier(wm, PFilePath()) wdd = wm.add_watch(find_path, mask, rec=True) print("now starting monitor %s" % find_path) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
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 loop(self): """Loop until done.""" self.start() try: # inotify interface wm_ = WatchManager() mask = IN_CLOSE_WRITE | IN_MOVED_TO # create notifier notifier = Notifier(wm_, self) # add watches for idir in self.input_dirs: wm_.add_watch(idir, mask) # loop forever notifier.loop() finally: self.stop() self.join()
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(): print "check event true." notifier.read_events() except KeyboardInterrupt: print "keyboard Interrupt." notifier.stop() break
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 "现在开始监视 %s." % path while True: try: notifier.process_events() if notifier.check_events(): print "检查事件是否正确." notifier.read_events() except KeyboardInterrupt: print "键盘中断." notifier.stop() break
def start(actual_directories): wm = WatchManager() flags = EventsCodes.ALL_FLAGS mask = flags['IN_MODIFY'] #| flags['IN_CREATE'] p = PTmp() notifier = Notifier(wm, p) for actual_directory in actual_directories: print "DIRECTORY", actual_directory wdd = wm.add_watch(actual_directory, mask, rec=True) # notifier = Notifier(wm, p, timeout=10) try: print "Waiting for stuff to happen..." notifier.loop() except KeyboardInterrupt: pass return 0
def watch(directory): logger.info("Watching {0}".format(directory)) flags = EventsCodes.ALL_FLAGS mask = flags['IN_CREATE'] | flags['IN_MODIFY'] | flags['IN_DELETE'] wm = WatchManager() wm.add_watch(directory, mask, rec=True) process = IndexProcess(wm, mask) notifier = Notifier(wm, process) def update_index(*args): while process.queue: # This is slightly sub-optimal, would be better to pop all # elements at once but this operation needs to be atomic. dist_dir = process.queue.pop() index(directory, only=[dist_dir]) signal.signal(signal.SIGALRM, update_index) notifier.loop()
def main(): me = os.path.splitext(os.path.basename(sys.argv[0]))[0] lgg = logging.getLogger('cli.' + me) runner = Runner() # Main parser parser = argparse.ArgumentParser(description="""Create test data.""") runner.add_parser_args(parser, which=[('config', True), ('locale', False)]) args = parser.parse_args() runner.init_app(args, lgg=lgg) watcher = Watcher(lgg, runner.in1, runner.out1, runner.in2, runner.out2) wm = WatchManager() notifier = Notifier(wm, watcher) watchdir = os.path.dirname(watcher.in1) lgg.info("Watching " + watchdir) wdd = wm.add_watch(watchdir, EventsCodes.ALL_FLAGS['IN_MODIFY'], rec=False) notifier.loop()
def get_notifier(paths,db_file,flush_interval,verbose,fs_encodings=[]): wm = WatchManager() handler = FileStatEventHandler(wm,db_file,flush_interval,verbose,fs_encodings) notifier = Notifier(wm, handler) for p in paths: if verbose: print ' Monitoring "%s" ... ' % p, wdd = wm.add_watch(p, mask, rec=True,auto_add=False) if verbose: print '[done]' return handler,notifier
class DirtyTracker(object): """Track the changes to (part of) a working tree.""" def __init__(self, tree, subpath='.'): self._tree = tree self._wm = WatchManager() self._process = _Process() self._notifier = Notifier(self._wm, self._process) self._notifier.coalesce_events(True) def check_excluded(p): return tree.is_control_filename(tree.relpath(p)) self._wdd = self._wm.add_watch(tree.abspath(subpath), MASK, rec=True, auto_add=True, exclude_filter=check_excluded) def _process_pending(self): if self._notifier.check_events(timeout=0): self._notifier.read_events() self._notifier.process_events() def __del__(self): self._notifier.stop() def mark_clean(self): """Mark the subtree as not having any changes.""" self._process_pending() self._process.paths.clear() self._process.created.clear() def is_dirty(self): """Check whether there are any changes.""" self._process_pending() return bool(self._process.paths) def paths(self): """Return the paths that have changed.""" self._process_pending() return self._process.paths def relpaths(self): """Return the paths relative to the tree root that changed.""" return set(self._tree.relpath(p) for p in self.paths())
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 monitorDir(): wm = WatchManager() mask = IN_DELETE | IN_CREATE | IN_MODIFY notifier = Notifier(wm, EventHandler()) wm.add_watch(ftpMonitorLocalSyncDir, pyinotify.ALL_EVENTS, rec=True) sleepSeconds = int(execSeconds) if sleepSeconds > 30: sleepSeconds = 30 print('对于监控目录而言,您配置的执行时间 execSeconds %s 过大, 已经自动为您调整成30秒 ' % execSeconds) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() time.sleep(sleepSeconds) except KeyboardInterrupt: notifier.stop() break
def monitor_loop(folders): """ Main loop, create everything needed for the notification and waits for a notification. Loop again when notification happens. """ wm = WatchManager() mask = IN_CLOSE_WRITE | IN_CREATE | IN_DELETE process = Process(wm, mask) notifier = Notifier(wm, process) for folder in folders: wdd = wm.add_watch(folder, mask, rec=True) try: while True: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop()
def Monitor(path): class PClose(ProcessEvent): def process_IN_CLOSE(self, event): f = event.name and os.path.join(event.path, event.name) or event.path print 'close event: ' + f wm = WatchManager() notifier = Notifier(wm, PClose()) wm.add_watch(path, IN_CLOSE_WRITE|IN_CLOSE_NOWRITE) try: while 1: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() return
def __init__(self, directory): self.directory = directory self.file_list = [] self.wm = WatchManager() self.active = False mask = IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO self.tracking = tracker(file_list=self.file_list) self.notifier = Notifier(self.wm, default_proc_fun=self.tracking) watch = self.wm.add_watch(directory, mask, rec=True)
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 Start Monitoring %s.\n" % path) while True: try: notifier.process_events() notifier.read_events() except KeyboardInterrupt: print("Keyboard Interrupt.") notifier.stop() break
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 Monitor(path_list): """主监控程序 """ wm = WatchManager() notifier = Notifier(wm, MyProcessEvent()) for path in path_list: print '增加监控路径: %s' % path wm.add_watch(os.path.abspath(path), MASK, rec=True, auto_add=True) # 递归并自动监控新建的路径 print 'Watching... \nPress Ctrl+C exit' try: while 1: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() return
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 _init_notifier(self): """ Initializes the pyinotify instance. :since: v1.0.0 """ with self._lock: if (self._pyinotify_instance is None): LogLine.debug("{0!r} mode is synchronous", self, context="dpt_vfs") self._pyinotify_instance = Notifier( self, WatcherPyinotifyCallback(self), timeout=5)
def __inotify(self): wm=WatchManager() mask=pyinotify.IN_CLOSE_WRITE class Process_handler(ProcessEvent): def __init__(self,main): self.main=main def process_IN_CLOSE_WRITE(self,event): for user in pwd.getpwall(): if user.pw_name == event.name: f=open(event.path+"/"+event.name) ticket=f.readlines()[0] f.close() self.main.tickets[user.pw_name]={} self.main.tickets[user.pw_name]["password"]=ticket self.main.tickets[user.pw_name]["date"]=time.time() break #self.generate_pam() notifier=Notifier(wm,Process_handler(self)) wdd=wm.add_watch(TicketsManager.WATCH_DIR,mask,rec=True) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except Exception as e: print(e) notifier.stop() return False
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(cls): wm = WatchManager() mask = pyinotify.IN_CLOSE_WRITE notifier = Notifier(wm, InotifyEventHandler()) wm.add_watch(os.path.dirname(cls.__configure_file_path), mask, auto_add=True, rec=True) # print("now starting monitor config directory." ) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: print("keyboard Interrupt.") notifier.stop() break
def FsMonitor(path='.'): global tempPath tempPath = os.path.join(path, "../BCFILES") if os.path.exists(tempPath): removeDir(tempPath) os.mkdir(tempPath) wm = WatchManager() mask = IN_DELETE | IN_CREATE | IN_MODIFY notifier = Notifier(wm, EventHandler()) wm.add_watch(path, mask, auto_add=True, rec=True) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
def _reloader_inotify(extra_files=None, interval=None): # Mutated by inotify loop when changes occur. changed = [False] # Setup inotify watches from pyinotify import WatchManager, Notifier # this API changed at one point, support both try: from pyinotify import EventsCodes as ec ec.IN_ATTRIB except (ImportError, AttributeError): import pyinotify as ec wm = WatchManager() mask = ec.IN_DELETE_SELF | ec.IN_MOVE_SELF | ec.IN_MODIFY | ec.IN_ATTRIB def signal_changed(event): if changed[0]: return _log('info', ' * Detected change in %r, reloading' % event.path) changed[:] = [True] for fname in extra_files or (): wm.add_watch(fname, mask, signal_changed) # ... And now we wait... notif = Notifier(wm) try: while not changed[0]: # always reiterate through sys.modules, adding them for fname in _iter_module_files(): wm.add_watch(fname, mask, signal_changed) 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 monitor(path, command_list): wm = WatchManager() """ IN_DELETE | IN_CREATE | IN_MODIFY | 监听不到ansible copy的事件? 试一试 IN_MOVED_TO? """ mask = IN_DELETE | IN_CREATE | IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO handler = EventHandler(logger, command_list=command_list, directory=path) notifier = Notifier(wm, handler) handler.start() wm.add_watch(path, mask, auto_add=True, rec=True) logger.info("now start monitor %s" % path) while 1: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() handler.stop() break