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 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()
class InoTailer(BaseTailer): 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 at_end(self): return self.file.tell() == os.path.getsize(self.file.name) def check_once(self): if self._initial_lines: self._initial_lines, result = None, self._initial_lines return result self.notifier.process_events() if self.notifier.check_events(self.timeout): self.notifier.read_events() if self._should_read_myself: result = self.file.read().rstrip('\n').split('\n') return result return True return None def wait(self, with_timeout=False): timeout = None if with_timeout: timeout = self.timeout self.notifier.check_events(timeout)
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 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 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 _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)
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 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()
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()
class Watcher(object): def __init__(self, incoming_path, logger=None): self.incoming_path = incoming_path self.logger = logger or logging.getLogger('Watcher') self.wm = WatchManager() mask = IN_CLOSE_WRITE # watched events self.event_processor = WatcherProcesEvent(logger=self.logger) self.notifier = Notifier(self.wm, self.event_processor) wdd = self.wm.add_watch(self.incoming_path, mask, rec=True) def loop(self): """ Check for inotify events and process then when found. Note, check_events() below is blocking and the loop will not complete until an event that is being listened for occurs. """ if self.notifier.check_events(): # read notified events and enqeue them self.notifier.read_events() # process the queue of events self.notifier.process_events() def stop(self): # destroy the inotify's instance on this interrupt (stop monitoring) self.notifier.stop()
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 AutoLibraryUpdate(EventPlugin): PLUGIN_ID = "Automatic library update" PLUGIN_NAME = _("Automatic Library Update") PLUGIN_DESC = _("Keeps your library up to date with inotify. " "Requires %s.") % "pyinotify" # TODO: make a config option USE_THREADS = True event_handler = None running = False 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 unthreaded_callback(self): """Processes as much of the inotify events as allowed""" assert self.notifier._timeout is not None, \ 'Notifier must be constructed with a [short] timeout' self.notifier.process_events() # loop in case more events appear while we are processing while self.notifier.check_events(): self.notifier.read_events() self.notifier.process_events() return True # disable hook, stop the notifier: def disabled(self): if self.running: self.running = False if self.notifier: print_d("Stopping inotify watch...") self.notifier.stop()
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 Monitor(path): notifier = Notifier(wm, PTmp()) wdd = wm.add_watch(path, mask, rec=True) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
def Monitor(path): notifier = Notifier(wm, PTmp()) wdd = wm.add_watch(path, mask, rec=True) newDevice.put('/dev/waggle_coresense') while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
def newDev(path): notifier = Notifier(wm, PTmpCreate()) wdd = wm.add_watch(path, maskCreate, rec=True) while True: # loop forever time.sleep(0.1) try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
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
def inotify_watch(): wm = WatchManager() mask = EventsCodes.ALL_FLAGS['IN_CLOSE_WRITE'] wdd = wm.add_watch('src', mask, rec=True) notifier = Notifier(wm, PTmp()) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
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()
class INOTIFYGlibSource(GLib.Source): #pylint: disable=W0613 def __new__(cls, watch_manager, path, handler): # ignore the rest of arguments return GLib.Source.__new__(cls) 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 get_handler(self): return self._handler #pylint: disable=W0613 def prepare(self, *args): # wait 10 milisecond before next prepare() call return (self._notifier.check_events(), 10) #pylint: disable=W0613 def check(self, *args): # just to be sure return self._notifier.check_events() #pylint: disable=W0613 def dispatch(self, *args): self._notifier.read_events() self._notifier.process_events() return True #pylint: disable=W0613 def finalize(self, *args): self._notifier.stop()
def run(self): for i in self.path: self.init_watch(i) print('开始监控 %s' % (i)) notifier = Notifier(self.wm, EventHandler()) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
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
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 run(self): self.send_from_outbox() wm = WatchManager() wm.add_watch(self.outbox_path.__str__(), IN_CREATE) notifier = Notifier(wm, MailerEventHandler(self.send_from_outbox)) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
def run(self): logger.info( '['+self.name+'] watching: '+ self.spath ) wm = WatchManager() mask = EventsCodes.OP_FLAGS["IN_DELETE"] | EventsCodes.OP_FLAGS["IN_CREATE"] |EventsCodes.OP_FLAGS["IN_MODIFY"] | EventsCodes.OP_FLAGS["IN_MOVED_TO"] | EventsCodes.OP_FLAGS["IN_MOVED_FROM"] notifier = Notifier(wm, WorkEvent("WEVNT",self.workqueue)) wm.add_watch(self.spath, mask,rec=True) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
def run(self): sys.stdout.write('Daemon started with pid %d\n' % os.getpid()) wm = WatchManager() mask = IN_CREATE | IN_MODIFY notifier = Notifier(wm, EventHandler()) wdd = wm.add_watch(FSpath, mask, rec=True) print 'now starting monitor %s' % (FSpath) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
def run (self): global watchQueue toWatch = watchQueue.get() if toWatch : notifier = Notifier(wm, PTmpCreate()) wdd = wm.add_watch(toWatch, maskCreate, rec=True) while True: # loop forever time.sleep(1) try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
def run(self): notifier = Notifier(self.watchmanager, self.event) descriptor = self.watchmanager.add_watch( self.folder, EventsCodes.ALL_FLAGS['ALL_EVENTS'], rec=self.recursive) logging.info('Watching %i folders', len(descriptor)) if self.first_run: self.event.run_cmd(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 main(): upload_queue = Queue.Queue() thread.start_new_thread(upload_thread, (upload_queue,)) # # Setup a watcher on the images folder. # wm = WatchManager() notifier = Notifier(wm, PTmp(upload_queue)) wm.add_watch(BASE_FOLDER, EventsCodes.OP_FLAGS['IN_CREATE'], rec=True) # # loop forever # while True: try: # # process the queue of events # notifier.process_events() if notifier.check_events(): # # read notified events and enqeue them # notifier.read_events() except KeyboardInterrupt: # # destroy the inotify's instance on this # interrupt (stop monitoring) # notifier.stop() # # Stop the upload queue. # upload_queue.put((None, None)) break
def FSMonitor(path='.'): wm = WatchManager() mask = IN_CREATE | IN_MODIFY | IN_MOVED_TO notifier = Notifier(wm, EventHandler()) path = join(_env.PREFIX, 'coffee') for i in os.listdir(path): p = join(path, i) if not isdir(p): continue p = link_path(p) wm.add_watch(p, mask, rec=True) wm.add_watch(path, mask, rec=True) print '开始实时编译 COFFEE SCRIPT %s' % (path) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
def Monitor(path): class PCreate(ProcessEvent): def process_IN_CREATE(self, event): f = event.name and os.path.join(event.path, event.name) or event.path # remove path element from entity item = f[len(path):] # start a socat instance socat_cmd = "socat " socat_cmd += path socat_cmd += item socat_cmd += " pty:,link=" socat_cmd += ptypath socat_cmd += item socat_cmd += " &" os.system(socat_cmd) # load in a screen session screen_cmd = "screen -S vmware -X eval 'screen -t " screen_cmd += item screen_cmd += " -L " screen_cmd += ptypath screen_cmd += item screen_cmd += " 9600'" os.system(screen_cmd) wm = WatchManager() notifier = Notifier(wm, PCreate()) wm.add_watch(path, pyinotify.IN_CREATE) try: while 1: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() return
class StatusMonitor(threading.Thread): """ Thread which runs in the background and updates the interface based on pyinotify. Linux Kernel 2.6.13+ are required!""" def __init__(self): super(StatusMonitor, self).__init__() self.quit = False self.wm = WatchManager() # watched events self.mask = 0x00000100 | 0x00000200 self.notifier = Notifier(self.wm, PXampp()) self._lampp = main_window.get_xampp_path() wdd = self.wm.add_watch( [ os.path.join(self._lampp, 'logs/'), #httpd.pid os.path.join(self._lampp, 'var/mysql/'), #hostname.pid os.path.join(self._lampp, 'var/') ], #proftpd.pid self.mask) def run(self): while not self.quit: try: # process the queue of events self.notifier.process_events() if self.notifier.check_events(): # read notified events and enqeue them self.notifier.read_events() # you can do some tasks here... except KeyboardInterrupt: print 'stop monitoring...' # destroy the inotify's instance on this interrupt (stop monitoring) self.notifier.stop() except Exception, err: # otherwise keep on looping pass print 'stop monitoring...' # destroy the inotify's instance on this interrupt (stop monitoring) self.notifier.stop()
def FSMonitor(path='.', deploy=None): """ Monitor the path-folder,when there is a new file generated,the IN_Create event will be triggered :param path:the path to be monitored :param deploy:the object if gru-model :return: """ wm = WatchManager() mask = IN_CREATE if deploy is None: logging.error('Model Uninitial!') return notifier = Notifier(wm, EventHandler(predict=deploy)) 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 start(conf): global_conf = conf wm = WatchManager() mask = EventsCodes.ALL_FLAGS['IN_CREATE'] | EventsCodes.ALL_FLAGS[ 'IN_MOVED_TO'] notifier = Notifier(wm, PTmp()) for watch_dir in conf['watch_directories']: watch_dir = os.path.expanduser(watch_dir) print("Now watching %s" % watch_dir) wm.add_watch(watch_dir, mask, rec=True) while True: try: notifier.process_events() if (notifier.check_events()): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
def wait_for_vm_boot(path, timeout=300): wait_for_vm_boot.ks_read = False def proc(_): wait_for_vm_boot.ks_read = True wm = WatchManager() notifier = Notifier(wm, timeout=1000) wm.add_watch(path, mask=IN_CLOSE_NOWRITE, rec=False, proc_fun=proc) start_time = int(time.time()) notifier.process_events() while not wait_for_vm_boot.ks_read: if int(time.time()) - start_time > timeout: break if notifier.check_events( ): #loop in case more events appear while we are processing notifier.read_events() notifier.process_events() else: return True print 'Timeout {} sec occured while waiting for Master Node reboot'.format( timeout)
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 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 _reloader_inotify(fnames, interval=None): #: Mutated by inotify loop when changes occur. changed = [False] # Setup inotify watches import pyinotify from pyinotify import WatchManager, EventsCodes, Notifier wm = WatchManager() mask = "IN_DELETE_SELF IN_MOVE_SELF IN_MODIFY IN_ATTRIB".split() try: # pyinotify 0.7 mask = reduce(lambda m, a: m | getattr(EventsCodes, a), mask, 0) except AttributeError: # pyinotify 0.8 and up mask = reduce(lambda m, a: m | getattr(pyinotify, 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)
class FSMonitor: 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 add_watch(self,path): mask = IN_MODIFY fileModifiedTime[path] = Queue(11); self.wm.add_watch(path, mask, auto_add=True, rec=True) def run(self): print 'now starting monitor' while True: try: self.notifier.process_events() if self.notifier.check_events(): self.notifier.read_events() except KeyboardInterrupt: self.notifier.stop() break
class LinuxFileSysMonitor(UFSMonitor): """File system monitor thread""" def __init__(self, name=None): super(LinuxFileSysMonitor, self).__init__(name) self.defaultMask = IN_DELETE | IN_CREATE | IN_MODIFY | IN_MOVED_TO | IN_MOVED_FROM self.wm = WatchManager() self.__lock = threading.Lock() def addWatch(self, path, mask=0): """Add watch for path""" if mask == 0: mask = self.defaultMask super(LinuxFileSysMonitor, self).addWatch(path, mask) self.wm.add_watch(path, mask, auto_add=True, rec=True) def rmWatch(self, path): """Remove watch for path""" super(LinuxFileSysMonitor, self).rmWatch(path) wd = self.wm.get_wd(path) self.wm.rm_watch(wd, rec=True) def run(self): """Thread entry""" super(LinuxFileSysMonitor, self).run() self.notifier = Notifier(self.wm, EventHandler(None, fsMonitor=self)) while self.isRunning: self.processMsg(1) if self.notifier.check_events(1000): self.notifier.read_events() self.notifier.process_events() def stop(self): """Stop watch""" super(LinuxFileSysMonitor, self).stop() self.notifier.stop()
def process_IN_CLOSE_WRITE(self, event): target = os.path.join(event.path, event.name) if self.regex.match(target): args = self.script.replace('$f', target).split() os.system("clear") sys.stdout.write("executing script: " + " ".join(args) + "\n") subprocess.call(args) sys.stdout.write("------------------------\n") 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: print '.' notifier.process_events() print '+' if notifier.check_events(): notifier.read_events() print '-' except Reload: pass except KeyboardInterrupt: notifier.stop() break
class Daemon: __monitorData = [] __monitorPaths = [] __timer = None __notifier = None __wm = None __watchPaths = None __pidfile = None __interval = 2*60 def __init__(self,pidfile): self.__monitorData = [] self.__monitorPaths = [] self.__timer = None self.__notifier = None self.__wm = None self.__watchPaths = None self.__pidfile = pidfile self.__interval = 2*60 # def __del__(self): # self.__cleanMonitorData() # if self.__notifier: # self.__notifier.stop() # if self.__timer: # self.__timer.cancel() # 守护进程 def __daemonize(self): try: pid = os.fork() # 第一次fork,生成子进程,脱离父进程 if pid > 0: sys.exit(0) # 退出主进程 except OSError as e: logger.error('fork #1 failed: %d (%s)\n' % (e.errno, e.strerror)) sys.exit(1) try: os.chdir("/") # 修改工作目录 os.setsid() # 设置新的会话连接 os.umask(0) # 重新设置文件创建权限 pid = os.fork() # 第二次fork,禁止进程打开终端 if pid > 0: sys.exit(0) except OSError as e: logger.error('fork #2 failed: %d (%s)\n' % (e.errno, e.strerror)) sys.exit(1) try: atexit.register(self.__delete_pidfile) logger.info("save daemon process pid to file") pid = str(os.getpid()) fo = open(self.__pidfile,'w+') fo.write('%s\n'%pid) fo.close() except Exception as e: logger.error("save daemon pid failed:{}".format(str(e))) sys.exit(1) def __delete_pidfile(self): try: if os.path.exists(self.__pidfile): logger.info("******delete daemon pid file*****") os.remove(self.__pidfile) except Exception as e: logger.error("delete daemon pid file failed:{}".format(str(e))) sys.exit(1) # 启动 def start(self): # 先判断是否已经启动了 try: fo = open(self.__pidfile,'r') pid = int(fo.read().strip()) fo.close() except Exception as e: pid = None if pid: logger.error("pidfile already exist.Daemon monitor already running?") sys.exit(1) # 启动监控 self.__daemonize() self.__run() # 停止 def stop(self): try: fo = open(self.__pidfile,'r') pid = int(fo.read().strip()) fo.close() except Exception as e: logger.error("get daemon pid file failed:{}".format(str(e))) pid = None if not pid: logger.error("pid file does not exist. not running?") # return logger.debug("read pid from pid file:{}".format(str(pid))) logger.info("stop daemon monitor process") # 杀进程,循环删除 try: if pid: while 1: os.kill(pid, SIGTERM) time.sleep(0.1) except OSError as err: err = str(err) if err.find('No such process') > 0: logger.error("no such process") if os.path.exists(self.__pidfile): os.remove(self.__pidfile) else: logger.error(str(err)) # sys.exit(1) try: for p in psutil.process_iter(attrs=["cmdline"]): cmdlines = p.info["cmdline"] if len(cmdlines) == 3 and (cmdlines[2] == "start" or cmdlines[2] == "restart"): p.kill() except Exception as e: logger.error("kill process failedc:{}".format(str(e))) def restart(self): self.stop() self.start() def getStatus(self): try: fo = open(self.__pidfile, 'r') pid = int(fo.read().strip()) fo.close() except Exception as e: pid = None try: process = [] for p in psutil.process_iter(attrs=["cmdline"]): cmdlines = p.info["cmdline"] if len(cmdlines) == 3 and (cmdlines[2] == "start" or cmdlines[2]== "restart"): pyPath = cmdlines[1] index = pyPath.rfind("__init__.py") if index == -1: continue if len(pyPath) - len("__init__.py") == index: process.append(p.pid) except Exception as e: process = [] if pid: if len(process) == 0: return "error" elif len(process) > 1: return "error" elif len(process) == 1: process_pid = process[0] if process_pid == pid: return "start" else: return "error" else: if len(process) == 0: return "stop" else: return "error" # if not pid: # return "stop" def __run(self): try: logger.info("pid is {}".format(os.getpid())) self.__timer = threading.Timer(1, self.monitor_models) self.__timer.start() except Exception as e: logger.error("start monitor failed:{}".format(str(e))) sys.exit(1) # 刷新前清理数据 def __cleanMonitorData(self): try: self.__monitorData = [] self.__monitorPaths = [] if connection: connection.close() except Exception as e: logger.error("clean monitor data failed:{}".format(str(e))) # 监听所有模型 def monitor_models(self): try: now = datetime.datetime.utcnow() - datetime.timedelta(hours=16) logger.info("refresh monitor info from database :{}".format(now.strftime("%Y-%m-%d %H:%M:%S"))) self.__cleanMonitorData() models = Model.objects.all() except Exception as e: info = "get models failed : {}".format(str(e)) logger.error(info) sys.exit(1) try: for model in models: text = model.text obj = json.loads(text) if not "monitor" in obj: continue user_id = model.user.uuid user_root = os.path.join(setting.UPLOADS_ROOT,str(user_id)) monitor = obj["monitor"] status = monitor["status"] if status == "on": data = monitor["data"] for d in data: d_path = d["path"] d["relative_path"] = d_path path = os.path.join(user_root,d_path[1:]) d["path"] = path self.__add_monitor_folder_path(path,str(model.uuid)) self.__monitorData.append({ "id": str(model.uuid), "data": data }) except Exception as e: logger.error("get monitor info failed:{}".format(str(e))) return try: mask = IN_CLOSE_WRITE paths = [] if not self.__wm: wm = WatchManager() self.__wm = wm self.__notifier = Notifier(self.__wm, EventHandler()) for monitorPath in self.__monitorPaths: path = monitorPath["path"] logger.info('now starting monitor %s' % (path)) paths.append(path) self.__watchPaths = self.__wm.add_watch(paths, mask, rec=False) connection.close() self.__timer = threading.Timer(self.__interval, self.monitor_models) self.__timer.start() while True: try: self.__notifier.process_events() if self.__notifier.check_events(): self.__notifier.read_events() except KeyboardInterrupt: self.__notifier.stop() break else: # 第二次访问,清理监听文件夹,新加入监听文件夹 rm_paths = [] for value in self.__watchPaths.values(): rm_paths.append(value) self.__wm.rm_watch(rm_paths) for monitorPath in self.__monitorPaths: path = monitorPath["path"] logger.info('now starting monitor %s' % (path)) paths.append(path) self.__watchPaths = self.__wm.add_watch(paths, mask, rec=False) connection.close() self.__timer = threading.Timer(self.__interval, self.monitor_models) self.__timer.start() except Exception as e: logger.error("run monitor failed:{}".format(str(e))) # 加入监听文件夹 def __add_monitor_folder_path(self,path,model_id): try: for p in self.__monitorPaths: models = p["models"]; if p["path"] == path: if not model_id in models : models.append(model_id) return self.__monitorPaths.append({ "path":path, "models":[model_id] }) except Exception as e: logger.error("add monitor path failed:{}".format(str(e))) # 上传文件后的判断 def verify(self,path,name): try: logger.debug("***********begin verify new file***********") for monitorPath in self.__monitorPaths: m_path = monitorPath["path"] if os.path.normpath(m_path) == os.path.normpath(path): for model in monitorPath["models"]: self.__verify_model(path,name,model) logger.debug("***********stop verify new file***********") except Exception as e: logger.error("verify file failed:{}".format(str(e))) # 根据模型id来获取监听信息 def __get_monitor_data(self,model_id): try: for monitorData in self.__monitorData: id = monitorData["id"] if id == model_id: return monitorData["data"] except Exception as e: logger.error("get monitor data failed:{}".format(str(e))) return None # 验证一个模型 def __verify_model(self,path,name,model_id): try: monitorData = self.__get_monitor_data(model_id) if not monitorData: return new_data = [] logger.info("new file[{}] match model[{}] path".format(name,model_id)) for data in monitorData: d_path = data["path"] d_prefix = data["prefix"] if os.path.normpath(d_path) == os.path.normpath(path): result = re.match(r"^" + d_prefix + "", name) if result == None: # 路径一致,但是前缀不符合 logger.info("new file[{}] does not matched prefix[{}]".format(name,d_prefix)) continue logger.info("new file[{}] match [{}]".format(name, d_prefix)) # 前缀与文件类型之间的名称 pos_1 = name.index(".") prefix_length = len(d_prefix) new_file_name = name[prefix_length:pos_1] data["new_path"] = os.path.join(d_path, name) data["new_file_name"] = new_file_name data["name"] = name # 数据有效 new_data.append(data) if len(new_data) == 0: logger.info("new file[{}] does not match any node".format(name)) return #单一输入 if len(new_data) == len(monitorData): createTask(model_id,new_data) return # 验证通过的数据 new_data_id = new_data[0]["id"] new_file_name = new_data[0]["new_file_name"] # 多个输入时考虑,去监听数据中根据路径查找名称相同的 for data in monitorData: d_path = data["path"] d_id = data["id"] if d_id == new_data_id: continue d_prefix = data["prefix"] prefix_length = len(d_prefix) flag = False for filename in os.listdir(d_path): fp = os.path.join(d_path, filename) if os.path.isfile(fp): result = re.match(r"^" + d_prefix + "",filename) if result == None: continue pos_1 = filename.index(".") middle_name = filename[prefix_length:pos_1] if middle_name == new_file_name: flag = True data["new_path"] = fp data["new_file_name"] = middle_name data["name"] = filename new_data.append(data) break if not flag: # 在另一个输入里面没有形同名称的文件,直接退出 logger.info("not found match file") return if len(new_data) == len(monitorData): createTask(model_id,new_data) return return except Exception as e: logger.error("verify model[{}] failed:{}".format(model_id,str(e))) return
class VdsmWatcher(WatchManager): """ Implements watch manager """ MASK = Codes.value('IN_DELETE') | Codes.value('IN_CREATE') def __init__(self, path_to_vdsm, path_to_config, service_name): """ C'tor * path_to_vdsm - path to vdsm bin file * path_to_config - path to code coverage config * service_name - name of vdsm service """ super(VdsmWatcher, self).__init__() self.config = path_to_config self.service_name = service_name self.notifier = Notifier(self, Handler(self)) self.path = path_to_vdsm.split(os.sep) self.wdd = {} @property def target_file(self): """ vdsm bin file """ return '/' + os.path.join(*self.path) def get_mask(self, path): """ returns mask for specific file """ if os.path.isfile(path): return self.MASK | Codes.value('IN_MODIFY') # return self.MASK | Codes.value('IN_CLOSE_WRITE') return self.MASK def add_watch(self, path): """ Overrides add_watch method to work with path only """ mask = self.get_mask(path) logger.info("Adding watch for %s, mask %s", path, mask) wdd = super(VdsmWatcher, self).add_watch(path, mask) for dir_, wd in wdd.items(): if wd >= 0: self.wdd[dir_] = wd def add(self): """ Adds watchers to all available nodes on path """ path = '/' for part in self.path: path = os.path.join(path, part) if not os.path.exists(path): break self.add_watch(path) def rm(self): """ Removes watchers on all available nodes on path """ for path in self.wdd.keys(): self.rm_watch(path) def rm_watch(self, path): """ Overrides rm_watch method to work with path only """ logger.info("Removing watch for %s", path) super(VdsmWatcher, self).rm_watch(self.wdd[path]) def run(self): """ Live cycle of watch manager """ self.add() try: self.enable_coverage(self.target_file) while True: self.notifier.process_events() if self.notifier.check_events(): self.notifier.read_events() finally: self.rm() self.notifier.stop() self.disable_coverage(self.target_file) def exec_cmd(self, cmd): """ Executes local command, and return RC """ p = Popen(cmd, stdout=PIPE, stderr=PIPE) out, err = p.communicate() rc = p.returncode logger.info("Excecuted %s", cmd) logger.info("stdout %s", out) logger.info("stderr %s", err) logger.info("rc %s", rc) return rc def restart_service(self): """ Restart service in order to take effect of instrumentation """ logger.info("vdsm was restarted") cmd = ['service', self.service_name, 'restart'] self.exec_cmd(cmd) def enable_coverage(self, path): """ Tries to instrument vdsm bin file """ if not os.path.exists(path): return logger.info("Instrument path: %s", path) cmd = ['grep', 'vdsmcodecoverage', path] rc = self.exec_cmd(cmd) if not rc: cmd = ['grep', self.config, path] rc = self.exec_cmd(cmd) if rc: cmd = [ 'sed', '-i', 's|instrument([^)]*)|instrument("%s")|g' % self.config, path ] rc = self.exec_cmd(cmd) else: return else: cmd = 'from vdsmcodecoverage.hook import instrument;'\ 'instrument("%s")' % self.config cmd = ['sed', '-i', '2 i \%s' % cmd, path] rc = self.exec_cmd(cmd) if rc: logger.error("failed to instrument vdsm") else: self.restart_service() def disable_coverage(self, path): """ Tries to un-instrument vdsm bin file """ if not os.path.exists(path): return cmd = ['sed', '-i', '/vdsmcodecoverage/ d', path] self.exec_cmd(cmd) self.restart_service()
class FileMonitor(threading.Thread): def __init__(self, listener): threading.Thread.__init__(self) self.__p = Processor(self, listener) self.manager = WatchManager() self.notifier = Notifier(self.manager, self.__p) self.event = threading.Event() self.setDaemon(True) self.watches = [] self.__isSuspended = False def suspend(self): self.__isSuspended = True def unsuspend(self): t = threading.Thread(target=self.__unsuspend) t.start() def __unsuspend(self): time.sleep(1.5) self.__isSuspended = False for watch in self.watches: if not os.path.exists(watch): _logger.debug("Removed stale watch on %s", watch) self.watches.remove(watch) def is_suspended(self): return self.__isSuspended def has_watch(self, path): return path in self.watches def add_watch(self, path): _logger.debug("Adding watch for %s", path) self.manager.add_watch(path, MASK, self.__p) self.watches.append(path) def remove_watch(self, path): _logger.debug("Removing watch for %s", path) wd = self.manager.get_wd(path) self.manager.rm_watch(wd, True) self.watches.remove(path) for i in range(len(self.watches)): try: if self.watches[i].startswith(path): self.watches.remove(self.watches[i]) except IndexError: break def run(self): while not self.event.isSet(): self.notifier.process_events() if self.notifier.check_events(1000): self.notifier.read_events() _logger.info("Shutting down file monitor") self.notifier.stop() def stop(self): self.event.set() self.join()
def Monitor(path): class PClose_Write(ProcessEvent): def process_IN_CLOSE_WRITE(self, event): modified_path = str(event.pathname) file_name = [] for i in reversed(modified_path): if i == '/': break file_name.append(i) file_name = ''.join(file_name) reversed_string = '' for i in range(len(file_name) - 1, -1, -1): reversed_string += file_name[i] file_name = reversed_string if "sass" in file_name: print '\nSASS--File:', file_name, 'modified', '- Calling rapydcss on: ' + event.path + '/' + file_name + '\n' subprocess.call('rapydcss ' + event.path + '/' + file_name, shell=True) elif "pyj" in file_name: file_name_js = '' for i in file_name: if i == '.': break file_name_js += i file_name_js = file_name_js + '.js' print '\nPYJ--File:', file_name, 'modified', '- Calling rapydscript on: ' + event.path + '/' + file_name + '\n' subprocess.call('rapydscript ' + event.path + '/' + file_name \ + ' -o ' + event.path + '/' + file_name_js, shell=True) else: print 'IGNORED--File:', file_name, 'modified. This file type is not accounted for. No action taken \n' sub_dirs = [] if monitor_all == '-r': sub_dirs = [ sub_dir for sub_dir in os.listdir(path) if os.path.isdir(os.path.join(path, sub_dir)) ] elif monitor_all != None: print '\nInvalid command. You typed: `' + monitor_all + '` Did you mean `-r`? Use `-r` to force BoS to watch for changes in all sub directories of ' + path sys.exit() wm = WatchManager() notifier = Notifier(wm, PClose_Write()) wm.add_watch(path, pyinotify.IN_CLOSE_WRITE) if monitor_all == '-r': for sub_dir in sub_dirs: wm.add_watch(path + '/' + sub_dir, pyinotify.IN_CLOSE_WRITE) print '\n---------------------------' print 'BoS is locked and loaded!' print 'Waiting for movement in:', path if monitor_all == '-r': print '\nWith sub directories: ' for sub_dir in sub_dirs: print '\n', sub_dir print '---------------------------' try: while 1: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: print '\n---------------------------' print 'Exiting BoS...' print '---------------------------' notifier.stop() return
def recon_view(pnet, rnet, onet, model, image_path, ip, margin=44, image_size=160): print('begin recon_view!!') with tf.Graph().as_default(): config = tf.ConfigProto() with tf.Session(config=config) as sess: # Load the model facenet.load_model(model) # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") # embeddings:输入端的特征向量 embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") # 加载库中的人脸并映射成embdings print('加载人脸库。。。。。。') embding, images_label_list = face_lib.get_face_lib() print('读取完毕.....') path_exp = os.path.expanduser(image_path) classes = os.listdir(path_exp) if not len(classes) == 0: classes.sort() nrof_classes = len(classes) for i in range(nrof_classes): images_path = os.path.join(path_exp, classes[i]) # get_face(sess, images_path, margin, image_size, images_placeholder, embeddings, # phase_train_placeholder, embding, images_label_list, pnet, rnet, onet) if os.path.isfile(images_path): get_video(sess, images_path, margin, image_size, images_placeholder, embeddings, phase_train_placeholder, embding, images_label_list, pnet, rnet, onet, ip) os.remove(images_path) wm = WatchManager() mask = IN_DELETE | IN_CREATE | IN_MODIFY | IN_CLOSE_WRITE notifier = Notifier( wm, EventHandler(sess, image_path, margin, image_size, images_placeholder, embeddings, phase_train_placeholder, embding, images_label_list, pnet, rnet, onet, ip)) wm.add_watch(image_path, mask, auto_add=True, rec=True) print('Please input video') while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break
threadName.exit() time.sleep(delay) print("%s:%s" % (threadName, time.ctime(time.time()))) counter -= 1 if __name__ == '__main__': notifier = Notifier(wm, EventHandler()) wdd = wm.add_watch('realtimedata/joy', mask, rec=True, auto_add=True) myThread2(1, "realtime", 1).start() #s = pyinotify.Stats() while True: x = EventHandler() try: notifier.process_events() if (notifier.check_events()): notifier.read_events() #repr(s) #print("s") #print(s) #print("x.path = " + x.path) if (x.path.split("/")[-1][0] != '.'): thread = myThread(1, "Thread" + x.path, 1) thread.start() #thread.join() except (KeyboardInterrupt): notifier.stop() break
def __init__(self,fs): self.hash = {} for f in fs: self.hash.setdefault(f,self.hash_file(f)) self.iptables = self.hash_iptables() def hash_iptables(self): return subprocess.Popen("iptables -nL|md5sum|awk '{print $1}'",stdout=subprocess.PIPE,shell=True).stdout.readline().strip() def hash_file(self,f): if os.path.exists(f): return hashlib.md5(open(f).read()).hexdigest() else: print '[ERROR] 监控文件不存在:%s'%f def check(self): ret = [ ] tmp_hash = None for f in self.hash.keys(): tmp_hash = self.hash_file(f) ret.append(tmp_hash) tmp_hash = self.hash_iptables() return (self.hash.keys(),ret) wm = WatchManager() wm.add_watch(m_file,mask) notifier = Notifier(wm,FSMonitor()) notifier.process_events() if notifier.check_events(600): notifier.read_events()
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('waiting for new file in %s to reconstruct' % sys.argv[1]) 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(): print('waiting for new file in %s to process' % sys.argv[1]) notifier.read_events() except Reload: pass except KeyboardInterrupt: notifier.stop() break
class AutoLibraryUpdate(EventPlugin): PLUGIN_ID = "Automatic library update" PLUGIN_NAME = _("Automatic Library Update") PLUGIN_DESC = _("Keeps your library up to date with inotify. " "Requires %s.") % "pyinotify" PLUGIN_ICON = Icons.VIEW_REFRESH # TODO: make a config option USE_THREADS = True event_handler = None running = 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 unthreaded_callback(self): """Processes as much of the inotify events as allowed""" assert self.notifier._timeout is not None, \ 'Notifier must be constructed with a [short] timeout' self.notifier.process_events() # loop in case more events appear while we are processing while self.notifier.check_events(): self.notifier.read_events() self.notifier.process_events() return True # disable hook, stop the notifier: def disabled(self): if self.running: self.running = False if self.notifier: print_d("Stopping inotify watch...") self.notifier.stop()
class AutoLibraryUpdate(EventPlugin): PLUGIN_ID = "Automatic library update" PLUGIN_NAME = _("Automatic Library Update") PLUGIN_DESC = _("Keeps your library up to date with inotify. " "Requires %s.") % "pyinotify" # TODO: make a config option USE_THREADS = True event_handler = None running = False 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 unthreaded_callback(self): """Processes as much of the inotify events as allowed""" assert self.notifier._timeout is not None, \ 'Notifier must be constructed with a [short] timeout' self.notifier.process_events() # loop in case more events appear while we are processing while self.notifier.check_events(): self.notifier.read_events() self.notifier.process_events() return True # disable hook, stop the notifier: def disabled(self): if self.running: self.running = False if self.notifier: print_d("Stopping inotify watch...") self.notifier.stop()
def run(self): #print dir(pyinotify.EventsCodes) wm = WatchManager() #mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE # watched events #mask = pyinotify.IN_CREATE # watched events #mask = pyinotify.IN_CLOSE_WRITE|pyinotify.ALL_EVENTS # watched events mask = pyinotify.IN_CLOSE_WRITE | pyinotify.IN_MOVE_SELF | pyinotify.IN_MOVED_TO class PTmp(ProcessEvent): def __init__(self, context): self.context = context self.tx = self.context.getTx() pass def load_pkt(self, pktf): if (pktf[-3:] == "pkt"): pktd = open(pktf, "r").read() pktdj = json.loads(pktd) pkt_len = len(pktdj) stag_p = pktdj[pkt_len - 1]["stag"].split(":") if len(stag_p) < 3: group = "public" type = stag_p[0] tag = stag_p[1] else: group = stag_p[0] type = stag_p[1] tag = stag_p[2] self.context.getTx().put((group, tag, type, pktdj)) def process_IN_MOVE_SELF(self, event): pktf = os.path.join(event.path, event.name) logging.info("MOVE SELF " + pktf) try: self.load_pkt(pktf) except Exception as e: logging.error("Loading paket: " + pktf) def process_IN_MOVED_TO(self, event): pktf = os.path.join(event.path, event.name) logging.info("MOVE TO " + pktf) try: self.load_pkt(pktf) except Exception as e: logging.error("Loading paket: " + pktf) def process_IN_CLOSE_WRITE(self, event): try: pktf = os.path.join(event.path, event.name) logging.info("INOTIFY CLOSE_WRITE 2: " + str(pktf)) self.load_pkt(pktf) except Exception as e: logging.error("Loading paket: " + pktf) def process_IN_DELETE(self, event): print logging.debug("Remove: %s" % os.path.join(event.path, event.name)) class Seedmp(ProcessEvent): def __init__(self, context): self.context = context self.tx = self.context.getTx() pass def load_seed(self, seed_fp): if (seed_fp[-2:] == "py"): context = self.context seed_name = context.load_seed(seed_fp) klass = PmkSeed.iplugins[seed_name] js = klass.getConfEntry() self.context.getProcGraph().updateRegistry( json.loads(js), loc="locallocal") if not klass.hasInputs(): #klass.run(klass.__rawpacket()) klass.rawrun() def process_IN_MOVED_TO(self, event): try: seed_fp = os.path.join(event.path, event.name) self.load_seed(seed_fp) except Exception as e: logging.error("Loading Seed: " + seed_fp + " " + str(e.message)) pass def process_IN_CLOSE_WRITE(self, event): try: seed_fp = os.path.join(event.path, event.name) self.load_seed(seed_fp) except Exception as e: logging.error("Loading paket: " + seed_fp) pass def process_IN_DELETE(self, event): print logging.debug("Remove: %s" % os.path.join(event.path, event.name)) if self.ext == "pkt": notifier = Notifier(wm, PTmp(self.context)) wdd = wm.add_watch(self.dir, mask, rec=True) if self.ext == "py": logging.debug("Adding watch on: " + self.dir) notifier = Notifier(wm, Seedmp(self.context)) wdd = wm.add_watch(self.dir, mask, rec=True) while True: # loop forever try: # process the queue of events as explained above notifier.process_events() if notifier.check_events(): # read notified events and enqeue them notifier.read_events() # you can do some tasks here... except KeyboardInterrupt: # destroy the inotify's instance on this interrupt (stop monitoring) notifier.stop() break pass