~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/filesystem.py

  • Committer: Matt Nordhoff
  • Date: 2009-05-02 14:01:05 UTC
  • Revision ID: mnordhoff@mattnordhoff.com-20090502140105-m07dxhtzfgsgu2ia
Make sure to close mkstemp's file descriptor (bug #370845)

Show diffs side-by-side

added added

removed removed

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