Exemple #1
0
  def trim(self, min_free_space):
    """Purges cache.

    Removes cache directories that were not accessed for a long time
    until there is enough free space and the number of caches is sane.

    If min_free_space is None, disk free space is not checked.

    Requires NamedCache to be open.
    """
    self._lock.assert_locked()
    if not os.path.isdir(self.root_dir):
      return

    free_space = 0
    if min_free_space is not None:
      file_path.get_free_space(self.root_dir)
    while ((min_free_space is not None and free_space < min_free_space)
           or len(self._lru) > MAX_CACHE_SIZE):
      try:
        name, (path, _) = self._lru.get_oldest()
      except KeyError:
        return
      named_dir = self._get_named_path(name)
      if fs.islink(named_dir):
        fs.unlink(named_dir)
      path_abs = os.path.join(self.root_dir, path)
      if os.path.isdir(path_abs):
        file_path.rmtree(path_abs)
      if min_free_space is not None:
        free_space = file_path.get_free_space(self.root_dir)
      self._lru.pop(name)
Exemple #2
0
    def delete_symlinks(self, root, named_caches):
        """Deletes symlinks from |root| for the specified named_caches.

    named_caches must be a list of (name, path) tuples.
    """
        for name, path in named_caches:
            logging.info('Unlinking named cache "%s"', name)
            try:
                _validate_named_cache_path(path)
                symlink_path = os.path.abspath(os.path.join(root, path))
                fs.unlink(symlink_path)
            except (OSError, Error) as ex:
                raise Error('cannot unlink cache named "%s" at "%s": %s' %
                            (name, symlink_path, ex))
Exemple #3
0
    def trim(self, min_free_space):
        """Purges cache.

    Removes cache directories that were not accessed for a long time
    until there is enough free space and the number of caches is sane.

    If min_free_space is None, disk free space is not checked.

    Requires NamedCache to be open.

    Returns:
      Number of caches deleted.
    """
        self._lock.assert_locked()
        if not os.path.isdir(self.root_dir):
            return 0

        total = 0
        free_space = 0
        if min_free_space:
            free_space = file_path.get_free_space(self.root_dir)
        while ((min_free_space and free_space < min_free_space)
               or len(self._lru) > MAX_CACHE_SIZE):
            logging.info('Making space for named cache %s > %s or %s > %s',
                         free_space, min_free_space, len(self._lru),
                         MAX_CACHE_SIZE)
            try:
                name, (path, _) = self._lru.get_oldest()
            except KeyError:
                return total
            named_dir = self._get_named_path(name)
            if fs.islink(named_dir):
                fs.unlink(named_dir)
            path_abs = os.path.join(self.root_dir, path)
            if os.path.isdir(path_abs):
                logging.info('Removing named cache %s', path_abs)
                file_path.rmtree(path_abs)
            if min_free_space:
                free_space = file_path.get_free_space(self.root_dir)
            self._lru.pop(name)
            total += 1
        return total
Exemple #4
0
    def _remove(self, name):
        """Removes a cache directory and entry.

    Returns:
      Number of caches deleted.
    """
        self._lock.assert_locked()
        # First try to remove the alias if it exists.
        named_dir = self._get_named_path(name)
        if fs.islink(named_dir):
            fs.unlink(named_dir)

        # Then remove the actual data.
        if name not in self._lru:
            return
        rel_path, _size = self._lru.get(name)
        abs_path = os.path.join(self.cache_dir, rel_path)
        if os.path.isdir(abs_path):
            file_path.rmtree(abs_path)
        self._lru.pop(name)
Exemple #5
0
    def _remove(self, name):
        """Removes a cache directory and entry.

    NamedCache must be open.

    Returns:
      Number of caches deleted.
    """
        self._lock.assert_locked()
        rel_path = self._lru.get(name)
        if not rel_path:
            return

        named_dir = self._get_named_path(name)
        if fs.islink(named_dir):
            fs.unlink(named_dir)

        abs_path = os.path.join(self.root_dir, rel_path)
        if os.path.isdir(abs_path):
            file_path.rmtree(abs_path)
        self._lru.pop(name)
Exemple #6
0
  def test_fileobj_path(self):
    # No path on in-memory objects
    self.assertIs(None, isolateserver.fileobj_path(io.BytesIO('hello')))

    # Path on opened files
    thisfile = os.path.join(test_env.TESTS_DIR, 'isolateserver_test.py')
    f = fs.open(thisfile)
    result = isolateserver.fileobj_path(f)
    self.assertIsInstance(result, six.text_type)
    self.assertSequenceEqual(result, thisfile)

    # Path on temporary files
    tf = tempfile.NamedTemporaryFile()
    result = isolateserver.fileobj_path(tf)
    self.assertIsInstance(result, six.text_type)
    self.assertSequenceEqual(result, tf.name)

    # No path on files which are no longer on the file system
    tf = tempfile.NamedTemporaryFile(delete=False)
    fs.unlink(tf.name.decode(sys.getfilesystemencoding()))
    self.assertIs(None, isolateserver.fileobj_path(tf))