File: which-pkg-broke

package info (click to toggle)
debian-goodies 0.27+etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 160 kB
  • ctags: 61
  • sloc: sh: 518; python: 370; makefile: 43
file content (69 lines) | stat: -rwxr-xr-x 1,755 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /usr/bin/python
#  which-pkg-broke: help find offending packages when something breaks
#  Placed in the public domain by Bill Gribble <grib@billgribble.com>

import sys
import os
import popen2
import time
from string import *
from stat import *

def pkgdeps(pkg):
    outstr, instr = popen2.popen4("LC_ALL=C apt-cache depends %s" % pkg)
    deps = []
    myline = outstr.readline()
    while(myline != ''):
        elts = map(strip, myline.split(':'))
        if len(elts) == 2:
            how, pkg = elts
            if how in ('Depends', 'PreDepends'):
                deps.append(pkg)
        myline = outstr.readline()
    return deps

def alldeps(pkg, ignore):
    deps = {}
    imm_deps = pkgdeps(pkg)
    for i in imm_deps:
        if ignore.get(i) is None:
            deps[i] = 1
            ignore[i] = 1
            childeps = alldeps(i, ignore)
            for c in childeps:
                deps[c] = 1
                ignore[i] = 1

    dlist = deps.keys()
    return dlist

def pkginstalltime(pkg):
    listfile = '/var/lib/dpkg/info/' + pkg + '.list'
    try:
        return os.stat(listfile)[ST_MTIME]
    except:
        print "Package", pkg, "has no install time info"
        return None

def what_broke(pname):
    def sortfun(a, b):
        return cmp(a[1], b[1])

    pkgs = [ pname ]
    pkgs.extend(alldeps(sys.argv[1], {}))

    itimes = []
    for p in pkgs:
        itimes.append([p, pkginstalltime(p)])
    itimes.sort(sortfun)
    for i in itimes:
        p, t = i
        if t is not None:
            print ljust(p, 54), time.asctime(time.localtime(float(t)))

if (len(sys.argv) != 2 or sys.argv[1][0] == '-'):
    print "Usage: what-broke <pkg-name>"
    sys.exit(-1)
else:
    what_broke(sys.argv[1])
    sys.exit(0)