| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
# debtags - Implement package tags support for Debian |
| 4 |
# |
| 5 |
# Copyright (C) 2003--2006 Enrico Zini <[email protected]> |
| 6 |
# |
| 7 |
# This program is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
# General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
import sys |
| 21 |
import re |
| 22 |
import subprocess |
| 23 |
from debian import debtags |
| 24 |
import apt |
| 25 |
|
| 26 |
class SmartSearcher: |
| 27 |
def __init__(self, fullcoll, query): |
| 28 |
self.apt_cache = apt.Cache() |
| 29 |
|
| 30 |
self.wanted = set() |
| 31 |
self.unwanted = set() |
| 32 |
self.ignored = set() |
| 33 |
self.interesting = [] |
| 34 |
|
| 35 |
self.tags_in_menu = [] |
| 36 |
|
| 37 |
self.fullcoll = fullcoll |
| 38 |
|
| 39 |
self.subcoll = fullcoll |
| 40 |
|
| 41 |
# Initialise the search |
| 42 |
self.compute_interesting(query) |
| 43 |
|
| 44 |
def compute_interesting(self, query): |
| 45 |
input = subprocess.Popen("apt-cache search " + query, shell=True, stdout = subprocess.PIPE, close_fds = True) |
| 46 |
|
| 47 |
# Read the package list and create the subcollection |
| 48 |
pkgs = [] |
| 49 |
for pkg in input.stdout: |
| 50 |
pkg, none = pkg.rstrip("\n").split(' - ', 1) |
| 51 |
#print(pkg) |
| 52 |
pkgs.append(pkg) |
| 53 |
|
| 54 |
subcoll = self.fullcoll.choose_packages(pkgs) |
| 55 |
|
| 56 |
rel_index = debtags.relevance_index_function(self.fullcoll, subcoll) |
| 57 |
|
| 58 |
# Get all the tags sorted by increasing relevance |
| 59 |
self.interesting = sorted(self.subcoll.iter_tags(), lambda b, a: cmp(rel_index(a), rel_index(b))) |
| 60 |
|
| 61 |
def tag_match(self, pkg): |
| 62 |
tags = self.fullcoll.tags_of_package(pkg) |
| 63 |
if len(self.wanted) > 0 and not self.wanted.issubset(tags): |
| 64 |
return False |
| 65 |
if len(self.unwanted) > 0 and len(tags.intersection(self.unwanted)) > 0: |
| 66 |
return False |
| 67 |
return True |
| 68 |
|
| 69 |
def refilter(self): |
| 70 |
# Regenerate subcoll |
| 71 |
self.subcoll = self.fullcoll.filter_packages(self.tag_match) |
| 72 |
|
| 73 |
def show_set(self, tags, type): |
| 74 |
for tag in tags: |
| 75 |
self.tags_in_menu.append(tag) |
| 76 |
print("%d) %s (%s)" % (len(self.tags_in_menu), tag, type)) |
| 77 |
|
| 78 |
def show_choice_sequence(self, seq, max = 7): |
| 79 |
for tag in seq: |
| 80 |
if tag in self.wanted or tag in self.unwanted or tag in self.ignored: |
| 81 |
continue |
| 82 |
self.tags_in_menu.append(tag) |
| 83 |
print("%d) %s (%d/%d)" % \ |
| 84 |
(len(self.tags_in_menu), tag, self.subcoll.card(tag), self.subcoll.package_count())) |
| 85 |
max = max - 1 |
| 86 |
if max == 0: break |
| 87 |
|
| 88 |
def show_tags(self): |
| 89 |
self.tags_in_menu = [] |
| 90 |
self.show_set(self.wanted, "wanted") |
| 91 |
self.show_set(self.unwanted, "unwanted") |
| 92 |
self.show_set(self.ignored, "ignored") |
| 93 |
print |
| 94 |
self.show_choice_sequence(self.interesting) |
| 95 |
print |
| 96 |
# Compute the most interesting tags by discriminance |
| 97 |
discr = sorted(self.subcoll.iter_tags(), \ |
| 98 |
lambda a, b: cmp(self.subcoll.discriminance(a), self.subcoll.discriminance(b))) |
| 99 |
self.show_choice_sequence(discr) |
| 100 |
|
| 101 |
def output_packages(self): |
| 102 |
for pkg in self.subcoll.iter_packages(): |
| 103 |
aptpkg = self.apt_cache[pkg] |
| 104 |
desc = aptpkg.raw_description.split("\n")[0] |
| 105 |
print(pkg, "-", desc) |
| 106 |
|
| 107 |
def interact(self): |
| 108 |
done = False |
| 109 |
while not done: |
| 110 |
print("Tag selection:") |
| 111 |
self.show_tags() |
| 112 |
print(self.subcoll.package_count(), " packages selected so far.") |
| 113 |
|
| 114 |
changed = False |
| 115 |
|
| 116 |
sys.stdout.write("Your choice (+#, -#, =#, K word, View, Done, Quit, ?): ") |
| 117 |
ans = sys.stdin.readline() |
| 118 |
if ans == None: |
| 119 |
break |
| 120 |
ans = ans.strip(" \t\n") |
| 121 |
|
| 122 |
# If we're setting a new keyword search, process now and skip |
| 123 |
# processing as a list |
| 124 |
if ans == "?": |
| 125 |
print("+ number select the tag with the given number as a tag you want") |
| 126 |
print("- number select the tag with the given number as a tag you do not want") |
| 127 |
print("= number select the tag with the given number as a tag you don't care about") |
| 128 |
print("K word recompute the set of interesting tags from a full-text search using the given word") |
| 129 |
print("V view the packages selected so far") |
| 130 |
print("D print the packages selected so far and exit") |
| 131 |
print("Q quit debtags smart search") |
| 132 |
print("? print this help information") |
| 133 |
elif ans[0] == 'k' or ans[0] == 'K': |
| 134 |
# Strip initial command and empty spaces |
| 135 |
ans = ans[1:].strip(); |
| 136 |
if len(ans) == 0: |
| 137 |
print("The 'k' command needs a keyword to use for finding new interesting tags.") |
| 138 |
else: |
| 139 |
self.compute_interesting(ans) |
| 140 |
ans = '' |
| 141 |
else: |
| 142 |
# Split the answer by spaces |
| 143 |
for cmd in ans.split(): |
| 144 |
if cmd[0] == '+' or cmd[0] == '-' or cmd[0] == '=': |
| 145 |
try: |
| 146 |
idx = int(cmd[1:]) |
| 147 |
except ValueError: |
| 148 |
print(cmd, "should have a number after +, - or =") |
| 149 |
continue |
| 150 |
if idx > len(self.tags_in_menu): |
| 151 |
print("Tag", idx, "was not on the menu.") |
| 152 |
else: |
| 153 |
tag = self.tags_in_menu[idx - 1] |
| 154 |
# cout << "Understood " << ans << " as " << ans[0] << tag.fullname() << endl; |
| 155 |
|
| 156 |
if cmd[0] == '+': |
| 157 |
self.wanted.add(tag) |
| 158 |
if tag in self.unwanted: self.unwanted.remove(tag) |
| 159 |
if tag in self.ignored: self.ignored.remove(tag) |
| 160 |
if cmd[0] == '-': |
| 161 |
if tag in self.wanted: self.wanted.remove(tag) |
| 162 |
self.unwanted.add(tag) |
| 163 |
if tag in self.ignored: self.ignored.remove(tag) |
| 164 |
if cmd[0] == '=': |
| 165 |
if tag in self.wanted: self.wanted.remove(tag) |
| 166 |
if tag in self.unwanted: self.unwanted.remove(tag) |
| 167 |
self.ignored.add(tag) |
| 168 |
changed = True |
| 169 |
elif cmd == "V" or cmd == "v": |
| 170 |
self.output_packages() |
| 171 |
elif cmd == "D" or cmd == "d": |
| 172 |
self.output_packages() |
| 173 |
done = True; |
| 174 |
elif cmd == "Q" or cmd == "q": |
| 175 |
done = True; |
| 176 |
else: |
| 177 |
print("Ignoring command \"%s\"" % (cmd)) |
| 178 |
if changed: |
| 179 |
self.refilter() |
| 180 |
|
| 181 |
|
| 182 |
if len(sys.argv) < 3: |
| 183 |
print("Usage: %s tagdb keywords..." % (sys.argv[0]), file=sys.stderr) |
| 184 |
sys.exit(1) |
| 185 |
|
| 186 |
|
| 187 |
# Main starts |
| 188 |
|
| 189 |
fullcoll = debtags.DB() |
| 190 |
# Read full database |
| 191 |
tag_filter = re.compile(r"^special::.+$|^.+::TODO$") |
| 192 |
fullcoll.read(open(sys.argv[1], "r"), lambda x: not tag_filter.match(x)) |
| 193 |
|
| 194 |
searcher = SmartSearcher(fullcoll, " ".join(sys.argv[2:])) |
| 195 |
searcher.interact() |
| 196 |
|
| 197 |
# vim:set ts=4 sw=4 expandtab: |