[maemo-users] Small useful script: whence

From: Marius Gedminas marius at pov.lt
Date: Thu Oct 15 19:49:00 EEST 2009
Every now and then somebody on IRC asks "how do I get package X".  You
have package X installed, but don't remember from which repository it
came.  apt-cache policy does not help, since it only shows the hostname
(repository.maemo.org) so you can't tell Extras from Extras-Devel from
Fremantle Tools.

Here's a short Python script that wraps apt-cache showpkg and produces a
concise summary of which repositories have which versions.  Sample

  ~ $ whence conboy
    0.3.2 0.4.0 0.4.1 0.4.2 0.4.3 0.4.4 0.4.5 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 1:0.5.6

Script is attached.

(I've been asked on IRC to package this into a deb, but knowing my
lazyness and capability of procrastination I thought I'd send a copy to
the list, in case someone else wants to package it first.  I won't mind.
Hint, hint.)

Marius Gedminas
What goes up, must come down. Ask any system administrator.
-------------- next part --------------
'whence packagename' tells the user in which repositories the package can be
found.  Useful since Extras, Extras-devel, Extras-testing, and SDK tools
repositories look the same ('repository.maemo.org') in apt-cache policy output.

Copyright (c) 2009 Marius Gedminas <marius at gedmin.as>
Licenced under the GNU GPL v2 or later.

import subprocess, sys, os, re, collections

def main():
    if len(sys.argv) < 2:
        sys.exit('usage: whence pkgname')

    pkgname = sys.argv[1]
    pipe = subprocess.Popen(['apt-cache', 'showpkg', pkgname],
    for line in pipe.stdout:
        if line == 'Versions: \n':
    sources = collections.defaultdict(list)
    for line in pipe.stdout:
        if line == 'Reverse Depends: \n':
        if line and not line[0].isspace():
            version = line.split(None, 1)[0]
            for filename in re.findall(r'[(]([^)]+)[)]', line):
                filename = os.path.basename(filename)
                if filename == 'status':
                    filename = 'installed'

    for source, versions in sorted(sources.items()):
        print source.replace('_', '/')
        print ' ', ' '.join(sorted(versions))

if __name__ == '__main__':

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.maemo.org/pipermail/maemo-users/attachments/20091015/36c6544f/attachment.pgp 
More information about the maemo-users mailing list