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

Contents of /src/packages/gentoo.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations) (download) (as text)
Fri Sep 17 02:16:17 2004 UTC (9 years, 10 months ago) by marduk
Branch: MAIN
Changes since 1.4: +7 -3 lines
File MIME type: text/x-python
Adding XML and p_objects work.

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.5 __revision__ = "$Revision: 1.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    
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 marduk 1.4 fp.write("""<?xml version="1.0" encoding="iso-8859-1"?>
287 marduk 1.1 <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 marduk 1.5 def main(argv=Null):
324     argv = argv or sys.argv
325 marduk 1.1 try:
326 marduk 1.5 if argv[1] == '-g':
327 marduk 1.1 ebuilddb.main()
328     except IndexError:
329 marduk 1.3 pass
330 marduk 1.2
331 marduk 1.1 db = ebuilddb.db_connect()
332 marduk 1.3 branches = ('', 'stable', 'testing')
333 marduk 1.1 for arch in [''] + config.ARCHLIST:
334     for branch in branches:
335 marduk 1.3 fullpath = os.path.join(config.LOCALHOME, "archs", arch, branch,
336     config.INDEX)
337 marduk 1.1 index = open(fullpath,'w')
338     if arch:
339     sarch = arch
340     else:
341     sarch = 'all'
342     if branch:
343     sbranch = branch
344     else:
345     sbranch = 'all'
346    
347     index.write("""<table border="0" cellpadding="0" cellspacing="5"
348     width="100%">\n""")
349     index.write("""<tr><td valign="top">\n""")
350 marduk 1.3 index.write('<!--#include file="archnav.html" -->\n\n</td></tr>\n'
351     '<tr><td>')
352     results = get_most_recent(db, arch=arch, branch=branch)
353 marduk 1.1 ebuilds = [ query_to_dict(i) for i in results ]
354     for ebuild in ebuilds:
355 marduk 1.3 new = is_new(db, ebuild)
356 marduk 1.1 pkgfilename = "%s/%s-%s.html" % (
357     config.EBUILD_FILES,ebuild['name'],ebuild['version'])
358 marduk 1.3 ebuild_html = ebuild_to_html(ebuild, new)
359     if arch == '' and branch == '':
360 marduk 1.1 pkgfile = open(pkgfilename,'w')
361     pkgfile.write(ebuild_html)
362     pkgfile.close()
363 marduk 1.3 ebuildfilename = "%s/%s/%s/%s-%s.ebuild" \
364     % (ebuilddb.config.PORTAGE_DIR,
365 marduk 1.1 ebuild['category'],ebuild['name'],ebuild['name'],
366     ebuild['version'])
367 marduk 1.2 os.system('touch -r %s %s || touch -d "today -1 year" %s'
368     % (ebuildfilename,pkgfilename,pkgfilename))
369 marduk 1.1
370     try:
371 marduk 1.2 index.write('%s\n\n' % (ebuild_html))
372 marduk 1.1 except IOError:
373 marduk 1.3 continue
374 marduk 1.1 index.write("""</table>\n""")
375     index.close()
376    
377 marduk 1.3 subtitle = ' %s %s' % (arch, branch)
378     rss = open(os.path.join(config.LOCALHOME, "archs", arch, branch,
379     config.RSS), 'w')
380     ebuilds_to_rss(rss, ebuilds, simple=False, subtitle=subtitle)
381 marduk 1.1 rss.close()
382    
383 marduk 1.3 rss2 = open(os.path.join(config.LOCALHOME, "archs", arch, branch,
384     config.RSS2), 'w')
385     ebuilds_to_rss(rss2, ebuilds, simple=True, subtitle=subtitle)
386 marduk 1.1 rss.close()
387 marduk 1.5
388     if __name__ == '__main__':
389     sys.exit(main())

  ViewVC Help
Powered by ViewVC 1.1.20