Blame plugins/repograph.py

Packit 3a9065
# repograph.py
Packit 3a9065
# DNF plugin adding a command to Output a full package dependency graph in dot
Packit 3a9065
# format.
Packit 3a9065
#
Packit 3a9065
# Copyright (C) 2015 Igor Gnatenko
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 __future__ import absolute_import
Packit 3a9065
from __future__ import unicode_literals
Packit 3a9065
from dnfpluginscore import _, logger
Packit 3a9065
Packit 3a9065
import dnf.cli
Packit 3a9065
Packit 3a9065
DOT_HEADER = """
Packit 3a9065
size="20.69,25.52";
Packit 3a9065
ratio="fill";
Packit 3a9065
rankdir="TB";
Packit 3a9065
orientation=port;
Packit 3a9065
node[style="filled"];
Packit 3a9065
"""
Packit 3a9065
Packit 3a9065
Packit 3a9065
class RepoGraph(dnf.Plugin):
Packit 3a9065
Packit 3a9065
    name = "repograph"
Packit 3a9065
Packit 3a9065
    def __init__(self, base, cli):
Packit 3a9065
        super(RepoGraph, self).__init__(base, cli)
Packit 3a9065
        if cli is None:
Packit 3a9065
            return
Packit 3a9065
        cli.register_command(RepoGraphCommand)
Packit 3a9065
Packit 3a9065
Packit 3a9065
class RepoGraphCommand(dnf.cli.Command):
Packit 3a9065
    aliases = ("repograph", "repo-graph",)
Packit 3a9065
    summary = _("Output a full package dependency graph in dot format")
Packit 3a9065
Packit 3a9065
    def configure(self):
Packit 3a9065
        demands = self.cli.demands
Packit 3a9065
        demands.sack_activation = True
Packit 3a9065
        demands.available_repos = True
Packit 3a9065
        if self.opts.repo:
Packit 3a9065
            for repo in self.base.repos.all():
Packit 3a9065
                if repo.id not in self.opts.repo:
Packit 3a9065
                    repo.disable()
Packit 3a9065
                else:
Packit 3a9065
                    repo.enable()
Packit 3a9065
Packit 3a9065
    def run(self):
Packit 3a9065
        self.do_dot(DOT_HEADER)
Packit 3a9065
Packit 3a9065
    def do_dot(self, header):
Packit 3a9065
        maxdeps = 0
Packit 3a9065
        deps = self._get_deps(self.base.sack)
Packit 3a9065
Packit 3a9065
        print("digraph packages {")
Packit 3a9065
        print("{}".format(header))
Packit 3a9065
Packit 3a9065
        for pkg in deps.keys():
Packit 3a9065
            if len(deps[pkg]) > maxdeps:
Packit 3a9065
                maxdeps = len(deps[pkg])
Packit 3a9065
Packit 3a9065
            # color calculations lifted from rpmgraph
Packit 3a9065
            h = 0.5 + (0.6 / 23 * len(deps[pkg]))
Packit 3a9065
            s = h + 0.1
Packit 3a9065
            b = 1.0
Packit 3a9065
Packit 3a9065
            print('"{}" [color="{:.12g} {:.12g} {}"];'.format(pkg, h, s, b))
Packit 3a9065
            print('"{}" -> {{'.format(pkg))
Packit 3a9065
            for req in deps[pkg]:
Packit 3a9065
                print('"{}"'.format(req))
Packit 3a9065
            print('}} [color="{:.12g} {:.12g} {}"];\n'.format(h, s, b))
Packit 3a9065
        print("}")
Packit 3a9065
Packit 3a9065
    @staticmethod
Packit 3a9065
    def _get_deps(sack):
Packit 3a9065
        requires = {}
Packit 3a9065
        prov = {}
Packit 3a9065
        skip = []
Packit 3a9065
Packit 3a9065
        available = sack.query().available()
Packit 3a9065
        for pkg in available:
Packit 3a9065
            xx = {}
Packit 3a9065
            for req in pkg.requires:
Packit 3a9065
                reqname = str(req)
Packit 3a9065
                if reqname in skip:
Packit 3a9065
                    continue
Packit 3a9065
                # XXX: https://bugzilla.redhat.com/show_bug.cgi?id=1186721
Packit 3a9065
                if reqname.startswith("solvable:"):
Packit 3a9065
                    continue
Packit 3a9065
                if reqname in prov:
Packit 3a9065
                    provider = prov[reqname]
Packit 3a9065
                else:
Packit 3a9065
                    provider = available.filter(provides=reqname)
Packit 3a9065
                    if not provider:
Packit 3a9065
                        logger.debug(_("Nothing provides: '%s'"), reqname)
Packit 3a9065
                        skip.append(reqname)
Packit 3a9065
                        continue
Packit 3a9065
                    else:
Packit 3a9065
                        provider = provider[0].name
Packit 3a9065
                    prov[reqname] = provider
Packit 3a9065
                if provider == pkg.name:
Packit 3a9065
                    xx[provider] = None
Packit 3a9065
                if provider in xx or provider in skip:
Packit 3a9065
                    continue
Packit 3a9065
                else:
Packit 3a9065
                    xx[provider] = None
Packit 3a9065
                requires[pkg.name] = xx.keys()
Packit 3a9065
        return requires