data.py
4.2 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/python
# AppRecommender - A GNU/Linux application recommender
#
# Copyright (C) 2010 Tassia Camoes <tassia@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import re
import xapian
import axi
from debian import debtags
import logging
import hashlib
class Item:
""" """
class Package(Item):
""" """
def __init__(self,package_name):
""" """
self.package_name = package_name
def load_package_info(self):
""" """
print "debian pkg",self.id
def normalize_tags(string):
"""
Normalize tag string so that it can be indexed and retrieved.
"""
return string.replace(':','_').replace('-','\'')
# FIXME Data repositories should be singleton
class DebtagsDB(debtags.DB):
def __init__(self,path):
self.path = path
def load(self):
tag_filter = re.compile(r"^special::.+$|^.+::TODO$")
try:
self.read(open(self.path, "r"), lambda x: not tag_filter.match(x))
return 1
except IOError:
logging.error("IOError: could not open debtags file \'%s\'" %
self.path)
return 0
def get_relevant_tags(self,pkgs_list,qtd_of_tags):
"""
Return most relevant tags considering a list of packages.
"""
relevant_db = self.choose_packages(pkgs_list)
relevance_index = debtags.relevance_index_function(self,relevant_db)
sorted_relevant_tags = sorted(relevant_db.iter_tags(),
lambda a, b: cmp(relevance_index(a),
relevance_index(b)))
return normalize_tags(' '.join(sorted_relevant_tags[-qtd_of_tags:]))
class DebtagsIndex(xapian.WritableDatabase):
def __init__(self,path):
self.path = path
self.db_md5 = 0
def load(self,debtags_db,reindex=0):
"""
Load an existing debtags index.
"""
self.debtags_db = debtags_db
db = open(debtags_db.path)
md5 = hashlib.md5()
md5.update(db.read())
self.db_md5 = md5.hexdigest()
if not reindex:
try:
logging.info("Opening existing debtags xapian index at \'%s\'"
% self.path)
xapian.Database.__init__(self,self.path)
md5 = self.get_metadata("md5")
if not md5 == self.db_md5:
logging.info("Index must be updated.")
reindex = 1
except xapian.DatabaseError:
logging.info("Could not open index.")
reindex =1
if reindex:
self.create_index(debtags_db)
def create_index(self,debtags_db):
"""
Create a xapian index for debtags info based on file 'debtags_db' and
place it at 'index_path'.
"""
if not os.path.exists(self.path):
os.makedirs(self.path)
try:
logging.info("Creating new xapian index for debtags at \'%s\'" %
self.path)
xapian.WritableDatabase.__init__(self,self.path,
xapian.DB_CREATE_OR_OVERWRITE)
except xapian.DatabaseError:
logging.critical("Could not create xapian index.")
exit(1)
self.set_metadata("md5",self.db_md5)
for pkg,tags in debtags_db.iter_packages_tags():
doc = xapian.Document()
doc.set_data(pkg)
for tag in tags:
doc.add_term(normalize_tags(tag))
doc_id = self.add_document(doc)
logging.debug("Indexing doc %d",doc_id)