~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:08:54 UTC
  • mto: This revision was merged to the branch mainline in revision 164.
  • Revision ID: michael.hudson@canonical.com-20080616110854-p4c18tqhlko5of0p
something like a directory listing

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import os
 
1
import cgi, os, tempfile
2
2
from bzrlib import branch, errors
3
3
from loggerhead.history import History
4
4
from loggerhead.wsgiapp import BranchWSGIApp, static_app
5
5
from paste.request import path_info_pop
 
6
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
6
7
from paste import httpexceptions
7
8
from paste import httpserver
8
9
from paste.httpexceptions import make_middleware
10
11
from loggerhead.changecache import FileChangeCache
11
12
 
12
13
 
 
14
sql_dir = tempfile.mkdtemp()
13
15
 
14
16
class BranchesFromFileSystemServer(object):
15
17
    def __init__(self, folder, root):
19
21
    def __call__(self, environ, start_response):
20
22
        segment = path_info_pop(environ)
21
23
        if segment is None:
22
 
            raise httpexceptions.HTTPNotFound()
 
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)
23
35
        relpath = os.path.join(self.folder, segment)
24
36
        f = os.path.join(self.root.folder, relpath)
25
37
        if not os.path.isdir(f):
34
46
            b.lock_read()
35
47
            try:
36
48
                _history = History.from_branch(b)
37
 
                _history.use_file_cache(FileChangeCache(_history, 'sql'))
 
49
                _history.use_file_cache(FileChangeCache(_history, sql_dir))
38
50
                h = BranchWSGIApp(_history, relpath).app
39
51
                self.root.cache[f] = h
40
52
                return h(environ, start_response)
54
66
            return BranchesFromFileSystemServer(
55
67
                segment, self)(environ, start_response)
56
68
 
57
 
app = BranchesFromFileSystemRoot('../..')
 
69
app = BranchesFromFileSystemRoot('.')
58
70
 
59
71
app = app
60
72
app = make_middleware(app)