1
"""Serve branches at urls that mimic a transport's file system layout."""
3
from bzrlib import branch, errors, lru_cache, urlutils
4
from bzrlib.transport import get_transport
5
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
7
5
from paste.request import path_info_pop
6
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
8
7
from paste import httpexceptions
9
from paste import urlparser
11
from loggerhead.apps.branch import BranchWSGIApp
12
from loggerhead.apps import favicon_app, static_app
13
from loggerhead.config import LoggerheadConfig
14
from loggerhead.controllers.directory_ui import DirectoryUI
17
class BranchesFromTransportServer(object):
19
def __init__(self, transport, root, name=None):
20
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):
23
self._config = root._config
25
def app_for_branch(self, branch):
27
name = branch._get_nick(local=True)
32
branch_app = BranchWSGIApp(
34
{'cachepath': self._config.SQL_DIR},
35
self.root.graph_cache, is_root=is_root,
36
use_cdn=self._config.get_option('use_cdn'))
39
def app_for_non_branch(self, environ):
40
segment = path_info_pop(environ)
42
raise httpexceptions.HTTPMovedPermanently(
43
environ['SCRIPT_NAME'] + '/')
49
return DirectoryUI(environ['loggerhead.static.url'],
53
new_transport = self.transport.clone(segment)
55
new_name = urlutils.join(self.name, segment)
57
new_name = '/' + segment
58
return BranchesFromTransportServer(new_transport, self.root, new_name)
21
def directory_listing(self, path, environ, start_response):
22
request = WSGIRequest(environ)
23
response = WSGIResponse()
24
listing = [d for d in os.listdir(path) if not d.startswith('.')]
25
response.headers['Content-Type'] = 'text/html'
26
print >> response, '<html><body>'
27
for d in sorted(listing):
28
if os.path.isdir(os.path.join(path, d)):
30
print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
31
print >> response, '</body></html>'
32
return response(environ, start_response)
34
def app_for_branch(self, b, path):
37
_history = History.from_branch(b)
38
_history.use_file_cache(FileChangeCache(_history, sql_dir))
40
name = os.path.basename(os.path.abspath(path))
43
h = BranchWSGIApp(_history, name).app
44
self.root.cache[path] = h
60
49
def __call__(self, environ, start_response):
50
path = os.path.join(self.root.folder, self.folder)
51
if not os.path.isdir(path):
52
raise httpexceptions.HTTPNotFound()
53
if path in self.root.cache:
54
return self.root.cache[path](environ, start_response)
62
b = branch.Branch.open_from_transport(self.transport)
56
b = branch.Branch.open(path)
63
57
except errors.NotBranchError:
64
if not self.transport.listable() or not self.transport.has('.'):
65
raise httpexceptions.HTTPNotFound()
66
return self.app_for_non_branch(environ)(environ, start_response)
58
segment = path_info_pop(environ)
60
raise httpexceptions.HTTPMovedPermanently(environ['SCRIPT_NAME'] + '/')
62
return self.directory_listing(path, environ, start_response)
64
relpath = os.path.join(self.folder, segment)
65
return BranchesFromFileSystemServer(relpath, self.root)(
66
environ, start_response)
68
return self.app_for_branch(b)(environ, start_response)
71
class BranchesFromTransportRoot(object):
73
def __init__(self, transport, config):
74
self.graph_cache = lru_cache.LRUCache(10)
75
self.transport = transport
68
return self.app_for_branch(b, path)(environ, start_response)
71
class BranchesFromFileSystemRoot(object):
72
def __init__(self, folder):
78
75
def __call__(self, environ, start_response):
79
76
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
80
77
if environ['PATH_INFO'].startswith('/static/'):
81
78
segment = path_info_pop(environ)
82
79
assert segment == 'static'
83
80
return static_app(environ, start_response)
84
elif environ['PATH_INFO'] == '/favicon.ico':
85
return favicon_app(environ, start_response)
86
elif environ['PATH_INFO'].endswith("/.bzr/smart"):
87
# Only do readonly for now
88
transport = get_transport("readonly+" + self.transport.base)
89
wsgi_app = wsgi.SmartWSGIApp(self.transport)
90
wsgi_app = wsgi.RelpathSetter(wsgi_app, '', 'PATH_INFO')
91
return wsgi_app(environ, start_response)
92
elif '/.bzr/' in environ['PATH_INFO']:
93
# TODO: Use something here that uses the transport API
94
# rather than relying on the local filesystem API.
96
path = urlutils.local_path_from_url(self.transport.base)
97
except errors.InvalidURL:
98
raise httpexceptions.HTTPNotFound()
100
app = urlparser.make_static(None, path)
101
return app(environ, start_response)
103
return BranchesFromTransportServer(
104
self.transport, self)(environ, start_response)
107
class UserBranchesFromTransportRoot(object):
109
def __init__(self, transport, config):
110
self.graph_cache = lru_cache.LRUCache(10)
111
self.transport = transport
112
self._config = config
113
self.trunk_dir = config.get_option('trunk_dir')
115
def __call__(self, environ, start_response):
116
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
117
path_info = environ['PATH_INFO']
118
if path_info.startswith('/static/'):
119
segment = path_info_pop(environ)
120
assert segment == 'static'
121
return static_app(environ, start_response)
122
elif path_info == '/favicon.ico':
123
return favicon_app(environ, start_response)
125
# segments starting with ~ are user branches
126
if path_info.startswith('/~'):
127
segment = path_info_pop(environ)
128
new_transport = self.transport.clone(segment[1:])
129
return BranchesFromTransportServer(
130
new_transport, self, segment)(environ, start_response)
132
new_transport = self.transport.clone(self.trunk_dir)
133
return BranchesFromTransportServer(
134
new_transport, self)(environ, start_response)
82
return BranchesFromFileSystemServer(
83
'', self)(environ, start_response)
85
app = BranchesFromFileSystemRoot('.')
88
app = make_middleware(app)
89
app = make_filter(app, None)
92
httpserver.serve(app, host='127.0.0.1', port='9876')