~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to wsgitest.py

  • Committer: Michael Hudson
  • Date: 2008-06-16 08:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 164.
  • Revision ID: michael.hudson@canonical.com-20080616084603-m0bkdbhz62znm0e9
try a bit harder with urls

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
7
 
 
8
 
from paste.request import path_info_pop
9
 
from paste import httpexceptions
10
 
 
11
 
from loggerhead.apps.branch import BranchWSGIApp
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
 
 
17
 
 
18
 
class BranchesFromFileSystemServer(object):
19
 
    def __init__(self, path, root, name=None):
20
 
        self.path = path
21
 
        self.root = root
22
 
        self.name = name
23
 
 
24
 
    def app_for_branch(self, branch):
25
 
        if not self.name:
26
 
            name = branch.nick
27
 
        else:
28
 
            name = self.name
29
 
        branch_app = BranchWSGIApp(
30
 
            branch, name, {'cachepath': sql_dir}, self.root.graph_cache)
31
 
        return branch_app.app
32
 
 
33
 
    def app_for_non_branch(self, environ):
34
 
        segment = path_info_pop(environ)
35
 
        if segment is None:
36
 
            raise httpexceptions.HTTPMovedPermanently(
37
 
                environ['SCRIPT_NAME'] + '/')
38
 
        elif segment == '':
39
 
            if self.name:
40
 
                name = self.name
41
 
            else:
42
 
                name = '/'
43
 
            return DirectoryUI(environ['loggerhead.static.url'], self.path, name)
44
 
        else:
45
 
            new_path = os.path.join(self.path, segment)
46
 
            if self.name:
47
 
                new_name = os.path.join(self.name, segment)
48
 
            else:
49
 
                new_name = '/' + segment
50
 
            return BranchesFromFileSystemServer(new_path, self.root, new_name)
51
 
 
52
 
    def __call__(self, environ, start_response):
53
 
        if not os.path.isdir(self.path):
54
 
            raise httpexceptions.HTTPNotFound()
55
 
        try:
56
 
            b = branch.Branch.open(self.path)
57
 
        except errors.NotBranchError:
58
 
            return self.app_for_non_branch(environ)(environ, start_response)
59
 
        else:
60
 
            return self.app_for_branch(b)(environ, start_response)
61
 
 
62
 
 
63
 
class BranchesFromFileSystemRoot(object):
64
 
 
65
 
    def __init__(self, folder):
66
 
        self.graph_cache = lru_cache.LRUCache()
67
 
        self.folder = folder
68
 
 
69
 
    def __call__(self, environ, start_response):
70
 
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
71
 
        if environ['PATH_INFO'].startswith('/static/'):
72
 
            segment = path_info_pop(environ)
73
 
            assert segment == 'static'
74
 
            return static_app(environ, start_response)
75
 
        elif environ['PATH_INFO'] == '/favicon.ico':
76
 
            return favicon_app(environ, start_response)
77
 
        else:
78
 
            return BranchesFromFileSystemServer(
79
 
                self.folder, self)(environ, start_response)
80
 
 
81
 
 
82
 
class UserBranchesFromFileSystemRoot(object):
83
 
 
84
 
    def __init__(self, folder, trunk_dir):
85
 
        self.graph_cache = lru_cache.LRUCache()
86
 
        self.folder = folder
87
 
        self.trunk_dir = trunk_dir
88
 
 
89
 
    def __call__(self, environ, start_response):
90
 
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
91
 
        path_info= environ['PATH_INFO']
92
 
        if path_info.startswith('/static/'):
93
 
            segment = path_info_pop(environ)
94
 
            assert segment == 'static'
95
 
            return static_app(environ, start_response)
96
 
        elif path_info == '/favicon.ico':
97
 
            return favicon_app(environ, start_response)
98
 
        else:
99
 
            # segments starting with ~ are user branches
100
 
            if path_info.startswith('/~'):
101
 
                segment = path_info_pop(environ)
102
 
                new_path = os.path.join(self.folder, segment[1:])
103
 
                return BranchesFromFileSystemServer(
104
 
                    new_path, self, segment)(environ, start_response)
105
 
            else:
106
 
                new_path = os.path.join(self.folder, self.trunk_dir)
107
 
                return BranchesFromFileSystemServer(
108
 
                    new_path, self)(environ, start_response)
 
1
from loggerhead.history import History
 
2
from loggerhead.wsgiapp import BranchWSGIApp
 
3
 
 
4
h = History.from_folder('.')
 
5
 
 
6
app = BranchWSGIApp(h)
 
7
 
 
8
 
 
9
from paste import httpserver
 
10
from paste.evalexception import EvalException
 
11
from paste.httpexceptions import make_middleware
 
12
from paste.translogger import make_filter
 
13
 
 
14
app = app.app
 
15
for w in EvalException, make_middleware:
 
16
    app = w(app)
 
17
 
 
18
app = make_filter(app, None)
 
19
 
 
20
httpserver.serve(app, host='127.0.0.1', port='9876')
 
21