Script that shows the dependency tree for a .deb package
Download

Sample output:
root@ubuntu:~# deptree.py irssi
irssi
.libc6
..locales
...belocs-locales-bin (seen: libc6 locales)
.libglib2.0-0 (seen: libc6)
.libncurses5 (seen: libc6)
.libperl5.8 (seen: libc6)
..perl-base
.libssl0.9.8 (seen: libc6)
..zlib1g (seen: libc6)
..|debconf
...|debconf-i18n (seen: debconf)
....liblocale-gettext-perl (seen: libc6)
....libtext-iconv-perl (seen: libc6 perl-base)
.....perlapi-5.8.7
....libtext-wrapi18n-perl
.....libtext-charwidth-perl (seen: libc6 perlapi-5.8.7 perl-base)
....libtext-charwidth-perl (seen: libc6 perlapi-5.8.7 perl-base)
...|debconf-english (seen: debconf)
..|debconf-2.0
.perlapi-5.8.7
.perl (seen: perl-base libc6)
..perl-modules (seen: perl)
..libdb4.3 (seen: libc6)
..libgdbm3 (seen: libc6)
.perl-base
root@ubuntu:~#
Changes

- 06.06.2006 hopefully works for alternative and versionless deps (is that all ?)
- 03.06.2006 first (incomplete) version

author

Source code

#!/usr/bin/python

import sys, os, re

def stripmod(ppkg):
    if ppkg[0] == '|': 
        return ppkg[1:]
    else:
        return ppkg


def getdeps(pkg):
    p = os.popen("apt-cache show " + pkg)
    reply = p.read()
    p.close()

    m = re.search("\nDepends: ([^\n]+)\n", reply)
    if not m:
        return []

    deplist = []

    depstr = m.groups(1)[0] + ","
    deps = re.finditer(" *([^,]+),", depstr)
    if not deps: raise "!match groups"
    
    for dep in [m.group(1) for m in deps]:
        alt = dep.find('|') != -1
        pkgs = re.finditer(" *([^|]+)", dep)
        if not pkgs: raise "!match pkgs"
        
        for pkg in [m.groups(1)[0] for m in pkgs]:
            name_m = re.search(" *([^( ]+) *(\\(.+\\))?", pkg)            
            if not name_m: raise "!match name"
            name = name_m.groups(1)[0]
            
            if alt: name = '|' + name
            deplist.append(name)

    return deplist


def deptree(ppkg, pool = {}, depth = 0):
    pkg = stripmod(ppkg)
        
    pool[pkg] = True

    seen = []
    unseen = []
    for dep in getdeps(pkg):
        if pool.has_key(dep):
            seen.append(dep)
        else:
            unseen.append(dep)
    
    seen_list = ' '.join(seen)
    if seen_list: seen_list = "(seen: %s)" % seen_list

    print ''.join(['.' for i in xrange(depth)]) + ppkg, seen_list
    
    for dep in unseen:
        deptree(dep, pool, depth + 1)


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print "syntax: %s pkgname" % sys.argv[0]
        sys.exit()
    
    deptree(sys.argv[1])