Blame plugins/debuginfo-install.py

Packit 3a9065
# debuginfo-install.py
Packit 3a9065
# Install the debuginfo of packages and their dependencies to debug this package.
Packit 3a9065
#
Packit 3a9065
# Copyright (C) 2014 Igor Gnatenko
Packit 3a9065
# Copyright (C) 2014-2019 Red Hat
Packit 3a9065
#
Packit 3a9065
# This copyrighted material is made available to anyone wishing to use,
Packit 3a9065
# modify, copy, or redistribute it subject to the terms and conditions of
Packit 3a9065
# the GNU General Public License v.2, or (at your option) any later version.
Packit 3a9065
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit 3a9065
# ANY WARRANTY expressed or implied, including the implied warranties of
Packit 3a9065
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Packit 3a9065
# Public License for more details. You should have received a copy of the
Packit 3a9065
# GNU General Public License along with this program; if not, write to the
Packit 3a9065
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 3a9065
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
Packit 3a9065
# source code or documentation are not subject to the GNU General Public
Packit 3a9065
# License and may only be used or replicated with the express permission of
Packit 3a9065
# Red Hat, Inc.
Packit 3a9065
#
Packit 3a9065
Packit 3a9065
from dnfpluginscore import _, logger
Packit 3a9065
Packit 3a9065
import dnf
Packit 3a9065
from dnf.package import Package
Packit 3a9065
Packit 3a9065
class DebuginfoInstall(dnf.Plugin):
Packit 3a9065
    """DNF plugin supplying the 'debuginfo-install' command."""
Packit 3a9065
Packit 3a9065
    name = 'debuginfo-install'
Packit 3a9065
Packit 3a9065
    def __init__(self, base, cli):
Packit 3a9065
        """Initialize the plugin instance."""
Packit 3a9065
        super(DebuginfoInstall, self).__init__(base, cli)
Packit 3a9065
        self.base = base
Packit 3a9065
        self.cli = cli
Packit 3a9065
        if cli is not None:
Packit 3a9065
            cli.register_command(DebuginfoInstallCommand)
Packit 3a9065
Packit 3a9065
    def config(self):
Packit 3a9065
        cp = self.read_config(self.base.conf)
Packit 3a9065
        autoupdate = (cp.has_section('main')
Packit 3a9065
                      and cp.has_option('main', 'autoupdate')
Packit 3a9065
                      and cp.getboolean('main', 'autoupdate'))
Packit 3a9065
Packit 3a9065
        if autoupdate:
Packit 3a9065
            # allow update of already installed debuginfo packages
Packit 3a9065
            dbginfo = dnf.sack._rpmdb_sack(self.base).query().filterm(name__glob="*-debuginfo")
Packit 3a9065
            if len(dbginfo):
Packit 3a9065
                self.base.repos.enable_debug_repos()
Packit 3a9065
Packit 3a9065
class DebuginfoInstallCommand(dnf.cli.Command):
Packit 3a9065
    """ DebuginfoInstall plugin for DNF """
Packit 3a9065
Packit 3a9065
    aliases = ("debuginfo-install",)
Packit 3a9065
    summary = _('install debuginfo packages')
Packit 3a9065
Packit 3a9065
    def __init__(self, cli):
Packit 3a9065
        super(DebuginfoInstallCommand, self).__init__(cli)
Packit 3a9065
Packit 3a9065
        self.available_debuginfo_missing = set()
Packit 3a9065
        self.available_debugsource_missing = set()
Packit 3a9065
        self.installed_debuginfo_missing = set()
Packit 3a9065
        self.installed_debugsource_missing = set()
Packit 3a9065
Packit 3a9065
    @staticmethod
Packit 3a9065
    def set_argparser(parser):
Packit 3a9065
        parser.add_argument('package', nargs='+')
Packit 3a9065
Packit 3a9065
    def configure(self):
Packit 3a9065
        demands = self.cli.demands
Packit 3a9065
        demands.resolving = True
Packit 3a9065
        demands.root_user = True
Packit 3a9065
        demands.sack_activation = True
Packit 3a9065
        demands.available_repos = True
Packit 3a9065
        self.base.repos.enable_debug_repos()
Packit 3a9065
Packit 3a9065
    def run(self):
Packit 3a9065
        errors_spec = []
Packit 3a9065
Packit 3a9065
        debuginfo_suffix_len = len(Package.DEBUGINFO_SUFFIX)
Packit 3a9065
        debugsource_suffix_len = len(Package.DEBUGSOURCE_SUFFIX)
Packit 3a9065
Packit 3a9065
        for pkgspec in self.opts.package:
Packit 3a9065
            solution = dnf.subject.Subject(pkgspec).get_best_solution(self.base.sack,
Packit 3a9065
                                                                      with_src=False)
Packit 3a9065
Packit 3a9065
            query = solution["query"]
Packit 3a9065
            if not query:
