~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to wsgitest.py

  • Committer: Michael Hudson
  • Date: 2007-10-29 16:19:30 UTC
  • mto: This revision was merged to the branch mainline in revision 141.
  • Revision ID: michael.hudson@canonical.com-20071029161930-oxqrd4rd8j1oz3hx
add do nothing check target

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 __call__(self, environ, start_response):
22
 
        segment = path_info_pop(environ)
23
 
        if segment is None:
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):
38
 
            raise httpexceptions.HTTPNotFound()
39
 
        if f in self.root.cache:
40
 
            return self.root.cache[f](environ, start_response)
41
 
        try:
42
 
            b = branch.Branch.open(f)
43
 
        except errors.NotBranchError:
44
 
            return BranchesFromFileSystemServer(relpath, self.root)(environ, start_response)
45
 
        else:
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()
55
 
 
56
 
class BranchesFromFileSystemRoot(object):
57
 
    def __init__(self, folder):
58
 
        self.cache = {}
59
 
        self.folder = folder
60
 
    def __call__(self, environ, start_response):
61
 
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
62
 
        segment = path_info_pop(environ)
63
 
        if segment == 'static':
64
 
            return static_app(environ, start_response)
65
 
        else:
66
 
            return BranchesFromFileSystemServer(
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