#! /usr/bin/env python
import SimpleHTTPServer
import pysvn.ra
from cStringIO import StringIO
REPO = pysvn.ra.connect('svn+ssh://codespeak.net/svn')
REV = 'HEAD'
HALFLIFE = 2000
CACHE = {}
class SvnHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def send_head(self):
"""Common code for GET and HEAD commands.
This sends the response code and MIME headers.
Return value is either a file object (which has to be copied
to the outputfile by the caller unless the command was HEAD,
and must be closed by the caller under all circumstances), or
None, in which case the caller has nothing further to do.
"""
global REV
try:
path = filter(None, self.path.split('/'))
path = '/'.join(path)
f = StringIO()
url = REPO.join(path) + '/'
print >> f, '
%s/' % (url,)
print >> f, '%s/
' % (path,)
print >> f, ''
#
try:
entries = CACHE[path]
except KeyError:
try:
REV, _, entries = REPO.get_dir(path, REV, want_props=False,
want_contents=True)
except pysvn.ra.SvnServeError, e:
entries = e
CACHE[path] = entries
if isinstance(entries, Exception):
raise entries
names = entries.keys()
names.sort()
for name in names:
entry = name
if 'svn:entry:committed-rev' in entries[name]:
rev = entries[name]['svn:entry:committed-rev']
color = getcolor(rev)
entry = '[%d] %s' % (color, rev,
entry)
if entries[name]['svn:entry:kind'] == 'dir':
entry = '%s' % (name, entry)
print >> f, '- %s' % entry
#
print >> f, '
'
print >> f, ''
f.seek(0)
contenttype = "text/html"
except:
import traceback
f = StringIO()
print >> f, '-'*40
traceback.print_exc(file=f)
print >> f, '-'*40
print f.getvalue()
f.seek(0)
contenttype = "text/plain"
self.send_response(200)
self.send_header("Content-type", contenttype)
self.end_headers()
return f
def address_string(self):
"""Override to return the client address with no formatting."""
return "%s:%d" % self.client_address
def getcolor(rev):
currenttime = REV - rev
factor = 0.5 ** (float(currenttime) / HALFLIFE)
assert 0.0 <= factor <= 1.0
intensity = 255 - int(255*factor)
intensity2 = int(intensity * 0.7)
return '%02X%02X%02X' % (intensity, intensity2, intensity2)
if __name__ == '__main__':
SimpleHTTPServer.test(SvnHTTPRequestHandler)