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):
19
def __init__(self, path, root, name=None):
24
def app_for_branch(self, branch):
31
branch_app = BranchWSGIApp(
32
branch, name, {'cachepath': sql_dir}, self.root.graph_cache,
36
def app_for_non_branch(self, environ):
37
segment = path_info_pop(environ)
39
raise httpexceptions.HTTPMovedPermanently(
40
environ['SCRIPT_NAME'] + '/')
46
return DirectoryUI(environ['loggerhead.static.url'], self.path, name)
48
new_path = os.path.join(self.path, segment)
50
new_name = os.path.join(self.name, segment)
52
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):
57
raise httpexceptions.HTTPNotFound()
59
b = branch.Branch.open(self.path)
60
except errors.NotBranchError:
61
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()
72
def __call__(self, environ, start_response):
73
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
74
if environ['PATH_INFO'].startswith('/static/'):
75
segment = path_info_pop(environ)
76
assert segment == 'static'
77
return static_app(environ, start_response)
78
elif environ['PATH_INFO'] == '/favicon.ico':
79
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
92
def __call__(self, environ, start_response):
93
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
94
path_info= environ['PATH_INFO']
95
if path_info.startswith('/static/'):
96
segment = path_info_pop(environ)
97
assert segment == 'static'
98
return static_app(environ, start_response)
99
elif path_info == '/favicon.ico':
100
return favicon_app(environ, start_response)
102
# segments starting with ~ are user branches
103
if path_info.startswith('/~'):
104
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)
109
new_path = os.path.join(self.folder, self.trunk_dir)
110
return BranchesFromFileSystemServer(
111
new_path, self)(environ, start_response)