|
Packit Service |
21c75c |
# remove_command.py
|
|
Packit Service |
21c75c |
# Remove CLI command.
|
|
Packit Service |
21c75c |
#
|
|
Packit Service |
21c75c |
# Copyright (C) 2012-2016 Red Hat, Inc.
|
|
Packit Service |
21c75c |
#
|
|
Packit Service |
21c75c |
# This copyrighted material is made available to anyone wishing to use,
|
|
Packit Service |
21c75c |
# modify, copy, or redistribute it subject to the terms and conditions of
|
|
Packit Service |
21c75c |
# the GNU General Public License v.2, or (at your option) any later version.
|
|
Packit Service |
21c75c |
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
Packit Service |
21c75c |
# ANY WARRANTY expressed or implied, including the implied warranties of
|
|
Packit Service |
21c75c |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
Packit Service |
21c75c |
# Public License for more details. You should have received a copy of the
|
|
Packit Service |
21c75c |
# GNU General Public License along with this program; if not, write to the
|
|
Packit Service |
21c75c |
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit Service |
21c75c |
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
|
|
Packit Service |
21c75c |
# source code or documentation are not subject to the GNU General Public
|
|
Packit Service |
21c75c |
# License and may only be used or replicated with the express permission of
|
|
Packit Service |
21c75c |
# Red Hat, Inc.
|
|
Packit Service |
21c75c |
#
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
from __future__ import absolute_import
|
|
Packit Service |
21c75c |
from __future__ import unicode_literals
|
|
Packit Service |
21c75c |
from dnf.cli import commands
|
|
Packit Service |
21c75c |
from dnf.i18n import _
|
|
Packit Service |
21c75c |
from dnf.cli.option_parser import OptionParser
|
|
Packit Service |
21c75c |
import dnf.base
|
|
Packit Service |
21c75c |
import argparse
|
|
Packit Service |
21c75c |
import hawkey
|
|
Packit Service |
21c75c |
import dnf.exceptions
|
|
Packit Service |
21c75c |
import logging
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
logger = logging.getLogger("dnf")
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
class RemoveCommand(commands.Command):
|
|
Packit Service |
21c75c |
"""Remove command."""
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
nevra_forms = {'remove-n': hawkey.FORM_NAME,
|
|
Packit Service |
21c75c |
'remove-na': hawkey.FORM_NA,
|
|
Packit Service |
21c75c |
'remove-nevra': hawkey.FORM_NEVRA,
|
|
Packit Service |
21c75c |
'erase-n': hawkey.FORM_NAME,
|
|
Packit Service |
21c75c |
'erase-na': hawkey.FORM_NA,
|
|
Packit Service |
21c75c |
'erase-nevra': hawkey.FORM_NEVRA}
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
aliases = ('remove', 'erase', 'rm') + tuple(nevra_forms.keys())
|
|
Packit Service |
21c75c |
summary = _('remove a package or packages from your system')
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
@staticmethod
|
|
Packit Service |
21c75c |
def set_argparser(parser):
|
|
Packit Service |
21c75c |
mgroup = parser.add_mutually_exclusive_group()
|
|
Packit Service |
21c75c |
mgroup.add_argument('--duplicates', action='store_true',
|
|
Packit Service |
21c75c |
dest='duplicated',
|
|
Packit Service |
21c75c |
help=_('remove duplicated packages'))
|
|
Packit Service |
21c75c |
mgroup.add_argument('--duplicated', action='store_true',
|
|
Packit Service |
21c75c |
help=argparse.SUPPRESS)
|
|
Packit Service |
21c75c |
mgroup.add_argument('--oldinstallonly', action='store_true',
|
|
Packit Service |
21c75c |
help=_(
|
|
Packit Service |
21c75c |
'remove installonly packages over the limit'))
|
|
Packit Service |
21c75c |
parser.add_argument('packages', nargs='*', help=_('Package to remove'),
|
|
Packit Service |
21c75c |
action=OptionParser.ParseSpecGroupFileCallback,
|
|
Packit Service |
21c75c |
metavar=_('PACKAGE'))
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def configure(self):
|
|
Packit Service |
21c75c |
demands = self.cli.demands
|
|
Packit Service |
21c75c |
# disable all available repos to delete whole dependency tree
|
|
Packit Service |
21c75c |
# instead of replacing removable package with available packages
|
|
Packit Service |
21c75c |
demands.resolving = True
|
|
Packit Service |
21c75c |
demands.root_user = True
|
|
Packit Service |
21c75c |
demands.sack_activation = True
|
|
Packit Service |
21c75c |
if self.opts.duplicated:
|
|
Packit Service |
21c75c |
demands.available_repos = True
|
|
Packit Service |
21c75c |
elif dnf.base.WITH_MODULES and self.opts.grp_specs:
|
|
Packit Service |
21c75c |
demands.available_repos = True
|
|
Packit Service |
21c75c |
demands.fresh_metadata = False
|
|
Packit Service |
21c75c |
demands.allow_erasing = True
|
|
Packit Service |
21c75c |
else:
|
|
Packit Service |
21c75c |
demands.allow_erasing = True
|
|
Packit Service |
21c75c |
demands.available_repos = False
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def run(self):
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
forms = []
|
|
Packit Service |
21c75c |
if self.opts.command in self.nevra_forms:
|
|
Packit Service |
21c75c |
forms = [self.nevra_forms[self.opts.command]]
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
# local pkgs not supported in erase command
|
|
Packit Service |
21c75c |
self.opts.pkg_specs += self.opts.filenames
|
|
Packit Service |
21c75c |
done = False
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
if self.opts.duplicated:
|
|
Packit Service |
21c75c |
q = self.base.sack.query()
|
|
Packit Service |
21c75c |
instonly = self.base._get_installonly_query(q.installed())
|
|
Packit Service |
21c75c |
dups = q.duplicated().difference(instonly)
|
|
Packit Service |
21c75c |
if not dups:
|
|
Packit Service |
21c75c |
raise dnf.exceptions.Error(_('No duplicated packages found for removal.'))
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
for (name, arch), pkgs_list in dups._na_dict().items():
|
|
Packit Service |
21c75c |
if len(pkgs_list) < 2:
|
|
Packit Service |
21c75c |
continue
|
|
Packit Service |
21c75c |
pkgs_list.sort(reverse=True)
|
|
Packit Service |
21c75c |
try:
|
|
Packit Service |
21c75c |
self.base.reinstall(str(pkgs_list[0]))
|
|
Packit Service |
21c75c |
except dnf.exceptions.PackagesNotAvailableError:
|
|
Packit Service |
21c75c |
xmsg = ''
|
|
Packit Service |
21c75c |
msg = _('Installed package %s%s not available.')
|
|
Packit Service |
21c75c |
logger.warning(msg, self.base.output.term.bold(str(pkgs_list[0])), xmsg)
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
for pkg in pkgs_list[1:]:
|
|
Packit Service |
21c75c |
self.base.package_remove(pkg)
|
|
Packit Service |
21c75c |
return
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
if self.opts.oldinstallonly:
|
|
Packit Service |
21c75c |
q = self.base.sack.query()
|
|
Packit Service |
21c75c |
instonly = self.base._get_installonly_query(q.installed()).latest(-1)
|
|
Packit Service |
21c75c |
# also remove running kernel from the set
|
|
Packit Service |
21c75c |
kernel = self.base.sack.get_running_kernel()
|
|
Packit Service |
21c75c |
if kernel is not None:
|
|
Packit Service |
21c75c |
running_installonly = instonly.filter(
|
|
Packit Service |
21c75c |
epoch=kernel.epoch, version=kernel.version, release=kernel.release)
|
|
Packit Service |
21c75c |
if running_installonly:
|
|
Packit Service |
21c75c |
instonly = instonly.difference(running_installonly)
|
|
Packit Service |
21c75c |
if instonly:
|
|
Packit Service |
21c75c |
for pkg in instonly:
|
|
Packit Service |
21c75c |
self.base.package_remove(pkg)
|
|
Packit Service |
21c75c |
else:
|
|
Packit Service |
21c75c |
raise dnf.exceptions.Error(
|
|
Packit Service |
21c75c |
_('No old installonly packages found for removal.'))
|
|
Packit Service |
21c75c |
return
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
# Remove groups.
|
|
Packit Service |
21c75c |
if self.opts.grp_specs and forms:
|
|
Packit Service |
21c75c |
for grp_spec in self.opts.grp_specs:
|
|
Packit Service |
21c75c |
msg = _('Not a valid form: %s')
|
|
Packit Service |
21c75c |
logger.warning(msg, self.base.output.term.bold(grp_spec))
|
|
Packit Service |
21c75c |
elif self.opts.grp_specs:
|
|
Packit Service |
21c75c |
if dnf.base.WITH_MODULES:
|
|
Packit Service |
21c75c |
module_base = dnf.module.module_base.ModuleBase(self.base)
|
|
Packit Service |
21c75c |
skipped_grps = module_base.remove(self.opts.grp_specs)
|
|
Packit Service |
21c75c |
if len(self.opts.grp_specs) != len(skipped_grps):
|
|
Packit Service |
21c75c |
done = True
|
|
Packit Service |
21c75c |
else:
|
|
Packit Service |
21c75c |
skipped_grps = self.opts.grp_specs
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
if skipped_grps:
|
|
Packit Service |
21c75c |
for group in skipped_grps:
|
|
Packit Service |
21c75c |
try:
|
|
Packit Service |
21c75c |
if self.base.env_group_remove([group]):
|
|
Packit Service |
21c75c |
done = True
|
|
Packit Service |
21c75c |
except dnf.exceptions.Error:
|
|
Packit Service |
21c75c |
pass
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
for pkg_spec in self.opts.pkg_specs:
|
|
Packit Service |
21c75c |
try:
|
|
Packit Service |
21c75c |
self.base.remove(pkg_spec, forms=forms)
|
|
Packit Service |
21c75c |
except dnf.exceptions.MarkingError as e:
|
|
Packit Service |
21c75c |
msg = '{}: {}'.format(e.value, self.base.output.term.bold(pkg_spec))
|
|
Packit Service |
21c75c |
logger.info(msg)
|
|
Packit Service |
21c75c |
else:
|
|
Packit Service |
21c75c |
done = True
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
if not done:
|
|
Packit Service |
21c75c |
logger.warning(_('No packages marked for removal.'))
|