Commit f3bda9378ab447bc4924ec15c4c2ac054b4f3773
1 parent
faf45176
Exists in
master
and in
1 other branch
small update.
Showing
1 changed file
with
121 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,121 @@ | @@ -0,0 +1,121 @@ | ||
1 | +#!/usr/bin/env python | ||
2 | +""" | ||
3 | + DemoRecommender - demonstration of a GNU/Linux application recommender. | ||
4 | +""" | ||
5 | +__author__ = "Tassia Camoes Araujo <tassia@gmail.com>" | ||
6 | +__copyright__ = "Copyright (C) 2011 Tassia Camoes Araujo" | ||
7 | +__license__ = """ | ||
8 | + This program is free software: you can redistribute it and/or modify | ||
9 | + it under the terms of the GNU General Public License as published by | ||
10 | + the Free Software Foundation, either version 3 of the License, or | ||
11 | + (at your option) any later version. | ||
12 | + | ||
13 | + This program is distributed in the hope that it will be useful, | ||
14 | + but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | + GNU General Public License for more details. | ||
17 | + | ||
18 | + You should have received a copy of the GNU General Public License | ||
19 | + along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
20 | +""" | ||
21 | + | ||
22 | +import os | ||
23 | +import sys | ||
24 | +sys.path.insert(0,'../') | ||
25 | +import commands | ||
26 | +import re | ||
27 | + | ||
28 | +import xapian | ||
29 | +from debian import debtags | ||
30 | +from strategy import PkgMatchDecider | ||
31 | + | ||
32 | +DB_PATH = "/var/lib/debtags/package-tags" | ||
33 | +INDEX_PATH = os.path.expanduser("~/.app-recommender/debtags_index") | ||
34 | + | ||
35 | +def load_debtags_db(path): | ||
36 | + """ Load debtags database. """ | ||
37 | + debtags_db = debtags.DB() | ||
38 | + tag_filter = re.compile(r"^special::.+$|^.+::TODO$") | ||
39 | + try: | ||
40 | + debtags_db.read(open(path, "r"), lambda x: not tag_filter.match(x)) | ||
41 | + except IOError: | ||
42 | + print >> sys.stderr, ("IOError: could not open debtags file \'%s\'" % | ||
43 | + path) | ||
44 | + exit(1) | ||
45 | + return debtags_db | ||
46 | + | ||
47 | +def get_system_pkgs(): | ||
48 | + """ Return set of system packages. """ | ||
49 | + dpkg_output = commands.getoutput('/usr/bin/dpkg --get-selections') | ||
50 | + return dpkg_output.replace('install','\t').split() | ||
51 | + | ||
52 | +def get_most_relevant_tags(debtags_db,pkgs_list): | ||
53 | + """ Return most relevant tags considering a list of packages. """ | ||
54 | + relevant_db = debtags_db.choose_packages(pkgs_list) | ||
55 | + relevance_index = debtags.relevance_index_function(debtags_db,relevant_db) | ||
56 | + sorted_relevant_tags = sorted(relevant_db.iter_tags(), | ||
57 | + lambda a, b: cmp(relevance_index(a), | ||
58 | + relevance_index(b))) | ||
59 | + return normalize_tags(' '.join(sorted_relevant_tags[-50:])) | ||
60 | + | ||
61 | +def normalize_tags(string): | ||
62 | + """ Normalize tag string so that it can be indexed and retrieved. """ | ||
63 | + return string.replace(':','_').replace('-','\'') | ||
64 | + | ||
65 | +def create_debtags_index(debtags_db,index_path): | ||
66 | + """ Create a xapian index for debtags info based on file 'debtags_db' and | ||
67 | + place it at 'index_path'. | ||
68 | + """ | ||
69 | + if not os.path.exists(index_path): | ||
70 | + os.makedirs(index_path) | ||
71 | + print "Creating new debtags xapian index at \'%s\'" % index_path | ||
72 | + debtags_index = xapian.WritableDatabase(index_path, | ||
73 | + xapian.DB_CREATE_OR_OVERWRITE) | ||
74 | + for pkg,tags in debtags_db.iter_packages_tags(): | ||
75 | + doc = xapian.Document() | ||
76 | + doc.set_data(pkg) | ||
77 | + for tag in tags: | ||
78 | + doc.add_term(normalize_tags(tag)) | ||
79 | + print "indexing ",debtags_index.add_document(doc) | ||
80 | + return debtags_index | ||
81 | + | ||
82 | +def load_debtags_index(debtags_db,reindex): | ||
83 | + """ Load an existing or new debtags index, based on boolean reindex. """ | ||
84 | + if not reindex: | ||
85 | + try: | ||
86 | + print ("Opening existing debtags xapian index at \'%s\'" % | ||
87 | + INDEX_PATH) | ||
88 | + debtags_index = xapian.Database(INDEX_PATH) | ||
89 | + except xapian.DatabaseError: | ||
90 | + print "Could not open debtags xapian index" | ||
91 | + reindex = 1 | ||
92 | + if reindex: | ||
93 | + debtags_index = create_debtags_index(debtags_db,INDEX_PATH) | ||
94 | + return debtags_index | ||
95 | + | ||
96 | + | ||
97 | +if __name__ == '__main__': | ||
98 | + | ||
99 | + reindex = 0 | ||
100 | + if len(sys.argv) == 2: | ||
101 | + DB_PATH = sys.argv[1] | ||
102 | + reindex = 1 | ||
103 | + print "reindex true" | ||
104 | + elif len(sys.argv) > 2: | ||
105 | + print >> sys.stderr, ("Usage: %s [PATH_TO_DEBTAGS_DATABASE]" % | ||
106 | + sys.argv[0]) | ||
107 | + sys.exit(1) | ||
108 | + | ||
109 | + debtags_db = load_debtags_db(DB_PATH) | ||
110 | + installed_pkgs = get_system_pkgs() | ||
111 | + best_tags = get_most_relevant_tags(debtags_db,installed_pkgs) | ||
112 | + | ||
113 | + debtags_index = load_debtags_index(debtags_db,reindex) | ||
114 | + qp = xapian.QueryParser() | ||
115 | + query = qp.parse_query(best_tags) | ||
116 | + enquire = xapian.Enquire(debtags_index) | ||
117 | + enquire.set_query(query) | ||
118 | + | ||
119 | + mset = enquire.get_mset(0, 20, None, PkgMatchDecider(installed_pkgs)) | ||
120 | + for m in mset: | ||
121 | + print "%2d: %s" % (m.rank, m.document.get_data()) |