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 directory_listing(self, path, environ, start_response):
22
request = WSGIRequest(environ)
23
response = WSGIResponse()
24
listing = [d for d in os.listdir(path) if not d.startswith('.')]
25
response.headers['Content-Type'] = 'text/html'
26
print >> response, '<html><body>'
27
for d in sorted(listing):
28
if os.path.isdir(os.path.join(path, d)):
30
print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
31
print >> response, '</body></html>'
32
return response(environ, start_response)
34
def app_for_branch(self, b, path):
37
_history = History.from_branch(b)
38
_history.use_file_cache(FileChangeCache(_history, sql_dir))
40
name = os.path.basename(os.path.abspath(path))
43
h = BranchWSGIApp(_history, name).app
44
self.root.cache[path] = h
49
def __call__(self, environ, start_response):
50
path = os.path.join(self.root.folder, self.folder)
51
if not os.path.isdir(path):
52
raise httpexceptions.HTTPNotFound()
53
if path in self.root.cache:
54
return self.root.cache[path](environ, start_response)
56
b = branch.Branch.open(path)
57
except errors.NotBranchError:
58
segment = path_info_pop(environ)
60
raise httpexceptions.HTTPMovedPermanently(environ['SCRIPT_NAME'] + '/')
62
return self.directory_listing(path, environ, start_response)
64
relpath = os.path.join(self.folder, segment)
65
return BranchesFromFileSystemServer(relpath, self.root)(
66
environ, start_response)
68
return self.app_for_branch(b, path)(environ, start_response)
71
class BranchesFromFileSystemRoot(object):
72
def __init__(self, folder):
75
def __call__(self, environ, start_response):
76
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
77
if environ['PATH_INFO'].startswith('/static/'):
78
segment = path_info_pop(environ)
79
assert segment == 'static'
80
return static_app(environ, start_response)
82
return BranchesFromFileSystemServer(
83
'', self)(environ, start_response)
85
app = BranchesFromFileSystemRoot('.')
88
app = make_middleware(app)
89
app = make_filter(app, None)
92
httpserver.serve(app, host='127.0.0.1', port='9876')