1
"""Serve branches at urls that mimic the file system layout."""
6
from bzrlib import branch, errors, lru_cache
1
# Copyright (C) 2008, 2009 Canonical Ltd.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Serve branches at urls that mimic a transport's file system layout."""
21
from bzrlib import branch, errors, lru_cache, urlutils
22
from bzrlib.config import LocationConfig
23
from bzrlib.transport import get_transport
24
from bzrlib.transport.http import wsgi
8
26
from paste.request import path_info_pop
9
27
from paste import httpexceptions
28
from paste import urlparser
11
30
from loggerhead.apps.branch import BranchWSGIApp
12
31
from loggerhead.apps import favicon_app, static_app
13
32
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):
35
'yes': True, 'no': False,
36
'on': True, 'off': False,
37
'1': True, '0': False,
38
'true': True, 'false': False,
41
class BranchesFromTransportServer(object):
43
def __init__(self, transport, root, name=None):
44
self.transport = transport
47
self._config = root._config
24
49
def app_for_branch(self, branch):
51
name = branch._get_nick(local=True)
31
56
branch_app = BranchWSGIApp(
32
branch, name, {'cachepath': sql_dir}, self.root.graph_cache,
59
{'cachepath': self._config.SQL_DIR},
60
self.root.graph_cache,
62
use_cdn=self._config.get_option('use_cdn'),
34
64
return branch_app.app
36
66
def app_for_non_branch(self, environ):
46
return DirectoryUI(environ['loggerhead.static.url'], self.path, name)
77
environ['loggerhead.static.url'], self.transport, name)
48
new_path = os.path.join(self.path, segment)
79
new_transport = self.transport.clone(segment)
50
new_name = os.path.join(self.name, segment)
81
new_name = urlutils.join(self.name, segment)
52
83
new_name = '/' + segment
53
return BranchesFromFileSystemServer(new_path, self.root, new_name)
55
def __call__(self, environ, start_response):
56
if not os.path.isdir(self.path):
84
return BranchesFromTransportServer(new_transport, self.root, new_name)
86
def app_for_bazaar_data(self, relpath):
87
if relpath == '/.bzr/smart':
88
wsgi_app = wsgi.SmartWSGIApp(self.transport)
89
return wsgi.RelpathSetter(wsgi_app, '', 'PATH_INFO')
91
# TODO: Use something here that uses the transport API
92
# rather than relying on the local filesystem API.
93
base = self.transport.base
94
readonly_prefix = 'readonly+'
95
if base.startswith(readonly_prefix):
96
base = base[len(readonly_prefix):]
98
path = urlutils.local_path_from_url(base)
99
except errors.InvalidURL:
100
raise httpexceptions.HTTPNotFound()
102
return urlparser.make_static(None, path)
104
def check_serveable(self, config):
105
value = config.get_user_option('http_serve')
108
elif not _bools.get(value.lower(), True):
57
109
raise httpexceptions.HTTPNotFound()
111
def __call__(self, environ, start_response):
112
path = environ['PATH_INFO']
59
b = branch.Branch.open(self.path)
114
b = branch.Branch.open_from_transport(self.transport)
60
115
except errors.NotBranchError:
116
if path.startswith('/.bzr'):
117
self.check_serveable(LocationConfig(self.transport.base))
118
return self.app_for_bazaar_data(path)(environ, start_response)
119
if not self.transport.listable() or not self.transport.has('.'):
120
raise httpexceptions.HTTPNotFound()
61
121
return self.app_for_non_branch(environ)(environ, start_response)
63
return self.app_for_branch(b)(environ, start_response)
66
class BranchesFromFileSystemRoot(object):
68
def __init__(self, folder):
69
self.graph_cache = lru_cache.LRUCache()
123
self.check_serveable(b.get_config())
124
if path.startswith('/.bzr'):
125
return self.app_for_bazaar_data(path)(environ, start_response)
127
return self.app_for_branch(b)(environ, start_response)
130
_transport_store = threading.local()
132
def get_transport_for_thread(base):
134
thread_transports = getattr(_transport_store, 'transports', None)
135
if thread_transports is None:
136
thread_transports = _transport_store.transports = {}
137
if base in thread_transports:
138
return thread_transports[base]
139
transport = get_transport(base)
143
class BranchesFromTransportRoot(object):
145
def __init__(self, base, config):
146
self.graph_cache = lru_cache.LRUCache(10)
148
self._config = config
72
150
def __call__(self, environ, start_response):
73
151
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
78
156
elif environ['PATH_INFO'] == '/favicon.ico':
79
157
return favicon_app(environ, start_response)
81
return BranchesFromFileSystemServer(
82
self.folder, self)(environ, start_response)
85
class UserBranchesFromFileSystemRoot(object):
87
def __init__(self, folder, trunk_dir):
88
self.graph_cache = lru_cache.LRUCache()
90
self.trunk_dir = trunk_dir
159
transport = get_transport_for_thread(self.base)
160
return BranchesFromTransportServer(
161
transport, self)(environ, start_response)
164
class UserBranchesFromTransportRoot(object):
166
def __init__(self, base, config):
167
self.graph_cache = lru_cache.LRUCache(10)
169
self._config = config
170
self.trunk_dir = config.get_option('trunk_dir')
92
172
def __call__(self, environ, start_response):
93
173
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
94
path_info= environ['PATH_INFO']
174
path_info = environ['PATH_INFO']
95
175
if path_info.startswith('/static/'):
96
176
segment = path_info_pop(environ)
97
177
assert segment == 'static'
99
179
elif path_info == '/favicon.ico':
100
180
return favicon_app(environ, start_response)
182
transport = get_transport_for_thread(self.base)
102
183
# segments starting with ~ are user branches
103
184
if path_info.startswith('/~'):
104
185
segment = path_info_pop(environ)
105
new_path = os.path.join(self.folder, segment[1:])
106
return BranchesFromFileSystemServer(
107
new_path, self, segment)(environ, start_response)
186
return BranchesFromTransportServer(
187
transport.clone(segment[1:]), self, segment)(
188
environ, start_response)
109
new_path = os.path.join(self.folder, self.trunk_dir)
110
return BranchesFromFileSystemServer(
111
new_path, self)(environ, start_response)
190
return BranchesFromTransportServer(
191
transport.clone(self.trunk_dir), self)(
192
environ, start_response)