Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 74 lines (59 sloc) 2.48 KB
#!/usr/bin/env python
# A simple program to show you the installed versions of
# all installed packages matching the given pattern.
# e.g. aptver libgtk2 tells you the versions of all the libgtk2
# packages you have installed, without your needing to remember
# package names like libgtk2.0-0.
# Also useful as a search tool that doesn't limit the descriptions to
# the width of the terminal, so you can use it with grep.
# Copyright 2013 by Akkana Peck -- share and enjoy under the GPL v2 or later.
import sys
import apt
class Colprinter():
'''Format rows (lists) in columns,
sized to the widest element in each column.
def __init__(self):
self.thelist = []
self.lengths = []
def add(self, lis):
'''Add a row (a list of items)'''
self.thelist.append(list(map(str, lis)))
for i, item in enumerate(lis):
itemlen = len(item)
# See if it's longer than anything else in this position so far:
if i >= len(self.lengths):
elif itemlen > self.lengths[i]:
self.lengths[i] = itemlen
def __repr__(self):
return '\n'.join(' '.join(str(col).ljust(self.lengths[c])
for c, col in enumerate(row))
for row in self.thelist)
def show_apt_matches(pat, cache, only_installed=False):
colpr = Colprinter()
for pkgname in list(cache.keys()):
if pat in pkgname:
pkg = cache[pkgname]
instver = pkg.installed
if instver:
colpr.add((, instver.version, instver.summary))
elif not only_installed:
candidate = pkg.candidate
colpr.add((, '(' + candidate.version + ')',
if __name__ == '__main__':
from optparse import OptionParser
usage = '''Usage: %prog pattern'''
LongVersion = '''%prog 0.1: search for packages and show versions.'''
optparser = OptionParser(usage=usage, version=LongVersion)
optparser.add_option("-i", "--installed",
action="store_true", dest="only_installed",
help="Show only pages that are already installed")
(options, args) = optparser.parse_args()
cache = apt.cache.Cache()
if not args:
show_apt_matches(args[0], cache, only_installed=options.only_installed)