~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to wsgitest.py

  • Committer: Michael Hudson
  • Date: 2008-06-16 11:25:25 UTC
  • mfrom: (160 trunk)
  • mto: This revision was merged to the branch mainline in revision 164.
  • Revision ID: michael.hudson@canonical.com-20080616112525-t1vsjh4s3knwpou3
merge trunk, rather brutally resolving conflicts

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