~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to wsgitest.py

  • Committer: Robey Pointer
  • Date: 2007-03-06 07:18:57 UTC
  • Revision ID: robey@lag.net-20070306071857-jqc8kphzcneb0bdd
add lsprof decorator; comment out unnecessary nbsp; conversion.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import cgi, os, tempfile
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.wsgiwrappers import WSGIRequest, WSGIResponse
7
 
from paste import httpexceptions
8
 
from paste import httpserver
9
 
from paste.httpexceptions import make_middleware
10
 
from paste.translogger import make_filter
11
 
from loggerhead.changecache import FileChangeCache
12
 
 
13
 
 
14
 
sql_dir = tempfile.mkdtemp()
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):
22
 
        path = os.path.join(self.root.folder, self.folder)
23
 
        if not os.path.isdir(path):
24
 
            raise httpexceptions.HTTPNotFound()
25
 
        if path in self.root.cache:
26
 
            return self.root.cache[path](environ, start_response)
27
 
        try:
28
 
            b = branch.Branch.open(path)
29
 
        except errors.NotBranchError:
30
 
            segment = path_info_pop(environ)
31
 
            if segment is None:
32
 
                raise httpexceptions.HTTPMovedPermanently(environ['SCRIPT_NAME'] + '/')
33
 
            elif segment == '':
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)
49
 
        else:
50
 
            b.lock_read()
51
 
            try:
52
 
                _history = History.from_branch(b)
53
 
                _history.use_file_cache(FileChangeCache(_history, sql_dir))
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
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']
70
 
        if environ['PATH_INFO'].startswith('/static/'):
71
 
            segment = path_info_pop(environ)
72
 
            assert segment == 'static'
73
 
            return static_app(environ, start_response)
74
 
        else:
75
 
            return BranchesFromFileSystemServer(
76
 
                '', self)(environ, start_response)
77
 
 
78
 
app = BranchesFromFileSystemRoot('.')
79
 
 
80
 
app = app
81
 
app = make_middleware(app)
82
 
app = make_filter(app, None)
83
 
 
84
 
 
85
 
httpserver.serve(app, host='127.0.0.1', port='9876')
86