Example #1
0
 def assert_patterns(event):
     if has_attribute(event, 'dest_path'):
         paths = [event.src_path, event.dest_path]
     else:
         paths = [event.src_path]
     filtered_paths = filter_paths(paths,
                                   included_patterns=patterns,
                                   excluded_patterns=ignore_patterns,
                                   case_sensitive=False)
     assert_true(filtered_paths)
 def assert_patterns(event):
   if has_attribute(event, 'dest_path'):
     paths = [event.src_path, event.dest_path]
   else:
     paths = [event.src_path]
   filtered_paths = filter_paths(paths,
                                 included_patterns=patterns,
                                 excluded_patterns=ignore_patterns,
                                 case_sensitive=False)
   self.assertTrue(filtered_paths)
def assert_patterns(event):
    if has_attribute(event, 'dest_path'):
        paths = [event.src_path, event.dest_path]
    else:
        paths = [event.src_path]
    filtered_paths = filter_paths(paths,
                                  included_patterns=['*.py', '*.txt'],
                                  excluded_patterns=["*.pyc"],
                                  case_sensitive=False)
    assert filtered_paths
def assert_patterns(event):
    if has_attribute(event, 'dest_path'):
        paths = [event.src_path, event.dest_path]
    else:
        paths = [event.src_path]
    filtered_paths = filter_paths(
        paths,
        included_patterns=['*.py', '*.txt'],
        excluded_patterns=["*.pyc"],
        case_sensitive=False)
    assert filtered_paths
    def run(self, args):

        parser = argparse.ArgumentParser(prog=args[0])
        parser.add_argument('directory', metavar='DIR',
                            help='Base directory')
        parser.add_argument('query', metavar='PATTERN',
                            help='Pattern or query string')
        parser.add_argument('-n', '--native-query', action='store_true',
                            help='PATTERN is a native query for the selected observer')

        parser.parse_args(args[1:], namespace=self)

        changes_queue = queue.Queue()

        stop_sentinel = object()
        def stdin_watch():
            while sys.stdin.read():
                pass
            changes_queue.put(stop_sentinel)

        stdin_watch_thread = threading.Thread(target=stdin_watch)
        stdin_watch_thread.start()

        pattern = self.query
        if not self.native_query:
            pattern = '*/' + pattern

        event_handler = EventHandler(pattern, changes_queue)

        observer = Observer()
        observer.schedule(event_handler, self.directory, recursive=True)
        observer.start()

        factory = MessageFactory()

        for root, dirs, files in os.walk(os.path.abspath(self.directory)):
            paths = [os.path.join(root, f) for f in files]
            inserts = tuple(filter_paths(paths, included_patterns=[pattern], case_sensitive=False))
            if len(inserts):
                changes_queue.put((tuple(), tuple(inserts)))

        while True:
            try:
                item = changes_queue.get(timeout=1000)
                if item == stop_sentinel:
                    break

                (deletable_paths, insertable_paths) = item

                insertable_meta = []
                insertable_paths_ok = []
                for path in insertable_paths[:]:
                    try:
                        insertable_meta.append({'stat': tuple(os.stat(path))})
                        insertable_paths_ok.append(path)
                    except OSError:
                        continue

                for msg in factory.update(deletable_paths, tuple(insertable_paths_ok), tuple(insertable_meta)):
                    self._out.write(msg)
                    self._out.flush()

                changes_queue.task_done()
            except queue.Empty:
                pass
            except KeyboardInterrupt:
                break

        observer.stop()
        observer.join()
        stdin_watch_thread.join()
Example #6
0
    def run(self, args):

        parser = argparse.ArgumentParser(prog=args[0])
        parser.add_argument('directory', metavar='DIR', help='Base directory')
        parser.add_argument('query',
                            metavar='PATTERN',
                            help='Pattern or query string')
        parser.add_argument(
            '-n',
            '--native-query',
            action='store_true',
            help='PATTERN is a native query for the selected observer')
        parser.add_argument(
            '-o',
            '--observer-class',
            metavar='CLASS',
            help=
            'Specify the watchdog observer implementation (fully qualified class name).'
        )

        parser.parse_args(args[1:], namespace=self)

        try:
            Observer = self.load_observer(self.observer_class)
        except:
            parser.error("Watchdog observer implementation not found")

        changes_queue = queue.Queue()

        stop_sentinel = object()

        def stdin_watch():
            while sys.stdin.read():
                pass
            changes_queue.put(stop_sentinel)

        stdin_watch_thread = threading.Thread(target=stdin_watch)
        stdin_watch_thread.start()

        pattern = self.query
        if not self.native_query:
            pattern = '*/' + pattern

        event_handler = EventHandler(pattern, changes_queue)

        observer = Observer()
        observer.schedule(event_handler, self.directory, recursive=True)
        observer.start()

        factory = MessageFactory()

        for root, dirs, files in os.walk(os.path.abspath(self.directory)):
            paths = [os.path.join(root, f) for f in files]
            inserts = tuple(
                filter_paths(paths,
                             included_patterns=[pattern],
                             case_sensitive=False))
            if len(inserts):
                changes_queue.put((tuple(), tuple(inserts)))

        while True:
            try:
                item = changes_queue.get(timeout=1000)
                if item == stop_sentinel:
                    break

                (deletable_paths, insertable_paths) = item

                insertable_meta = []
                insertable_paths_ok = []
                for path in insertable_paths[:]:
                    try:
                        insertable_meta.append({'stat': tuple(os.stat(path))})
                        insertable_paths_ok.append(path)
                    except OSError:
                        continue

                for msg in factory.update(deletable_paths,
                                          tuple(insertable_paths_ok),
                                          tuple(insertable_meta)):
                    self._out.write(msg)
                    self._out.flush()

                changes_queue.task_done()
            except queue.Empty:
                pass
            except KeyboardInterrupt:
                break

        observer.stop()
        observer.join()
        stdin_watch_thread.join()