1
"""Serve branches at urls that mimic the file system layout."""
5
from bzrlib import branch, errors
6
from bzrlib import branch, errors, lru_cache
7
8
from paste.request import path_info_pop
8
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
9
9
from paste import httpexceptions
10
from paste import urlparser
11
12
from loggerhead.apps.branch import BranchWSGIApp
12
13
from loggerhead.apps import favicon_app, static_app
15
sql_dir = tempfile.mkdtemp()
14
from loggerhead.controllers.directory_ui import DirectoryUI
16
sql_dir = tempfile.mkdtemp(prefix='loggerhead-cache-')
17
19
class BranchesFromFileSystemServer(object):
18
def __init__(self, folder, root):
21
def __init__(self, path, root, name=None):
22
def directory_listing(self, path, environ, start_response):
23
request = WSGIRequest(environ)
24
response = WSGIResponse()
25
listing = [d for d in os.listdir(path) if not d.startswith('.')]
26
response.headers['Content-Type'] = 'text/html'
27
print >> response, '<html><body>'
28
for d in sorted(listing):
29
if os.path.isdir(os.path.join(path, d)):
31
print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
32
print >> response, '</body></html>'
33
return response(environ, start_response)
35
def app_for_branch(self, b, path):
37
name = os.path.basename(os.path.abspath(path))
40
h = BranchWSGIApp(path, name, {'cachepath': sql_dir})
41
self.root.cache[path] = h
26
def app_for_branch(self, branch):
28
name = branch.get_config().get_nickname()
33
branch_app = BranchWSGIApp(
34
branch, name, {'cachepath': sql_dir}, self.root.graph_cache,
38
def app_for_non_branch(self, environ):
39
segment = path_info_pop(environ)
41
raise httpexceptions.HTTPMovedPermanently(
42
environ['SCRIPT_NAME'] + '/')
48
return DirectoryUI(environ['loggerhead.static.url'],
52
new_path = os.path.join(self.path, segment)
54
new_name = os.path.join(self.name, segment)
56
new_name = '/' + segment
57
return BranchesFromFileSystemServer(new_path, self.root, new_name)
44
59
def __call__(self, environ, start_response):
45
path = os.path.join(self.root.folder, self.folder)
46
if not os.path.isdir(path):
60
if not os.path.isdir(self.path):
47
61
raise httpexceptions.HTTPNotFound()
48
cached = self.root.cache.get(path)
49
if cached is not None:
50
return cached.app(environ, start_response)
52
b = branch.Branch.open(path)
63
b = branch.Branch.open(self.path)
53
64
except errors.NotBranchError:
54
segment = path_info_pop(environ)
56
raise httpexceptions.HTTPMovedPermanently(
57
environ['SCRIPT_NAME'] + '/')
59
return self.directory_listing(path, environ, start_response)
61
relpath = os.path.join(self.folder, segment)
62
return BranchesFromFileSystemServer(relpath, self.root)(
63
environ, start_response)
65
return self.app_for_non_branch(environ)(environ, start_response)
65
return self.app_for_branch(b, path)(environ, start_response)
67
return self.app_for_branch(b)(environ, start_response)
68
70
class BranchesFromFileSystemRoot(object):
69
72
def __init__(self, folder):
73
self.graph_cache = lru_cache.LRUCache()
71
74
self.folder = folder
72
76
def __call__(self, environ, start_response):
73
77
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
74
78
if environ['PATH_INFO'].startswith('/static/'):
77
81
return static_app(environ, start_response)
78
82
elif environ['PATH_INFO'] == '/favicon.ico':
79
83
return favicon_app(environ, start_response)
84
elif '/.bzr/' in environ['PATH_INFO']:
85
app = urlparser.make_static(None, self.folder)
86
return app(environ, start_response)
81
88
return BranchesFromFileSystemServer(
82
'', self)(environ, start_response)
89
self.folder, self)(environ, start_response)
92
class UserBranchesFromFileSystemRoot(object):
94
def __init__(self, folder, trunk_dir):
95
self.graph_cache = lru_cache.LRUCache()
97
self.trunk_dir = trunk_dir
99
def __call__(self, environ, start_response):
100
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
101
path_info= environ['PATH_INFO']
102
if path_info.startswith('/static/'):
103
segment = path_info_pop(environ)
104
assert segment == 'static'
105
return static_app(environ, start_response)
106
elif path_info == '/favicon.ico':
107
return favicon_app(environ, start_response)
109
# segments starting with ~ are user branches
110
if path_info.startswith('/~'):
111
segment = path_info_pop(environ)
112
new_path = os.path.join(self.folder, segment[1:])
113
return BranchesFromFileSystemServer(
114
new_path, self, segment)(environ, start_response)
116
new_path = os.path.join(self.folder, self.trunk_dir)
117
return BranchesFromFileSystemServer(
118
new_path, self)(environ, start_response)