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

Contents of /src/packages/gentoo.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16.2.5 - (hide annotations) (download) (as text)
Mon Mar 26 18:00:10 2007 UTC (7 years ago) by marduk
Branch: v1_3
Changes since 1.16.2.4: +3 -4 lines
File MIME type: text/x-python
Remove unused import (string). module name re-assigned as variable (forums)
in general_info_to_html()

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.5 __revision__ = "$Revision: 1.16.2.4 $"
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.3 def ebuilds_to_rss(fp, ebuilds, simple=False, subtitle=""):
330 marduk 1.1 """write out ebuild info to RSS file (fp)"""
331 marduk 1.11
332     # web link for RSS feed
333     if subtitle:
334     link = '%s/%s' % (config.FEHOME, subtitle.replace(' ','/',1))
335     else:
336     link = config.FEHOME
337    
338     pubDate = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
339 marduk 1.9 fp.write("""<?xml version="1.0" encoding="UTF-8"?>
340 marduk 1.11 <rss version="2.0">
341 marduk 1.1 <channel>
342 marduk 1.7 <title>packages.gentoo.org [ %s ]</title>
343 marduk 1.1 <link>%s</link>
344     <description>Latest ebuilds from the Gentoo Linux portage tree</description>
345     <webMaster>www@gentoo.org</webMaster>
346 marduk 1.11
347     <image>
348     <title>Online Package Database</title>
349     <url>%s</url>
350     <link>%s</link>
351     </image>
352    
353 marduk 1.1 <managingEditor>marduk@gentoo.org</managingEditor>
354 marduk 1.14 <pubDate>%s</pubDate>""" % (subtitle, link, config.RSS_IMAGE,
355 marduk 1.11 config.FEHOME, pubDate))
356 marduk 1.1
357     for ebuild in ebuilds:
358     if simple:
359     description = escape(ebuild['description'])
360     else:
361 marduk 1.8 description = ('\n'
362 marduk 1.11 '<![CDATA[\n'
363     '<link rel="stylesheet" type="text/css" href="%s"></link>\n'
364     '%s\n]]>' % (config.STYLESHEET, ebuild_to_html(ebuild, full=True))
365     )
366 marduk 1.1
367     fp.write("""<item>
368 marduk 1.12 <title>%s/%s %s</title>
369 marduk 1.1 <link>%sebuilds/?%s-%s</link>
370     <description>
371     %s
372     </description>
373     <pubDate>%s</pubDate>
374     </item>
375 marduk 1.12 """ % (ebuild['category'],
376     ebuild['name'],
377 marduk 1.1 ebuild['version'],
378     config.FEHOME,
379     ebuild['name'],
380     ebuild['version'],
381     description,
382 marduk 1.14 ebuild['time'].strftime("%a, %d %b %Y %H:%M:%S +0000"))
383 marduk 1.1 )
384 marduk 1.11
385     fp.write("\n\
386 marduk 1.13 <textInput>\n\
387 marduk 1.16.2.4 <title>Search</title>\n\
388 marduk 1.11 <link>%s/search/</link>\n\
389 marduk 1.16.2.4 <description>Search the Online Package Database</description>\n\
390 marduk 1.11 <name>sstring</name>\n\
391 marduk 1.13 </textInput>\n\
392 marduk 1.11 </channel>\n\
393     </rss>\n" % config.FEHOME)
394    
395 marduk 1.6 def main(argv=None):
396     if argv is None:
397     argv = sys.argv
398 marduk 1.1 try:
399 marduk 1.5 if argv[1] == '-g':
400 marduk 1.1 ebuilddb.main()
401     except IndexError:
402 marduk 1.3 pass
403 marduk 1.11
404 marduk 1.1 db = ebuilddb.db_connect()
405 marduk 1.3 branches = ('', 'stable', 'testing')
406 marduk 1.1 for arch in [''] + config.ARCHLIST:
407     for branch in branches:
408 marduk 1.11 fullpath = os.path.join(config.LOCALHOME, "archs", arch, branch,
409 marduk 1.3 config.INDEX)
410 marduk 1.1 index = open(fullpath,'w')
411 marduk 1.11
412     index.write("""<table border="0" cellpadding="0" cellspacing="5"
413 marduk 1.1 width="100%">\n""")
414     index.write("""<tr><td valign="top">\n""")
415 marduk 1.3 index.write('<!--#include file="archnav.html" -->\n\n</td></tr>\n'
416 marduk 1.11 '<tr><td>')
417 marduk 1.3 results = get_most_recent(db, arch=arch, branch=branch)
418 marduk 1.1 ebuilds = [ query_to_dict(i) for i in results ]
419     for ebuild in ebuilds:
420 marduk 1.3 new = is_new(db, ebuild)
421 marduk 1.1 pkgfilename = "%s/%s-%s.html" % (
422     config.EBUILD_FILES,ebuild['name'],ebuild['version'])
423 marduk 1.8 ebuild_html = ebuild_to_html(ebuild, new, show_bugs = False)
424 marduk 1.3 if arch == '' and branch == '':
425 marduk 1.1 pkgfile = open(pkgfilename,'w')
426     pkgfile.write(ebuild_html)
427     pkgfile.close()
428 marduk 1.3 ebuildfilename = "%s/%s/%s/%s-%s.ebuild" \
429     % (ebuilddb.config.PORTAGE_DIR,
430 marduk 1.1 ebuild['category'],ebuild['name'],ebuild['name'],
431     ebuild['version'])
432 marduk 1.11 os.system('touch -r %s %s || touch -d "today -1 year" %s'
433 marduk 1.2 % (ebuildfilename,pkgfilename,pkgfilename))
434 marduk 1.11
435 marduk 1.1 try:
436 marduk 1.2 index.write('%s\n\n' % (ebuild_html))
437 marduk 1.1 except IOError:
438 marduk 1.3 continue
439 marduk 1.1 index.write("""</table>\n""")
440     index.close()
441 marduk 1.11
442 marduk 1.3 subtitle = ' %s %s' % (arch, branch)
443     rss = open(os.path.join(config.LOCALHOME, "archs", arch, branch,
444     config.RSS), 'w')
445     ebuilds_to_rss(rss, ebuilds, simple=False, subtitle=subtitle)
446 marduk 1.1 rss.close()
447 marduk 1.11
448 marduk 1.3 rss2 = open(os.path.join(config.LOCALHOME, "archs", arch, branch,
449     config.RSS2), 'w')
450     ebuilds_to_rss(rss2, ebuilds, simple=True, subtitle=subtitle)
451 marduk 1.1 rss.close()
452 marduk 1.5
453     if __name__ == '__main__':
454     sys.exit(main())

  ViewVC Help
Powered by ViewVC 1.1.20