Packit 3a9065
                logger.info(_('No match for argument: %s'), self.base.output.term.bold(pkgspec))
Packit 3a9065
                errors_spec.append(pkgspec)
Packit 3a9065
                continue
Packit 3a9065
Packit 3a9065
            package_dict = query.available()._name_dict()
Packit 3a9065
            # installed versions of packages have priority, replace / add them to the dict
Packit 3a9065
            package_dict.update(query.installed()._name_dict())
Packit 3a9065
Packit 3a9065
            # Remove debuginfo packages if their base packages are in the query.
Packit 3a9065
            # They can get there through globs and they break the installation
Packit 3a9065
            # of debug packages with the same version as the installed base
Packit 3a9065
            # packages. If the base package of a debuginfo package is not in
Packit 3a9065
            # the query, the user specified a debug package on the command
Packit 3a9065
            # line. We don't want to ignore those, so we will install them.
Packit 3a9065
            # But, in this case the version will not be matched to the
Packit 3a9065
            # installed version of the base package, as that would require
Packit 3a9065
            # another query and is further complicated if the user specifies a
Packit 3a9065
            # version themselves etc.
Packit 3a9065
            for name in list(package_dict.keys()):
Packit 3a9065
                if name.endswith(Package.DEBUGINFO_SUFFIX):
Packit 3a9065
                    if name[:-debuginfo_suffix_len] in package_dict:
Packit 3a9065
                        package_dict.pop(name)
Packit 3a9065
                if name.endswith(Package.DEBUGSOURCE_SUFFIX):
Packit 3a9065
                    if name[:-debugsource_suffix_len] in package_dict:
Packit 3a9065
                        package_dict.pop(name)
Packit 3a9065
Packit 3a9065
            # attempt to install debuginfo and debugsource for the highest
Packit 3a9065
            # listed version of the package (in case the package is installed,
Packit 3a9065
            # only the installed version is listed)
Packit 3a9065
            for pkgs in package_dict.values():
Packit 3a9065
                first_pkg = pkgs[0]
Packit 3a9065
Packit 3a9065
                # for packages from system (installed) there can be more
Packit 3a9065
                # packages with different architectures listed and we want to
Packit 3a9065
                # install debuginfo for all of them
Packit 3a9065
                if first_pkg._from_system:
Packit 3a9065
                    # we need to split them by architectures and install the
Packit 3a9065
                    # latest version for each architecture
Packit 3a9065
                    arch_dict = {}
Packit 3a9065
Packit 3a9065
                    for pkg in pkgs:
Packit 3a9065
                        arch_dict.setdefault(pkg.arch, []).append(pkg)
Packit 3a9065
Packit 3a9065
                    for package_arch_list in arch_dict.values():
Packit 3a9065
                        pkg = package_arch_list[0]
Packit 3a9065
Packit 3a9065
                        if not self._install_debug_from_system(pkg.debug_name, pkg):
Packit 3a9065
                            if not self._install_debug_from_system(pkg.source_debug_name, pkg):
Packit 3a9065
                                self.installed_debuginfo_missing.add(str(pkg))
Packit 3a9065
Packit 3a9065
                        if not self._install_debug_from_system(pkg.debugsource_name, pkg):
Packit 3a9065
                            self.installed_debugsource_missing.add(str(pkg))
Packit 3a9065
Packit 3a9065
                    continue
Packit 3a9065
Packit 3a9065
                # if the package in question is -debuginfo or -debugsource, install it directly
Packit 3a9065
                if first_pkg.name.endswith(Package.DEBUGINFO_SUFFIX) \
Packit 3a9065
                        or first_pkg.name.endswith(Package.DEBUGSOURCE_SUFFIX):
Packit 3a9065
Packit 3a9065
                    self._install(pkgs)  # pass all pkgs to the solver, it will pick the best one
Packit 3a9065
                    continue
Packit 3a9065
Packit 3a9065
                # if we have NEVRA parsed from the pkgspec, use it to install the package
Packit 3a9065
                if solution["nevra"] is not None:
Packit 3a9065
                    if not self._install_debug(first_pkg.debug_name, solution["nevra"]):
Packit 3a9065
                        if not self._install_debug(first_pkg.source_debug_name, solution["nevra"]):
Packit 3a9065
                            self.available_debuginfo_missing.add(
Packit 3a9065
                                "{}-{}".format(first_pkg.name, first_pkg.evr))
Packit 3a9065
Packit 3a9065
                    if not self._install_debug(first_pkg.debugsource_name, solution["nevra"]):
Packit 3a9065
                        self.available_debugsource_missing.add(
Packit 3a9065
                            "{}-{}".format(first_pkg.name, first_pkg.evr))
Packit 3a9065
Packit 3a9065
                    continue
Packit 3a9065
Packit 3a9065
                # if we don't have NEVRA from the pkgspec, pass nevras from
