~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to wsgitest.py

  • Committer: Michael Hudson
  • Date: 2008-06-16 23:40:30 UTC
  • mto: This revision was merged to the branch mainline in revision 164.
  • Revision ID: michael.hudson@canonical.com-20080616234030-3wl34s0q3woo2ywb
be more flexible about where wsgitest.py can be run from

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
        self.root = root
20
20
 
21
21
    def __call__(self, environ, start_response):
22
 
        segment = path_info_pop(environ)
23
 
        if segment is None:
24
 
            f = os.path.join(self.root.folder, self.folder)
25
 
            request = WSGIRequest(environ)
26
 
            response = WSGIResponse()
27
 
            listing = [d for d in os.listdir(f) if not d.startswith('.')]
28
 
            response.headers['Content-Type'] = 'text/html'
29
 
            print >> response, '<html><body>'
30
 
            for d in sorted(listing):
31
 
                d = cgi.escape(d)
32
 
                print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
33
 
            print >> response, '</body></html>'
34
 
            return response(environ, start_response)
35
 
        relpath = os.path.join(self.folder, segment)
36
 
        f = os.path.join(self.root.folder, relpath)
37
 
        if not os.path.isdir(f):
 
22
        path = os.path.join(self.root.folder, self.folder)
 
23
        if not os.path.isdir(path):
38
24
            raise httpexceptions.HTTPNotFound()
39
 
        if f in self.root.cache:
40
 
            return self.root.cache[f](environ, start_response)
 
25
        if path in self.root.cache:
 
26
            return self.root.cache[path](environ, start_response)
41
27
        try:
42
 
            b = branch.Branch.open(f)
 
28
            b = branch.Branch.open(path)
43
29
        except errors.NotBranchError:
44
 
            return BranchesFromFileSystemServer(relpath, self.root)(environ, start_response)
 
30
            segment = path_info_pop(environ)
 
31
            if segment is None:
 
32
                request = WSGIRequest(environ)
 
33
                response = WSGIResponse()
 
34
                listing = [d for d in os.listdir(path) if not d.startswith('.')]
 
35
                response.headers['Content-Type'] = 'text/html'
 
36
                print >> response, '<html><body>'
 
37
                for d in sorted(listing):
 
38
                    if os.path.isdir(os.path.join(path, d)):
 
39
                        d = cgi.escape(d)
 
40
                        print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
 
41
                print >> response, '</body></html>'
 
42
                return response(environ, start_response)
 
43
            else:
 
44
                relpath = os.path.join(self.folder, segment)
 
45
                return BranchesFromFileSystemServer(relpath, self.root)(
 
46
                    environ, start_response)
45
47
        else:
46
48
            b.lock_read()
47
49
            try:
48
50
                _history = History.from_branch(b)
49
51
                _history.use_file_cache(FileChangeCache(_history, sql_dir))
50
 
                h = BranchWSGIApp(_history, relpath).app
51
 
                self.root.cache[f] = h
 
52
                print '!!!', self.folder, path
 
53
                if not self.folder:
 
54
                    name = os.path.basename(os.path.abspath(path))
 
55
                else:
 
56
                    name = self.folder
 
57
                h = BranchWSGIApp(_history, name).app
 
58
                self.root.cache[path] = h
52
59
                return h(environ, start_response)
53
60
            finally:
54
61
                b.unlock()
59
66
        self.folder = folder
60
67
    def __call__(self, environ, start_response):
61
68
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
62
 
        segment = path_info_pop(environ)
63
 
        if segment == 'static':
 
69
        if environ['PATH_INFO'].startswith('/static/'):
 
70
            segment = path_info_pop(environ)
 
71
            assert segment == 'static'
64
72
            return static_app(environ, start_response)
65
73
        else:
66
74
            return BranchesFromFileSystemServer(
67
 
                segment, self)(environ, start_response)
 
75
                '', self)(environ, start_response)
68
76
 
69
77
app = BranchesFromFileSystemRoot('.')
70
78