def watch(default_build_targets: List[str], build_directories: List[str], patterns: str, ignore_patterns_string: str, exclude_list: List[Path], restart: bool):
    'Watches files and runs Ninja commands when they change.'
    _LOG.info('Starting Pigweed build watcher')
    if (os.environ['PW_ROOT'] is None):
        _exit_due_to_pigweed_not_installed()
    pw_root = Path(os.environ['PW_ROOT']).resolve()
    if (Path.cwd().resolve() not in [pw_root, *pw_root.parents]):
        _exit_due_to_pigweed_not_installed()
    exclude_list += get_common_excludes()
    build_commands = [BuildCommand(Path(build_dir[0]), tuple(build_dir[1:])) for build_dir in build_directories]
    if (default_build_targets or (not build_directories)):
        build_dir = _find_build_dir()
        if (build_dir is None):
            _die("No build dirs found. Did you forget to run 'gn gen out'?")
        build_commands.append(BuildCommand(build_dir, tuple(default_build_targets)))
    for (i, build_target) in enumerate(build_commands, 1):
        if (not build_target.build_dir.is_dir()):
            _die("Build directory doesn't exist: %s", build_target)
        else:
            _LOG.info('Will build [%d/%d]: %s', i, len(build_commands), build_target)
    _LOG.debug('Patterns: %s', patterns)
    path_to_log = str(Path().resolve()).replace(str(Path.home()), '$HOME')
    ignore_patterns = (ignore_patterns_string.split(_WATCH_PATTERN_DELIMITER) if ignore_patterns_string else [])
    env = pw_cli.env.pigweed_environment()
    if env.PW_EMOJI:
        charset = _EMOJI_CHARSET
    else:
        charset = _ASCII_CHARSET
    event_handler = PigweedBuildWatcher(patterns=patterns.split(_WATCH_PATTERN_DELIMITER), ignore_patterns=ignore_patterns, build_commands=build_commands, charset=charset, restart=restart)
    try:
        _LOG.info('Attaching filesystem watcher to %s/...', path_to_log)
        observers = []
        for (path, rec) in minimal_watch_directories(Path.cwd(), exclude_list):
            observer = Observer()
            observer.schedule(event_handler, str(path), recursive=rec)
            observer.start()
            observers.append(observer)
        event_handler.debouncer.press('Triggering initial build...')
        for observer in observers:
            while observer.is_alive():
                observer.join(1)
    except (KeyboardInterrupt, EOFError):
        _exit_due_to_interrupt()
    except OSError as err:
        if (err.args[0] == _ERRNO_INOTIFY_LIMIT_REACHED):
            _exit_due_to_inotify_limit()
        else:
            raise err
    _LOG.critical('Should never get here')
    observer.join()
Watches files and runs Ninja commands when they change.
def watch(default_build_targets: List[str], build_directories: List[str], patterns: str, ignore_patterns_string: str, exclude_list: List[Path], restart: bool):
    
    _LOG.info('Starting Pigweed build watcher')
    if (os.environ['PW_ROOT'] is None):
        _exit_due_to_pigweed_not_installed()
    pw_root = Path(os.environ['PW_ROOT']).resolve()
    if (Path.cwd().resolve() not in [pw_root, *pw_root.parents]):
        _exit_due_to_pigweed_not_installed()
    exclude_list += get_common_excludes()
    build_commands = [BuildCommand(Path(build_dir[0]), tuple(build_dir[1:])) for build_dir in build_directories]
    if (default_build_targets or (not build_directories)):
        build_dir = _find_build_dir()
        if (build_dir is None):
            _die("No build dirs found. Did you forget to run 'gn gen out'?")
        build_commands.append(BuildCommand(build_dir, tuple(default_build_targets)))
    for (i, build_target) in enumerate(build_commands, 1):
        if (not build_target.build_dir.is_dir()):
            _die("Build directory doesn't exist: %s", build_target)
        else:
            _LOG.info('Will build [%d/%d]: %s', i, len(build_commands), build_target)
    _LOG.debug('Patterns: %s', patterns)
    path_to_log = str(Path().resolve()).replace(str(Path.home()), '$HOME')
    ignore_patterns = (ignore_patterns_string.split(_WATCH_PATTERN_DELIMITER) if ignore_patterns_string else [])
    env = pw_cli.env.pigweed_environment()
    if env.PW_EMOJI:
        charset = _EMOJI_CHARSET
    else:
        charset = _ASCII_CHARSET
    event_handler = PigweedBuildWatcher(patterns=patterns.split(_WATCH_PATTERN_DELIMITER), ignore_patterns=ignore_patterns, build_commands=build_commands, charset=charset, restart=restart)
    try:
        _LOG.info('Attaching filesystem watcher to %s/...', path_to_log)
        observers = []
        for (path, rec) in minimal_watch_directories(Path.cwd(), exclude_list):
            observer = Observer()
            observer.schedule(event_handler, str(path), recursive=rec)
            observer.start()
            observers.append(observer)
        event_handler.debouncer.press('Triggering initial build...')
        for observer in observers:
            while observer.is_alive():
                observer.join(1)
    except (KeyboardInterrupt, EOFError):
        _exit_due_to_interrupt()
    except OSError as err:
        if (err.args[0] == _ERRNO_INOTIFY_LIMIT_REACHED):
            _exit_due_to_inotify_limit()
        else:
            raise err
    _LOG.critical('Should never get here')
    observer.join()