Prune stale cache files

    If the option --cache-target-max-entry is greater than zero, then prune will remove all but n
    old cache files for each target/task.

    :param str root: The path under which cacheable artifacts will be cleaned
329171
def prune(self, root):
    

    max_entries_per_target = self._max_entries_per_target
    if os.path.isdir(root) and max_entries_per_target:
      safe_rm_oldest_items_in_dir(root, max_entries_per_target)
975313
def __clean_tmp(sfn):
    '''
    
    '''
    if sfn.startswith(os.path.join(tempfile.gettempdir(),
                                   salt.utils.files.TEMPFILE_PREFIX)):
        # Don't remove if it exists in file_roots (any saltenv)
        all_roots = itertools.chain.from_iterable(
                six.itervalues(__opts__['file_roots']))
        in_roots = any(sfn.startswith(root) for root in all_roots)
        # Only clean up files that exist
        if os.path.exists(sfn) and not in_roots:
            os.remove(sfn)
152853
private void pruneIfNeeded(int neededSpace) {
        if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
            return;
        }
        if (VolleyLog.DEBUG) {
            VolleyLog.v("Pruning old cache entries.");
        }

        long before = mTotalSize;
        int prunedFiles = 0;
        long startTime = System.currentTimeMillis();

        Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, CacheHeader> entry = iterator.next();
            CacheHeader e = entry.getValue();
            boolean deleted = getFileForKey(e.key).delete();
            if (deleted) {
                mTotalSize -= e.size;
            } else {
               VolleyLog.d("Could not delete cache entry for key=%s, filename=%s",
                       e.key, getFilenameForKey(e.key));
            }
            iterator.remove();
            prunedFiles++;

            if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
                break;
            }
        }

        if (VolleyLog.DEBUG) {
            VolleyLog.v("pruned %d files, %d bytes, %d ms",
                    prunedFiles, (mTotalSize - before), System.currentTimeMillis() - startTime);
        }
    }
1167197
def delete(self, cachedir=None, cfgstr=None, verbose=True or VERBOSE or util_arg.VERBOSE):
        
        fpath = self.get_fpath(cachedir, cfgstr=cfgstr)
        if verbose:
            print('[Cachable] cache delete: %r' % (basename(fpath),))
        os.remove(fpath)
964553
def sweep(self):
        '''
        
        '''
        if self.max_age < time.time() - self.timestamp:
            self.clear()
            self.timestamp = time.time()
        else:
            paterns = list(self.cache.values())
            paterns.sort()
            for idx in range(self.clear_size):
                del self.cache[paterns[idx][2]]
625017
def clean_tmpdir
      self.root_dir.each do |d|
        if d != '..' and d!='.'
          if d.to_i < Time.now.to_i-TIME_LIMIT
            FileUtils.rm_rf(File.join(self.root_dir.path, d))
          end
        end
      end
    end
23659
def prune_before(self, timestamp):
        
        n = 0
        pruned = []
        for r in self.resources:
            if (r.timestamp is None):
                raise Exception("Entry %s has no timestamp" % (r.uri))
            elif (r.timestamp >= timestamp):
                pruned.append(r)
            else:
                n += 1
        self.resources = pruned
        return(n)
1236743
def _cleanup(self):
        """"""
        for filename in os.listdir(self._storage_dir):
            file_path = path.join(self._storage_dir, filename)
            file_stat = os.stat(file_path)
            evaluate = max(file_stat.st_ctime, file_stat.st_mtime)
            if evaluate + self._duration < time.time():
                LOGGER.debug('Removing stale file: %s', file_path)
                os.unlink(file_path)
1018927
def prune_old(self):
        
        path = self.pubdir
        dirmask = self.dirmask
        expire = self.expire
        expire_limit = int(time.time()) - (86400 * expire)

        logger.info('Pruning directories older than %d days', expire)

        if not os.path.isdir(path):
            logger.warning('Dir %r not found -- skipping pruning', path)
            return

        for entry in os.listdir(path):
            logger.debug('Found: %r', entry)
            if os.path.isdir(os.path.join(path, entry)):
                try: 
                    stamp = time.mktime(time.strptime(entry, dirmask))
                except ValueError as e:
                    logger.info('Dir %r did not match dirmask %r: %r', entry, dirmask, e)
                    logger.info('Skipping %r', entry)
                    continue

                if stamp < expire_limit:
                    shutil.rmtree(os.path.join(path, entry))
                    logger.info('File Publisher: Pruned old dir: %r', entry)
                else:
                    logger.info('%r is still active', entry)
            else:
                logger.info('%r is not a directory. Skipping.', entry)

        logger.info('Finished with pruning')
1293482
def cleanup(used_cache_entries)
      used = Set.new(used_cache_entries)

      Dir[@cache_dir + "/*.dat"].each do |file|
        FileUtils.rm_rf(file) unless used.include?(file)
      end
    end
1083626
def remove_stale_symlinks(path)
      Dir.glob("#{path}/*").each { |f| File.unlink(f) if not File.exist?(f) }
    end
