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.smart import request
24
from bzrlib.transport import get_transport
25
from bzrlib.transport.http import wsgi
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
27
5
from paste.request import path_info_pop
6
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
28
7
from paste import httpexceptions
29
from paste import urlparser
31
from loggerhead.apps.branch import BranchWSGIApp
32
from loggerhead.apps import favicon_app, robots_app, static_app
33
from loggerhead.controllers.directory_ui import DirectoryUI
36
'yes': True, 'no': False,
37
'on': True, 'off': False,
38
'1': True, '0': False,
39
'true': True, 'false': False,
42
class BranchesFromTransportServer(object):
44
def __init__(self, transport, root, name=None):
45
self.transport = transport
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):
48
self._config = root._config
50
def app_for_branch(self, branch):
52
name = branch._get_nick(local=True)
57
branch_app = BranchWSGIApp(
60
{'cachepath': self._config.SQL_DIR},
61
self.root.graph_cache,
63
use_cdn=self._config.get_option('use_cdn'),
67
def app_for_non_branch(self, environ):
21
def __call__(self, environ, start_response):
68
22
segment = path_info_pop(environ)
69
23
if segment is None:
70
raise httpexceptions.HTTPMovedPermanently(
71
environ['SCRIPT_NAME'] + '/')
78
environ['loggerhead.static.url'], self.transport, name)
80
new_transport = self.transport.clone(segment)
82
new_name = urlutils.join(self.name, segment)
84
new_name = '/' + segment
85
return BranchesFromTransportServer(new_transport, self.root, new_name)
87
def app_for_bazaar_data(self, relpath):
88
if relpath == '/.bzr/smart':
89
root_transport = get_transport_for_thread(self.root.base)
90
wsgi_app = wsgi.SmartWSGIApp(root_transport)
91
return wsgi.RelpathSetter(wsgi_app, '', 'loggerhead.path_info')
93
# TODO: Use something here that uses the transport API
94
# rather than relying on the local filesystem API.
95
base = self.transport.base
96
readonly_prefix = 'readonly+'
97
if base.startswith(readonly_prefix):
98
base = base[len(readonly_prefix):]
100
path = urlutils.local_path_from_url(base)
101
except errors.InvalidURL:
102
raise httpexceptions.HTTPNotFound()
104
return urlparser.make_static(None, path)
106
def check_serveable(self, config):
107
value = config.get_user_option('http_serve')
110
elif not _bools.get(value.lower(), True):
24
f = os.path.join(self.root.folder, self.folder)
25
request = WSGIRequest(environ)
26
response = WSGIResponse()
27
listing = [d for d in os.listdir(f) if not d.startswith('.')]
28
response.headers['Content-Type'] = 'text/html'
29
print >> response, '<html><body>'
30
for d in sorted(listing):
32
print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
33
print >> response, '</body></html>'
34
return response(environ, start_response)
35
relpath = os.path.join(self.folder, segment)
36
f = os.path.join(self.root.folder, relpath)
37
if not os.path.isdir(f):
111
38
raise httpexceptions.HTTPNotFound()
113
def __call__(self, environ, start_response):
114
path = environ['PATH_INFO']
39
if f in self.root.cache:
40
return self.root.cache[f](environ, start_response)
116
b = branch.Branch.open_from_transport(self.transport)
42
b = branch.Branch.open(f)
117
43
except errors.NotBranchError:
118
if path.startswith('/.bzr'):
119
self.check_serveable(LocationConfig(self.transport.base))
120
return self.app_for_bazaar_data(path)(environ, start_response)
121
if not self.transport.listable() or not self.transport.has('.'):
122
raise httpexceptions.HTTPNotFound()
123
return self.app_for_non_branch(environ)(environ, start_response)
125
self.check_serveable(b.get_config())
126
if path.startswith('/.bzr'):
127
return self.app_for_bazaar_data(path)(environ, start_response)
129
return self.app_for_branch(b)(environ, start_response)
132
_transport_store = threading.local()
134
def get_transport_for_thread(base):
135
thread_transports = getattr(_transport_store, 'transports', None)
136
if thread_transports is None:
137
thread_transports = _transport_store.transports = {}
138
if base in thread_transports:
139
return thread_transports[base]
140
transport = get_transport(base)
141
thread_transports[base] = transport
145
class BranchesFromTransportRoot(object):
147
def __init__(self, base, config):
148
self.graph_cache = lru_cache.LRUCache(10)
150
self._config = config
152
def __call__(self, environ, start_response):
153
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
154
environ['loggerhead.path_info'] = environ['PATH_INFO']
155
if environ['PATH_INFO'].startswith('/static/'):
156
segment = path_info_pop(environ)
157
assert segment == 'static'
158
return static_app(environ, start_response)
159
elif environ['PATH_INFO'] == '/favicon.ico':
160
return favicon_app(environ, start_response)
161
elif environ['PATH_INFO'] == '/robots.txt':
162
return robots_app(environ, start_response)
164
transport = get_transport_for_thread(self.base)
165
return BranchesFromTransportServer(
166
transport, self)(environ, start_response)
169
class UserBranchesFromTransportRoot(object):
171
def __init__(self, base, config):
172
self.graph_cache = lru_cache.LRUCache(10)
174
self._config = config
175
self.trunk_dir = config.get_option('trunk_dir')
177
def __call__(self, environ, start_response):
178
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
179
environ['loggerhead.path_info'] = environ['PATH_INFO']
180
path_info = environ['PATH_INFO']
181
if path_info.startswith('/static/'):
182
segment = path_info_pop(environ)
183
assert segment == 'static'
184
return static_app(environ, start_response)
185
elif path_info == '/favicon.ico':
186
return favicon_app(environ, start_response)
187
elif environ['PATH_INFO'] == '/robots.txt':
188
return robots_app(environ, start_response)
190
transport = get_transport_for_thread(self.base)
191
# segments starting with ~ are user branches
192
if path_info.startswith('/~'):
193
segment = path_info_pop(environ)
194
return BranchesFromTransportServer(
195
transport.clone(segment[1:]), self, segment)(
196
environ, start_response)
198
return BranchesFromTransportServer(
199
transport.clone(self.trunk_dir), self)(
200
environ, start_response)
44
return BranchesFromFileSystemServer(relpath, self.root)(environ, start_response)
48
_history = History.from_branch(b)
49
_history.use_file_cache(FileChangeCache(_history, sql_dir))
50
h = BranchWSGIApp(_history, relpath).app
51
self.root.cache[f] = h
52
return h(environ, start_response)
56
class BranchesFromFileSystemRoot(object):
57
def __init__(self, folder):
60
def __call__(self, environ, start_response):
61
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
62
segment = path_info_pop(environ)
63
if segment == 'static':
64
return static_app(environ, start_response)
66
return BranchesFromFileSystemServer(
67
segment, self)(environ, start_response)
69
app = BranchesFromFileSystemRoot('.')
72
app = make_middleware(app)
73
app = make_filter(app, None)
76
httpserver.serve(app, host='127.0.0.1', port='9876')