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
path = os.path.join(self.root.folder, self.folder)
23
if not os.path.isdir(path):
24
raise httpexceptions.HTTPNotFound()
25
if path in self.root.cache:
26
return self.root.cache[path](environ, start_response)
28
b = branch.Branch.open(path)
29
except errors.NotBranchError:
30
segment = path_info_pop(environ)
32
raise httpexceptions.HTTPMovedPermanently(environ['SCRIPT_NAME'] + '/')
34
request = WSGIRequest(environ)
35
response = WSGIResponse()
36
listing = [d for d in os.listdir(path) if not d.startswith('.')]
37
response.headers['Content-Type'] = 'text/html'
38
print >> response, '<html><body>'
39
for d in sorted(listing):
40
if os.path.isdir(os.path.join(path, d)):
42
print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
43
print >> response, '</body></html>'
44
return response(environ, start_response)
46
relpath = os.path.join(self.folder, segment)
47
return BranchesFromFileSystemServer(relpath, self.root)(
48
environ, start_response)
52
_history = History.from_branch(b)
53
_history.use_file_cache(FileChangeCache(_history, sql_dir))
55
name = os.path.basename(os.path.abspath(path))
58
h = BranchWSGIApp(_history, name).app
59
self.root.cache[path] = h
60
return h(environ, start_response)
64
class BranchesFromFileSystemRoot(object):
65
def __init__(self, folder):
68
def __call__(self, environ, start_response):
69
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
70
if environ['PATH_INFO'].startswith('/static/'):
71
segment = path_info_pop(environ)
72
assert segment == 'static'
73
return static_app(environ, start_response)
75
return BranchesFromFileSystemServer(
76
'', self)(environ, start_response)
78
app = BranchesFromFileSystemRoot('.')
81
app = make_middleware(app)
82
app = make_filter(app, None)
85
httpserver.serve(app, host='127.0.0.1', port='9876')