~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:46:16 UTC
  • mto: This revision was merged to the branch mainline in revision 164.
  • Revision ID: michael.hudson@canonical.com-20080616104616-buapylh1ojge3dgc
hacking towards server from urls based on file paths.

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
10
 
14
 
app = app.app
15
 
for w in EvalException, make_middleware:
16
 
    app = w(app)
17
 
 
 
11
 
 
12
 
 
13
class BranchesFromFileSystemServer(object):
 
14
    def __init__(self, folder, root):
 
15
        self.folder = folder
 
16
        self.root = root
 
17
 
 
18
    def __call__(self, environ, start_response):
 
19
        segment = path_info_pop(environ)
 
20
        if segment is None:
 
21
            raise httpexceptions.HTTPNotFound()
 
22
        f = os.path.join(self.folder, segment)
 
23
        print f
 
24
        if not os.path.isdir(f):
 
25
            raise httpexceptions.HTTPNotFound()
 
26
        if f in self.root.cache:
 
27
            return self.root.cache[f](environ, start_response)
 
28
        try:
 
29
            b = branch.Branch.open(f)
 
30
        except errors.NotBranchError:
 
31
            return BranchesFromFileSystemServer(f, self.root)(environ, start_response)
 
32
        else:
 
33
            b.lock_read()
 
34
            try:
 
35
                h = BranchWSGIApp(History.from_branch(b), 'hello').app
 
36
                self.root.cache[f] = h
 
37
                return h(environ, start_response)
 
38
            finally:
 
39
                b.unlock()
 
40
 
 
41
class BranchesFromFileSystemRoot(object):
 
42
    def __init__(self, folder):
 
43
        self.cache = {}
 
44
        self.folder = folder
 
45
    def __call__(self, environ, start_response):
 
46
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
 
47
        segment = path_info_pop(environ)
 
48
        if segment == 'static':
 
49
            return static_app(environ, start_response)
 
50
        else:
 
51
            return BranchesFromFileSystemServer(
 
52
                os.path.join(self.folder, segment), self)(environ, start_response)
 
53
 
 
54
app = BranchesFromFileSystemRoot('../..')
 
55
 
 
56
app = app
 
57
app = make_middleware(app)
18
58
app = make_filter(app, None)
19
59
 
20
 
httpserver.serve(app, host='127.0.0.1', port='9876')
 
60
from paste.evalexception.middleware import EvalException
 
61
 
 
62
httpserver.serve(EvalException(app), host='127.0.0.1', port='9876')
21
63