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

Contents of /src/packages/gentoo.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download) (as text)
Thu May 27 03:20:28 2004 UTC (10 years, 3 months ago) by marduk
Branch: MAIN
Changes since 1.2: +47 -37 lines
File MIME type: text/x-python
More code cleanups.  pylint now gives 7.70/10 :-)

1 marduk 1.1 #!/usr/bin/python -OO
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.3 __revision__ = "$Revision: 1.2 $"
6     # $Source: /home/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    
18 marduk 1.2 # import portage, but temporarily redirect stderr
19 marduk 1.3 if 'portage' not in dir():
20 marduk 1.2 null = open('/dev/null', 'w')
21     tmp = sys.stderr
22     sys.stderr = null
23     sys.path = ["/usr/lib/portage/pym"]+sys.path
24     import portage
25     sys.stderr = tmp
26 marduk 1.3 sys.path = sys.path[1:]
27 marduk 1.2
28     tree = portage.portdbapi('/usr/portage')
29    
30     def is_masked(ebuild):
31     """Return true if ebuild is masked"""
32    
33     return (not tree.visible(['%(category)s/%(name)s-%(version)s' % ebuild]))
34    
35 marduk 1.3 def is_new(db, ebuild):
36 marduk 1.1 """Check for newness. An ebuild is considered new if it is the
37     only ebuild with that category/name in ebuild and it has no prevarch"""
38    
39     c = db.cursor()
40 marduk 1.2 query = ('SELECT prevarch FROM ebuild WHERE category="%s" AND name="%s"'
41     % (ebuild['category'], ebuild['name']))
42 marduk 1.1 c.execute(query)
43     results = c.fetchall()
44     if len(results) == 1 and results[0][0] is None:
45     return 1
46     return 0
47    
48 marduk 1.2 def changelog_to_html(changelog):
49     """HTML-ize a changelog entry"""
50     html = ""
51     for char in changelog:
52     if char == '\n':
53     html = "%s<br>" % html
54 marduk 1.1 else:
55 marduk 1.2 html = "%s%s" % (html, escape(char))
56     html = changelogs.bugs_to_html(html)
57     return html
58    
59     def homepage_to_html(homepage):
60     """convert HOMEPAGE entry to HTML"""
61 marduk 1.3 if not homepage.strip():
62     return "?"
63 marduk 1.2 homepage = homepage.replace('|',' ')
64     pieces = homepage.split()
65     count = len(pieces)
66     if count == 1:
67     return ('<a class="homepage" href="%s">'
68     'Homepage</a>' % pieces[0])
69    
70     html = ['[<a href="%s">%s</a>]' % (page, index + 1) for index,
71     page in enumerate(pieces)]
72     return " ".join(['<span class="homepage">Homepages'] + html +
73     ['</span>'])
74    
75     def license_to_html(license):
76 marduk 1.3 """Create link to license[s]"""
77 marduk 1.2 if not license.strip(): return "?"
78     license = license.replace('|',' ')
79     pieces = license.split()
80     html = ['<a href="http://www.gentoo.org/cgi-bin/viewcvs.cgi/*checkout*/'
81     'licenses/%s">%s</a>' % (piece, piece) for piece in pieces]
82     return '<br>\n'.join(html)
83 marduk 1.1
84 marduk 1.3 def package_to_html(pkginfo, db):
85     """This function needs a database (db) connection because it performs a
86     query_to_dict on the package"""
87 marduk 1.2
88     table_begin = '<table class="ebuild">'
89     name = '<tr><td class="fields">%s</td></tr>' % pkginfo['name']
90     description = ('<tr><td class="item">'
91     '<img align="right" alt="" src="%s/%s.png">'
92     '<b>Description: </b>%s</td></tr>' % (config.ICONS,
93     pkginfo['category'], escape(pkginfo['description'])))
94 marduk 1.3 ebuilds = get_recent_releases(pkginfo, db)
95 marduk 1.2 releases = '<tr><td>%s</td></tr>' % archs_to_html(ebuilds, 'Releases')
96     #bug_string = ('<br><h3>Related bugs:</h3>\n%s'
97     # % bugs_to_html(pkginfo['name']))
98     general = '<tr><td>%s</td></tr>' % general_info_to_html(pkginfo)
99     table_end = '</table>'
100     rows = '\n\t'.join([name, description, releases, general])
101     return '\n\t'.join([table_begin, rows, table_end])
102    
103     def archs_to_html(ebuilds, heading = None):
104 marduk 1.3 """Create table for availability on each architecture"""
105 marduk 1.2 heading = heading or '&nbsp;'
106     table_begin = '<table class="releases">'
107     header_row = ''.join(['<tr><td><b>%s</b></td>' % heading] +
108     ['<th class="arch">%s</th>' % i for i in config.ARCHLIST] +
109     ['</tr>']
110     )
111     rows = []
112     for ebuild in ebuilds:
113     masked = is_masked(ebuild)
114     archs = ebuild['arch'].split()
115     row_start = ('<tr>\n\t<th class="releases"><a href="%sebuilds/?%s-%s"'
116     ' title="%s">%s</a></th>\n' % (config.FEHOME,
117     ebuild['name'], ebuild['version'], ebuild['time'],
118     ebuild['version']))
119     row_data = []
120     for arch in config.ARCHLIST:
121     if arch in archs:
122     arch_string = '+'
123     elif '~%s' % arch in archs:
124     arch_string = '~'
125     else:
126     arch_string = '-'
127     if arch_string != '-' and masked:
128     arch_string = 'M' + arch_string
129     row_data.append('\t<td class="archcell" arch="%s">%s</td>'
130     % (arch_string, arch_string))
131     row_end = '</tr>'
132     rows.append('\n\t'.join([row_start] + row_data + [row_end]))
133     table_end = '</table>'
134     return '\n\t'.join([table_begin] + [header_row] + rows + [table_end])
135 marduk 1.1
136 marduk 1.2 def ebuild_to_html(ebinfo, new=0, show_bugs=0):
137 marduk 1.3 """Convert ebuild (dict) to html, if new, print out a "this is new" notice
138     if show_bugs, show bugs for this particular ebuild (requires access to
139     bugzilla"""
140 marduk 1.1 if new:
141     new_string = """ &nbsp;&nbsp;<span class="new">new!</span> """
142     else:
143     new_string = ""
144    
145 marduk 1.2 table_begin = '<table class="ebuild">'
146     name_and_date = ('<tr><td class="fields">'
147 marduk 1.1 '<a href="%spackages/?category=%s;name=%s">%s</a> %s%s<br>'
148 marduk 1.2 '<span class="time">%s</span>'
149     '</td></tr>' % (config.FEHOME, quote(ebinfo['category']),
150     quote(ebinfo['name']),
151     ebinfo['name'],
152     ebinfo['version'],
153     new_string,
154     ebinfo['time'].localtime().strftime("%c %Z")))
155    
156     desc_and_changes = ('<tr><td class="item" valign="top">'
157     '<img alt="" align="right" src="%simages/%s.png">'
158     '<p><b>Description:</b> %s</p>'
159     '<p><b>Changes:</b><br>'
160     '%s</p></td></tr>' % (
161     config.FEHOME,
162     ebinfo['category'],
163     escape(ebinfo['description']),
164     changelog_to_html(ebinfo['changelog'])))
165    
166     archs = '<tr><td>%s</td></tr>' % archs_to_html([ebinfo])
167     general = '<tr><td>%s</td></tr>' % general_info_to_html(ebinfo)
168     table_end = '</table>'
169    
170     if 1 == 1:
171     bug_string = ''
172     else:
173 marduk 1.3 bug_string = '<br><h3>Related bugs:</h3>%s' \
174     % bugs_to_html(ebinfo['name'])
175 marduk 1.2
176     return '\n\t'.join([table_begin,
177     name_and_date,
178     desc_and_changes,
179     archs,
180     general,
181     table_end,
182     bug_string])
183    
184 marduk 1.1 def general_info_to_html(pkg):
185     """This actually will (should) take either a package or ebuild dict
186     as an argument"""
187    
188     changelogurl = ('http://www.gentoo.org/cgi-bin/viewcvs.cgi/*checkout*/'
189     '%s/%s/ChangeLog' % (pkg['category'],pkg['name']))
190 marduk 1.2 cat_header = '<th class="category">Category</th>'
191     license_header = '<th class="license">License</th>'
192     category = ('<td class="category">'
193     '<a href="%spackages/?category=%s">%s</a></td>' % (config.FEHOME,
194     pkg['category'], pkg['category']))
195     homepage = ('<td class="homepage" rowspan="2">%s</td>'
196     % homepage_to_html(pkg['homepage']))
197     license = ('<td class="license">%s</td>'
198     % license_to_html(pkg['license']))
199     changelog = ('<td class="changelog" rowspan="2">'
200     '<a href="%s">ChangeLog</a><td>' % changelogurl)
201    
202     return '\n\t'.join(['<table class="general_info">',
203     '<tr>',
204     cat_header,
205     homepage,
206     license_header,
207     changelog,
208     '</tr>',
209     '<tr>',
210     category,
211     license,
212     '</tr>',
213     '</table>'])
214 marduk 1.1
215 marduk 1.2 def bugs_to_html(package):
216     """Given package name (no version #s), return html text of bugs as
217     reported by bugzilla"""
218     # Right now we have an issue with the bugzilla site. New interface
219     # needs to be written, Bail out.
220     return ""
221     import urllib2
222     url = ('http://bugs.gentoo.org/buglist.cgi?query_format='
223     '&short_desc_type=allwords&short_desc=%s'
224     '&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED'
225     '&bug_status=REOPENED'
226     '&ctype=csv' % package)
227     fp = urllib2.urlopen(url)
228     factory = bugs.BugFactory()
229     package_bugs = factory.fromCSV(fp)
230     if package_bugs:
231     writer = bugs.HTMLWriter(package_bugs, 'bugs.gentoo.org')
232     return str(writer)
233     else:
234     return "None"
235    
236 marduk 1.3 def get_most_recent(db, max=config.MAXPERPAGE, arch="", branch=""):
237 marduk 1.1 c = db.cursor()
238     extra = ''
239     if arch:
240     stable_extra = ('ebuild.arch REGEXP "^%s| %s" '
241     ' AND ebuild.prevarch NOT REGEXP"^%s| %s"'
242     % (arch,arch,arch,arch))
243     testing_extra = ('ebuild.arch REGEXP "^~%s| ~%s" '
244     ' AND ebuild.prevarch NOT REGEXP "^~%s| ~%s"'
245     % (arch,arch,arch,arch))
246     if branch == 'stable':
247     extra = ' AND (%s) ' % stable_extra
248     elif branch == 'testing':
249     extra = ' AND (%s) ' % testing_extra
250     else:
251     extra = ' AND ((%s) OR (%s)) ' % (stable_extra, testing_extra)
252     query = """SELECT ebuild.category,ebuild.name,version,when_found,description,
253     changelog,arch,homepage,license FROM ebuild,package WHERE ebuild.name=\
254     package.name AND ebuild.category=package.category %s ORDER by when_found DESC \
255     LIMIT %s""" % (extra,max)
256     c.execute(query)
257     results = c.fetchall()
258     return results
259    
260     def query_to_dict(d):
261 marduk 1.3 """Convert a SQL query to a dict"""
262 marduk 1.1 einfo = {}
263 marduk 1.3 keys = ('category', 'name', 'version', 'time', 'description', 'changelog',
264     'arch', 'homepage', 'license')
265 marduk 1.1 for i in range(len(keys)):
266     try:
267     einfo[keys[i]] = d[i]
268     except IndexError:
269     continue
270     return einfo
271    
272 marduk 1.3 def get_recent_releases(pkg, db, max=config.MAX_RECENT_RELEASES):
273 marduk 1.1 """Return MAX_RECENT_RELEASES most recent releases for pkg. Returns and
274     ebuild-type dict"""
275     c = db.cursor()
276 marduk 1.3 query = ('SELECT category,name,version,when_found,NULL,changelog,arch ,'
277     'NULL,NULL FROM ebuild WHERE name="%s" AND category="%s" ORDER BY '
278     'version DESC LIMIT %s' % (pkg['name'],pkg['category'],max))
279 marduk 1.1 c.execute(query)
280     results = c.fetchall()
281     #print results
282     return [ query_to_dict(i) for i in results ]
283    
284 marduk 1.3 def ebuilds_to_rss(fp, ebuilds, simple=False, subtitle=""):
285 marduk 1.1 """write out ebuild info to RSS file (fp)"""
286     fp.write("""<?xml version="1.0" encoding="utf-8"?>
287     <rss version="0.92">
288     <channel>
289     <title>Fresh ebuilds %s</title>
290     <link>%s</link>
291     <description>Latest ebuilds from the Gentoo Linux portage tree</description>
292 marduk 1.2 <![CDATA[<link rel="stylesheet" href="%s" type="text/css" title="styled" />]]>
293 marduk 1.1 <webMaster>www@gentoo.org</webMaster>
294     <managingEditor>marduk@gentoo.org</managingEditor>
295 marduk 1.2 <pubDate>%s</pubDate>""" % (subtitle,config.FEHOME, config.STYLESHEET,
296 marduk 1.1 time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())))
297    
298     for ebuild in ebuilds:
299     if simple:
300     description = escape(ebuild['description'])
301     else:
302     description = '\n<![CDATA[\n%s\n]]>' % ebuild_to_html(ebuild)
303    
304     fp.write("""<item>
305     <title>%s %s</title>
306     <link>%sebuilds/?%s-%s</link>
307     <description>
308     %s
309     </description>
310     <pubDate>%s</pubDate>
311     </item>
312     """ % (ebuild['name'],
313     ebuild['version'],
314     config.FEHOME,
315     ebuild['name'],
316     ebuild['version'],
317     description,
318     ebuild['time'].gmtime().strftime("%a, %d %b %Y %H:%M:%S +0000"))
319     )
320    
321     fp.write("</channel>\n</rss>\n")
322    
323     if __name__ == '__main__':
324     try:
325     if sys.argv[1] == '-g':
326     ebuilddb.main()
327     except IndexError:
328 marduk 1.3 pass
329 marduk 1.2
330 marduk 1.1 db = ebuilddb.db_connect()
331 marduk 1.3 branches = ('', 'stable', 'testing')
332 marduk 1.1 for arch in [''] + config.ARCHLIST:
333     for branch in branches:
334 marduk 1.3 fullpath = os.path.join(config.LOCALHOME, "archs", arch, branch,
335     config.INDEX)
336 marduk 1.1 index = open(fullpath,'w')
337     if arch:
338     sarch = arch
339     else:
340     sarch = 'all'
341     if branch:
342     sbranch = branch
343     else:
344     sbranch = 'all'
345    
346     index.write("""<table border="0" cellpadding="0" cellspacing="5"
347     width="100%">\n""")
348     index.write("""<tr><td valign="top">\n""")
349 marduk 1.3 index.write('<!--#include file="archnav.html" -->\n\n</td></tr>\n'
350     '<tr><td>')
351     results = get_most_recent(db, arch=arch, branch=branch)
352 marduk 1.1 ebuilds = [ query_to_dict(i) for i in results ]
353     for ebuild in ebuilds:
354 marduk 1.3 new = is_new(db, ebuild)
355 marduk 1.1 pkgfilename = "%s/%s-%s.html" % (
356     config.EBUILD_FILES,ebuild['name'],ebuild['version'])
357 marduk 1.3 ebuild_html = ebuild_to_html(ebuild, new)
358     if arch == '' and branch == '':
359 marduk 1.1 pkgfile = open(pkgfilename,'w')
360     pkgfile.write(ebuild_html)
361     pkgfile.close()
362 marduk 1.3 ebuildfilename = "%s/%s/%s/%s-%s.ebuild" \
363     % (ebuilddb.config.PORTAGE_DIR,
364 marduk 1.1 ebuild['category'],ebuild['name'],ebuild['name'],
365     ebuild['version'])
366 marduk 1.2 os.system('touch -r %s %s || touch -d "today -1 year" %s'
367     % (ebuildfilename,pkgfilename,pkgfilename))
368 marduk 1.1
369     try:
370 marduk 1.2 index.write('%s\n\n' % (ebuild_html))
371 marduk 1.1 except IOError:
372 marduk 1.3 continue
373 marduk 1.1 index.write("""</table>\n""")
374     index.close()
375    
376 marduk 1.3 subtitle = ' %s %s' % (arch, branch)
377     rss = open(os.path.join(config.LOCALHOME, "archs", arch, branch,
378     config.RSS), 'w')
379     ebuilds_to_rss(rss, ebuilds, simple=False, subtitle=subtitle)
380 marduk 1.1 rss.close()
381    
382 marduk 1.3 rss2 = open(os.path.join(config.LOCALHOME, "archs", arch, branch,
383     config.RSS2), 'w')
384     ebuilds_to_rss(rss2, ebuilds, simple=True, subtitle=subtitle)
385 marduk 1.1 rss.close()

  ViewVC Help
Powered by ViewVC 1.1.20