| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
# depgraph |
| 4 |
# Copyright (C) 2008 Stefano Zacchiroli <[email protected]> |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
|
| 11 |
"""Graph the dependencies of all packages contained in a given Packages |
| 12 |
file. |
| 13 |
|
| 14 |
Only consider Depends fields. Versioned dependencies are considered as they |
| 15 |
were not versioned. The graph is returned in output as a (non normalized) |
| 16 |
graphviz graph suitable to be processed by dot (for an unstable/main Packages |
| 17 |
file, generating the final graph will take a while ...).""" |
| 18 |
|
| 19 |
import sys |
| 20 |
from debian import deb822 |
| 21 |
|
| 22 |
__fresh_id = 0 |
| 23 |
|
| 24 |
def main(): |
| 25 |
if len(sys.argv) != 2: |
| 26 |
print("Usage: depgraph PACKAGES_FILE") |
| 27 |
sys.exit(2) |
| 28 |
|
| 29 |
def get_id(): |
| 30 |
global __fresh_id |
| 31 |
__fresh_id += 1 |
| 32 |
return ("NODE_%d" % __fresh_id) |
| 33 |
|
| 34 |
def emit_arc(node1, node2): |
| 35 |
print(' "%s" -> "%s" ;' % (node1, node2)) |
| 36 |
def emit_node(node, dsc): |
| 37 |
print(' "%s" [label="%s"] ;' % (node, dsc)) |
| 38 |
|
| 39 |
print("digraph depgraph {") |
| 40 |
for pkg in deb822.Packages.iter_paragraphs(open(sys.argv[1])): |
| 41 |
name = pkg['package'] |
| 42 |
rels = pkg.relations |
| 43 |
for deps in rels['depends']: |
| 44 |
if len(deps) == 1: |
| 45 |
emit_arc(name, deps[0]['name']) |
| 46 |
else: # output an OR node |
| 47 |
or_node = get_id() |
| 48 |
emit_arc(name, or_node) |
| 49 |
emit_node(or_node, 'OR') |
| 50 |
for dep in deps: |
| 51 |
emit_arc(or_node, dep['name'].lower()) |
| 52 |
# even though it is forbidden by policy, there are some |
| 53 |
# dependencies with upper case letter in the archive, |
| 54 |
# apparently apt-get turn them to lowercase ... |
| 55 |
print("}") |
| 56 |
|
| 57 |
if __name__ == '__main__': |
| 58 |
main() |
| 59 |
|