1
1
"""Serve branches at urls that mimic the file system layout."""
7
6
from bzrlib import branch, errors, lru_cache
9
8
from paste.request import path_info_pop
10
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
11
9
from paste import httpexceptions
13
11
from loggerhead.apps.branch import BranchWSGIApp
14
12
from loggerhead.apps import favicon_app, static_app
16
sql_dir = tempfile.mkdtemp()
19
class DirectoryListing(object):
21
def __init__(self, path):
13
from loggerhead.controllers.directory_ui import DirectoryUI
15
sql_dir = tempfile.mkdtemp(prefix='loggerhead-cache-')
18
class BranchesFromFileSystemServer(object):
19
def __init__(self, path, root, name=None):
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
24
def app_for_branch(self, branch):
45
name = os.path.basename(os.path.abspath(self.root.folder))
48
29
branch_app = BranchWSGIApp(
49
30
branch, name, {'cachepath': sql_dir}, self.root.graph_cache)
50
31
return branch_app.app
55
36
raise httpexceptions.HTTPMovedPermanently(
56
37
environ['SCRIPT_NAME'] + '/')
57
38
elif segment == '':
58
return DirectoryListing(os.path.join(self.root.folder, self.folder))
43
return DirectoryUI(environ['loggerhead.static.url'], self.path, name)
60
relpath = os.path.join(self.folder, segment)
61
return BranchesFromFileSystemServer(relpath, self.root)
45
new_path = os.path.join(self.path, segment)
47
new_name = os.path.join(self.name, segment)
49
new_name = '/' + segment
50
return BranchesFromFileSystemServer(new_path, self.root, new_name)
63
52
def __call__(self, environ, start_response):
64
path = os.path.join(self.root.folder, self.folder)
65
if not os.path.isdir(path):
53
if not os.path.isdir(self.path):
66
54
raise httpexceptions.HTTPNotFound()
68
b = branch.Branch.open(path)
56
b = branch.Branch.open(self.path)
69
57
except errors.NotBranchError:
70
58
return self.app_for_non_branch(environ)(environ, start_response)
88
76
return favicon_app(environ, start_response)
90
78
return BranchesFromFileSystemServer(
91
'', self)(environ, start_response)
79
self.folder, self)(environ, start_response)
82
class UserBranchesFromFileSystemRoot(object):
84
def __init__(self, folder, trunk_dir):
85
self.graph_cache = lru_cache.LRUCache()
87
self.trunk_dir = trunk_dir
89
def __call__(self, environ, start_response):
90
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
91
path_info= environ['PATH_INFO']
92
if path_info.startswith('/static/'):
93
segment = path_info_pop(environ)
94
assert segment == 'static'
95
return static_app(environ, start_response)
96
elif path_info == '/favicon.ico':
97
return favicon_app(environ, start_response)
99
# segments starting with ~ are user branches
100
if path_info.startswith('/~'):
101
segment = path_info_pop(environ)
102
new_path = os.path.join(self.folder, segment[1:])
103
return BranchesFromFileSystemServer(
104
new_path, self, segment)(environ, start_response)
106
new_path = os.path.join(self.folder, self.trunk_dir)
107
return BranchesFromFileSystemServer(
108
new_path, self)(environ, start_response)