/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
1
import os
2
from bzrlib import branch, errors
159.2.1 by Michael Hudson
things start to work a little already!
3
from loggerhead.history import History
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
4
from loggerhead.wsgiapp import BranchWSGIApp, static_app
5
from paste.request import path_info_pop
6
from paste import httpexceptions
159.2.1 by Michael Hudson
things start to work a little already!
7
from paste import httpserver
159.2.2 by Michael Hudson
more workingness
8
from paste.httpexceptions import make_middleware
159.2.4 by Michael Hudson
more progress
9
from paste.translogger import make_filter
159.2.12 by Michael Hudson
improvements borne out of the fires of contact with reality &c
10
from loggerhead.changecache import FileChangeCache
159.2.4 by Michael Hudson
more progress
11
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
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()
159.2.12 by Michael Hudson
improvements borne out of the fires of contact with reality &c
23
        relpath = os.path.join(self.folder, segment)
24
        f = os.path.join(self.root.folder, relpath)
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
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:
159.2.12 by Michael Hudson
improvements borne out of the fires of contact with reality &c
32
            return BranchesFromFileSystemServer(relpath, self.root)(environ, start_response)
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
33
        else:
34
            b.lock_read()
35
            try:
159.2.12 by Michael Hudson
improvements borne out of the fires of contact with reality &c
36
                _history = History.from_branch(b)
37
                _history.use_file_cache(FileChangeCache(_history, 'sql'))
38
                h = BranchWSGIApp(_history, relpath).app
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
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(
159.2.12 by Michael Hudson
improvements borne out of the fires of contact with reality &c
55
                segment, self)(environ, start_response)
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
56
57
app = BranchesFromFileSystemRoot('../..')
58
59
app = app
60
app = make_middleware(app)
159.2.4 by Michael Hudson
more progress
61
app = make_filter(app, None)
62
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
63
from paste.evalexception.middleware import EvalException
64
65
httpserver.serve(EvalException(app), host='127.0.0.1', port='9876')
159.2.1 by Michael Hudson
things start to work a little already!
66