1
"""Serve branches at urls that mimic the file system layout."""
7
from bzrlib import branch, errors, lru_cache
9
from paste.request import path_info_pop
10
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
11
from paste import httpexceptions
13
from loggerhead.apps.branch import BranchWSGIApp
14
from loggerhead.apps import favicon_app, static_app
16
sql_dir = tempfile.mkdtemp()
19
class DirectoryListing(object):
21
def __init__(self, path):
24
def __call__(self, environ, start_response):
25
request = WSGIRequest(environ)
26
response = WSGIResponse()
27
listing = [d for d in os.listdir(self.path) if not d.startswith('.')]
28
response.headers['Content-Type'] = 'text/html'
29
print >> response, '<html><body>'
30
for d in sorted(listing):
31
if os.path.isdir(os.path.join(self.path, d)):
33
print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
34
print >> response, '</body></html>'
35
return response(environ, start_response)
38
class BranchesFromFileSystemServer(object):
39
def __init__(self, folder, root):
43
def app_for_branch(self, branch):
45
name = os.path.basename(os.path.abspath(self.root.folder))
48
branch_app = BranchWSGIApp(
49
branch, name, {'cachepath': sql_dir}, self.root.graph_cache)
52
def app_for_non_branch(self, environ):
53
segment = path_info_pop(environ)
55
raise httpexceptions.HTTPMovedPermanently(
56
environ['SCRIPT_NAME'] + '/')
58
return DirectoryListing(os.path.join(self.root.folder, self.folder))
60
relpath = os.path.join(self.folder, segment)
61
return BranchesFromFileSystemServer(relpath, self.root)
63
def __call__(self, environ, start_response):
64
path = os.path.join(self.root.folder, self.folder)
65
if not os.path.isdir(path):
66
raise httpexceptions.HTTPNotFound()
68
b = branch.Branch.open(path)
69
except errors.NotBranchError:
70
return self.app_for_non_branch(environ)(environ, start_response)
72
return self.app_for_branch(b)(environ, start_response)
75
class BranchesFromFileSystemRoot(object):
77
def __init__(self, folder):
78
self.graph_cache = lru_cache.LRUCache()
81
def __call__(self, environ, start_response):
82
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
83
if environ['PATH_INFO'].startswith('/static/'):
84
segment = path_info_pop(environ)
85
assert segment == 'static'
86
return static_app(environ, start_response)
87
elif environ['PATH_INFO'] == '/favicon.ico':
88
return favicon_app(environ, start_response)
90
return BranchesFromFileSystemServer(
91
'', self)(environ, start_response)
1
from loggerhead.history import History
2
from loggerhead.wsgiapp import BranchWSGIApp
4
h = History.from_folder('.')
9
from paste import httpserver
10
from paste.evalexception import EvalException
11
from paste.httpexceptions import make_middleware
12
from paste.translogger import make_filter
15
for w in EvalException, make_middleware:
18
app = make_filter(app, None)
20
httpserver.serve(app, host='127.0.0.1', port='9876')