~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to wsgitest.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2008-03-06 04:54:39 UTC
  • mfrom: (148.1.3 changelog-order-aaaargh)
  • Revision ID: launchpad@pqm.canonical.com-20080306045439-qe3j5ryuhb7bmqr8
[r=jamesh] fix the order of the changelog view when the revision cache is not used

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
5
 
from paste.request import path_info_pop
6
 
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
7
 
from paste import httpexceptions
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()
15
 
 
16
 
class BranchesFromFileSystemServer(object):
17
 
    def __init__(self, folder, root):
18
 
        self.folder = folder
19
 
        self.root = root
20
 
 
21
 
    def directory_listing(self, path, environ, start_response):
22
 
        request = WSGIRequest(environ)
23
 
        response = WSGIResponse()
24
 
        listing = [d for d in os.listdir(path) if not d.startswith('.')]
25
 
        response.headers['Content-Type'] = 'text/html'
26
 
        print >> response, '<html><body>'
27
 
        for d in sorted(listing):
28
 
            if os.path.isdir(os.path.join(path, d)):
29
 
                d = cgi.escape(d)
30
 
                print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
31
 
        print >> response, '</body></html>'
32
 
        return response(environ, start_response)
33
 
 
34
 
    def app_for_branch(self, b, path):
35
 
        b.lock_read()
36
 
        try:
37
 
            _history = History.from_branch(b)
38
 
            _history.use_file_cache(FileChangeCache(_history, sql_dir))
39
 
            if not self.folder:
40
 
                name = os.path.basename(os.path.abspath(path))
41
 
            else:
42
 
                name = self.folder
43
 
            h = BranchWSGIApp(_history, name).app
44
 
            self.root.cache[path] = h
45
 
            return h
46
 
        finally:
47
 
            b.unlock()
48
 
 
49
 
    def __call__(self, environ, start_response):
50
 
        path = os.path.join(self.root.folder, self.folder)
51
 
        if not os.path.isdir(path):
52
 
            raise httpexceptions.HTTPNotFound()
53
 
        if path in self.root.cache:
54
 
            return self.root.cache[path](environ, start_response)
55
 
        try:
56
 
            b = branch.Branch.open(path)
57
 
        except errors.NotBranchError:
58
 
            segment = path_info_pop(environ)
59
 
            if segment is None:
60
 
                raise httpexceptions.HTTPMovedPermanently(environ['SCRIPT_NAME'] + '/')
61
 
            elif segment == '':
62
 
                return self.directory_listing(path, environ, start_response)
63
 
            else:
64
 
                relpath = os.path.join(self.folder, segment)
65
 
                return BranchesFromFileSystemServer(relpath, self.root)(
66
 
                    environ, start_response)
67
 
        else:
68
 
            return self.app_for_branch(b, path)(environ, start_response)
69
 
 
70
 
 
71
 
class BranchesFromFileSystemRoot(object):
72
 
    def __init__(self, folder):
73
 
        self.cache = {}
74
 
        self.folder = folder
75
 
    def __call__(self, environ, start_response):
76
 
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
77
 
        if environ['PATH_INFO'].startswith('/static/'):
78
 
            segment = path_info_pop(environ)
79
 
            assert segment == 'static'
80
 
            return static_app(environ, start_response)
81
 
        else:
82
 
            return BranchesFromFileSystemServer(
83
 
                '', self)(environ, start_response)
84
 
 
85
 
app = BranchesFromFileSystemRoot('.')
86
 
 
87
 
app = app
88
 
app = make_middleware(app)
89
 
app = make_filter(app, None)
90
 
 
91
 
 
92
 
httpserver.serve(app, host='127.0.0.1', port='9876')
93