def loop_inotify(self, mail_delta, recursive=False): directory = self.file_consumer.consume inotify = INotify() inotify_flags = flags.CLOSE_WRITE | flags.MOVED_TO if recursive: inotify.add_watch_recursive(directory, inotify_flags) else: inotify.add_watch(directory, inotify_flags) # Run initial mail fetch and consume all currently existing documents self.loop_step(mail_delta, recursive=recursive) next_mail_time = self.mail_fetcher.last_checked + mail_delta while True: # Consume documents until next_mail_time while True: delta = next_mail_time - time.time() if delta > 0: for event in inotify.read(timeout=delta): if recursive: path = inotify.get_path(event.wd) else: path = directory file = os.path.join(path, event.name) if os.path.isfile(file): self.file_consumer.try_consume_file(file) else: self.logger.warning( "Skipping %s as it is not a file", file) else: break self.mail_fetcher.pull() next_mail_time = self.mail_fetcher.last_checked + mail_delta
def handle_inotify(self, directory, recursive): logging.getLogger(__name__).info( f"Using inotify to watch directory for changes: {directory}") inotify = INotify() inotify_flags = flags.CLOSE_WRITE | flags.MOVED_TO if recursive: descriptor = inotify.add_watch_recursive(directory, inotify_flags) else: descriptor = inotify.add_watch(directory, inotify_flags) try: while not self.stop_flag: for event in inotify.read(timeout=1000): if recursive: path = inotify.get_path(event.wd) else: path = directory filepath = os.path.join(path, event.name) _consume(filepath) except KeyboardInterrupt: pass inotify.rm_watch(descriptor) inotify.close()
wd_dirs = {} if not quiet: print('Waiting for new files in folder', dropbox_dir) mask = flags.CREATE | flags.DELETE | flags.MODIFY | flags.DELETE_SELF # TODO: Fix flags wd = inotify.add_watch_recursive(dropbox_dir, mask, inotifyrecursive_filter) wd_dirs[wd] = dropbox_dir ignore_file_dir = os.path.dirname(ignore_file) if not os.path.samefile(dropbox_dir, ignore_file_dir): if not quiet: print('Waiting for changes for', ignore_file, 'in', ignore_file_dir) wd = inotify.add_watch(ignore_file_dir, flags.CREATE | flags.DELETE | flags.MODIFY | flags.DELETE_SELF) # TODO: Fix flags wd_dirs[wd] = ignore_file_dir while True: for event in inotify.read(): if verbose: print(event) for flag in flags.from_mask(event.mask): print(' ' + str(flag)) if event.wd in wd_dirs: path = os.path.join(wd_dirs[event.wd], event.name) if os.path.isfile(ignore_file) and os.path.samefile( ignore_file, path): if verbose: print('Ignore file changed, reloading') spec = load_ignorefile()