Пример #1
0
    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
Пример #2
0
    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()
Пример #3
0
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)")
Пример #4
0
    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()
                update_ignore_attr(path)
            else:
                print('Unexpected wd', event.wd, 'exiting', file=sys.stderr)
                exit(1)