1
"""Serve branches at urls that mimic the file system layout."""
6
from bzrlib import branch, errors, lru_cache
8
from paste.request import path_info_pop
9
from paste import httpexceptions
11
from loggerhead.apps.branch import BranchWSGIApp
12
from loggerhead.apps import favicon_app, static_app
13
from loggerhead.controllers.directory_ui import DirectoryUI
15
sql_dir = tempfile.mkdtemp(prefix='loggerhead-cache-')
18
class BranchesFromFileSystemServer(object):
20
def __init__(self, path, root, name=None):
25
def app_for_branch(self, branch):
27
name = branch.get_config().get_nickname()
32
branch_app = BranchWSGIApp(
33
branch, name, {'cachepath': sql_dir}, self.root.graph_cache,
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_path = os.path.join(self.path, segment)
53
new_name = os.path.join(self.name, segment)
55
new_name = '/' + segment
56
return BranchesFromFileSystemServer(new_path, self.root, new_name)
58
def __call__(self, environ, start_response):
59
if not os.path.isdir(self.path):
60
raise httpexceptions.HTTPNotFound()
62
b = branch.Branch.open(self.path)
63
except errors.NotBranchError:
64
return self.app_for_non_branch(environ)(environ, start_response)
66
return self.app_for_branch(b)(environ, start_response)
69
class BranchesFromFileSystemRoot(object):
71
def __init__(self, folder):
72
self.graph_cache = lru_cache.LRUCache()
75
def __call__(self, environ, start_response):
76
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
77
if environ['PATH_INFO'].startswith('/static/'):
78
segment = path_info_pop(environ)
79
assert segment == 'static'
80
return static_app(environ, start_response)
81
elif environ['PATH_INFO'] == '/favicon.ico':
82
return favicon_app(environ, start_response)
84
return BranchesFromFileSystemServer(
85
self.folder, self)(environ, start_response)
88
class UserBranchesFromFileSystemRoot(object):
90
def __init__(self, folder, trunk_dir):
91
self.graph_cache = lru_cache.LRUCache()
93
self.trunk_dir = trunk_dir
95
def __call__(self, environ, start_response):
96
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
97
path_info= environ['PATH_INFO']
98
if path_info.startswith('/static/'):
99
segment = path_info_pop(environ)
100
assert segment == 'static'
101
return static_app(environ, start_response)
102
elif path_info == '/favicon.ico':
103
return favicon_app(environ, start_response)
105
# segments starting with ~ are user branches
106
if path_info.startswith('/~'):
107
segment = path_info_pop(environ)
108
new_path = os.path.join(self.folder, segment[1:])
109
return BranchesFromFileSystemServer(
110
new_path, self, segment)(environ, start_response)
112
new_path = os.path.join(self.folder, self.trunk_dir)
113
return BranchesFromFileSystemServer(
114
new_path, self)(environ, start_response)