Skip to content

Commit

Permalink
Command "clean": add support for "--allrepos" (RhBug:1278225)
Browse files Browse the repository at this point in the history
"--allrepos" is much powerfull then "--enablerepo=*" because it works
for all repositories. Eg. repositories for different release version
are included.
  • Loading branch information
jrohel committed Sep 12, 2017
1 parent 37af021 commit c319ed5
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions dnf/cli/commands/clean.py
Expand Up @@ -78,6 +78,13 @@ def _is_repo_cached(repo, files):
return bool(matches)


def _cached_repos(files):
"""Return the repo IDs that have some cached metadata around."""
metapat = dnf.repo.CACHE_FILES['metadata']
matches = (re.match(metapat, f) for f in files)
return set(m.group('repoid') for m in matches if m)


class CleanCommand(commands.Command):
"""A class containing methods needed by the cli to execute the
clean command.
Expand All @@ -88,12 +95,15 @@ class CleanCommand(commands.Command):

@staticmethod
def set_argparser(parser):
parser.add_argument('--allrepos', action='store_true',
dest='allrepos',
help=_('Clean all repositories'))
parser.add_argument('type', nargs='+',
choices=_CACHE_TYPES.keys(),
help=_('Metadata type to clean'))

def run(self):
if not self.base.repos._any_enabled():
if not self.opts.allrepos and not self.base.repos._any_enabled():
logger.info(_('There are no enabled repos. Nothing to do.'))
return
cachedir = self.base.conf.cachedir
Expand All @@ -108,16 +118,22 @@ def run(self):
logger.debug(_('Cleaning data: ' + ' '.join(types)))

if 'expire-cache' in types:
repos_expire = set()
for repo in self.base.repos.iter_enabled():
if _is_repo_cached(repo, files):
repos_expire.add(repo.id)
if self.opts.allrepos:
repos_expire = _cached_repos(files)
else:
repos_expire = set()
for repo in self.base.repos.iter_enabled():
if _is_repo_cached(repo, files):
repos_expire.add(repo.id)
self.base._repo_persistor.expired_to_add.update(repos_expire)
types.remove('expire-cache')
logger.info(_('Cache was expired'))

patterns = [dnf.repo.repo_cache_files_pattern(t, repo)
for t in types for repo in self.base.repos.iter_enabled()]
if self.opts.allrepos:
patterns = [dnf.repo.CACHE_FILES[t] for t in types]
else:
patterns = [dnf.repo.repo_cache_files_pattern(t, repo)
for t in types for repo in self.base.repos.iter_enabled()]
count = _clean(cachedir, _filter(files, patterns))
logger.info(P_('%d file removed', '%d files removed', count) % count)
return
Expand Down

0 comments on commit c319ed5

Please sign in to comment.