1
"""Serve branches at urls that mimic a transport's file system layout."""
3
from bzrlib import branch, errors, lru_cache, urlutils
5
from paste.request import path_info_pop
6
from paste import httpexceptions
7
from paste import urlparser
9
from loggerhead.apps.branch import BranchWSGIApp
10
from loggerhead.apps import favicon_app, static_app
11
from loggerhead.config import LoggerheadConfig
12
from loggerhead.controllers.directory_ui import DirectoryUI
15
class BranchesFromTransportServer(object):
17
def __init__(self, transport, root, name=None):
18
self.transport = transport
21
self._config = root._config
23
def app_for_branch(self, branch):
25
name = branch._get_nick(local=True)
30
branch_app = BranchWSGIApp(
32
{'cachepath': self._config.SQL_DIR},
33
self.root.graph_cache, is_root=is_root,
34
use_cdn=self._config.get_option('use_cdn'))
37
def app_for_non_branch(self, environ):
38
segment = path_info_pop(environ)
40
raise httpexceptions.HTTPMovedPermanently(
41
environ['SCRIPT_NAME'] + '/')
47
return DirectoryUI(environ['loggerhead.static.url'],
51
new_transport = self.transport.clone(segment)
53
new_name = urlutils.join(self.name, segment)
55
new_name = '/' + segment
56
return BranchesFromTransportServer(new_transport, self.root, new_name)
58
def __call__(self, environ, start_response):
60
b = branch.Branch.open_from_transport(self.transport)
61
except errors.NotBranchError:
62
if not self.transport.listable() or not self.transport.has('.'):
63
raise httpexceptions.HTTPNotFound()
64
return self.app_for_non_branch(environ)(environ, start_response)
66
return self.app_for_branch(b)(environ, start_response)
69
class BranchesFromTransportRoot(object):
71
def __init__(self, transport, config):
72
self.graph_cache = lru_cache.LRUCache(10)
73
self.transport = transport
76
def __call__(self, environ, start_response):
77
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
78
if environ['PATH_INFO'].startswith('/static/'):
79
segment = path_info_pop(environ)
80
assert segment == 'static'
81
return static_app(environ, start_response)
82
elif environ['PATH_INFO'] == '/favicon.ico':
83
return favicon_app(environ, start_response)
84
elif '/.bzr/' in environ['PATH_INFO']:
85
app = urlparser.make_static(None, self.transport)
86
return app(environ, start_response)
88
return BranchesFromTransportServer(
89
self.transport, self)(environ, start_response)
92
class UserBranchesFromTransportRoot(object):
94
def __init__(self, transport, config):
95
self.graph_cache = lru_cache.LRUCache(10)
96
self.transport = transport
98
self.trunk_dir = config.get_option('trunk_dir')
100
def __call__(self, environ, start_response):
101
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
102
path_info = environ['PATH_INFO']
103
if path_info.startswith('/static/'):
104
segment = path_info_pop(environ)
105
assert segment == 'static'
106
return static_app(environ, start_response)
107
elif path_info == '/favicon.ico':
108
return favicon_app(environ, start_response)
110
# segments starting with ~ are user branches
111
if path_info.startswith('/~'):
112
segment = path_info_pop(environ)
113
new_transport = self.transport.clone(segment[1:])
114
return BranchesFromTransportServer(
115
new_transport, self, segment)(environ, start_response)
117
new_transport = self.transport.clone(self.trunk_dir)
118
return BranchesFromTransportServer(
119
new_transport, self)(environ, start_response)