| 1 |
#!/usr/bin/python3 |
| 2 |
# which-pkg-broke: help find offending packages when something breaks |
| 3 |
# Placed in the public domain by Bill Gribble <[email protected]> |
| 4 |
|
| 5 |
import sys |
| 6 |
import os |
| 7 |
import subprocess |
| 8 |
import time |
| 9 |
from string import * |
| 10 |
from stat import * |
| 11 |
|
| 12 |
def force_posix_locale(): |
| 13 |
os.environ['LC_ALL'] = 'C' |
| 14 |
|
| 15 |
def pkgdeps(pkgs): |
| 16 |
apt_cache = subprocess.Popen( |
| 17 |
['apt-cache', 'depends', *pkgs], |
| 18 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 19 |
universal_newlines=True, |
| 20 |
preexec_fn=force_posix_locale, |
| 21 |
) |
| 22 |
deps = [] |
| 23 |
for myline in apt_cache.stdout: |
| 24 |
elts = list(s.strip() for s in myline.split(':')) |
| 25 |
if len(elts) == 2: |
| 26 |
how, pkg = elts |
| 27 |
how = how.replace('|', '') |
| 28 |
if how in ('Depends', 'PreDepends'): |
| 29 |
deps.append(pkg) |
| 30 |
apt_cache.wait() |
| 31 |
return deps |
| 32 |
|
| 33 |
def alldeps(pkg): |
| 34 |
seen = set() |
| 35 |
todo = set([pkg]) |
| 36 |
while todo: |
| 37 |
new = pkgdeps(todo) |
| 38 |
seen |= todo |
| 39 |
todo = set(new) - seen |
| 40 |
return seen |
| 41 |
|
| 42 |
def localarchitectures(): |
| 43 |
architectures = [] |
| 44 |
dpkg_arch = subprocess.Popen( |
| 45 |
['dpkg', '--print-architecture'], |
| 46 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 47 |
universal_newlines=True, |
| 48 |
preexec_fn=force_posix_locale, |
| 49 |
) |
| 50 |
for arch in dpkg_arch.stdout.readlines(): |
| 51 |
architectures.append(arch.rstrip()) |
| 52 |
|
| 53 |
try: |
| 54 |
dpkg_archs = subprocess.Popen( |
| 55 |
['dpkg', '--print-foreign-architecture'], |
| 56 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 57 |
universal_newlines=True, |
| 58 |
preexec_fn=force_posix_locale, |
| 59 |
) |
| 60 |
for arch in dpkg_archs.stdout.readlines(): |
| 61 |
architectures.append(arch.rstrip()) |
| 62 |
|
| 63 |
except OSError: |
| 64 |
pass # non-multiarchy dpkg, which is ok |
| 65 |
|
| 66 |
return architectures |
| 67 |
|
| 68 |
def pkginstalltime(pkg, architectures): |
| 69 |
times = [] |
| 70 |
for arch in architectures: |
| 71 |
listfile = '/var/lib/dpkg/info/' + pkg + ':' + arch + '.list' |
| 72 |
try: |
| 73 |
times.append([pkg + ':' + arch, os.stat(listfile)[ST_MTIME]]) |
| 74 |
except OSError: |
| 75 |
pass |
| 76 |
|
| 77 |
if not times: |
| 78 |
listfile = '/var/lib/dpkg/info/' + pkg + '.list' |
| 79 |
try: |
| 80 |
times.append([pkg, os.stat(listfile)[ST_MTIME]]) |
| 81 |
except OSError: |
| 82 |
print("Package", pkg, "has no install time info") |
| 83 |
|
| 84 |
return times |
| 85 |
|
| 86 |
def what_broke(pname): |
| 87 |
pkgs = alldeps(pname) |
| 88 |
|
| 89 |
architectures = localarchitectures() |
| 90 |
|
| 91 |
itimes = [] |
| 92 |
for p in pkgs: |
| 93 |
itimes.extend(pkginstalltime(p, architectures)) |
| 94 |
itimes.sort(key=lambda k: k[1]) |
| 95 |
for i in itimes: |
| 96 |
p, t = i |
| 97 |
if t is not None: |
| 98 |
print(p.ljust(54), time.asctime(time.localtime(float(t)))) |
| 99 |
|
| 100 |
if (len(sys.argv) != 2 or sys.argv[1][0] == '-'): |
| 101 |
print("Usage: which-pkg-broke <pkg-name>") |
| 102 |
sys.exit(-1) |
| 103 |
else: |
| 104 |
what_broke(sys.argv[1]) |
| 105 |
sys.exit(0) |