Blame doc/examples/install_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 ensures that given features are present."""
Packit 6f3914
Packit 6f3914
Packit 6f3914
import dnf.cli
Packit 6f3914
from dnf.i18n import _
Packit 6f3914
from dnf.cli.option_parser import OptionParser
Packit 6f3914
Packit 6f3914
# The parent class allows registration to the CLI manager.
Packit 6f3914
class Command(dnf.cli.Command):
Packit 6f3914
Packit 6f3914
    """A command that ensures that given features are present."""
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
    def configure(self):
Packit 6f3914
        """Setup the demands."""
Packit 6f3914
        # Repositories are needed if we want to install anything.
Packit 6f3914
        self.cli.demands.available_repos = True
Packit 6f3914
        # A sack is required by marking methods and dependency resolving.
Packit 6f3914
        self.cli.demands.sack_activation = True
Packit 6f3914
        # Resolving performs a transaction that installs the packages.
Packit 6f3914
        self.cli.demands.resolving = True
Packit 6f3914
        # Based on the system, privileges are required to do an installation.
Packit 6f3914
        self.cli.demands.root_user = True  # <-- SET YOUR FLAG HERE.
Packit 6f3914
Packit 6f3914
    @staticmethod
Packit 6f3914
    def set_argparser(parser):
Packit 6f3914
        """Parse command line arguments."""
Packit 6f3914
        parser.add_argument('package', nargs='+', metavar=_('PACKAGE'),
Packit 6f3914
                            action=OptionParser.ParseSpecGroupFileCallback,
Packit 6f3914
                            help=_('Package to install'))
Packit 6f3914
Packit 6f3914
    def run(self):
Packit 6f3914
        """Run the command."""
Packit 6f3914
        # Feature marking methods set the user request.
Packit 6f3914
        for ftr_spec in self.opts.pkg_specs:
Packit 6f3914
            try:
Packit 6f3914
                self.base.install(ftr_spec)
Packit 6f3914
            except dnf.exceptions.MarkingError:
Packit 6f3914
                raise dnf.exceptions.Error('feature(s) not found: ' + ftr_spec)
Packit 6f3914
        # Package marking methods set the user request.
Packit 6f3914
        for pkg in self.base.add_remote_rpms(self.opts.filenames, strict=False):
Packit 6f3914
            try:
Packit 6f3914
                self.base.package_install(pkg, strict=False)
Packit 6f3914
            except dnf.exceptions.MarkingError as e:
Packit 6f3914
                raise dnf.exceptions.Error(e)
Packit 6f3914
        # Comps data reading initializes the base.comps attribute.
Packit 6f3914
        if self.opts.grp_specs:
Packit 6f3914
            self.base.read_comps(arch_filter=True)
Packit 6f3914
        # Group marking methods set the user request.
Packit 6f3914
        for grp_spec in self.opts.grp_specs:
Packit 6f3914
            group = self.base.comps.group_by_pattern(grp_spec)
Packit 6f3914
            if not group:
Packit 6f3914
                raise dnf.exceptions.Error('group not found: ' + grp_spec)
Packit 6f3914
            self.base.group_install(group.id, ['mandatory', 'default'])
Packit 6f3914
Packit 6f3914
Packit 6f3914
# Every plugin must be a subclass of dnf.Plugin.
Packit 6f3914
class Plugin(dnf.Plugin):
Packit 6f3914
Packit 6f3914
    """A plugin that registers our custom command."""
Packit 6f3914
Packit 6f3914
    # Every plugin must provide its name.
Packit 6f3914
    name = 'foo'  # <-- SET YOUR NAME HERE.
Packit 6f3914
Packit 6f3914
    # Every plugin must provide its own initialization function.
Packit 6f3914
    def __init__(self, base, cli):
Packit 6f3914
        """Initialize the plugin."""
Packit 6f3914
        super(Plugin, self).__init__(base, cli)
Packit 6f3914
        if cli:
Packit 6f3914
            cli.register_command(Command)