444360
public static void prune(TreeNodeVisitor root, PruningMethod method, ClassificationDataSet testSet)
    {
        //TODO add vargs for extra arguments that may be used by pruning methods
        if(method == PruningMethod.NONE )
            return;
        else if(method == PruningMethod.REDUCED_ERROR)
            pruneReduceError(null, -1, root, testSet);
        else if(method == PruningMethod.ERROR_BASED)
            pruneErrorBased(null, -1, root, testSet, 0.25);
        else
            throw new RuntimeException("BUG: please report");
    }
802379
def clear_caches(delete_all=False):
    
    global _time_caches

    if delete_all:
        _time_caches = []
        _parser = { "default": CodeParser() }
    else:
        # normally just kill the expired entries, not all
        for tc in _time_caches:
            # check time_cache for expired entries
            for key, (t, value) in list(tc.items()):
                if t < time.time():
                    # delete expired entries
                    del tc[key]
416223
def clear(self) -> None:
        
        if self._cache_directory is not None:
            # Safety - if there isn't a cache directory file, this probably isn't a valid cache
            assert os.path.exists(self._cache_directory_index), "Attempt to clear a non-existent cache"
            self._load()            # Shouldn't have any impact but...
            for e in self._cache.values():
                if os.path.exists(e.loc):
                    os.remove(e.loc)
            self._cache.clear()
            self._update()
        self._cache = {}
1175216
def clear_file_list_cache(self, load):
        '''
        
        '''
        if 'env' in load:
            # "env" is not supported; Use "saltenv".
            load.pop('env')

        saltenv = load.get('saltenv', [])
        if saltenv is not None:
            if not isinstance(saltenv, list):
                try:
                    saltenv = [x.strip() for x in saltenv.split(',')]
                except AttributeError:
                    saltenv = [x.strip() for x in six.text_type(saltenv).split(',')]

            for idx, val in enumerate(saltenv):
                if not isinstance(val, six.string_types):
                    saltenv[idx] = six.text_type(val)

        ret = {}
        fsb = self.backends(load.pop('fsbackend', None))
        list_cachedir = os.path.join(self.opts['cachedir'], 'file_lists')
        try:
            file_list_backends = os.listdir(list_cachedir)
        except OSError as exc:
            if exc.errno == errno.ENOENT:
                log.debug('No file list caches found')
                return {}
            else:
                log.error(
                    'Failed to get list of saltenvs for which the master has '
                    'cached file lists: %s', exc
                )

        for back in file_list_backends:
            # Account for the fact that the file_list cache directory for gitfs
            # is 'git', hgfs is 'hg', etc.
            back_virtualname = re.sub('fs$', '', back)
            try:
                cache_files = os.listdir(os.path.join(list_cachedir, back))
            except OSError as exc:
                log.error(
                    'Failed to find file list caches for saltenv \'%s\': %s',
                    back, exc
                )
                continue
            for cache_file in cache_files:
                try:
                    cache_saltenv, extension = cache_file.rsplit('.', 1)
                except ValueError:
                    # Filename has no dot in it. Not a cache file, ignore.
                    continue
                if extension != 'p':
                    # Filename does not end in ".p". Not a cache file, ignore.
                    continue
                elif back_virtualname not in fsb or \
                        (saltenv is not None and cache_saltenv not in saltenv):
                    log.debug(
                        'Skipping %s file list cache for saltenv \'%s\'',
                        back, cache_saltenv
                    )
                    continue
                try:
                    os.remove(os.path.join(list_cachedir, back, cache_file))
                except OSError as exc:
                    if exc.errno != errno.ENOENT:
                        log.error('Failed to remove %s: %s',
                                  exc.filename, exc.strerror)
                else:
                    ret.setdefault(back, []).append(cache_saltenv)
                    log.debug(
                        'Removed %s file list cache for saltenv \'%s\'',
                        cache_saltenv, back
                    )
        return ret
1171027
def _clear_cache(tgt=None,
                 tgt_type='glob',
                 clear_pillar_flag=False,
                 clear_grains_flag=False,
                 clear_mine_flag=False,
                 clear_mine_func_flag=None):
    '''
    
    '''
    if tgt is None:
        return False
    pillar_util = salt.utils.master.MasterPillarUtil(tgt, tgt_type,
                                                     use_cached_grains=True,
                                                     grains_fallback=False,
                                                     use_cached_pillar=True,
                                                     pillar_fallback=False,
                                                     opts=__opts__)
    return pillar_util.clear_cached_minion_data(clear_pillar=clear_pillar_flag,
                                                clear_grains=clear_grains_flag,
                                                clear_mine=clear_mine_flag,
                                                clear_mine_func=clear_mine_func_flag)
350450
def clean_cache(self, section=None):
        """"""
        self.remove_all_locks()
        if section is not None and "/" in section:
            raise ValueError("invalid section '{0}'".format(section))
        if section is not None:
            path = os.path.join(self._full_base, section)
        else:
            path = self._full_base
        if not os.path.exists(path):
            return
        shutil.rmtree(path)