~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/transport.py

  • Committer: Matt Nordhoff
  • Date: 2009-05-29 13:26:20 UTC
  • mfrom: (356.1.1 trunk)
  • Revision ID: mnordhoff@mattnordhoff.com-20090529132620-jlb2il586qa46y7o
Fix displaying symlinks to nonexistent targets in the directory UI. (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 cgi
4
 
import os
5
 
import tempfile
6
 
 
7
 
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
8
4
 
9
5
from paste.request import path_info_pop
10
 
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
11
6
from paste import httpexceptions
 
7
from paste import urlparser
12
8
 
13
9
from loggerhead.apps.branch import BranchWSGIApp
14
10
from loggerhead.apps import favicon_app, static_app
15
 
 
16
 
sql_dir = tempfile.mkdtemp(prefix='loggerhead-cache-')
17
 
 
18
 
 
19
 
class DirectoryListing(object):
20
 
 
21
 
    def __init__(self, path):
22
 
        self.path = path
23
 
 
24
 
    def __call__(self, environ, start_response):
25
 
        request = WSGIRequest(environ)
26
 
        response = WSGIResponse()
27
 
        listing = [d for d in os.listdir(self.path) if not d.startswith('.')]
28
 
        response.headers['Content-Type'] = 'text/html'
29
 
        print >> response, '<html><body>'
30
 
        for d in sorted(listing):
31
 
            if os.path.isdir(os.path.join(self.path, d)):
32
 
                d = cgi.escape(d)
33
 
                print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
34
 
        print >> response, '</body></html>'
35
 
        return response(environ, start_response)
36
 
 
37
 
 
38
 
class BranchesFromFileSystemServer(object):
39
 
    def __init__(self, folder, root):
40
 
        self.folder = folder
 
11
from loggerhead.config import LoggerheadConfig
 
12
from loggerhead.controllers.directory_ui import DirectoryUI
 
13
 
 
14
 
 
15
class BranchesFromTransportServer(object):
 
16
 
 
17
    def __init__(self, transport, root, name=None):
 
18
        self.transport = transport
41
19
        self.root = root
 
20
        self.name = name
 
21
        self._config = root._config
42
22
 
43
23
    def app_for_branch(self, branch):
44
 
        if not self.folder:
45
 
            name = os.path.basename(os.path.abspath(self.root.folder))
 
24
        if not self.name:
 
25
            name = branch._get_nick(local=True)
 
26
            is_root = True
46
27
        else:
47
 
            name = self.folder
 
28
            name = self.name
 
29
            is_root = False
48
30
        branch_app = BranchWSGIApp(
49
 
            branch, name, {'cachepath': sql_dir}, self.root.graph_cache)
 
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'))
50
35
        return branch_app.app
51
36
 
52
37
    def app_for_non_branch(self, environ):
55
40
            raise httpexceptions.HTTPMovedPermanently(
56
41
                environ['SCRIPT_NAME'] + '/')
57
42
        elif segment == '':
58
 
            return DirectoryListing(os.path.join(self.root.folder, self.folder))
 
43
            if self.name:
 
44
                name = self.name
 
45
            else:
 
46
                name = '/'
 
47
            return DirectoryUI(environ['loggerhead.static.url'],
 
48
                               self.transport,
 
49
                               name)
59
50
        else:
60
 
            relpath = os.path.join(self.folder, segment)
61
 
            return BranchesFromFileSystemServer(relpath, self.root)
 
51
            new_transport = self.transport.clone(segment)
 
52
            if self.name:
 
53
                new_name = urlutils.join(self.name, segment)
 
54
            else:
 
55
                new_name = '/' + segment
 
56
            return BranchesFromTransportServer(new_transport, self.root, new_name)
62
57
 
63
58
    def __call__(self, environ, start_response):
64
 
        path = os.path.join(self.root.folder, self.folder)
65
 
        if not os.path.isdir(path):
66
 
            raise httpexceptions.HTTPNotFound()
67
59
        try:
68
 
            b = branch.Branch.open(path)
 
60
            b = branch.Branch.open_from_transport(self.transport)
69
61
        except errors.NotBranchError:
 
62
            if not self.transport.listable() or not self.transport.has('.'):
 
63
                raise httpexceptions.HTTPNotFound()
70
64
            return self.app_for_non_branch(environ)(environ, start_response)
71
65
        else:
72
66
            return self.app_for_branch(b)(environ, start_response)
73
67
 
74
68
 
75
 
class BranchesFromFileSystemRoot(object):
 
69
class BranchesFromTransportRoot(object):
76
70
 
77
 
    def __init__(self, folder):
78
 
        self.graph_cache = lru_cache.LRUCache()
79
 
        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
80
75
 
81
76
    def __call__(self, environ, start_response):
82
77
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
86
81
            return static_app(environ, start_response)
87
82
        elif environ['PATH_INFO'] == '/favicon.ico':
88
83
            return favicon_app(environ, start_response)
89
 
        else:
90
 
            return BranchesFromFileSystemServer(
91
 
                '', self)(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
        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')
 
106
 
 
107
    def __call__(self, environ, start_response):
 
108
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
 
109
        path_info = environ['PATH_INFO']
 
110
        if path_info.startswith('/static/'):
 
111
            segment = path_info_pop(environ)
 
112
            assert segment == 'static'
 
113
            return static_app(environ, start_response)
 
114
        elif path_info == '/favicon.ico':
 
115
            return favicon_app(environ, start_response)
 
116
        else:
 
117
            # segments starting with ~ are user branches
 
118
            if path_info.startswith('/~'):
 
119
                segment = path_info_pop(environ)
 
120
                new_transport = self.transport.clone(segment[1:])
 
121
                return BranchesFromTransportServer(
 
122
                    new_transport, self, segment)(environ, start_response)
 
123
            else:
 
124
                new_transport = self.transport.clone(self.trunk_dir)
 
125
                return BranchesFromTransportServer(
 
126
                    new_transport, self)(environ, start_response)