Packit 3a9065
                # all packages that were found (while replacing the name with
Packit 3a9065
                # the -debuginfo and -debugsource variant) to the solver, which
Packit 3a9065
                # will pick the correct version and architecture
Packit 3a9065
                if not self._install_debug_no_nevra(first_pkg.debug_name, pkgs):
Packit 3a9065
                    if not self._install_debug_no_nevra(first_pkg.source_debug_name, pkgs):
Packit 3a9065
                        self.available_debuginfo_missing.add(
Packit 3a9065
                            "{}-{}".format(first_pkg.name, first_pkg.evr))
Packit 3a9065
Packit 3a9065
                if not self._install_debug_no_nevra(first_pkg.debugsource_name, pkgs):
Packit 3a9065
                    self.available_debugsource_missing.add(
Packit 3a9065
                        "{}-{}".format(first_pkg.name, first_pkg.evr))
Packit 3a9065
Packit 3a9065
        if self.available_debuginfo_missing:
Packit 3a9065
            logger.info(
Packit 3a9065
                _("Could not find debuginfo package for the following available packages: %s"),
Packit 3a9065
                ", ".join(sorted(self.available_debuginfo_missing)))
Packit 3a9065
Packit 3a9065
        if self.available_debugsource_missing:
Packit 3a9065
            logger.info(
Packit 3a9065
                _("Could not find debugsource package for the following available packages: %s"),
Packit 3a9065
                ", ".join(sorted(self.available_debugsource_missing)))
Packit 3a9065
Packit 3a9065
        if self.installed_debuginfo_missing:
Packit 3a9065
            logger.info(
Packit 3a9065
                _("Could not find debuginfo package for the following installed packages: %s"),
Packit 3a9065
                ", ".join(sorted(self.installed_debuginfo_missing)))
Packit 3a9065
Packit 3a9065
        if self.installed_debugsource_missing:
Packit 3a9065
            logger.info(
Packit 3a9065
                _("Could not find debugsource package for the following installed packages: %s"),
Packit 3a9065
                ", ".join(sorted(self.installed_debugsource_missing)))
Packit 3a9065
Packit 3a9065
        if errors_spec and self.base.conf.strict:
Packit 3a9065
            raise dnf.exceptions.PackagesNotAvailableError(_("Unable to find a match"),
Packit 3a9065
                                                           pkg_spec=' '.join(errors_spec))
Packit 3a9065
Packit 3a9065
    def _install_debug_from_system(self, debug_name, pkg):
Packit 3a9065
        query = self.base.sack.query().filter(name=debug_name,
Packit 3a9065
                                              epoch=pkg.epoch,
Packit 3a9065
                                              version=pkg.version,
Packit 3a9065
                                              release=pkg.release,
Packit 3a9065
                                              arch=pkg.arch)
Packit 3a9065
Packit 3a9065
        if query:
Packit 3a9065
            self._install(query)
Packit 3a9065
            return True
Packit 3a9065
Packit 3a9065
        return False
Packit 3a9065
Packit 3a9065
    def _install_debug(self, debug_name, base_nevra):
Packit 3a9065
        kwargs = {}
Packit 3a9065
Packit 3a9065
        # if some part of EVRA was specified in the argument, add it as a filter
Packit 3a9065
        if base_nevra.epoch is not None:
Packit 3a9065
            kwargs["epoch__glob"] = base_nevra.epoch
Packit 3a9065
        if base_nevra.version is not None:
Packit 3a9065
            kwargs["version__glob"] = base_nevra.version
Packit 3a9065
        if base_nevra.release is not None:
Packit 3a9065
            kwargs["release__glob"] = base_nevra.release
Packit 3a9065
        if base_nevra.arch is not None:
Packit 3a9065
            kwargs["arch__glob"] = base_nevra.arch
Packit 3a9065
Packit 3a9065
        query = self.base.sack.query().filter(name=debug_name, **kwargs)
Packit 3a9065
Packit 3a9065
        if query:
Packit 3a9065
            self._install(query)
Packit 3a9065
            return True
Packit 3a9065
Packit 3a9065
        return False
Packit 3a9065
Packit 3a9065
    def _install_debug_no_nevra(self, debug_name, pkgs):
Packit 3a9065
        query = self.base.sack.query().filterm(
Packit 3a9065
            nevra_strict=["{}-{}.{}".format(debug_name, p.evr, p.arch) for p in pkgs])
Packit 3a9065
        if query:
Packit 3a9065
            self._install(query)
Packit 3a9065
            return True
Packit 3a9065
Packit 3a9065
        return False
Packit 3a9065
Packit 3a9065
    def _install(self, pkgs):
Packit 3a9065
        selector = dnf.selector.Selector(self.base.sack)
Packit 3a9065
        selector.set(pkg=pkgs)
Packit 3a9065
        self.base.goal.install(select=selector, optional=not self.base.conf.strict)