def watch_folder(module, params): log.debug('Watching folder %s for screenshots ...' % params['in']) screen_wm = pyinotify.WatchManager() screen_mask = pyinotify.IN_CLOSE_WRITE screen_notifier = pyinotify.AsyncNotifier(screen_wm, ScreenshotHandler()) screen_wdd = screen_wm.add_watch(params['in'], screen_mask, rec=True) asyncore.loop()
def listen_for_events(self): """ Listen for new files added to /data directory """ pyinotify.AsyncNotifier(self.wm, self) self.wm.add_watch(paths.DATA_DIR, EVENTS_MASK, rec=True) asyncore.loop()
def watch(self): """ Block the current thread and start processing incomming files """ self.logger.info("Watching %s for incomming CDRs" % self.watch_dir) notifier = pyinotify.AsyncNotifier(self.wm, self.handler) asyncore.loop()
def __init__(self, filename): self.agent_mgr = sdk.get_agent_mgr() self.tracer = eossdk.Tracer("FileWatcher") eossdk_utils.EosSdkAgent.__init__(self) eossdk.AgentHandler.__init__(self, self.agent_mgr) eossdk.FdHandler.__init__(self) self.tracer.trace0("Watching %r" % filename) self.wm = pyinotify.WatchManager() # Pass the InotifyHandler's that wille be created a pointer to # ourselves by wrapping it in functools.partial: handler = functools.partial(InotifyHandler, parent=self) # pylint: disable-msg=E1101 self.wm.watch_transient_file(filename, pyinotify.IN_MODIFY, handler) # pylint: enable-msg=E1101 self.inotifier = pyinotify.AsyncNotifier(self.wm, InotifyHandler(parent=self)) # We coalesce events because some editors (or unix operations) cause # multiple changes on one save. self.inotifier.coalesce_events(True) # Now that we've set up our inotify watcher and notifier, grab # the underlying file descriptor and pass it to the SDK to be # watched. When the OS detects a change to the file, we'll self.inotify_fd = self.wm.get_fd() self.tracer.trace0("Watching inotify fd: %d" % self.inotify_fd) self.watch_readable(self.inotify_fd, True)
def monitor(): global _wm if not _wm: raise "Cannot monitor on Windows, or without pyinotify." class EventHandler(pyinotify.ProcessEvent): def __init__(self, sids): self.sids = sids def _rw_process(self, event): try: _scan_file(_fix_codepage_1252(event.pathname, self.sids)) except Exception as e: _add_scan_error(filename, e) def process_IN_CREATE(self, event): self._rw_process(event) def process_IN_MODIFY(self, event): self._rw_process(event) def process_IN_DELETE(self, event): _disable_file(event.pathname) cache.set("backend_scan", "monitoring") mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY notifiers = [] descriptors = [] for dir, sids in _directories.iteritems(): notifiers.append(pyinotify.AsyncNotifier(_wm, EventHandler(sids))) descriptors.append(_wm.add_watch(dir, mask, rec=True, auto_add=True)) print "Monitoring" asyncore.loop() cache.set("backend_scan", "off")
def add_watch(self, watched): self.log.info("watching %s" % watched) wm = pyinotify.WatchManager() mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_CLOSE_WRITE | pyinotify.IN_MOVED_TO # watched events handler = EventHandler(self, self.log) notifier = pyinotify.AsyncNotifier(wm, handler) wdd = wm.add_watch(watched, mask, rec=True)
def __init__(self, sdk): # Carry out SDK-specific initialisation syslog.syslog("Initialization starting") agent_mgr = sdk.get_agent_mgr() acl_mgr = sdk.get_acl_mgr() intf_mgr = sdk.get_intf_mgr() self.agent_mgr = agent_mgr self.acl_mgr = acl_mgr self.intf_mgr = intf_mgr eossdk.AgentHandler.__init__(self, agent_mgr) eossdk.AclHandler.__init__(self, acl_mgr) eossdk.IntfHandler.__init__(self, intf_mgr) eossdk.FdHandler.__init__(self) self.tracer = eossdk.Tracer("ACLeratePythonAgent") self.tracer.trace0("Python agent constructed") # Now register with inotify to receive be notified of changes to the config file self.config_file = ACLerate_config_file self.wm = pyinotify.WatchManager() handler = functools.partial(InotifyHandler, parent=self) mask = pyinotify.IN_MODIFY | pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_CLOSE_WRITE self.wm.watch_transient_file(ACLerate_config_file, mask, handler) self.inotifier = pyinotify.AsyncNotifier(self.wm, InotifyHandler(parent=self)) self.inotifier.coalesce_events(True) self.inotify_fd = self.wm.get_fd() self.watch_readable(self.inotify_fd, True)
def start(self): # The watch manager stores the watches and provides operations on watches wm = pyinotify.WatchManager() mask = pyinotify.IN_MODIFY # watched events handler = EventHandler(self) notifier = pyinotify.AsyncNotifier(wm, handler) wdd = wm.add_watch(self.input_path, mask) asyncore.loop()
def monitor(directory, callback): wm = pyinotify.WatchManager() # rec=True, to monitor all sub directories recursively. # auto_add=True, to monitor added new sub directories. wm.add_watch(directory, mask, rec=True, auto_add=True) # specify the event handler to process the events. pyinotify.AsyncNotifier(wm, EventHandler(callback)) # start the asyncore loop to monitor and process events. asyncore.loop()
def section_job(section): # process to consume items from this producer watch_queue = multiprocessing.Queue() watchjob = multiprocessing.Process(target=watch_job, args=( watch_queue, section, )) watchjob.start() wm = pyinotify.WatchManager() notifier = pyinotify.AsyncNotifier(wm, EventHandler(watch_queue)) exclude = pyinotify.ExcludeFilter(section['exclude']) template = {} # [small index date, next triggered seconds, wdd] for include_dir in section['include']: key = include_dir template[key] = [0, 0, None] template[key][0] = small_idx_date(include_dir) if template[key][0] > -1: now = tuple_date() template[key][1] = next_seconds(now, template[key][0]) include_dir = substitute_date(include_dir, now) print "New watch with include directory ", include_dir template[key][2] = wm.add_watch(include_dir, watch_mask, rec=True, auto_add=True, exclude_filter=exclude) next_trigger = min(map(lambda x: template[x][1], template)) while True: notifier.process_events() while notifier.check_events(): notifier.read_events() notifier.process_events() # check for updated watch if next_trigger and time.time() >= next_trigger: # update watch for include_dir in section['include']: key = include_dir if template[key][1] != next_trigger: continue now = tuple_date(next_trigger) template[key][1] = next_seconds(now, template[key][0]) next_trigger = min(map(lambda x: template[x][1], template)) wm.rm_watch(template[key][2].values(), rec=True) include_dir = substitute_date(include_dir, now) print "Updated with new include directory ", include_dir template[key][2] = wm.add_watch(include_dir, watch_mask, rec=True, auto_add=True, exclude_filter=exclude) watchjob.join()
def start(self): mask = inf.IN_DELETE | inf.IN_CREATE | inf.IN_MODIFY wm = inf.WatchManager() inf.AsyncNotifier(wm, self) wm.add_watch(conf.ROOT, mask, rec=True) # Run the initial grab of data self.process_default(None) asyncore.loop()
def watch_folder(properties): log.debug('Function "folder" launched with params %s: ' % properties) # inotify kernel watchdog stuff wm = pyinotify.WatchManager() mask = pyinotify.IN_CLOSE_WRITE notifier = pyinotify.AsyncNotifier(wm, EventHandler()) wdd = wm.add_watch(properties['in'], mask, rec=True) log.debug('watching :: %s' % properties['in']) asyncore.loop()
def watch(self): wm = pyinotify.WatchManager() # Watch Manager mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE # watched events evthdl = EventHandler(self) pyinotify.AsyncNotifier(wm, evthdl) wm.add_watch(self._path, mask, rec=True) asyncore.loop()
def setup_watchers(course: Course): taskwmngr = pyinotify.WatchManager() notifier = pyinotify.AsyncNotifier(taskwmngr, TaskHandler()) lessonmngr = pyinotify.WatchManager() notifier = pyinotify.AsyncNotifier(lessonmngr, LessonHandler()) modulemngr = pyinotify.WatchManager() notifier = pyinotify.AsyncNotifier(modulemngr, ModuleHandler()) coursemngr = pyinotify.WatchManager() notifier = pyinotify.AsyncNotifier(coursemngr, CourseHandler()) coursemngr.add_watch(course.path, pyinotify.ALL_EVENTS) for module in course.modules(): modulemngr.add_watch(module.path, pyinotify.ALL_EVENTS) for lesson in module.lessons(): lessonmngr.add_watch(lesson.path, pyinotify.ALL_EVENTS) for task in lesson.tasks(): taskwmngr.add_watch(task.path, pyinotify.ALL_EVENTS, rec=True)
def main(pathToWatch): wm = pyinotify.WatchManager() # Watch Manager mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY | pyinotify.IN_CLOSE_WRITE | pyinotify.IN_MOVED_TO | pyinotify.IN_MOVED_FROM # watched events notifier = pyinotify.AsyncNotifier(wm, EventHandler()) wm.add_watch('/tmp/directorywatch', mask) while (True): asyncore.poll() sleep(2) notifier.stop()
def run(self): self.wm = pyinotify.WatchManager() self.handler = EventHandler() #self.mask = pyinotify.ALL_EVENTS ^ (pyinotify.IN_ISDIR|pyinotify.IN_CLOSE_NOWRITE|pyinotify.IN_OPEN|pyinotify.IN_ACCESS|pyinotify.IN_ATTRIB) self.mask = pyinotify.IN_MODIFY self.notifier = pyinotify.AsyncNotifier(self.wm, self.handler) self.ignore = set([".git"]) self.exclusionFun = lambda path: reduce(lambda a,c: a or (c in path), self.ignore, False) self.exclusionFun = excfun(self.ignore) self.watchpath = '/home/user/Documents/repos/CReaMpy_utils/test' self.wdd = self.wm.add_watch(self.watchpath, self.mask, rec=True, auto_add=True, exclude_filter=self.exclusionFun) self.notifier.loop()
def main(): source_path = sys.argv[1] wm = pyinotify.WatchManager() notifier = pyinotify.AsyncNotifier(wm, EventHandler()) wdd = wm.add_watch(source_path, pyinotify.IN_CLOSE_WRITE) try: asyncore.loop() except KeyboardInterrupt: pass
def _setup_watchers(self, folders): """Setup watchers for all folders.""" manager = pyinotify.WatchManager() pyinotify.AsyncNotifier(manager) for folder in folders: manager.add_watch(folder.path, mask=folder.listen_to, proc_fun=folder, rec=folder.recursive) return {'manager': manager}
def setup(contr): owner = contr.owner print( "initial sweep") # check if there are already blendfiles in the folder initial_sweep(owner['load_map.dirtowatch']) print("init pyinotify") owner[ 'load_map.mask'] = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY | pyinotify.IN_MOVED_TO | pyinotify.IN_MOVED_FROM # watched events owner['load_map.wm'] = pyinotify.WatchManager() # Watch Manager owner['load_map.notifier'] = pyinotify.AsyncNotifier( owner['load_map.wm'], EventHandler()) owner['load_map.wm'].add_watch(owner['load_map.dirtowatch'], owner['load_map.mask'])
def watch_folder(properties): log.debug('Function "folder" launched with params %s: ' % properties) # inotify kernel watchdog stuff excl_lst = ['^/.*\\.tmp$'] excl = pyinotify.ExcludeFilter(excl_lst) wm = pyinotify.WatchManager() mask = pyinotify.IN_CLOSE_WRITE notifier = pyinotify.AsyncNotifier(wm, EventHandler()) wdd = wm.add_watch(properties['in'], mask, rec=True, auto_add=True, exclude_filter=excl) log.debug('watching :: %s' % properties['in']) asyncore.loop()
def __init__(self, addcb = None, delcb = None, updcb = None): threading.Thread.__init__( self ) self.EventHandler = UserInfoChangeHandler() self.EventHandler.set_add_callback(addcb) self.EventHandler.set_del_callback(delcb) self.EventHandler.set_upd_callback(updcb) wm = pyinotify.WatchManager() mask = pyinotify.IN_DELETE | pyinotify.IN_MODIFY | pyinotify.IN_CREATE |\ pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVED_TO | pyinotify.IN_ISDIR |\ pyinotify.IN_ATTRIB notifier = pyinotify.AsyncNotifier(wm, self.EventHandler) wdd = wm.add_watch(config.watch_path, mask, rec=True, auto_add = True) #self.setDaemon(True) self.start()
def run(self): # mask_events = pyinotify.IN_MODIFY # event handler eh = MyEventHandler(self.widget) wm = pyinotify.WatchManager() # Watch Manager # notifier = pyinotify.AsyncNotifier(wm, lambda *args : ex.update(args)) # notifier = pyinotify.AsyncNotifier(wm, test) # wdd = wm.add_watch('./', mask_events) wm.add_watch('./', pyinotify.ALL_EVENTS, rec=True) # notifier = pyinotify.Notifier(wm, eh) # notifier = pyinotify.AsyncNotifier(wm, eh) notifier = pyinotify.AsyncNotifier(wm, self.widget.reload()) notifier.loop()
def __init__(self, sdk, config_file="MplsTunnelLivenessConfig.json"): """ Create the agent. Requires an eossdk handle, as well as the input configuration """ self.agent_mgr = sdk.get_agent_mgr() self.eth_intf_mgr = sdk.get_eth_intf_mgr() self.ip_intf_mgr = sdk.get_ip_intf_mgr() self.mac_table_mgr = sdk.get_mac_table_mgr() self.neighbor_table_mgr = sdk.get_neighbor_table_mgr() self.tracer = eossdk.Tracer("MplsTunnelLivenessAgent") eossdk_utils.EosSdkAgent.__init__(self) eossdk.AgentHandler.__init__(self, self.agent_mgr) eossdk.TimeoutHandler.__init__(self, sdk.get_timeout_mgr()) eossdk.FdHandler.__init__(self) self.tracer.trace0("MPLS tunnel liveness agent constructed") self.initialized = False self.pid = os.getpid() # The l3 interface we should grab our "SRC IP" from. Read from # the config: self.src_intf = None self.src_ip = None # Resolved after reading from config # A UDP socket that receives liveness packets from other # agents. Created during on_initialized self.rx_sock = None # A mapping from remote switch IP to RemoteSwitch() self.remote_switches = {} self.config_file = config_file self.wm = pyinotify.WatchManager() handler = functools.partial(InotifyHandler, parent=self) # pylint: disable-msg=E1101 self.wm.watch_transient_file(config_file, pyinotify.IN_MODIFY, handler) # pylint: enable-msg=E1101 self.notifier = pyinotify.AsyncNotifier(self.wm, InotifyHandler(parent=self)) self.notifier.coalesce_events(True) self.inotify_fd = self.wm.get_fd() self.watch_readable(self.inotify_fd, True) # Read our initial configuration self.process_config()
def setup(self): """ Set up inotify manager. See https://github.com/seb-m/pyinotify/. """ if not pyinotify.WatchManager: raise error.UserError("You need to install 'pyinotify' to use %s (%s)!" % ( self.__class__.__name__, pyinotify._import_error)) # pylint: disable=E1101, W0212 self.manager = pyinotify.WatchManager() self.handler = TreeWatchHandler(job=self) self.notifier = pyinotify.AsyncNotifier(self.manager, self.handler) if self.LOG.isEnabledFor(logging.DEBUG): mask = pyinotify.ALL_EVENTS else: mask = pyinotify.IN_CLOSE_WRITE | pyinotify.IN_MOVED_TO # bogus pylint: disable=E1101 # Add all configured base dirs for path in self.config.path: self.manager.add_watch(path.strip(), mask, rec=True, auto_add=True)
def watch_section_job(section_name, section): setproctitle.setproctitle(sys.argv[0] + ':' + section_name) # process to consume items from this producer watch_queue = multiprocessing.Queue() watchjob = multiprocessing.Process(target=watcher, args=( watch_queue, section, )) watchjob.start() deleted_wd = [] max_deleted_queue = 10 wm = pyinotify.WatchManager() notifier = pyinotify.AsyncNotifier(wm, EventHandler(deleted_wd, watch_queue), timeout=10) exclude = pyinotify.ExcludeFilter(section['exclude']) template = {} # [small index date, next triggered seconds, wdd] for include_dir in section['include']: key = include_dir template[key] = [0, 0, None] template[key][0] = small_idx_date(include_dir) if template[key][0] > -1: now = tuple_date() template[key][1] = next_seconds(now, template[key][0]) sys.stdout.flush() include_dir = substitute_date(include_dir, now) if not os.path.exists(include_dir): continue template[key][2] = wm.add_watch(include_dir, watch_mask, rec=True, auto_add=True, exclude_filter=exclude) next_trigger = min(map(lambda x: template[x][1], template)) while not is_exit.value: notifier.process_events() while notifier.check_events(): notifier.read_events() notifier.process_events() if len(deleted_wd) > max_deleted_queue: while len(deleted_wd): sys.stdout.write("%s : Deleting unnecessary object : " % time.ctime()) wd = deleted_wd.pop() wm.del_watch(wd) sys.stdout.write("Done\n") sys.stdout.flush() # check for updated watch if next_trigger and time.time() >= next_trigger: # update watch sys.stdout.write("%s\n" % next_trigger) sys.stdout.write("%s : Updating watched directory\n" % time.ctime()) sys.stdout.flush() for include_dir in section['include']: key = include_dir if template[key][1] != next_trigger: continue now = tuple_date(next_trigger) include_dir = substitute_date(include_dir, now) if not os.path.exists(include_dir): continue # wait until directory exists template[key][1] = next_seconds(now, template[key][0]) next_trigger = min(map(lambda x: template[x][1], template)) wm.rm_watch(template[key][2].values(), rec=True) template[key][2] = wm.add_watch(include_dir, watch_mask, rec=True, auto_add=True, exclude_filter=exclude) notifier.stop() watchjob.join()
def process_IN_CLOSE_WRITE(self, event): logging.info("File writer closed file") self.get_dset_shape() logging.debug("Good bye!") sys.exit(0) if __name__ == "__main__": logging.basicConfig(format='%(asctime)s %(levelname)s\t%(message)s', level=logging.INFO) file_name = "swmr.h5" if len(sys.argv) > 1: file_name = sys.argv[1] dataset_name = "data" if len(sys.argv) > 2: dataset_name = sys.argv[2] wm = pyinotify.WatchManager() # Watch Manager mask = pyinotify.IN_MODIFY | pyinotify.IN_CLOSE_WRITE evh = EventHandler() evh.monitor_dataset(file_name, dataset_name) notifier = pyinotify.AsyncNotifier(wm, evh) wdd = wm.add_watch(file_name, mask, rec=False) # Sit in this loop() until the file writer closes the file # or the user hits ctrl-c asyncore.loop()
"""Watches the directory and triggers a player update""" def process_event(self, event): """Invoked when any of the events below are detected""" logging.info("Received event %s for %s", event.maskname, event.pathname) if not event.pathname.endswith('.mp3'): return update_player() process_IN_CLOSE_WRITE = \ process_IN_MOVED_TO = \ process_IN_MOVE_SELF = \ process_IN_DELETE = \ process_event wm = pyinotify.WatchManager() loop = asyncio.get_event_loop() notifier = pyinotify.AsyncNotifier(wm, EventProcessor()) wm.add_watch( DIRECTORY, pyinotify.IN_CLOSE_WRITE | pyinotify.IN_MOVED_TO | pyinotify.IN_MOVE_SELF | pyinotify.IN_DELETE) # update player once at startup to process any changes that happened while watcher wasn't running update_player() # Loop indefinetely asyncore.loop() notifier.stop()
def wrapper(self, *args, **kwargs): wm = inf.WatchManager() inf.AsyncNotifier(wm, EventHandler(self)) wm.add_watch(path, mask, rec=True) asyncore.loop()
# publish old message and exchange_key exchange_key = 'exp.dd.notify.' + key_final publisher.old_publish(url, exchange_key, filename) # publish old message and exchange_key exchange_key = 'v00.dd.notify.' + key_final publisher.old_publish(msg, exchange_key, filename) # start inotify engine wm = pyinotify.WatchManager() notifier = pyinotify.AsyncNotifier(wm, EventHandler()) # ========================================= # setup the watch of the sources # ========================================= # read in the directory for spath in SRC: entries = os.listdir(spath) wdd = wm.add_watch(spath, pyinotify.IN_CLOSE_WRITE, rec=True) print "watching = " + spath for d in entries: currentDir = spath + '/' + d
def main(): config = shatag.Config() parser = argparse.ArgumentParser( description='Monitors files with inotify and automatically update') parser.add_argument('-u', '--update', action='store_true', help='Only update already tagged files') parser.add_argument('-p', '--put', action='store_true', help='Send new hashes to database') parser.add_argument('-v', '--verbose', action='store_true', help='report missing/invalid checksums') parser.add_argument('-b', '--backend', metavar='BACKEND', help='backend for local tag storage', default=config.backend) parser.add_argument('-r', '--recursive', action='store_true', help='watch recursively') parser.add_argument('-d', '--daemon', action='store_true', help='daemonize') parser.add_argument('paths', metavar='PATH', nargs='+', help='paths to monitor') args = parser.parse_args() backend = shatag.backend(args.backend) store = None if args.put: store = shatag.Store(name=config.name, url=config.database) print("shatagd: updating database {0} with name {1}".format( config.database, store.name), file=sys.stderr) class Handler(pyinotify.ProcessEvent): def process_IN_CLOSE_WRITE(self, evt): try: file = backend.file(evt.pathname) if args.update: file.update() else: file.tag() if args.verbose: print(evt.pathname) if args.put: store.put(file) store.commit() except IOError as e: print('shatagd: "{0}": IOError {1}: {2}'.format( filename, e.errno, e.strerror), file=sys.stderr) except OSError as e: print('shatagd: {0}'.format(e), file=sys.stderr) wm = pyinotify.WatchManager() nf = pyinotify.AsyncNotifier(wm, Handler()) nf.coalesce_events() for path in args.paths: if (args.daemon and path[0] != '/'): print( "Warning: relative path {0} ignored in daemon mode. Use absolute paths." .format(path), file=sys.stderr) else: wm.add_watch(path, pyinotify.IN_CLOSE_WRITE | pyinotify.IN_CREATE, rec=args.recursive, auto_add=args.recursive) if (args.daemon): print("Daemonizing...", file=sys.stderr) try: pid = os.fork() if pid > 0: sys.exit(0) os.chdir("/") os.setsid() os.umask(0) pid = os.fork() if pid > 0: sys.exit(0) except OSError as e: print("OS Error: {0}".format(e), file=sys.stderr) sys.exit(1) asyncore.loop()