~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/transport.py

Don't use Repository.get_revision_inventory(), removed in bzr 2.2. (#528194)

(The original commit message is wrong: The replacement is named get_inventory, not get_revision.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Serve branches at urls that mimic the file system layout."""
2
 
 
3
 
import os
4
 
 
5
 
from bzrlib import branch, errors, lru_cache
 
1
# Copyright (C) 2008, 2009 Canonical Ltd.
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
#
 
17
"""Serve branches at urls that mimic a transport's file system layout."""
 
18
 
 
19
import threading
 
20
 
 
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
6
26
 
7
27
from paste.request import path_info_pop
8
28
from paste import httpexceptions
9
29
from paste import urlparser
10
30
 
11
31
from loggerhead.apps.branch import BranchWSGIApp
12
 
from loggerhead.apps import favicon_app, static_app
13
 
from loggerhead.config import LoggerheadConfig
 
32
from loggerhead.apps import favicon_app, robots_app, static_app
14
33
from loggerhead.controllers.directory_ui import DirectoryUI
15
34
 
16
 
 
17
 
class BranchesFromFileSystemServer(object):
18
 
 
19
 
    def __init__(self, path, root, name=None):
20
 
        self.path = path
 
35
_bools = {
 
36
    'yes': True, 'no': False,
 
37
    'on': True, 'off': False,
 
38
    '1': True, '0': False,
 
39
    'true': True, 'false': False,
 
40
    }
 
41
 
 
42
class BranchesFromTransportServer(object):
 
43
 
 
44
    def __init__(self, transport, root, name=None):
 
45
        self.transport = transport
21
46
        self.root = root
22
47
        self.name = name
23
 
        self._config = LoggerheadConfig()
 
48
        self._config = root._config
24
49
 
25
50
    def app_for_branch(self, branch):
26
51
        if not self.name:
27
 
            name = branch.get_config().get_nickname()
 
52
            name = branch._get_nick(local=True)
28
53
            is_root = True
29
54
        else:
30
55
            name = self.name
31
56
            is_root = False
32
57
        branch_app = BranchWSGIApp(
33
 
            branch, name,
 
58
            branch,
 
59
            name,
34
60
            {'cachepath': self._config.SQL_DIR},
35
 
            self.root.graph_cache, is_root=is_root)
 
61
            self.root.graph_cache,
 
62
            is_root=is_root,
 
63
            use_cdn=self._config.get_option('use_cdn'),
 
64
            )
36
65
        return branch_app.app
37
66
 
38
67
    def app_for_non_branch(self, environ):
45
74
                name = self.name
46
75
            else:
47
76
                name = '/'
48
 
            return DirectoryUI(environ['loggerhead.static.url'],
49
 
                               self.path,
50
 
                               name)
 
77
            return DirectoryUI(
 
78
                environ['loggerhead.static.url'], self.transport, name)
51
79
        else:
52
 
            new_path = os.path.join(self.path, segment)
 
80
            new_transport = self.transport.clone(segment)
53
81
            if self.name:
54
 
                new_name = os.path.join(self.name, segment)
 
82
                new_name = urlutils.join(self.name, segment)
55
83
            else:
56
84
                new_name = '/' + segment
57
 
            return BranchesFromFileSystemServer(new_path, self.root, new_name)
58
 
 
59
 
    def __call__(self, environ, start_response):
60
 
        if not os.path.isdir(self.path):
 
85
            return BranchesFromTransportServer(new_transport, self.root, new_name)
 
86
 
 
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')
 
92
        else:
 
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):]
 
99
            try:
 
100
                path = urlutils.local_path_from_url(base)
 
101
            except errors.InvalidURL:
 
102
                raise httpexceptions.HTTPNotFound()
 
103
            else:
 
104
                return urlparser.make_static(None, path)
 
105
 
 
106
    def check_serveable(self, config):
 
107
        value = config.get_user_option('http_serve')
 
108
        if value is None:
 
109
            return
 
110
        elif not _bools.get(value.lower(), True):
61
111
            raise httpexceptions.HTTPNotFound()
 
112
 
 
113
    def __call__(self, environ, start_response):
 
114
        path = environ['PATH_INFO']
62
115
        try:
63
 
            b = branch.Branch.open(self.path)
 
116
            b = branch.Branch.open_from_transport(self.transport)
64
117
        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()
