-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathaptver
executable file
·73 lines (59 loc) · 2.48 KB
/
aptver
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
70
71
72
#!/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):
self.lengths.append(itemlen)
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((pkg.name, instver.version, instver.summary))
elif not only_installed:
candidate = pkg.candidate
colpr.add((pkg.name, '(' + candidate.version + ')',
candidate.summary))
print(colpr)
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:
optparser.print_usage()
sys.exit(1)
show_apt_matches(args[0], cache, only_installed=options.only_installed)