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()
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 start(): config = read_config() args = parse_arguments(config) if args.generate_init: print(files.generateInit(sys.argv)) return if args.generate_config: print(files.generateConfig(args)) return logging.basicConfig(filename=args.logfile, level=args.loglevel.upper(), format="%(asctime)s %(levelname)s %(message)s") if args.rebuild_index: print("Adding files to media-index (this may take some time)...") for path in args.path: add_to_index_recursive(path) return signal.signal(signal.SIGTERM, on_sigterm) try: inotify = INotify() filter = build_filter(args) mask = flags.DELETE | flags.CREATE | flags.MOVED_TO | flags.MOVED_FROM | flags.MODIFY | flags.CLOSE_WRITE for path in args.path: logging.info("Adding watch for path: %s", path) inotify.add_watch_recursive(path, mask, filter) logging.info("Waiting for media file changes...") modified_files = set() while True: for event in inotify.read(): is_dir = event.mask & flags.ISDIR fullpath = os.path.join(inotify.get_path(event.wd), event.name) if event.mask & flags.CREATE or event.mask & flags.MODIFY: if is_dir: add_to_index(fullpath, is_dir) else: modified_files.add(fullpath) elif event.mask & flags.MOVED_TO: add_to_index(fullpath, is_dir) elif event.mask & flags.DELETE or event.mask & flags.MOVED_FROM: remove_from_index(fullpath, is_dir) elif event.mask & flags.CLOSE_WRITE and (fullpath in modified_files): modified_files.remove(fullpath) add_to_index(fullpath, is_dir) except OSError as e: if e.errno == 28: logging.error( "No inode watchers left (see https://github.com/letorbi/synoindexwatcher#faq)" ) exit(127) raise except KeyboardInterrupt: logging.info("Watching interrupted by user (CTRL+C)")
if ignore or not os.path.isdir(full): continue if should_watch_dir(full): if verbose: print('Recurse', full) update_ignore_dir(full) spec = load_ignorefile() if spec is None and not force: exit(1) update_ignore_dir(dropbox_dir) if watch: inotify = INotify() def inotifyrecursive_filter(name, wd, dir): if wd == -1: return True path = inotify.get_path(wd) should_watch = should_watch_dir(path) print("inotifyrecursive_filter", name, wd, dir, path, should_watch) return should_watch 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