def _add_watch_for_path(self, path):
        # Must be called with _inotify_fd_lock held.
        logging.debug('_add_watch_for_path(%r)', path)

        if path not in self._directory_to_rootdir:  # a newly created dir, perhaps
            self._directory_to_rootdir[path] = (
                self._directory_to_rootdir[os.path.dirname(path)])

        # Get the skip-files-re that applies to this subtree, if any.
        rootdir = self._directory_to_rootdir[path]
        skip_files_re = self._skip_files_re.get(rootdir)

        for dirpath, directories, _ in itertools.chain(
            [(os.path.dirname(path), [os.path.basename(path)], None)],
                os.walk(path, topdown=True, followlinks=True)):
            relative_dirpath = os.path.relpath(dirpath, rootdir)
            if relative_dirpath == '.':
                relative_dirpath = ''
            if relative_dirpath != '..':  # never skip the top-level directory
                watcher_common.skip_ignored_dirs(directories, relative_dirpath,
                                                 skip_files_re)
            # TODO: this is not an ideal solution as there are other ways for
            # symlinks to confuse our algorithm but a general solution is going to
            # be very complex and this is good enough to solve the immediate problem
            # with Dart's directory structure.
            watcher_common.skip_local_symlinks(self._real_directories, dirpath,
                                               directories)
            for directory in directories:
                directory_path = os.path.join(dirpath, directory)
                # dirpath cannot be used as the parent directory path because it is the
                # empty string for symlinks :-(
                parent_path = os.path.dirname(directory_path)

                watch_descriptor = _libc.inotify_add_watch(
                    self._inotify_fd,
                    ctypes.create_string_buffer(directory_path),
                    _INTERESTING_INOTIFY_EVENTS)
                if watch_descriptor < 0:
                    if ctypes.get_errno() == errno.ENOSPC:
                        logging.warning(
                            'There are too many directories in your application for '
                            'changes in all of them to be monitored. You may have to '
                            'restart the development server to see some changes to your '
                            'files.')
                        return
                    error = OSError('could not add watch for %r' %
                                    directory_path)
                    error.errno = ctypes.get_errno()
                    error.strerror = errno.errorcode[ctypes.get_errno()]
                    error.filename = directory_path
                    raise error

                if parent_path in self._directory_to_subdirs:
                    self._directory_to_subdirs[parent_path].add(directory_path)
                self._watch_to_directory[watch_descriptor] = directory_path
                self._directory_to_watch_descriptor[
                    directory_path] = watch_descriptor
                self._directory_to_subdirs[directory_path] = set()
                self._directory_to_rootdir[directory_path] = (
                    self._directory_to_rootdir[path])
  def _add_watch_for_path(self, path):
    # Must be called with _inotify_fd_lock held.
    logging.debug('_add_watch_for_path(%r)', path)

    if path not in self._directory_to_rootdir:   # a newly created dir, perhaps
      self._directory_to_rootdir[path] = (
        self._directory_to_rootdir[os.path.dirname(path)])

    # Get the skip-files-re that applies to this subtree, if any.
    rootdir = self._directory_to_rootdir[path]
    skip_files_re = self._skip_files_re.get(rootdir)

    for dirpath, directories, _ in itertools.chain(
        [(os.path.dirname(path), [os.path.basename(path)], None)],
        os.walk(path, topdown=True, followlinks=True)):
      relative_dirpath = os.path.relpath(dirpath, rootdir)
      if relative_dirpath == '.':
        relative_dirpath = ''
      if relative_dirpath != '..':     # never skip the top-level directory
        watcher_common.skip_ignored_dirs(directories, relative_dirpath,
                                         skip_files_re)
      # TODO: this is not an ideal solution as there are other ways for
      # symlinks to confuse our algorithm but a general solution is going to
      # be very complex and this is good enough to solve the immediate problem
      # with Dart's directory structure.
      watcher_common.skip_local_symlinks(
          self._real_directories, dirpath, directories)
      for directory in directories:
        directory_path = os.path.join(dirpath, directory)
        # dirpath cannot be used as the parent directory path because it is the
        # empty string for symlinks :-(
        parent_path = os.path.dirname(directory_path)

        watch_descriptor = _libc.inotify_add_watch(
            self._inotify_fd,
            ctypes.create_string_buffer(directory_path),
            _INTERESTING_INOTIFY_EVENTS)
        if watch_descriptor < 0:
          if ctypes.get_errno() == errno.ENOSPC:
            logging.warning(
                'There are too many directories in your application for '
                'changes in all of them to be monitored. You may have to '
                'restart the development server to see some changes to your '
                'files.')
            return
          error = OSError('could not add watch for %r' % directory_path)
          error.errno = ctypes.get_errno()
          error.strerror = errno.errorcode[ctypes.get_errno()]
          error.filename = directory_path
          raise error

        if parent_path in self._directory_to_subdirs:
          self._directory_to_subdirs[parent_path].add(directory_path)
        self._watch_to_directory[watch_descriptor] = directory_path
        self._directory_to_watch_descriptor[directory_path] = watch_descriptor
        self._directory_to_subdirs[directory_path] = set()
        self._directory_to_rootdir[directory_path] = (
          self._directory_to_rootdir[path])
    def _add_watch_for_path(self, path):
        # Must be called with _inotify_fd_lock held.
        logging.debug("_add_watch_for_path(%r)", path)

        for dirpath, directories, _ in itertools.chain(
            [(os.path.dirname(path), [os.path.basename(path)], None)], os.walk(path, topdown=True, followlinks=True)
        ):
            watcher_common.skip_ignored_dirs(directories)
            # TODO: this is not an ideal solution as there are other ways for
            # symlinks to confuse our algorithm but a general solution is going to
            # be very complex and this is good enough to solve the immediate problem
            # with Dart's directory structure.
            watcher_common.skip_local_symlinks(self._real_directories, dirpath, directories)
            for directory in directories:
                directory_path = os.path.join(dirpath, directory)
                # dirpath cannot be used as the parent directory path because it is the
                # empty string for symlinks :-(
                parent_path = os.path.dirname(directory_path)

                watch_descriptor = _libc.inotify_add_watch(
                    self._inotify_fd, ctypes.create_string_buffer(directory_path), _INTERESTING_INOTIFY_EVENTS
                )
                if watch_descriptor < 0:
                    if ctypes.get_errno() == errno.ENOSPC:
                        logging.warning(
                            "There are too many directories in your application for "
                            "changes in all of them to be monitored. You may have to "
                            "restart the development server to see some changes to your "
                            "files."
                        )
                        return
                    error = OSError("could not add watch for %r" % directory_path)
                    error.errno = ctypes.get_errno()
                    error.strerror = errno.errorcode[ctypes.get_errno()]
                    error.filename = directory_path
                    raise error

                if parent_path in self._directory_to_subdirs:
                    self._directory_to_subdirs[parent_path].add(directory_path)
                self._watch_to_directory[watch_descriptor] = directory_path
                self._directory_to_watch_descriptor[directory_path] = watch_descriptor
                self._directory_to_subdirs[directory_path] = set()