/[gentoo]/src/packages/gentoo.py
Gentoo

Contents of /src/packages/gentoo.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16.2.6 - (hide annotations) (download) (as text)
Sat Jun 23 18:04:54 2007 UTC (7 years, 1 month ago) by marduk
Branch: v1_3
Changes since 1.16.2.5: +50 -15 lines
File MIME type: text/x-python
Clean up implementation of main RSS link and "subtitle". Fixes bug #111952

1 marduk 1.8 #!/usr/bin/python -O
2 marduk 1.2 """These functions mainly take ebuild info (grabbed from the database and
3     convert it to HTML. See the "main" function at the bottom."""
4    
5 marduk 1.16.2.6 __revision__ = "$Revision: 1.16.2.5 $"
6 marduk 1.4 # $Source: /var/cvsroot/gentoo/src/packages/gentoo.py,v $
7 marduk 1.1
8     import config
9     import os
10     import time
11     import sys
12     import ebuilddb
13 marduk 1.2 import bugs
14 marduk 1.1 import changelogs
15     from cgi import escape
16     from urllib import quote
17 marduk 1.16.2.3 from portage_versions import pkgcmp, pkgsplit
18 marduk 1.2
19 marduk 1.3 def is_new(db, ebuild):
20 marduk 1.8 """Check for newness."""
21 marduk 1.11
22 marduk 1.1 c = db.cursor()
23 marduk 1.8 query = ('SELECT new FROM package WHERE category="%s" AND name="%s"'
24 marduk 1.2 % (ebuild['category'], ebuild['name']))
25 marduk 1.1 c.execute(query)
26     results = c.fetchall()
27 marduk 1.8 if len(results) == 1 and results[0][0]:
28 marduk 1.1 return 1
29     return 0
30    
31 marduk 1.15 changelog_to_html = changelogs.bugs_to_html
32 marduk 1.2
33     def homepage_to_html(homepage):
34     """convert HOMEPAGE entry to HTML"""
35 marduk 1.11 if not homepage.strip():
36 marduk 1.3 return "?"
37 marduk 1.2 homepage = homepage.replace('|',' ')
38     pieces = homepage.split()
39     count = len(pieces)
40     if count == 1:
41     return ('<a class="homepage" href="%s">'
42     'Homepage</a>' % pieces[0])
43 marduk 1.11
44 marduk 1.2 html = ['[<a href="%s">%s</a>]' % (page, index + 1) for index,
45     page in enumerate(pieces)]
46 marduk 1.11 return " ".join(['<span class="homepage">Homepages'] + html +
47 marduk 1.2 ['</span>'])
48    
49     def license_to_html(license):
50 marduk 1.3 """Create link to license[s]"""
51 marduk 1.2 if not license.strip(): return "?"
52     license = license.replace('|',' ')
53 marduk 1.8 license = license.replace('(', '')
54     license = license.replace(')', '')
55 marduk 1.2 pieces = license.split()
56 marduk 1.16.2.2 html = ['<a href="http://sources.gentoo.org/viewcvs.py/*checkout*/'
57     'gentoo-x86/licenses/%s">%s</a>' % (piece, piece) for piece in pieces]
58 marduk 1.2 return '<br>\n'.join(html)
59 marduk 1.1
60 marduk 1.8 def package_to_html(pkginfo, db, full=False):
61 marduk 1.11 """This function needs a database (db) connection because it performs a
62 marduk 1.3 query_to_dict on the package"""
63 marduk 1.11
64 marduk 1.2 table_begin = '<table class="ebuild">'
65     name = '<tr><td class="fields">%s</td></tr>' % pkginfo['name']
66 marduk 1.8 if full:
67     image = ('<img class="pkgimg"alt="" src="%s/%s/%s.jpg" align="right">' %
68     (config.ICONS, pkginfo['category'], pkginfo['name'])
69     )
70     else:
71     image = ''
72 marduk 1.2 description = ('<tr><td class="item">'
73 marduk 1.11 '%s<b>Description: </b>%s</td></tr>'
74 marduk 1.8 % (image, escape(pkginfo['description']))
75     )
76 marduk 1.3 ebuilds = get_recent_releases(pkginfo, db)
77 marduk 1.2 releases = '<tr><td>%s</td></tr>' % archs_to_html(ebuilds, 'Releases')
78 marduk 1.11 #bug_string = ('<br><h3>Related bugs:</h3>\n%s'
79 marduk 1.2 # % bugs_to_html(pkginfo['name']))
80     general = '<tr><td>%s</td></tr>' % general_info_to_html(pkginfo)
81 marduk 1.11 #similar = '<tr><td>%s</td></tr>' % create_similar_pkgs_link(pkginfo)
82 marduk 1.2 table_end = '</table>'
83     rows = '\n\t'.join([name, description, releases, general])
84     return '\n\t'.join([table_begin, rows, table_end])
85    
86     def archs_to_html(ebuilds, heading = None):
87 marduk 1.3 """Create table for availability on each architecture"""
88 marduk 1.2 heading = heading or '&nbsp;'
89     table_begin = '<table class="releases">'
90     header_row = ''.join(['<tr><td><b>%s</b></td>' % heading] +
91 marduk 1.7 ['<th class="arch">%s</th>' % i.replace('-',' ') for i in config.ARCHLIST] +
92 marduk 1.2 ['</tr>']
93     )
94     rows = []
95 marduk 1.7 ebuilds.sort(cmp_ebuilds)
96     ebuilds.reverse()
97 marduk 1.2 for ebuild in ebuilds:
98 marduk 1.16 archs = ebuild['arch'].split(',')
99 marduk 1.2 row_start = ('<tr>\n\t<th class="releases"><a href="%sebuilds/?%s-%s"'
100 marduk 1.11 ' title="%s">%s</a></th>\n' % (config.FEHOME,
101     ebuild['name'], ebuild['version'], ebuild['time'],
102 marduk 1.2 ebuild['version']))
103     row_data = []
104     for arch in config.ARCHLIST:
105     if arch in archs:
106     arch_string = '+'
107     elif '~%s' % arch in archs:
108     arch_string = '~'
109     else:
110     arch_string = '-'
111 marduk 1.8 if arch_string != '-' and ebuild['masked']:
112 marduk 1.2 arch_string = 'M' + arch_string
113     row_data.append('\t<td class="archcell" arch="%s">%s</td>'
114     % (arch_string, arch_string))
115     row_end = '</tr>'
116     rows.append('\n\t'.join([row_start] + row_data + [row_end]))
117     table_end = '</table>'
118     return '\n\t'.join([table_begin] + [header_row] + rows + [table_end])
119 marduk 1.11
120 marduk 1.8 def ebuild_to_html(ebinfo, new=0, show_bugs=0, full = False):
121 marduk 1.3 """Convert ebuild (dict) to html, if new, print out a "this is new" notice
122     if show_bugs, show bugs for this particular ebuild (requires access to
123     bugzilla"""
124 marduk 1.1 if new:
125     new_string = """ &nbsp;&nbsp;<span class="new">new!</span> """
126     else:
127     new_string = ""
128 marduk 1.11
129 marduk 1.2 table_begin = '<table class="ebuild">'
130     name_and_date = ('<tr><td class="fields">'
131 marduk 1.1 '<a href="%spackages/?category=%s;name=%s">%s</a> %s%s<br>'
132 marduk 1.2 '<span class="time">%s</span>'
133     '</td></tr>' % (config.FEHOME, quote(ebinfo['category']),
134     quote(ebinfo['name']),
135     ebinfo['name'],
136     ebinfo['version'],
137     new_string,
138 marduk 1.14 ebinfo['time'].strftime("%c %Z")))
139 marduk 1.11
140 marduk 1.8 if full:
141     image = ('<img class="pkgimg" alt="" src="%s/%s/%s.jpg" align="right">' %
142     (config.ICONS, ebinfo['category'], ebinfo['name'])
143     )
144     else:
145     image = ''
146 marduk 1.2 desc_and_changes = ('<tr><td class="item" valign="top">'
147 marduk 1.8 '<p><b>Description:</b> %s %s</p>'
148 marduk 1.2 '<p><b>Changes:</b><br>'
149     '%s</p></td></tr>' % (
150     escape(ebinfo['description']),
151 marduk 1.8 image,
152 marduk 1.2 changelog_to_html(ebinfo['changelog'])))
153    
154     archs = '<tr><td>%s</td></tr>' % archs_to_html([ebinfo])
155     general = '<tr><td>%s</td></tr>' % general_info_to_html(ebinfo)
156     table_end = '</table>'
157 marduk 1.11
158 marduk 1.8 bug_string = ''
159     if show_bugs:
160     bug_string = bugs_to_html(ebinfo['name'])
161     if bug_string:
162     bug_string = '<br><h3 class="bugs">Related bugs:</h3>%s' \
163     % bug_string
164 marduk 1.11
165     return '\n\t'.join([table_begin,
166     name_and_date,
167     desc_and_changes,
168 marduk 1.2 archs,
169 marduk 1.11 general,
170     table_end,
171 marduk 1.2 bug_string])
172 marduk 1.11
173 marduk 1.1 def general_info_to_html(pkg):
174     """This actually will (should) take either a package or ebuild dict
175     as an argument"""
176 marduk 1.11
177 marduk 1.8 import forums
178 marduk 1.11
179 marduk 1.16.2.2 changelogurl = ('http://sources.gentoo.org/viewcvs.py/*checkout*/'
180     'gentoo-x86/%s/%s/ChangeLog' % (pkg['category'],pkg['name']))
181 marduk 1.2 cat_header = '<th class="category">Category</th>'
182     license_header = '<th class="license">License</th>'
183     category = ('<td class="category">'
184 marduk 1.11 '<a href="%spackages/?category=%s">%s</a></td>' % (config.FEHOME,
185 marduk 1.2 pkg['category'], pkg['category']))
186 marduk 1.11 homepage = ('<td class="homepage" rowspan="2">%s</td>'
187 marduk 1.2 % homepage_to_html(pkg['homepage']))
188 marduk 1.11 license = ('<td class="license">%s</td>'
189 marduk 1.2 % license_to_html(pkg['license']))
190     changelog = ('<td class="changelog" rowspan="2">'
191 marduk 1.7 '<a href="%s">ChangeLog</a></td>' % changelogurl)
192     similar = ('<td class="similar" rowspan="2">'
193     '%s</td>' % create_similar_pkgs_link(pkg))
194 marduk 1.8 related_bugs = ('<td class="related_bugs" rowspan="2">'
195     '%s</td>' % create_related_bugs_link(pkg))
196 marduk 1.16.2.5 forums_link = ('<td class="forums" rowspan="2">'
197 marduk 1.8 '%s</td>' % forums.create_forums_link(pkg))
198 marduk 1.11
199 marduk 1.2 return '\n\t'.join(['<table class="general_info">',
200     '<tr>',
201     cat_header,
202     homepage,
203     license_header,
204     changelog,
205 marduk 1.7 similar,
206 marduk 1.8 related_bugs,
207 marduk 1.16.2.5 forums_link,
208 marduk 1.2 '</tr>',
209     '<tr>',
210     category,
211     license,
212     '</tr>',
213     '</table>'])
214 marduk 1.11
215 marduk 1.7 def create_similar_pkgs_link(pkg):
216     """Create a link to similar packages"""
217 marduk 1.11
218 marduk 1.16.2.1 return '<a href="/similar/?package=%(category)s/%(name)s">Similar</a>' % pkg
219 marduk 1.11
220 marduk 1.8 def create_related_bugs_link(pkg):
221     """Create a link to related bugs"""
222 marduk 1.11
223 marduk 1.8 url = ('http://bugs.gentoo.org/buglist.cgi?query_format='
224     '&amp;short_desc_type=allwords'
225     '&amp;short_desc=%s'
226     '&amp;bug_status=UNCONFIRMED'
227     '&amp;bug_status=NEW'
228     '&amp;bug_status=ASSIGNED'
229 marduk 1.11 '&amp;bug_status=REOPENED'
230 marduk 1.8 % escape(pkg['name']))
231 marduk 1.11
232 marduk 1.8 return '<a title="bugs.gentoo.org" href="%s">Bugs</a>' % url
233 marduk 1.11
234 marduk 1.2 def bugs_to_html(package):
235     """Given package name (no version #s), return html text of bugs as
236     reported by bugzilla"""
237     # Right now we have an issue with the bugzilla site. New interface
238     # needs to be written, Bail out.
239 marduk 1.8 #return ""
240 marduk 1.2 import urllib2
241     url = ('http://bugs.gentoo.org/buglist.cgi?query_format='
242 marduk 1.8 '&amp;short_desc_type=allwords'
243     '&amp;short_desc=%s'
244     '&amp;bug_status=UNCONFIRMED'
245     '&amp;bug_status=NEW'
246     '&amp;bug_status=ASSIGNED'
247     '&amp;bug_status=REOPENED'
248 marduk 1.11 '&amp;ctype=csv'
249 marduk 1.8 % package)
250 marduk 1.2 fp = urllib2.urlopen(url)
251     factory = bugs.BugFactory()
252     package_bugs = factory.fromCSV(fp)
253     if package_bugs:
254     writer = bugs.HTMLWriter(package_bugs, 'bugs.gentoo.org')
255     return str(writer)
256     else:
257 marduk 1.8 return ""
258 marduk 1.11
259 marduk 1.8 def get_most_recent(db, max=config.MAXPERPAGE, arch="", branch="", new = False):
260 marduk 1.1 c = db.cursor()
261     extra = ''
262     if arch:
263 marduk 1.16 stable_extra = ('FIND_IN_SET("%s", ebuild.arch) > 0 AND '
264     'FIND_IN_SET("%s", ebuild.prevarch) = 0 ' % (arch, arch))
265     testing_extra = ('FIND_IN_SET("~%s", ebuild.arch) > 0 AND '
266     'FIND_IN_SET("~%s", ebuild.prevarch) = 0 ' % (arch, arch))
267 marduk 1.1 if branch == 'stable':
268     extra = ' AND (%s) ' % stable_extra
269     elif branch == 'testing':
270     extra = ' AND (%s) ' % testing_extra
271     else:
272     extra = ' AND ((%s) OR (%s)) ' % (stable_extra, testing_extra)
273 marduk 1.11
274 marduk 1.8 if new:
275     extra = ('%s AND package.new=1 ' % extra)
276 marduk 1.11
277 marduk 1.16.2.1 query = """SELECT ebuild.category,ebuild.name,version,ebuild.when_found,description,
278 marduk 1.8 changelog,arch,homepage,license,is_masked FROM ebuild,package WHERE ebuild.name=\
279 marduk 1.16.2.1 package.name AND ebuild.category=package.category %s ORDER by ebuild.when_found DESC \
280 marduk 1.1 LIMIT %s""" % (extra,max)
281     c.execute(query)
282     results = c.fetchall()
283     return results
284 marduk 1.11
285 marduk 1.8 def get_most_recent_bumps(db, max=config.MAXPERPAGE):
286     """Return most recent version bumps (pkgs with no prevarch)"""
287     c = db.cursor()
288     query = ('SELECT ebuild.category, ebuild.name, version, when_found, '
289     'description, changelog, arch, homepage, license,is_masked FROM ebuild, package '
290     'WHERE ebuild.name=package.name AND ebuild.category=package.category '
291     'AND prevarch="" AND version NOT LIKE "%%-r_" AND version NOT LIKE '
292     '"%%-r__" AND NOT new ORDER by when_found '
293     'DESC LIMIT %s' % max)
294 marduk 1.11
295 marduk 1.8 c.execute(query)
296     results = c.fetchall()
297     return results
298 marduk 1.11
299 marduk 1.1 def query_to_dict(d):
300 marduk 1.3 """Convert a SQL query to a dict"""
301 marduk 1.1 einfo = {}
302 marduk 1.3 keys = ('category', 'name', 'version', 'time', 'description', 'changelog',
303 marduk 1.8 'arch', 'homepage', 'license', 'masked')
304 marduk 1.1 for i in range(len(keys)):
305     try:
306     einfo[keys[i]] = d[i]
307     except IndexError:
308     continue
309     return einfo
310    
311 marduk 1.3 def get_recent_releases(pkg, db, max=config.MAX_RECENT_RELEASES):
312 marduk 1.1 """Return MAX_RECENT_RELEASES most recent releases for pkg. Returns and
313     ebuild-type dict"""
314     c = db.cursor()
315 marduk 1.3 query = ('SELECT category,name,version,when_found,NULL,changelog,arch ,'
316 marduk 1.8 'NULL,NULL,is_masked FROM ebuild WHERE name="%s" AND category="%s" ORDER BY '
317 marduk 1.3 'version DESC LIMIT %s' % (pkg['name'],pkg['category'],max))
318 marduk 1.1 c.execute(query)
319     results = c.fetchall()
320     #print results
321     return [ query_to_dict(i) for i in results ]
322 marduk 1.7
323     def cmp_ebuilds(a, b):
324     """Compare two ebuilds"""
325 marduk 1.8 fields_a = pkgsplit('%s-%s' % (a['name'], a['version']))
326     fields_b = pkgsplit('%s-%s' % (b['name'], b['version']))
327     return pkgcmp(fields_a, fields_b)
328    
329 marduk 1.16.2.6 def ebuilds_to_rss(fp, ebuilds, simple=False, subtitle='', arch='', branch=''):
330 marduk 1.1 """write out ebuild info to RSS file (fp)"""
331 marduk 1.11
332     # web link for RSS feed
333 marduk 1.16.2.6 link = config.FEHOME
334     subtitle = subtitle
335     if arch != '':
336     link = '%sarchs/%s/' % (link, arch)
337     subtitle = '%s' % arch
338     if branch != '':
339     link = '%s%s/' % (link, branch)
340     subtitle = '%s %s' % (subtitle, branch)
341    
342     title = 'packages.gentoo.org'
343 marduk 1.11 if subtitle:
344 marduk 1.16.2.6 title = '%s [ %s ]' % (title, subtitle)
345 marduk 1.11
346     pubDate = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
347 marduk 1.16.2.6 fp.write("""<rss version="2.0">
348 marduk 1.1 <channel>
349 marduk 1.16.2.6 <title>%s</title>
350 marduk 1.1 <link>%s</link>
351     <description>Latest ebuilds from the Gentoo Linux portage tree</description>
352     <webMaster>www@gentoo.org</webMaster>
353 marduk 1.11
354     <image>
355     <title>Online Package Database</title>
356     <url>%s</url>
357     <link>%s</link>
358     </image>
359    
360 marduk 1.1 <managingEditor>marduk@gentoo.org</managingEditor>
361 marduk 1.16.2.6 <pubDate>%s</pubDate>""" % (title, link, config.RSS_IMAGE,
362 marduk 1.11 config.FEHOME, pubDate))
363 marduk 1.1
364     for ebuild in ebuilds:
365     if simple:
366     description = escape(ebuild['description'])
367     else:
368 marduk 1.8 description = ('\n'
369 marduk 1.11 '<![CDATA[\n'
370     '<link rel="stylesheet" type="text/css" href="%s"></link>\n'
371     '%s\n]]>' % (config.STYLESHEET, ebuild_to_html(ebuild, full=True))
372     )
373 marduk 1.1
374     fp.write("""<item>
375 marduk 1.12 <title>%s/%s %s</title>
376 marduk 1.1 <link>%sebuilds/?%s-%s</link>
377     <description>
378     %s
379     </description>
380     <pubDate>%s</pubDate>
381     </item>
382 marduk 1.12 """ % (ebuild['category'],
383     ebuild['name'],
384 marduk 1.1 ebuild['version'],
385     config.FEHOME,
386     ebuild['name'],
387     ebuild['version'],
388     description,
389 marduk 1.14 ebuild['time'].strftime("%a, %d %b %Y %H:%M:%S +0000"))
390 marduk 1.1 )
391 marduk 1.11
392     fp.write("\n\
393 marduk 1.13 <textInput>\n\
394 marduk 1.16.2.4 <title>Search</title>\n\
395 marduk 1.11 <link>%s/search/</link>\n\
396 marduk 1.16.2.4 <description>Search the Online Package Database</description>\n\
397 marduk 1.11 <name>sstring</name>\n\
398 marduk 1.13 </textInput>\n\
399 marduk 1.11 </channel>\n\
400     </rss>\n" % config.FEHOME)
401    
402 marduk 1.6 def main(argv=None):
403     if argv is None:
404     argv = sys.argv
405 marduk 1.1 try:
406 marduk 1.5 if argv[1] == '-g':
407 marduk 1.1 ebuilddb.main()
408     except IndexError:
409 marduk 1.3 pass
410 marduk 1.11
411 marduk 1.1 db = ebuilddb.db_connect()
412 marduk 1.3 branches = ('', 'stable', 'testing')
413 marduk 1.1 for arch in [''] + config.ARCHLIST:
414     for branch in branches:
415 marduk 1.11 fullpath = os.path.join(config.LOCALHOME, "archs", arch, branch,
416 marduk 1.3 config.INDEX)
417 marduk 1.1 index = open(fullpath,'w')
418 marduk 1.11
419     index.write("""<table border="0" cellpadding="0" cellspacing="5"
420 marduk 1.1 width="100%">\n""")
421     index.write("""<tr><td valign="top">\n""")
422 marduk 1.3 index.write('<!--#include file="archnav.html" -->\n\n</td></tr>\n'
423 marduk 1.11 '<tr><td>')
424 marduk 1.3 results = get_most_recent(db, arch=arch, branch=branch)
425 marduk 1.1 ebuilds = [ query_to_dict(i) for i in results ]
426     for ebuild in ebuilds:
427 marduk 1.3 new = is_new(db, ebuild)
428 marduk 1.1 pkgfilename = "%s/%s-%s.html" % (
429     config.EBUILD_FILES,ebuild['name'],ebuild['version'])
430 marduk 1.8 ebuild_html = ebuild_to_html(ebuild, new, show_bugs = False)
431 marduk 1.3 if arch == '' and branch == '':
432 marduk 1.1 pkgfile = open(pkgfilename,'w')
433     pkgfile.write(ebuild_html)
434     pkgfile.close()
435 marduk 1.3 ebuildfilename = "%s/%s/%s/%s-%s.ebuild" \
436     % (ebuilddb.config.PORTAGE_DIR,
437 marduk 1.1 ebuild['category'],ebuild['name'],ebuild['name'],
438     ebuild['version'])
439 marduk 1.11 os.system('touch -r %s %s || touch -d "today -1 year" %s'
440 marduk 1.2 % (ebuildfilename,pkgfilename,pkgfilename))
441 marduk 1.11
442 marduk 1.1 try:
443 marduk 1.2 index.write('%s\n\n' % (ebuild_html))
444 marduk 1.1 except IOError:
445 marduk 1.3 continue
446 marduk 1.1 index.write("""</table>\n""")
447     index.close()
448 marduk 1.11
449 marduk 1.3 subtitle = ' %s %s' % (arch, branch)
450 marduk 1.16.2.6 rss = open(
451     os.path.join(
452     config.LOCALHOME,
453     "archs",
454     arch,
455     branch,
456     config.RSS
457     ),
458     'w'
459     )
460     ebuilds_to_rss(
461     rss,
462     ebuilds,
463     simple=False,
464     arch=arch,
465     branch=branch
466     )
467 marduk 1.1 rss.close()
468 marduk 1.11
469 marduk 1.16.2.6 rss2 = open(
470     os.path.join(
471     config.LOCALHOME,
472     "archs",
473     arch,
474     branch,
475     config.RSS2
476     ),
477     'w'
478     )
479     ebuilds_to_rss(
480     rss2,
481     ebuilds,
482     simple=True,
483     arch=arch,
484     branch=branch
485     )
486 marduk 1.1 rss.close()
487 marduk 1.5
488     if __name__ == '__main__':
489     sys.exit(main())

  ViewVC Help
Powered by ViewVC 1.1.20