Blame doc/examples/list_obsoletes_plugin.py

Packit 6f3914
# Copyright (C) 2015  Red Hat, Inc.
Packit 6f3914
#
Packit 6f3914
# This copyrighted material is made available to anyone wishing to use,
Packit 6f3914
# modify, copy, or redistribute it subject to the terms and conditions of
Packit 6f3914
# the GNU General Public License v.2, or (at your option) any later version.
Packit 6f3914
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit 6f3914
# ANY WARRANTY expressed or implied, including the implied warranties of
Packit 6f3914
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 6f3914
# Public License for more details.  You should have received a copy of the
Packit 6f3914
# GNU General Public License along with this program; if not, write to the
Packit 6f3914
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 6f3914
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
Packit 6f3914
# source code or documentation are not subject to the GNU General Public
Packit 6f3914
# License and may only be used or replicated with the express permission of
Packit 6f3914
# Red Hat, Inc.
Packit 6f3914
Packit 6f3914
"""A plugin that lists installed packages that are obsolted by any available package"""
Packit 6f3914
Packit 6f3914
from dnf.i18n import _
Packit 6f3914
import dnf
Packit 6f3914
import dnf.cli
Packit 6f3914
Packit 6f3914
Packit 6f3914
# If you only plan to create a new dnf subcommand in a plugin
Packit 6f3914
# you can use @dnf.plugin.register_command decorator instead of creating
Packit 6f3914
# a Plugin class which only registers the command
Packit 6f3914
# (for full-fledged Plugin class see examples/install_plugin.py)
Packit 6f3914
@dnf.plugin.register_command
Packit 6f3914
class Command(dnf.cli.Command):
Packit 6f3914
Packit 6f3914
    """A command that lists packages installed on the system that are
Packit 6f3914
       obsoleted by packages in any known repository."""
Packit 6f3914
Packit 6f3914
    # An alias is needed to invoke the command from command line.
Packit 6f3914
    aliases = ['foo']  # <-- SET YOUR ALIAS HERE.
Packit 6f3914
Packit 6f3914
    @staticmethod
Packit 6f3914
    def set_argparser(parser):
Packit 6f3914
        parser.add_argument("package", nargs='*', metavar=_('PACKAGE'))
Packit 6f3914
Packit 6f3914
    def configure(self):
Packit 6f3914
        """Setup the demands."""
Packit 6f3914
        # Repositories serve as sources of information about packages.
Packit 6f3914
        self.cli.demands.available_repos = True
Packit 6f3914
        # A sack is needed for querying.
Packit 6f3914
        self.cli.demands.sack_activation = True
Packit 6f3914
Packit 6f3914
    def run(self):
Packit 6f3914
        """Run the command."""
Packit 6f3914
Packit 6f3914
        obs_tuples = []
Packit 6f3914
        # A query matches all available packages
Packit 6f3914
        q = self.base.sack.query()
Packit 6f3914
Packit 6f3914
        if not self.opts.package:
Packit 6f3914
            # Narrow down query to match only installed packages
Packit 6f3914
            inst = q.installed()
Packit 6f3914
            # A dictionary containing list of obsoleted packages
Packit 6f3914
            for new in q.filter(obsoletes=inst):
Packit 6f3914
                obs_reldeps = new.obsoletes
Packit 6f3914
                obsoleted = inst.filter(provides=obs_reldeps).run()
Packit 6f3914
                obs_tuples.extend([(new, old) for old in obsoleted])
Packit 6f3914
        else:
Packit 6f3914
            for pkg_spec in self.opts.package:
Packit 6f3914
                # A subject serves for parsing package format from user input
Packit 6f3914
                subj = dnf.subject.Subject(pkg_spec)
Packit 6f3914
                # A query restricted to installed packages matching given subject
Packit 6f3914
                inst = subj.get_best_query(self.base.sack).installed()
Packit 6f3914
                for new in q.filter(obsoletes=inst):
Packit 6f3914
                    obs_reldeps = new.obsoletes
Packit 6f3914
                    obsoleted = inst.filter(provides=obs_reldeps).run()
Packit 6f3914
                    obs_tuples.extend([(new, old) for old in obsoleted])
Packit 6f3914
Packit 6f3914
        if not obs_tuples:
Packit 6f3914
            raise dnf.exceptions.Error('No matching Packages to list')
Packit 6f3914
Packit 6f3914
        for (new, old) in obs_tuples:
Packit 6f3914
            print('%s.%s obsoletes %s.%s' %
Packit 6f3914
                  (new.name, new.arch, old.name, old.arch))
Packit 6f3914
Packit 6f3914