Blame dnf/cli/commands/mark.py

Packit Service 21c75c
# mark.py
Packit Service 21c75c
# Mark CLI command.
Packit Service 21c75c
#
Packit Service 21c75c
# Copyright (C) 2015-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 print_function
Packit Service 21c75c
from __future__ import unicode_literals
Packit Service 21c75c
Packit Service 21c75c
import libdnf.transaction
Packit Service 21c75c
Packit Service 21c75c
from dnf.i18n import _
Packit Service 21c75c
from dnf.cli import commands
Packit Service 21c75c
Packit Service 21c75c
import dnf
Packit Service 21c75c
import functools
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 MarkCommand(commands.Command):
Packit Service 21c75c
Packit Service 21c75c
    aliases = ('mark',)
Packit Service 21c75c
    summary = _('mark or unmark installed packages as installed by user.')
Packit Service 21c75c
Packit Service 21c75c
    @staticmethod
Packit Service 21c75c
    def set_argparser(parser):
Packit Service 21c75c
        parser.add_argument('mark', nargs=1, choices=['install', 'remove', 'group'],
Packit Service 21c75c
                            help=_("install: mark as installed by user\n"
Packit Service 21c75c
                                   "remove: unmark as installed by user\n"
Packit Service 21c75c
                                   "group: mark as installed by group"))
Packit Service 21c75c
        parser.add_argument('package', nargs='+', metavar="PACKAGE",
Packit Service 21c75c
                            help=_("Package specification"))
Packit Service 21c75c
Packit Service 21c75c
    def _mark_install(self, pkg):
Packit Service 21c75c
        self.base.history.set_reason(pkg, libdnf.transaction.TransactionItemReason_USER)
Packit Service 21c75c
        logger.info(_('%s marked as user installed.'), str(pkg))
Packit Service 21c75c
Packit Service 21c75c
    def _mark_remove(self, pkg):
Packit Service 21c75c
        self.base.history.set_reason(pkg, libdnf.transaction.TransactionItemReason_DEPENDENCY)
Packit Service 21c75c
        logger.info(_('%s unmarked as user installed.'), str(pkg))
Packit Service 21c75c
Packit Service 21c75c
    def _mark_group(self, pkg):
Packit Service 21c75c
        self.base.history.set_reason(pkg, libdnf.transaction.TransactionItemReason_GROUP)
Packit Service 21c75c
        logger.info(_('%s marked as group installed.'), str(pkg))
Packit Service 21c75c
Packit Service 21c75c
    def configure(self):
Packit Service 21c75c
        demands = self.cli.demands
Packit Service 21c75c
        demands.sack_activation = True
Packit Service 21c75c
        demands.root_user = True
Packit Service 21c75c
        demands.available_repos = False
Packit Service 21c75c
        demands.resolving = False
Packit Service 21c75c
Packit Service 21c75c
    def run(self):
Packit Service 21c75c
        cmd = self.opts.mark[0]
Packit Service 21c75c
        pkgs = self.opts.package
Packit Service 21c75c
Packit Service 21c75c
        mark_func = functools.partial(getattr(self, '_mark_' + cmd))
Packit Service 21c75c
Packit Service 21c75c
        notfound = []
Packit Service 21c75c
        for pkg in pkgs:
Packit Service 21c75c
            subj = dnf.subject.Subject(pkg)
Packit Service 21c75c
            q = subj.get_best_query(self.base.sack)
Packit Service 21c75c
            for pkg in q:
Packit Service 21c75c
                mark_func(pkg)
Packit Service 21c75c
            if len(q) == 0:
Packit Service 21c75c
                notfound.append(pkg)
Packit Service 21c75c
Packit Service 21c75c
        if notfound:
Packit Service 21c75c
            logger.error(_('Error:'))
Packit Service 21c75c
            for pkg in notfound:
Packit Service 21c75c
                logger.error(_('Package %s is not installed.'), pkg)
Packit Service 21c75c
            raise dnf.cli.CliError
Packit Service 21c75c
Packit Service 21c75c
        old = self.base.history.last()
Packit Service 21c75c
        if old is None:
Packit Service 21c75c
            rpmdb_version = self.sack._rpmdb_version()
Packit Service 21c75c
        else:
Packit Service 21c75c
            rpmdb_version = old.end_rpmdb_version
Packit Service 21c75c
Packit Service 21c75c
        self.base.history.beg(rpmdb_version, [], [])
Packit Service 21c75c
        self.base.history.end(rpmdb_version)