~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/filesystem.py

  • Committer: Martin Pool
  • Date: 2009-03-10 01:01:04 UTC
  • mto: This revision was merged to the branch mainline in revision 298.
  • Revision ID: mbp@sourcefrog.net-20090310010104-o75ncguznrridwaz
optparse may have already parsed port as an int

Show diffs side-by-side

added added

removed removed

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