aboutsummaryrefslogtreecommitdiff
blob: dd5b0acac6a32ff2523cfcfc2ec0fcefd3aa466c (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from time import localtime,strftime
from pkgcore.ebuild.cpv import CPV
import datetime

def latest_per_day(cpvlist, daycount):
    """generate list of sets with days and pv's as value"""

    dayno = 0
    result = []
    last_day = ""
    last_day_cpvs = []
    for cpvstr, timestamp in cpvlist:
        cur_day = strftime("%A", localtime(timestamp))
        if last_day != cur_day:
            if last_day:
                dayno += 1
                if dayno == daycount:
                    break
                result.append([last_day,last_day_cpvs])
            last_day = cur_day
            last_day_cpvs = []
        last_day_cpvs.append(cpvstr.split("/",1)[1])
    if last_day_cpvs:
        result.append([last_day,last_day_cpvs])
    return result

def last_modified(latest_entry):
    """return a formatted timestamp of latest entry (being last change)"""
    
    return strftime("%d %b %Y %H:%M %Z",localtime(latest_entry[1]))   

def build_centerpkg_list(latest_entries, get_package_details_cpv, count, **kwds):
    center_pkgs = []
    shown_cp = []
    for cpv, change in latest_entries:
        #print 'cpv=%s' % (cpv)
        atom = CPV(str(cpv))
        if 'use_fullver' in kwds and kwds['use_fullver']:
            version_check = atom.cpvstr
        else:
            version_check = atom.key
        if version_check not in shown_cp:
            pkg_details = get_package_details_cpv(version_check)
            if 'filter_func' in kwds:
                if not kwds['filter_func'](pkg_details, kwds['filter_value']):
                    continue
            center_pkgs.append(pkg_details)
            shown_cp.append(version_check)
            if count and len(center_pkgs) == count:
                break
    return center_pkgs


class PackageDB(object):

    # This should match /usr/portage/profiles/arch.list
    arches = frozenset(['alpha', 'amd64', 'arm', 'hppa', 'ia64', 'm68k', 'mips', 'ppc', 'ppc64', 's390', 'sh', 'sparc', 'sparc-fbsd', 'x86', 'x86-fbsd'])

    def get_latest_cpvs(self, limit=None):
        """return modified cpvs"""
        params = ()

        sql = """SELECT %s, versions.mtime 
            FROM versions JOIN packages USING (cp) JOIN categories USING (c)
            ORDER by versions.mtime DESC""" % (self.columns_category_pn_pv)
        if limit is not None:
            try:
                sql += " LIMIT 0, ?" 
                params = (int(limit),)
            except ValueError:
                return []
        #print 'sql=%s' % (sql)
        self.cursor.execute(sql.replace('?',self.syntax_placeholder), params)
        entries = self.cursor.fetchall()
        #print 'get_latest_cpvs=%s'  % (entries,)
        return entries
    
    def get_latest_cpvs_by_pkgname(self, pkgname, limit=None):
        """return modified cpvs"""

        sql = """SELECT %s, versions.mtime 
            FROM versions JOIN packages USING (cp) JOIN categories USING (c)
            WHERE pn = ?
            ORDER by versions.mtime DESC""" % (self.columns_category_pn_pv)
        params = (pkgname,)
        if limit is not None:
            try:
                sql += " LIMIT 0, ?" 
                params += (int(limit),)
            except ValueError:
                return []

        self.cursor.execute(sql.replace('?',self.syntax_placeholder), params)
        entries = self.cursor.fetchall()
        #print 'get_latest_cpvs_by_pkgname=%s' % (entries,)
        return entries
    
    def get_latest_cpvs_by_category(self, category, limit=None):
        """return modified cpvs"""

        sql = """SELECT %s, versions.mtime 
            FROM versions JOIN packages USING (cp) JOIN categories USING (c)
            WHERE category = ?
            ORDER by versions.mtime DESC""" % (self.columns_category_pn_pv)
        params = (category,)
        if limit is not None:
            try:
                sql += " LIMIT 0, ?" 
                params += (int(limit),)
            except ValueError:
                return []

        self.cursor.execute(sql.replace('?',self.syntax_placeholder), params)
        entries = self.cursor.fetchall()
        return entries
    
    def get_latest_cpvs_by_category_pkgname(self, category, pkgname, limit=None):
        """return modified cpvs"""

        sql = """SELECT %s, versions.mtime 
            FROM versions JOIN packages USING (cp) JOIN categories USING (c)
            WHERE category = ? AND pn = ?
            ORDER by versions.mtime DESC""" % (self.columns_category_pn_pv)
        params = (category,pkgname,)
        if limit is not None:
            try:
                sql += " LIMIT 0, ?" 
                params += (int(limit),)
            except ValueError:
                return []

        self.cursor.execute(sql.replace('?',self.syntax_placeholder), params)
        entries = self.cursor.fetchall()
        #print 'get_latest_cpvs_by_category_pkgname=%s' % (entries,)
        return entries

    def get_latest_cpvs_by_arch(self, arch, mode, limit=None):
        """return modified cpvs limited by arch and mode"""

        valid_modes = ['+','~','M','M+','M~','']
        params = ()
        if mode not in valid_modes or arch not in self.arches:
            return []
            
        if mode != '':
            sql = """SELECT %s, versions.mtime 
                FROM arches JOIN keywords USING (a) JOIN versions USING (cpv) JOIN packages USING (cp) JOIN categories USING (c)
                WHERE arches.arch=? AND SUBSTR(keywords.mode,1,1)=?
                ORDER by versions.mtime DESC""" % (self.columns_category_pn_pv)
            params = (arch, str(mode)[0])
        else:
            sql = """SELECT %s, versions.mtime 
                FROM arches JOIN keywords USING (a) JOIN versions USING (cpv) JOIN packages USING (cp) JOIN categories USING (c)
                WHERE arches.arch=?
                ORDER by versions.mtime DESC""" % (self.columns_category_pn_pv)
            params = (arch,)
        if limit is not None:
            try:
                limit = int(limit)
            except ValueError:
                return []
            sql += " LIMIT 0, ?" 
            params += (limit,)
        #else:
        #    print 'limit was %s' % (repr(limit),)

        #print "arch=%s mode=%s limit=%s sql=%s params=%s" % (repr(arch),repr(mode),repr(limit),repr(sql),repr(params))
        self.cursor.execute(sql.replace('?',self.syntax_placeholder),params)
        entries = self.cursor.fetchall()
        #print 'get_latest_cpvs_by_arch=%s' % (entries,)
        #print 'get_latest_cpvs_by_arch params=%s sql=%s' % (params,sql,)
        return entries

    def get_package_details_cpv(self, cpvstr):
        """get package details with versionfiltered keywords"""

        atom = CPV(str(cpvstr))
        pkg_metadata = self.get_package_details(atom.key)
        pkg = {}
        pkg['atom'] = atom
        pkg['meta'] = pkg_metadata
        pkg['keywords'] = self.get_package_keywords(cpvstr)
        pkg['license'] = []
        for l in pkg_metadata['license'].split():
            if l[0].isalnum():
                l = '<a href="http://sources.gentoo.org/viewcvs.py/*checkout*/gentoo-x86/licenses/%s">%s</a>' % (l,l)
            pkg['license'].append(l)
        pkg['license'] = ' '.join(pkg['license'])

        pkg['changelog'] = 'http://sources.gentoo.org/viewcvs.py/*checkout*/gentoo-x86/%s/ChangeLog' % (atom.key)
        pkg['bugzilla'] = 'http://bugs.gentoo.org/buglist.cgi?query_format=&short_desc_type=allwords&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&short_desc=%s' % (atom.package)
        pkg['forums'] = 'http://forums.gentoo.org//search.php?search_terms=all&show_results=topics&mode=results&search_keywords=%s' % (atom.package)
        return pkg

    def get_package_details(self, cp):
        """get package details"""
        (category,pn) = cp.split('/')
        sql = """SELECT license, homepage, description, changelog 
              FROM metadata JOIN packages USING (cp) JOIN categories USING (c)
              WHERE category=? AND pn=?"""
        self.cursor.execute(sql.replace('?', self.syntax_placeholder), (category,pn))
        row = self.cursor.fetchone()
        if not row:
            return
        
        author, commitmsg = row[3].split(":",1)

        try:
            date, author = author.split(";",1)
        except ValueError:
            date = ""
        author = author[:author.find(">")+1]
        split_test = date.split("\n",1)
        if len(split_test) == 2:
            date = split_test[1]
            
        sql = """SELECT %s AS cp, mtime 
                FROM versions JOIN packages USING (cp) JOIN categories USING (c) 
                WHERE category = ? AND pn = ?
                ORDER BY pv""" % (self.columns_category_pn)
        self.cursor.execute(sql.replace('?', self.syntax_placeholder), (category,pn))

        for changerow in self.cursor:
            if CPV(str(changerow[0])).key == cp:
                ebuildchange = changerow[1]
        
        package_details = { 'atom':CPV(str(cp)),
                            'license':row[0],
                            'homepage': row[1],
                            'author': author,
                            'description':row[2],
                            'commitmsg': commitmsg,
                            'changelogdate': date,
                            'ebuildchange':  datetime.datetime.utcfromtimestamp(ebuildchange)}
        return package_details
    
    def get_package_keywords(self, query):
        """return cpv keywords"""

        atom = CPV(str(query))
        sql = """SELECT %s, arches.arch, mode
                FROM arches JOIN keywords USING (a) JOIN versions USING (cpv) JOIN packages USING (cp) JOIN categories USING (c)
                WHERE category = ? AND pn = ?""" % (self.columns_category_pn_pv)
        params = (atom.category,atom.package)
        if atom.fullver is not None and len(atom.fullver) >= 1:
            sql += ' AND pv = ?'
            params += (atom.fullver,)
        sql += ' ORDER BY pv ASC'
        #print 'get_package_keywords sql=%s' % (sql,)
        self.cursor.execute(sql.replace('?',self.syntax_placeholder),params)
        #print 'get_package_keywords(atom)=%s' % (atom,)
        
        pkgs = {}
        for row in self.cursor.fetchall():
            pkg = {}
            cpv = str(row[0])
            arch = str(row[1])
            mode = str(row[2])

            pkg['atom'] = CPV(cpv)

            # Filter out packages starting with our cp name but not belonging to us
            # TODO check if we actually hit this in the new version
            if pkg['atom'].key != atom.key:
                continue

            if cpv not in pkgs:
                pkgs[cpv] = pkg
                for a in self.arches:
                    pkgs[cpv][a] = {'mode': '', 'name': 'blank'}
                
            if mode == "":
                name = "blank"
            elif mode.startswith("M"):
                name = "masked"
            elif mode == "~":
                name = "testing"
            else:
                name = "stable"
            pkgs[cpv][arch] = {'mode': mode, 'name': name}
      
        # Give it a nice order
        cpvs = pkgs.keys()
        cpvs.sort()
        cpvs.reverse()
        final = []
        for k in cpvs:
            final.append(pkgs[k])
        return final
    
    def get_category_packages(self, category):
        """get a list of all packages for a category"""
        
        sql = """SELECT %s, 0
                FROM packages JOIN categories USING (c) 
                WHERE category = ? 
                ORDER BY pn""" % (self.columns_category_pn)
        
        self.cursor.execute(sql.replace('?', self.syntax_placeholder), (category,))
        return self.cursor.fetchall()

    columns_category_pn = None
    columns_category_pn_pv = None

class SQLitePackageDB(PackageDB):
    """override for sqlite backend"""
    
    columns_category_pn = "category || '/' || pn"
    columns_category_pn_pv = "category || '/' || pn || '-' || pv"

    def __init__(self, config={}):
        try:
            import sqlite3 as sqlite
        except ImportError:
            try:
                import pysqlite2.dbapi2 as sqlite
            except ImportError:
                print "Please install PySQLite or use Python 2.5 with sqlite"
                sys.exit(1)
        self.syntax_placeholder = '?'
        self.db = sqlite.connect(config)
        self.cursor = self.db.cursor()

class MySQLPackageDB(PackageDB):
    """override for MySQL backend"""
    
    columns_category_pn = "CONCAT(category, '/', pn)"
    columns_category_pn_pv = "CONCAT(category, '/', pn, '-', pv)"

    def __init__(self, config={}):
        self.syntax_placeholder = '%s'
        try:
            import MySQLdb
        except ImportError:
            print "Please install a recent version of MySQLdb for Python"
            sys.exit(1)
        #self.db = MySQLdb.connect(host=host, user=username, passwd=password, db=database, charset='utf8')
        self.db = MySQLdb.connect(**config)
        self.cursor = self.db.cursor()