~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:56:29 UTC
  • mto: This revision was merged to the branch mainline in revision 164.
  • Revision ID: michael.hudson@canonical.com-20080616105629-ea9dahymo4rl9cad
improvements borne out of the fires of contact with reality &c

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
from bzrlib import branch, errors
1
3
from loggerhead.history import History
2
 
from loggerhead.wsgiapp import BranchWSGIApp
3
 
 
4
 
h = History.from_folder('.')
5
 
 
6
 
app = BranchWSGIApp(h)
7
 
 
8
 
 
 
4
from loggerhead.wsgiapp import BranchWSGIApp, static_app
 
5
from paste.request import path_info_pop
 
6
from paste import httpexceptions
9
7
from paste import httpserver
10
 
from paste.evalexception import EvalException
11
8
from paste.httpexceptions import make_middleware
12
9
from paste.translogger import make_filter
13
 
 
14
 
app = app.app
15
 
for w in EvalException, make_middleware:
16
 
    app = w(app)
17
 
 
 
10
from loggerhead.changecache import FileChangeCache
 
11
 
 
12
 
 
13
 
 
14
class BranchesFromFileSystemServer(object):
 
15
    def __init__(self, folder, root):
 
16
        self.folder = folder
 
17
        self.root = root
 
18
 
 
19
    def __call__(self, environ, start_response):
 
20
        segment = path_info_pop(environ)
 
21
        if segment is None:
 
22
            raise httpexceptions.HTTPNotFound()
 
23
        relpath = os.path.join(self.folder, segment)
 
24
        f = os.path.join(self.root.folder, relpath)
 
25
        if not os.path.isdir(f):
 
26
            raise httpexceptions.HTTPNotFound()
 
27
        if f in self.root.cache:
 
28
            return self.root.cache[f](environ, start_response)
 
29
        try:
 
30
            b = branch.Branch.open(f)
 
31
        except errors.NotBranchError:
 
32
            return BranchesFromFileSystemServer(relpath, self.root)(environ, start_response)
 
33
        else:
 
34
            b.lock_read()
 
35
            try:
 
36
                _history = History.from_branch(b)
 
37
                _history.use_file_cache(FileChangeCache(_history, 'sql'))
 
38
                h = BranchWSGIApp(_history, relpath).app
 
39
                self.root.cache[f] = h
 
40
                return h(environ, start_response)
 
41
            finally:
 
42
                b.unlock()
 
43
 
 
44
class BranchesFromFileSystemRoot(object):
 
45
    def __init__(self, folder):
 
46
        self.cache = {}
 
47
        self.folder = folder
 
48
    def __call__(self, environ, start_response):
 
49
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
 
50
        segment = path_info_pop(environ)
 
51
        if segment == 'static':
 
52
            return static_app(environ, start_response)
 
53
        else:
 
54
            return BranchesFromFileSystemServer(
 
55
                segment, self)(environ, start_response)
 
56
 
 
57
app = BranchesFromFileSystemRoot('../..')
 
58
 
 
59
app = app
 
60
app = make_middleware(app)
18
61
app = make_filter(app, None)
19
62
 
20
 
httpserver.serve(app, host='127.0.0.1', port='9876')
 
63
from paste.evalexception.middleware import EvalException
 
64
 
 
65
httpserver.serve(EvalException(app), host='127.0.0.1', port='9876')
21
66