Debian Codegraph @

examples/debtags/pkgwalk

raw
1 #!/usr/bin/python3
2 #
3 # Copyright (C) 2007 Enrico Zini <[email protected]>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 # Navigate among related Debian packages
19
20 import sys
21
22 # Requires python-extractor, python-magic and python-debtags
23 from debian import debtags
24 import re
25 from optparse import OptionParser
26 import apt
27
28
29 VERSION="0.1"
30
31 class Parser(OptionParser):
32 def __init__(self, *args, **kwargs):
33 OptionParser.__init__(self, *args, **kwargs)
34
35 def error(self, msg):
36 sys.stderr.write("%s: error: %s\n\n" % (self.get_prog_name(), msg))
37 self.print_help(sys.stderr)
38 sys.exit(2)
39
40 if __name__ == '__main__':
41 parser = Parser(usage="usage: %prog [options] pkgname",
42 version="%prog "+ VERSION,
43 description="walk through Debian packages")
44 parser.add_option("--tagdb", default="/var/lib/debtags/package-tags", help="Tag database to use (default: %default)")
45
46 (options, args) = parser.parse_args()
47
48 if len(args) == 0:
49 parser.error("Please provide the name of an initial package")
50
51 # Read full database
52 db = debtags.DB()
53 tag_filter = re.compile(r"^special::.+$|^.+::TODO$")
54 db.read(open(options.tagdb, "r"), lambda x: not tag_filter.match(x))
55
56 apt_cache = apt.Cache()
57
58 # Maximum number of previous packages to remember
59 maxlen = 5
60 # Initial package selection
61 trail = [ args[0] ]
62
63 # Loop until the user chooses to quit
64 done = False
65 while not done:
66 # Compute a package weight according to how old it is in the
67 # trail
68 pkgweight = {}
69 for idx, pkg in enumerate(trail):
70 pkgweight[pkg] = 1.-(idx/maxlen)
71
72 # For every tag, find the number of packages in trail that have the tag
73 tagscores = {}
74 for pkg in trail:
75 for tag in db.tags_of_package(pkg):
76 if tag in tagscores:
77 tagscores[tag] += pkgweight[pkg]
78 else:
79 tagscores[tag] = pkgweight[pkg]
80
81 # Divide every tag score by the number of packages in the trail,
82 # obtaining a 'tag weight'. A package can be later scored by summing
83 # the weight of all its tags.
84 for tag in tagscores:
85 tagscores[tag] = float(tagscores[tag]) / float(len(trail))
86
87 # Find the merged tagset of the packages in trail
88 trailtags = set(tagscores.keys())
89
90 # Get the list of packages whose tagsets intersect the trail tagset
91 nextpkgs = set()
92 for pkg, tags in db.iter_packages_tags():
93 if trailtags & tags:
94 nextpkgs.add(pkg)
95
96 # Score every package by the sum of the weight of its tags
97 def pkgscore(pkg):
98 score = 0.0
99 for tag in db.tags_of_package(pkg):
100 if tag in tagscores:
101 score += tagscores[tag]
102 return score
103
104 # Show the first 20 packages in reverse score order
105 #display = sorted(nextpkgs - set(trail), key=pkgscore, reverse=True)[:20]
106 display = sorted(nextpkgs, key=pkgscore, reverse=True)[:20]
107 for num, pkg in enumerate(display):
108 aptpkg = apt_cache[pkg]
109 desc = aptpkg.raw_description.split("\n")[0]
110 print("%2d) %s - %s" % (num + 1, pkg, desc))
111
112 # Ask the user to choose a new package
113 while True:
114 ans = input("> ").strip()
115 if ans[0] == 'q':
116 done = True
117 break
118 elif ans.isdigit():
119 num = int(ans) - 1
120 if num < len(display):
121 # TODO: on a different kind of interface, display the full
122 # description of pkg
123 trail = [display[num]] + trail[:maxlen]
124 break
125 else:
126 print("The number is too high")
127
128
129 # vim:set ts=4 sw=4: