~loggerhead-team/loggerhead/trunk-rich

159.2.13 by Michael Hudson
something like a directory listing
1
import cgi, os, tempfile
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
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
159.2.13 by Michael Hudson
something like a directory listing
6
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
7
from paste import httpexceptions
159.2.1 by Michael Hudson
things start to work a little already!
8
from paste import httpserver
159.2.2 by Michael Hudson
more workingness
9
from paste.httpexceptions import make_middleware
159.2.4 by Michael Hudson
more progress
10
from paste.translogger import make_filter
159.2.12 by Michael Hudson
improvements borne out of the fires of contact with reality &c
11
from loggerhead.changecache import FileChangeCache
159.2.4 by Michael Hudson
more progress
12
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
13
159.2.13 by Michael Hudson
something like a directory listing
14
sql_dir = tempfile.mkdtemp()
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
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):
159.2.20 by Michael Hudson
be more flexible about where wsgitest.py can be run from
22
        path = os.path.join(self.root.folder, self.folder)
23
        if not os.path.isdir(path):
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
24
            raise httpexceptions.HTTPNotFound()
159.2.20 by Michael Hudson
be more flexible about where wsgitest.py can be run from
25
        if path in self.root.cache:
26
            return self.root.cache[path](environ, start_response)
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
27
        try:
159.2.20 by Michael Hudson
be more flexible about where wsgitest.py can be run from
28
            b = branch.Branch.open(path)
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
29
        except errors.NotBranchError:
159.2.20 by Michael Hudson
be more flexible about where wsgitest.py can be run from
30
            segment = path_info_pop(environ)
31
            if segment is None:
159.2.21 by Michael Hudson
redirect from /directory to /directory/
32
                raise httpexceptions.HTTPMovedPermanently(environ['SCRIPT_NAME'] + '/')
33
            elif segment == '':
159.2.20 by Michael Hudson
be more flexible about where wsgitest.py can be run from
34
                request = WSGIRequest(environ)
35
                response = WSGIResponse()
36
                listing = [d for d in os.listdir(path) if not d.startswith('.')]
37
                response.headers['Content-Type'] = 'text/html'
38
                print >> response, '<html><body>'
39
                for d in sorted(listing):
40
                    if os.path.isdir(os.path.join(path, d)):
41
                        d = cgi.escape(d)
42
                        print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
43
                print >> response, '</body></html>'
44
                return response(environ, start_response)
45
            else:
46
                relpath = os.path.join(self.folder, segment)
47
                return BranchesFromFileSystemServer(relpath, self.root)(
48
                    environ, start_response)
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
49
        else:
50
            b.lock_read()
51
            try:
159.2.12 by Michael Hudson
improvements borne out of the fires of contact with reality &c
52
                _history = History.from_branch(b)
159.2.13 by Michael Hudson
something like a directory listing
53
                _history.use_file_cache(FileChangeCache(_history, sql_dir))
159.2.20 by Michael Hudson
be more flexible about where wsgitest.py can be run from
54
                if not self.folder:
55
                    name = os.path.basename(os.path.abspath(path))
56
                else:
57
                    name = self.folder
58
                h = BranchWSGIApp(_history, name).app
59
                self.root.cache[path] = h
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
60
                return h(environ, start_response)
61
            finally:
62
                b.unlock()
63
64
class BranchesFromFileSystemRoot(object):
65
    def __init__(self, folder):
66
        self.cache = {}
67
        self.folder = folder
68
    def __call__(self, environ, start_response):
69
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
159.2.20 by Michael Hudson
be more flexible about where wsgitest.py can be run from
70
        if environ['PATH_INFO'].startswith('/static/'):
71
            segment = path_info_pop(environ)
72
            assert segment == 'static'
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
73
            return static_app(environ, start_response)
74
        else:
75
            return BranchesFromFileSystemServer(
159.2.20 by Michael Hudson
be more flexible about where wsgitest.py can be run from
76
                '', self)(environ, start_response)
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
77
159.2.13 by Michael Hudson
something like a directory listing
78
app = BranchesFromFileSystemRoot('.')
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
79
80
app = app
81
app = make_middleware(app)
159.2.4 by Michael Hudson
more progress
82
app = make_filter(app, None)
83
159.2.11 by Michael Hudson
hacking towards server from urls based on file paths.
84
159.2.14 by Michael Hudson
oopsie
85
httpserver.serve(app, host='127.0.0.1', port='9876')
159.2.1 by Michael Hudson
things start to work a little already!
86