~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/transport.py

  • Committer: Martin Albisetti
  • Date: 2009-06-03 13:05:20 UTC
  • mfrom: (359.1.2 lh)
  • Revision ID: martin.albisetti@canonical.com-20090603130520-60d328gn7xe7kh3u
Avoid diluting sys.path if loggerhead is installed systemwide, install loggerhead as a plugin (Jelmer Vernooij)

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
 
import tempfile
5
 
 
6
 
from bzrlib import branch, errors, lru_cache
 
1
"""Serve branches at urls that mimic a transport's file system layout."""
 
2
 
 
3
from bzrlib import branch, errors, lru_cache, urlutils
7
4
 
8
5
from paste.request import path_info_pop
9
6
from paste import httpexceptions
11
8
 
12
9
from loggerhead.apps.branch import BranchWSGIApp
13
10
from loggerhead.apps import favicon_app, static_app
 
11
from loggerhead.config import LoggerheadConfig
14
12
from loggerhead.controllers.directory_ui import DirectoryUI
15
13
 
16
 
sql_dir = tempfile.mkdtemp(prefix='loggerhead-cache-')
17
 
 
18
 
 
19
 
class BranchesFromFileSystemServer(object):
20
 
 
21
 
    def __init__(self, path, root, name=None):
22
 
        self.path = path
 
14
 
 
15
class BranchesFromTransportServer(object):
 
16
 
 
17
    def __init__(self, transport, root, name=None):
 
18
        self.transport = transport
23
19
        self.root = root
24
20
        self.name = name
 
21
        self._config = root._config
25
22
 
26
23
    def app_for_branch(self, branch):
27
24
        if not self.name:
28
 
            name = branch.get_config().get_nickname()
 
25
            name = branch._get_nick(local=True)
29
26
            is_root = True
30
27
        else:
31
28
            name = self.name
32
29
            is_root = False
33
30
        branch_app = BranchWSGIApp(
34
 
            branch, name, {'cachepath': sql_dir}, self.root.graph_cache,
35
 
            is_root=is_root)
 
31
            branch, name,
 
32
            {'cachepath': self._config.SQL_DIR},
 
33
            self.root.graph_cache, is_root=is_root,
 
34
            use_cdn=self._config.get_option('use_cdn'))
36
35
        return branch_app.app
37
36
 
38
37
    def app_for_non_branch(self, environ):
46
45
            else:
47
46
                name = '/'
48
47
            return DirectoryUI(environ['loggerhead.static.url'],
49
 
                               self.path,
 
48
                               self.transport,
50
49
                               name)
51
50
        else:
52
 
            new_path = os.path.join(self.path, segment)
 
51
            new_transport = self.transport.clone(segment)
53
52
            if self.name:
54
 
                new_name = os.path.join(self.name, segment)
 
53
                new_name = urlutils.join(self.name, segment)
55
54
            else:
56
55
                new_name = '/' + segment
57
 
            return BranchesFromFileSystemServer(new_path, self.root, new_name)
 
56
            return BranchesFromTransportServer(new_transport, self.root, new_name)
58
57
 
59
58
    def __call__(self, environ, start_response):
60
 
        if not os.path.isdir(self.path):
61
 
            raise httpexceptions.HTTPNotFound()
62
59
        try:
63
 
            b = branch.Branch.open(self.path)
 
60
            b = branch.Branch.open_from_transport(self.transport)
64
61
        except errors.NotBranchError:
 
62
            if not self.transport.listable() or not self.transport.has('.'):
 
63
                raise httpexceptions.HTTPNotFound()
65
64
            return self.app_for_non_branch(environ)(environ, start_response)
66
65
        else:
67
66
            return self.app_for_branch(b)(environ, start_response)
68
67
 
69
68
 
70
 
class BranchesFromFileSystemRoot(object):
 
69
class BranchesFromTransportRoot(object):
71
70
 
72
 
    def __init__(self, folder):
73
 
        self.graph_cache = lru_cache.LRUCache()
74
 
        self.folder = folder
 
71
    def __init__(self, transport, config):
 
72
        self.graph_cache = lru_cache.LRUCache(10)
 
73
        self.transport = transport
 
74
        self._config = config
75
75
 
76
76
    def __call__(self, environ, start_response):
77
77
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
82
82
        elif environ['PATH_INFO'] == '/favicon.ico':
83
83
            return favicon_app(environ, start_response)
84
84
        elif '/.bzr/' in environ['PATH_INFO']:
85
 
            app = urlparser.make_static(None, self.folder)
86
 
            return app(environ, start_response)
 
85
            # TODO: Use something here that uses the transport API 
 
86
            # rather than relying on the local filesystem API.
 
87
            try:
 
88
                path = urlutils.local_path_from_url(self.transport.base)
 
89
            except errors.InvalidURL:
 
90
                raise httpexceptions.HTTPNotFound()
 
91
            else:
 
92
                app = urlparser.make_static(None, path)
 
93
                return app(environ, start_response)
87
94
        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
 
95
            return BranchesFromTransportServer(
 
96
                self.transport, self)(environ, start_response)
 
97
 
 
98
 
 
99
class UserBranchesFromTransportRoot(object):
 
100
 
 
101
    def __init__(self, transport, config):
 
102
        self.graph_cache = lru_cache.LRUCache(10)
 
103
        self.transport = transport
 
104
        self._config = config
 
105
        self.trunk_dir = config.get_option('trunk_dir')
98
106
 
99
107
    def __call__(self, environ, start_response):
100
108
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
101
 
        path_info= environ['PATH_INFO']
 
109
        path_info = environ['PATH_INFO']
102
110
        if path_info.startswith('/static/'):
103
111
            segment = path_info_pop(environ)
104
112
            assert segment == 'static'
109
117
            # segments starting with ~ are user branches
110
118
            if path_info.startswith('/~'):
111
119
                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)
 
120
                new_transport = self.transport.clone(segment[1:])
 
121
                return BranchesFromTransportServer(
 
122
                    new_transport, self, segment)(environ, start_response)
115
123
            else:
116
 
                new_path = os.path.join(self.folder, self.trunk_dir)
117
 
                return BranchesFromFileSystemServer(
118
 
                    new_path, self)(environ, start_response)
 
124
                new_transport = self.transport.clone(self.trunk_dir)
 
125
                return BranchesFromTransportServer(
 
126
                    new_transport, self)(environ, start_response)