def __init__(self, queue: asyncio.Queue, media_dir: str, monitor_file_name: str = "remove-when-done"): """ :arg queue: queue where we send notifications :arg media_dir: directory where we manage the monitor file :arg monitor_file_name: name to use for the monitor file """ self.queue = queue self.media_dir = os.path.abspath(media_dir) self.monitor_file_name = monitor_file_name self.monitor_file = os.path.join(self.media_dir, self.monitor_file_name) # Set up pyinotify. # See https://stackoverflow.com/questions/26414052/watch-for-a-file-with-asyncio self.watch_manager = pyinotify.WatchManager() self.watch = self.watch_manager.add_watch(self.media_dir, pyinotify.IN_DELETE) self.notifier = pyinotify.AsyncioNotifier( self.watch_manager, asyncio.get_event_loop(), default_proc_fun=self.on_event) # Create the monitor file if it does not exist if not os.path.exists(self.monitor_file): self.create_monitor_file()
def initialize_watcher(self): self.watch_manager = pyinotify.WatchManager() # pylint: disable=no-member mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY loop = asyncio.get_event_loop() self.notifier = pyinotify.AsyncioNotifier( self.watch_manager, loop, default_proc_fun=PolicyWatcher(self)) if str(self.path) not in self.default_policy_paths \ and os.path.exists(self.path): self.watches.append( self.watch_manager.add_watch(str(self.path), mask, rec=True, auto_add=True)) for path in self.default_policy_paths: if not os.path.exists(path): continue self.watches.append( self.watch_manager.add_watch(str(path), mask, rec=True, auto_add=True))
def main(): parser = argparse.ArgumentParser( description= """Cloud MailDir Sync is able to download email messages from a cloud provider and store them in a local maildir. It uses the REST interface from the cloud provider rather than IMAP and uses OAUTH to authenticate. Once downloaded the tool tracks changes in the local mail dir and uploads them back to the cloud.""") parser.add_argument("-c", dest="CFG", default="cms.cfg", help="Configuration file to use") parser.add_argument( "--offline", dest="OFFLINE", default=False, action="store_true", help= "Enable offline mode, local changes to message flags will be considered authoritative." ) args = parser.parse_args() cfg = config.Config() cfg.args = args cfg.load_config(args.CFG) cfg.loop = asyncio.get_event_loop() with contextlib.closing(pyinotify.WatchManager()) as wm, \ contextlib.closing(messages.MessageDB(cfg)) as msgdb: pyinotify.AsyncioNotifier(wm, cfg.loop) cfg.watch_manager = wm cfg.msgdb = msgdb cfg.loop.run_until_complete(synchronize_mail(cfg)) cfg.loop.run_until_complete(cfg.loop.shutdown_asyncgens()) cfg.loop.close()
def start(self, loop=None): loop = (loop or self._loop) self._watch_manager.add_watch(self._path, pyinotify.ALL_EVENTS, rec=self._rec, auto_add=self._auto_add, do_glob=True) self._notifier = pyinotify.AsyncioNotifier( self._watch_manager, loop, default_proc_fun=self._event_map)
def main(): loop = asyncio.get_event_loop() wm = pyinotify.WatchManager() gtk_app = NotificationApp(wm) handler = EventHandler(loop=loop, gtk_app=gtk_app) pyinotify.AsyncioNotifier(wm, loop, default_proc_fun=handler) loop.run_forever()
def run(self, loop): self.load_all_modules() wm = pyinotify.WatchManager() self.__notifier = pyinotify.AsyncioNotifier(wm, loop, default_proc_fun=self) wm.add_watch(self.module_dir_path, self.__watch_flags, exclude_filter=pyinotify.ExcludeFilter( self.__exclude_list))
def watch(loop, directory:str, handler:pyinotify.ProcessEvent) -> pyinotify.AsyncioNotifier: """Watch selecected directory using inotify""" wm = pyinotify.WatchManager() mask = pyinotify.IN_MOVED_TO | pyinotify.IN_CLOSE_WRITE notifier = pyinotify.AsyncioNotifier(wm, loop, default_proc_fun=handler()) wdd = wm.add_watch( directory, mask, rec=True, auto_add=True ) return notifier
def watch(self): watcher = pyinotify.WatchManager() loop = asyncio.get_event_loop() notifier = pyinotify.AsyncioNotifier(watcher, loop, callback=self._rebuild) mask = pyinotify.IN_CLOSE_WRITE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY self._add_watch_dir(watcher, self._cfg["scss"]["in_dir"], mask) self._add_watch_dir(watcher, self._cfg["dotjs"]["in_dir"], mask) loop.run_forever() notifier.stop()
def __init__(self, config: Config): self.watch_manager = pyinotify.WatchManager() self.loop = asyncio.get_event_loop() # first we need to initially search self.handler = EventHandler(config, self.get_available_devices) # second we start an internal watchdog for hotplugging self.notifier = pyinotify.AsyncioNotifier( self.watch_manager, self.loop, default_proc_fun=self.handler) self.watch_manager.add_watch('/dev/input', pyinotify.IN_CREATE | pyinotify.IN_DELETE, rec=True, auto_add=True)
def inotify_start(loop): print('inotify start . . .') # watch manager wm = pyinotify.WatchManager() mask = pyinotify.IN_MODIFY # watched events wm.add_watch(root_path, pyinotify.ALL_EVENTS, rec=True) wm.add_watch(tao_path, pyinotify.ALL_EVENTS, rec=True) # handler = EventHandler(file=filename, loop=loop) handler = EventHandler(loop=loop) notifier = pyinotify.AsyncioNotifier(wm, loop, default_proc_fun=handler)
def main(): loop = asyncio.get_event_loop() mask = pyinotify.ALL_EVENTS wm = pyinotify.WatchManager() while True: if not os.path.exists(DATA): time.sleep(0.5) else: wm.add_watch(FROM, mask) handler = EventHandler(loop=loop) pyinotify.AsyncioNotifier(wm, loop, default_proc_fun=handler) loop.run_forever()
def __init__(self, app: "Application"): self.app = app self.loop = asyncio.get_event_loop() # Set up pyinotify. # See https://stackoverflow.com/questions/26414052/watch-for-a-file-with-asyncio self.watch_manager = pyinotify.WatchManager() # Map absolute paths to Watch handlers self.watches: Dict[str, pyinotify.Watch] = {} # self.watch = self.watch_manager.add_watch(self.media_dir, pyinotify.IN_DELETE) self.notifier = pyinotify.AsyncioNotifier( self.watch_manager, self.loop, default_proc_fun=self.on_event) # Pending trigger self.pending = None
def run_filesystem_observer(root): global LOOP, WATCH_MANAGER inf(f'starting file observer in {root}') LOOP = asyncio.get_event_loop() WATCH_MANAGER = pyinotify.WatchManager() pyinotify.AsyncioNotifier(WATCH_MANAGER, LOOP, default_proc_fun=EventHandler()) mask = pyinotify.IN_CLOSE_WRITE | pyinotify.IN_DELETE | pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVED_TO WATCH_MANAGER.add_watch(root, mask, rec=True, auto_add=True) signal.signal(signal.SIGINT, terminate) signal.signal(signal.SIGTERM, terminate)
def start_polling_changes(self): def process_inotify_event(event): async def task(): print("EVENT: {}".format(str(event))) try: stats = os.stat(event.pathname, follow_symlinks=False) except FileNotFoundError: stats = None await self.process_change(event.pathname, stats) self.loop.create_task(task()) self.notifier = pyinotify.AsyncioNotifier( self.watch_manager, self.loop, default_proc_fun=process_inotify_event )
def main(): repo = '/home/takluyver/Code/ipython' loop = asyncio.get_event_loop() git_config = GitConfig(repo) wm = pyinotify.WatchManager() wm.add_watch(os.path.join(repo, '.git/config'), pyinotify.IN_MODIFY, git_config.invalidate_cache) lbw = LocalBranchWatcher(repo, git_config=git_config) lbw.setup_watch(wm) #wm.add_watch(os.path.join(repo, '.git/HEAD'), pyinotify.IN_MODIFY, on_head_changed) notifier = pyinotify.AsyncioNotifier(wm, loop) try: loop.run_forever() except KeyboardInterrupt: print()
def cli(debug,logfile): if debug: logger.setLevel(logging.DEBUG) if logfile: fh = logging.FileHandler(logfile) fh.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s %(levelname)-6s %(message)s') fh.setFormatter(formatter) logger.addHandler(fh) else: handler = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s %(levelname)-6s %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) if not os.path.exists(BATSKY_SOCK_DIR): os.makedirs(BATSKY_SOCK_DIR) try: os.unlink(BATSKY_NOTIFY_SOCK) except OSError: if os.path.exists(BATSKY_NOTIFY_SOCK): raise sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.bind(BATSKY_NOTIFY_SOCK) sock.listen(1) global batsky_sock batsky_sock , _ = sock.accept() logger.info('\nBatsky connected...') notifier = pyinotify.AsyncioNotifier(wm, loop, default_proc_fun=EventHandler()) wdd = wm.add_watch(BATSKY_SOCK_DIR, mask, rec=True) try: loop.run_forever() except: logger.info('\nShutting down...') loop.stop() notifier.stop()
def watch(self): watcher = pyinotify.WatchManager() loop = asyncio.get_event_loop() notifier = pyinotify.AsyncioNotifier( watcher, loop, callback=self._rebuild ) mask = pyinotify.IN_CLOSE_WRITE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY self._add_watch_dir(watcher, self._cfg["scss"]["in_dir"], mask) tpl_dirs = list(map(lambda item: item["root"], self._cfg["dotjs"]["mapping"])) for tpl_dir in set(tpl_dirs): print("(watch tpl)", tpl_dir) self._add_watch_dir(watcher, tpl_dir, mask) loop.run_forever() notifier.stop()
def __init__(self, loop, paths, callback): """ @param loop: asyncio or trollius event loop instance. @type loop: asyncio.BaseEventLoop or trollius.BaseEventLoop instance. @param callback: Functor called if the list of dashboard changed. Expects to receive the list of dashboards as single parameter. @type callback: awaitable functor. """ super().__init__() self.paths = [Path(p) for p in paths] self.callback = callback # We start watching the folders before initializing the # dashboards to avoid race conditions. wm = pyinotify.WatchManager() notifier = pyinotify.AsyncioNotifier(wm, loop, default_proc_fun=self, callback=self.schedule_callback) self.logger.info('Watching paths %s', ', '.join('"' + str(d) + '"' for d in self.paths)) for path in self.paths: wm.add_watch(str(path), self.mask, rec=True, auto_add=True) self.dashboards = {} for path in self.paths: for dashboard in path.iterdir(): self.load_dashboard(dashboard) current_dashboards = self.get_dashboard_names() if current_dashboards: self.logger.info( 'Initializing with dashboards %s', ', '.join('"' + d + '"' for d in current_dashboards)) else: self.logger.info('Initializing without any dashboard') self.schedule_callback(notifier)
async def inotify_start(loop, files: [str], callback=None, event_mask=None): from os.path import dirname, isdir files = tuple(set(map( lambda x: x if isdir(x) else dirname(x), files ))) if event_mask is None: event_mask = pyi.IN_MODIFY | pyi.IN_ATTRIB | pyi.IN_CLOSE_WRITE | \ pyi.IN_DELETE | pyi.IN_CREATE | pyi.IN_MOVE_SELF wm = pyi.WatchManager() watches = wm.add_watch(files, event_mask, rec=True, do_glob=True, auto_add=True) config.log(watches, category='inotify_start') handler = InotifyEvent(files=files, callback=callback) pyi.AsyncioNotifier( watch_manager=wm, loop=loop, default_proc_fun=handler )
def update_tasks_in(directory, tail_tasks, tail_events, loop): wm = pyinotify.WatchManager() mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY class Handler(pyinotify.ProcessEvent): def process_IN_CREATE(self, event): path = event.pathname if os.path.isfile(path): if path in tail_tasks: logger.debug("Cancelling existing task for {}".format(path)) tail_tasks[path].cancel() del tail_tasks[path] logger.debug("Creating task for {}".format(path)) modify_event = asyncio.Event(loop=loop) tail_events[path] = modify_event tail_tasks[path] = loop.create_task(tail_task(path, modify_event, loop, new=True)) else: logger.debug("New path {} is not a file, ignoring".format(path)) def process_IN_DELETE(self, event): path = event.pathname if path in tail_tasks: logger.debug("Cancelling task for {}".format(path)) tail_tasks[path].cancel() del tail_tasks[path] else: logger.debug("No task to cancel for {}".format(path)) def process_IN_MODIFY(self, event): path = event.pathname if path in tail_events: logger.debug("Event set for {}".format(path)) tail_events[path].set() else: logger.debug("No event to set for {}".format(path)) notifier = pyinotify.AsyncioNotifier(wm, loop, default_proc_fun=Handler()) wm.add_watch(directory, mask, rec=True, auto_add=True)
def __init__(self, extra_search_path: Optional[str] = None) -> None: inotify = pyinotify.WatchManager() self.files = glob("/usr/lib/libpython*") if not self.files: self.files = glob("/lib/libpython*") if extra_search_path is not None: # Handle custom installation paths for file in Path(extra_search_path).rglob("libpython*"): self.files.append(str(file.resolve())) log.debug("Watching for %s", self.files) self.notifier = pyinotify.AsyncioNotifier( inotify, asyncio.get_event_loop(), self.notified, pyinotify.ProcessEvent(), ) inotify.add_watch( self.files, pyinotify.EventsCodes.ALL_FLAGS["IN_OPEN"] | pyinotify.EventsCodes.ALL_FLAGS["IN_CLOSE_NOWRITE"], )
def create_reader(self, filename, read_last_n_lines=10): """Opens the specified file and creates the reader callback. Optionally returns the last few lines of content. Args: filename (str): Path of the file to create reader for. read_last_n_lines (Optional[int]): Optional number of lines of content to be returned Returns: tuple: A file descriptor and possibly empty string of content """ filename = os.path.abspath(filename) logger.debug('Creating reader for {}'.format(filename)) fd = open(filename, 'rb') if read_last_n_lines: logger.debug('Reading last {} lines'.format(read_last_n_lines)) content, _ = self.read_last_lines_from_file(read_last_n_lines, fd) content = '\n'.join(content) else: fd.seek(0, os.SEEK_END) content = '' # add_watch returns a dict with the filename as a key and a watch # descriptor as a value watch_descriptor = self._watch_manager.add_watch(filename, self._mask) if watch_descriptor[filename] < 0: raise CouldNotCreateDescriptorError() handler = EventHandler(file=filename, registry=self) pyinotify.AsyncioNotifier(self._watch_manager, asyncio.get_event_loop(), default_proc_fun=handler) return watch_descriptor[filename], content
print("test") port_file.seek(0, 0) line = "".join(port_file.readlines()).rstrip('\n').replace('\n', ' ') global dest dest = ("localhost", int(line)) print("port changed to:" + line) # Init event loop for monitoring chatlog and discord events wm = pyinotify.WatchManager() loop = asyncio.get_event_loop() client = discord.Client(loop=loop) pyinotify.AsyncioNotifier(wm, loop, callback=handle_factorio_chat, default_proc_fun=lambda x: None) wm.add_watch(chatlog_file, pyinotify.IN_MODIFY) #maybe i need another loop? # 2nd watch manager for port file, can i use the same loop? i really only want to watch to file # with 2 different event handlers wm2 = pyinotify.WatchManager() pyinotify.AsyncioNotifier(wm2, loop, callback=handle_port_change, default_proc_fun=lambda x: None) wm2.add_watch(port_file_path, pyinotify.IN_MODIFY) def replace_mentions(match):
np.empty((1, 2600, 2000, 1)), np.empty((1, 2600, 2000, 1)) ] preprocess_one_image(self.X, event.pathname) self.image_count += 1 self.last_listdir = current_dir self.proc_list.append(current_dir) if __name__ == '__main__': wm = pyinotify.WatchManager() # Watch Manager mask = pyinotify.IN_CLOSE_WRITE # watched events loop = asyncio.get_event_loop() notifier = pyinotify.AsyncioNotifier(wm, loop, default_proc_fun=EventHandler()) wdd = wm.add_watch('/incoming/data', mask, rec=True, auto_add=True) try: loop.run_forever() except: print('\nshutting down...') loop.stop() notifier.stop()
def process_IN_MODIFY(self, event): # prevent doule tasks if self.val is not None: self.val.cancel() self.val = asyncio.ensure_future(train()) print("Modify:", event.pathname) # def process_default(self, event): # print(event) handler = EventHandler() loop = asyncio.get_event_loop() loop.create_task(whereami()) notifier = pyinotify.AsyncioNotifier(wm, loop, default_proc_fun=handler) # notifier = pyinotify.Notifier(wm, handler) base_dir = "/sys/class/backlight/" for path in os.listdir(base_dir): full_path = os.path.join(base_dir, path) print(full_path) wm.add_watch(full_path, mask) async def f**k(x): asyncio.sleep(0) print("f**k", x) if __name__ == "__main__":
end = time.time() log("Finished in "+str(round(end-start,2))+" seconds") except Exception as e: log(e) if notifier: notifier.loop.stop() try: if not isdir(SRC): raise Exception(SRC+" is not a directory") # Initialize by running the handle_event function handle_event() # Credit this snippit to https://github.com/seb-m # https://github.com/seb-m/pyinotify/blob/master/python3/examples/asyncio_notifier.py wm = pyinotify.WatchManager() loop = asyncio.get_event_loop() notifier = pyinotify.AsyncioNotifier(wm,loop,callback=handle_event) wm.add_watch(SRC,pyinotify.ALL_EVENTS) log() log("Watching "+SRC) loop.run_forever() notifier.stop() except Exception as e: log(e) notifier.loop.stop()
fn = device.get('input_fn', None) device['remappings'].clear() if (name, phys, fn) in update_map: device['remappings'].update(update_map[(name, phys, fn)]) if __name__ == '__main__': os.chdir(os.path.dirname(os.path.realpath(__file__))) config = load_config_file('config-x11.yaml') loop = asyncio.get_event_loop() croutonlockcmd = ("echo -n $(cat `which croutoncycle` | grep " "CROUTONLOCKDIR= | cut -d '=' -f 2 | tr -d \"'\")") croutonlockdir = subprocess.check_output(croutonlockcmd, shell=True) crouton_display_path = croutonlockdir.decode("utf-8") + "/display" wm = pyinotify.WatchManager() loop = asyncio.get_event_loop() notifier = pyinotify.AsyncioNotifier(wm, loop) handler = functools.partial(CroutonCycleHandler, config=config) wm.watch_transient_file(crouton_display_path, pyinotify.IN_MODIFY, handler) evdevremapkeys.load_config = lambda z: config ArgsShim = namedtuple("ArgsShim", "config_file") while True: try: evdevremapkeys.run_loop(ArgsShim(None)) except OSError as e: print("Another instance has grabbed the input device, waiting...") time.sleep(3) else: break
def main(): parser = argparse.ArgumentParser(description='Simple photo gallery server') parser.add_argument('-r', '--root', type=str, required=True, help="Root directory of the photo gallery") parser.add_argument('-c', '--cache', type=str, default="/var/cache/zurished", help="Cache folder to use") parser.add_argument('-s', '--static', type=str, default="", help="Static content to serve") parser.add_argument('-p', '--port', type=int, default=8080, help="Port to listen to") parser.add_argument('--resize-cmd', type=str, default="", help="""Command used to resize images You can use {src} {dst} {width} and {height}""") parser.add_argument('--thumbnail-cmd', type=str, default="", help="""Command used to resize images You can use {src} {dst} {width} and {height}""") args = parser.parse_args() # Create the main gallery cache = Cache(args.cache) converter = Converter(img_resize_cmd=args.resize_cmd, img_resize_square_cmd=args.thumbnail_cmd) gallery = Gallery(args.root, cache, converter) # Don't wait for the first request to create the listing gallery.list_all_medias() # Add a watch on folder changes wm = pyinotify.WatchManager() loop = asyncio.get_event_loop() notifier = pyinotify.AsyncioNotifier(wm, loop) wm.add_watch(args.root, pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY | pyinotify.IN_MOVED_TO | pyinotify.IN_MOVED_FROM, proc_fun=gallery.on_folder_change, rec=True, auto_add=True) # Run the webserver app = web.Application() app.router.add_route('GET', '/gallery', gallery.web_gallery_handler) app.router.add_route('GET', '/medias/resized/{width}x{height}/{path:.*}', gallery.web_resize_handler) app.router.add_route('GET', '/medias/thumbnail/{path:.*}', gallery.web_thumbnail_handler) app.router.add_static('/medias/full/', args.root) app.router.add_static('/', args.static) web.run_app(app, port=args.port)
class HandleEvent(pyinotify.ProcessEvent): def my_init(self): self.known = set([x for x in psutil.process_iter()]) return None def process_default(self, event): latest = set([x for x in psutil.process_iter()]) if len(latest - self.known) > 0: for proc in latest - self.known: try: print(proc.parent(), 'spawned', proc.exe(), 'at', proc.pid) except psutil.NoSuchProcess: print('process exited') self.known = latest else: print( "something happened but wasn't represented by psutil (transient process)" ) wm = pyinotify.WatchManager() loop = asyncio.get_event_loop() notifier = pyinotify.AsyncioNotifier(wm, loop, callback=None, default_proc_fun=HandleEvent()) wm.add_watch('/lib/ld-linux*', pyinotify.IN_ACCESS, do_glob=True) loop.run_forever() notifier.stop()
import pyinotify import asyncio def handle_read_callback(notifier): """ Just stop receiving IO read events after the first iteration (unrealistic example). """ print('handle_read callback') notifier.loop.stop() wm = pyinotify.WatchManager() loop = asyncio.get_event_loop() notifier = pyinotify.AsyncioNotifier(wm, loop, callback=handle_read_callback) wm.add_watch('/tmp', pyinotify.ALL_EVENTS) loop.run_forever() notifier.stop()