~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/filesystem.py

  • Committer: Michael Hudson
  • Date: 2008-06-19 01:18:01 UTC
  • mto: This revision was merged to the branch mainline in revision 164.
  • Revision ID: michael.hudson@canonical.com-20080619011801-fwnrw5ntwnz7q3cu
testingĀ isĀ grate

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Serve branches at urls that mimic the file system layout."""
2
 
 
 
1
import cgi
3
2
import os
4
3
import tempfile
5
4
 
6
 
from bzrlib import branch, errors, lru_cache
 
5
from bzrlib import branch, errors
7
6
 
8
7
from paste.request import path_info_pop
 
8
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
9
9
from paste import httpexceptions
10
10
 
11
11
from loggerhead.apps.branch import BranchWSGIApp
12
12
from loggerhead.apps import favicon_app, static_app
13
 
from loggerhead.controllers.directory_ui import DirectoryUI
14
 
 
15
 
sql_dir = tempfile.mkdtemp(prefix='loggerhead-cache-')
16
 
 
 
13
 
 
14
 
 
15
sql_dir = tempfile.mkdtemp()
17
16
 
18
17
class BranchesFromFileSystemServer(object):
19
 
 
20
 
    def __init__(self, path, root, name=None):
21
 
        self.path = path
 
18
    def __init__(self, folder, root):
 
19
        self.folder = folder
22
20
        self.root = root
23
 
        self.name = name
24
 
 
25
 
    def app_for_branch(self, branch):
26
 
        if not self.name:
27
 
            name = branch.nick
28
 
            is_root = True
29
 
        else:
30
 
            name = self.name
31
 
            is_root = False
32
 
        branch_app = BranchWSGIApp(
33
 
            branch, name, {'cachepath': sql_dir}, self.root.graph_cache,
34
 
            is_root=is_root)
35
 
        return branch_app.app
36
 
 
37
 
    def app_for_non_branch(self, environ):
38
 
        segment = path_info_pop(environ)
39
 
        if segment is None:
40
 
            raise httpexceptions.HTTPMovedPermanently(
41
 
                environ['SCRIPT_NAME'] + '/')
42
 
        elif segment == '':
43
 
            if self.name:
44
 
                name = self.name
45
 
            else:
46
 
                name = '/'
47
 
            return DirectoryUI(environ['loggerhead.static.url'],
48
 
                               self.path,
49
 
                               name)
50
 
        else:
51
 
            new_path = os.path.join(self.path, segment)
52
 
            if self.name:
53
 
                new_name = os.path.join(self.name, segment)
54
 
            else:
55
 
                new_name = '/' + segment
56
 
            return BranchesFromFileSystemServer(new_path, self.root, new_name)
 
21
 
 
22
    def directory_listing(self, path, environ, start_response):
 
23
        request = WSGIRequest(environ)
 
24
        response = WSGIResponse()
 
25
        listing = [d for d in os.listdir(path) if not d.startswith('.')]
 
26
        response.headers['Content-Type'] = 'text/html'
 
27
        print >> response, '<html><body>'
 
28
        for d in sorted(listing):
 
29
            if os.path.isdir(os.path.join(path, d)):
 
30
                d = cgi.escape(d)
 
31
                print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
 
32
        print >> response, '</body></html>'
 
33
        return response(environ, start_response)
 
34
 
 
35
    def app_for_branch(self, b, path):
 
36
        if not self.folder:
 
37
            name = os.path.basename(os.path.abspath(path))
 
38
        else:
 
39
            name = self.folder
 
40
        h = BranchWSGIApp(path, name, {'cachepath': sql_dir})
 
41
        self.root.cache[path] = h
 
42
        return h.app
57
43
 
58
44
    def __call__(self, environ, start_response):
59
 
        if not os.path.isdir(self.path):
 
45
        path = os.path.join(self.root.folder, self.folder)
 
46
        if not os.path.isdir(path):
60
47
            raise httpexceptions.HTTPNotFound()
 
48
        cached = self.root.cache.get(path)
 
49
        if cached is not None:
 
50
            return cached.app(environ, start_response)
61
51
        try:
62
 
            b = branch.Branch.open(self.path)
 
52
            b = branch.Branch.open(path)
63
53
        except errors.NotBranchError:
64
 
            return self.app_for_non_branch(environ)(environ, start_response)
 
54
            segment = path_info_pop(environ)
 
55
            if segment is None:
 
56
                raise httpexceptions.HTTPMovedPermanently(
 
57
                    environ['SCRIPT_NAME'] + '/')
 
58
            elif segment == '':
 
59
                return self.directory_listing(path, environ, start_response)
 
60
            else:
 
61
                relpath = os.path.join(self.folder, segment)
 
62
                return BranchesFromFileSystemServer(relpath, self.root)(
 
63
                    environ, start_response)
65
64
        else:
66
 
            return self.app_for_branch(b)(environ, start_response)
 
65
            return self.app_for_branch(b, path)(environ, start_response)
67
66
 
68
67
 
69
68
class BranchesFromFileSystemRoot(object):
70
 
 
71
69
    def __init__(self, folder):
72
 
        self.graph_cache = lru_cache.LRUCache()
 
70
        self.cache = {}
73
71
        self.folder = folder
74
 
 
75
72
    def __call__(self, environ, start_response):
76
73
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
77
74
        if environ['PATH_INFO'].startswith('/static/'):
82
79
            return favicon_app(environ, start_response)
83
80
        else:
84
81
            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
94
 
 
95
 
    def __call__(self, environ, start_response):
96
 
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
97
 
        path_info= environ['PATH_INFO']
98
 
        if path_info.startswith('/static/'):
99
 
            segment = path_info_pop(environ)
100
 
            assert segment == 'static'
101
 
            return static_app(environ, start_response)
102
 
        elif path_info == '/favicon.ico':
103
 
            return favicon_app(environ, start_response)
104
 
        else:
105
 
            # segments starting with ~ are user branches
106
 
            if path_info.startswith('/~'):
107
 
                segment = path_info_pop(environ)
108
 
                new_path = os.path.join(self.folder, segment[1:])
109
 
                return BranchesFromFileSystemServer(
110
 
                    new_path, self, segment)(environ, start_response)
111
 
            else:
112
 
                new_path = os.path.join(self.folder, self.trunk_dir)
113
 
                return BranchesFromFileSystemServer(
114
 
                    new_path, self)(environ, start_response)
 
82
                '', self)(environ, start_response)