65
123
            return self.app_for_non_branch(environ)(environ, start_response)
66
124
        else:
67
 
            return self.app_for_branch(b)(environ, start_response)
68
 
 
69
 
 
70
 
class BranchesFromFileSystemRoot(object):
71
 
 
72
 
    def __init__(self, folder):
73
 
        self.graph_cache = lru_cache.LRUCache()
74
 
        self.folder = folder
 
125
            self.check_serveable(b.get_config())
 
126
            if path.startswith('/.bzr'):
 
127
                return self.app_for_bazaar_data(path)(environ, start_response)
 
128
            else:
 
129
                return self.app_for_branch(b)(environ, start_response)
 
130
 
 
131
 
 
132
_transport_store = threading.local()
 
133
 
 
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
 
142
    return transport
 
143
 
 
144
 
 
145
class BranchesFromTransportRoot(object):
 
146
 
 
147
    def __init__(self, base, config):
 
148
        self.graph_cache = lru_cache.LRUCache(10)
 
149
        self.base = base
 
150
        self._config = config
75
151
 
76
152
    def __call__(self, environ, start_response):
77
153
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
 
154
        environ['loggerhead.path_info'] = environ['PATH_INFO']
78
155
        if environ['PATH_INFO'].startswith('/static/'):
79
156
            segment = path_info_pop(environ)
80
157
            assert segment == 'static'
81
158
            return static_app(environ, start_response)
82
159
        elif environ['PATH_INFO'] == '/favicon.ico':
83
160
            return favicon_app(environ, start_response)
84
 
        elif '/.bzr/' in environ['PATH_INFO']:
85
 
            app = urlparser.make_static(None, self.folder)
86
 
            return app(environ, start_response)
 
161
        elif environ['PATH_INFO'] == '/robots.txt':
 
162
            return robots_app(environ, start_response)
87
163
        else:
88
 
            return BranchesFromFileSystemServer(
89
 
                self.folder, self)(environ, start_response)
90
 
 
91
 
 
92
 
class UserBranchesFromFileSystemRoot(object):
93
 
 
94
 
    def __init__(self, folder, trunk_dir):
95
 
        self.graph_cache = lru_cache.LRUCache()
96
 
        self.folder = folder
97
 
        self.trunk_dir = trunk_dir
 
164
            transport = get_transport_for_thread(self.base)
 
165
            return BranchesFromTransportServer(
 
166
                transport, self)(environ, start_response)
 
167
 
 
168
 
 
169
class UserBranchesFromTransportRoot(object):
 
170
 
 
171
    def __init__(self, base, config):
 
172
        self.graph_cache = lru_cache.LRUCache(10)
 
173
        self.base = base
 
174
        self._config = config
 
175
        self.trunk_dir = config.get_option('trunk_dir')
98
176
 
99
177
    def __call__(self, environ, start_response):
100
178
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
101
 
        path_info= environ['PATH_INFO']
 
179
        environ['loggerhead.path_info'] = environ['PATH_INFO']
 
180
        path_info = environ['PATH_INFO']
102
181
        if path_info.startswith('/static/'):
103
182
            segment = path_info_pop(environ)
104
183
            assert segment == 'static'
105
184
            return static_app(environ, start_response)
106
185
        elif path_info == '/favicon.ico':
107
186
            return favicon_app(environ, start_response)
 
187
        elif environ['PATH_INFO'] == '/robots.txt':
 
188
            return robots_app(environ, start_response)
108
189
        else:
 
190
            transport = get_transport_for_thread(self.base)
109
191
            # segments starting with ~ are user branches
110
192
            if path_info.startswith('/~'):
111
193
                segment = path_info_pop(environ)
112
 
                new_path = os.path.join(self.folder, segment[1:])
113
 
                return BranchesFromFileSystemServer(
114
 
                    new_path, self, segment)(environ, start_response)
 
194
                return BranchesFromTransportServer(
 
195
                    transport.clone(segment[1:]), self, segment)(
 
196
                    environ, start_response)
115
197
            else:
116
 
                new_path = os.path.join(self.folder, self.trunk_dir)
117
 
                return BranchesFromFileSystemServer(
118
 
                    new_path, self)(environ, start_response)
 
198
                return BranchesFromTransportServer(
 
199
                    transport.clone(self.trunk_dir), self)(
 
200
                    environ, start_response)