~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to wsgitest.py

  • Committer: Michael Hudson
  • Date: 2008-06-16 10:13:40 UTC
  • mto: This revision was merged to the branch mainline in revision 164.
  • Revision ID: michael.hudson@canonical.com-20080616101340-2bsyaxiom1okfm4w
betterĀ staticĀ 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 cgi
4
 
import os
5
 
import tempfile
6
 
 
7
 
from bzrlib import branch, errors, lru_cache
8
 
 
9
 
from paste.request import path_info_pop
10
 
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
11
 
from paste import httpexceptions
12
 
 
13
 
from loggerhead.apps.branch import BranchWSGIApp
14
 
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):
22
 
        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
 
        self.root = root
42
 
 
43
 
    def app_for_branch(self, branch):
44
 
        if not self.folder:
45
 
            name = os.path.basename(os.path.abspath(self.root.folder))
46
 
        else:
47
 
            name = self.folder
48
 
        branch_app = BranchWSGIApp(
49
 
            branch, name, {'cachepath': sql_dir}, self.root.graph_cache)
50
 
        return branch_app.app
51
 
 
52
 
    def app_for_non_branch(self, environ):
53
 
        segment = path_info_pop(environ)
54
 
        if segment is None:
55
 
            raise httpexceptions.HTTPMovedPermanently(
56
 
                environ['SCRIPT_NAME'] + '/')
57
 
        elif segment == '':
58
 
            return DirectoryListing(os.path.join(self.root.folder, self.folder))
59
 
        else:
60
 
            relpath = os.path.join(self.folder, segment)
61
 
            return BranchesFromFileSystemServer(relpath, self.root)
62
 
 
63
 
    def __call__(self, environ, start_response):
64
 
        path = os.path.join(self.root.folder, self.folder)
65
 
        if not os.path.isdir(path):
66
 
            raise httpexceptions.HTTPNotFound()
67
 
        try:
68
 
            b = branch.Branch.open(path)
69
 
        except errors.NotBranchError:
70
 
            return self.app_for_non_branch(environ)(environ, start_response)
71
 
        else:
72
 
            return self.app_for_branch(b)(environ, start_response)
73
 
 
74
 
 
75
 
class BranchesFromFileSystemRoot(object):
76
 
 
77
 
    def __init__(self, folder):
78
 
        self.graph_cache = lru_cache.LRUCache()
79
 
        self.folder = folder
80
 
 
81
 
    def __call__(self, environ, start_response):
82
 
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
83
 
        if environ['PATH_INFO'].startswith('/static/'):
84
 
            segment = path_info_pop(environ)
85
 
            assert segment == 'static'
86
 
            return static_app(environ, start_response)
87
 
        elif environ['PATH_INFO'] == '/favicon.ico':
88
 
            return favicon_app(environ, start_response)
89
 
        else:
90
 
            return BranchesFromFileSystemServer(
91
 
                '', 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