~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to wsgitest.py

  • Committer: Michael Hudson
  • Date: 2007-12-12 08:36:51 UTC
  • mto: This revision was merged to the branch mainline in revision 143.
  • Revision ID: michael.hudson@canonical.com-20071212083651-oyh34swjxbzceyi1
add a comment for spiv

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import os
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 import httpexceptions
7
 
from paste import httpserver
8
 
from paste.httpexceptions import make_middleware
9
 
from paste.translogger import make_filter
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)
61
 
app = make_filter(app, None)
62
 
 
63
 
from paste.evalexception.middleware import EvalException
64
 
 
65
 
httpserver.serve(EvalException(app), host='127.0.0.1', port='9876')
66