1
import cgi, os, tempfile
2
from bzrlib import branch, errors
3
from loggerhead.history import History
4
from loggerhead.wsgiapp import BranchWSGIApp, static_app
5
from paste.request import path_info_pop
6
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
7
from paste import httpexceptions
8
from paste import httpserver
9
from paste.httpexceptions import make_middleware
10
from paste.translogger import make_filter
11
from loggerhead.changecache import FileChangeCache
14
sql_dir = tempfile.mkdtemp()
16
class BranchesFromFileSystemServer(object):
17
def __init__(self, folder, root):
21
def __call__(self, environ, start_response):
22
segment = path_info_pop(environ)
24
f = os.path.join(self.root.folder, self.folder)
25
request = WSGIRequest(environ)
26
response = WSGIResponse()
27
listing = [d for d in os.listdir(f) if not d.startswith('.')]
28
response.headers['Content-Type'] = 'text/html'
29
print >> response, '<html><body>'
30
for d in sorted(listing):
32
print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
33
print >> response, '</body></html>'
34
return response(environ, start_response)
35
relpath = os.path.join(self.folder, segment)
36
f = os.path.join(self.root.folder, relpath)
37
if not os.path.isdir(f):
38
raise httpexceptions.HTTPNotFound()
39
if f in self.root.cache:
40
return self.root.cache[f](environ, start_response)
42
b = branch.Branch.open(f)
43
except errors.NotBranchError:
44
return BranchesFromFileSystemServer(relpath, self.root)(environ, start_response)
48
_history = History.from_branch(b)
49
_history.use_file_cache(FileChangeCache(_history, sql_dir))
50
h = BranchWSGIApp(_history, relpath).app
51
self.root.cache[f] = h
52
return h(environ, start_response)
56
class BranchesFromFileSystemRoot(object):
57
def __init__(self, folder):
60
def __call__(self, environ, start_response):
61
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
62
segment = path_info_pop(environ)
63
if segment == 'static':
64
return static_app(environ, start_response)
66
return BranchesFromFileSystemServer(
67
segment, self)(environ, start_response)
69
app = BranchesFromFileSystemRoot('.')
72
app = make_middleware(app)
73
app = make_filter(app, None)
76
httpserver.serve(app, host='127.0.0.1', port='9876')