~malept/loggerhead/standalone-auth

« back to all changes in this revision

Viewing changes to loggerhead/apps/filesystem.py

  • Committer: Martin Albisetti
  • Date: 2008-07-22 00:40:03 UTC
  • mfrom: (182 loggerhead.search_integration)
  • mto: This revision was merged to the branch mainline in revision 187.
  • Revision ID: argentina@gmail.com-20080722004003-lx1fp0tthpp6kl92
Merged from trunk, resolved billions of conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Serve branches at urls that mimic the file system layout."""
 
2
 
1
3
import cgi
2
4
import os
3
5
import tempfile
4
6
 
5
 
from bzrlib import branch, errors
 
7
from bzrlib import branch, errors, lru_cache
6
8
 
7
9
from paste.request import path_info_pop
8
10
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
11
13
from loggerhead.apps.branch import BranchWSGIApp
12
14
from loggerhead.apps import favicon_app, static_app
13
15
 
14
 
 
15
16
sql_dir = tempfile.mkdtemp()
16
17
 
17
 
class BranchesFromFileSystemServer(object):
18
 
    def __init__(self, folder, root):
19
 
        self.folder = folder
20
 
        self.root = root
21
 
 
22
 
    def directory_listing(self, path, environ, start_response):
 
18
 
 
19
class DirectoryListing(object):
 
20
 
 
21
    def __init__(self, path):
 
22
        self.path = path
 
23
 
 
24
    def __call__(self, environ, start_response):
23
25
        request = WSGIRequest(environ)
24
26
        response = WSGIResponse()
25
 
        listing = [d for d in os.listdir(path) if not d.startswith('.')]
 
27
        listing = [d for d in os.listdir(self.path) if not d.startswith('.')]
26
28
        response.headers['Content-Type'] = 'text/html'
27
29
        print >> response, '<html><body>'
28
30
        for d in sorted(listing):
29
 
            if os.path.isdir(os.path.join(path, d)):
 
31
            if os.path.isdir(os.path.join(self.path, d)):
30
32
                d = cgi.escape(d)
31
33
                print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
32
34
        print >> response, '</body></html>'
33
35
        return response(environ, start_response)
34
36
 
35
 
    def app_for_branch(self, b, path):
 
37
 
 
38
class BranchesFromFileSystemServer(object):
 
39
    def __init__(self, folder, root):
 
40
        self.folder = folder
 
41
        self.root = root
 
42
 
 
43
    def app_for_branch(self, branch):
36
44
        if not self.folder:
37
 
            name = os.path.basename(os.path.abspath(path))
 
45
            name = os.path.basename(os.path.abspath(self.root.folder))
38
46
        else:
39
47
            name = self.folder
40
 
        h = BranchWSGIApp(path, name, {'cachepath': sql_dir})
41
 
        self.root.cache[path] = h
42
 
        return h.app
 
48
        branch_app = BranchWSGIApp(
 
49
            branch, name, {'cachepath': sql_dir}, self.root.graph_cache)
 
50
        return branch_app.app
 
51
 
 
52
    def app_for_non_branch(self, environ):
 
53
        segment = path_info_pop(environ)
 
54
        if segment is None:
 
55
            raise httpexceptions.HTTPMovedPermanently(
 
56
                environ['SCRIPT_NAME'] + '/')
 
57
        elif segment == '':
 
58
            return DirectoryListing(os.path.join(self.root.folder, self.folder))
 
59
        else:
 
60
            relpath = os.path.join(self.folder, segment)
 
61
            return BranchesFromFileSystemServer(relpath, self.root)
43
62
 
44
63
    def __call__(self, environ, start_response):
45
64
        path = os.path.join(self.root.folder, self.folder)
46
65
        if not os.path.isdir(path):
47
66
            raise httpexceptions.HTTPNotFound()
48
 
        cached = self.root.cache.get(path)
49
 
        if cached is not None:
50
 
            return cached.app(environ, start_response)
51
67
        try:
52
68
            b = branch.Branch.open(path)
53
69
        except errors.NotBranchError:
54
 
            segment = path_info_pop(environ)
55
 
            if segment is None:
56
 
                raise httpexceptions.HTTPMovedPermanently(
57
 
                    environ['SCRIPT_NAME'] + '/')
58
 
            elif segment == '':
59
 
                return self.directory_listing(path, environ, start_response)
60
 
            else:
61
 
                relpath = os.path.join(self.folder, segment)
62
 
                return BranchesFromFileSystemServer(relpath, self.root)(
63
 
                    environ, start_response)
 
70
            return self.app_for_non_branch(environ)(environ, start_response)
64
71
        else:
65
 
            return self.app_for_branch(b, path)(environ, start_response)
 
72
            return self.app_for_branch(b)(environ, start_response)
66
73
 
67
74
 
68
75
class BranchesFromFileSystemRoot(object):
 
76
 
69
77
    def __init__(self, folder):
70
 
        self.cache = {}
 
78
        self.graph_cache = lru_cache.LRUCache()
71
79
        self.folder = folder
 
80
 
72
81
    def __call__(self, environ, start_response):
73
82
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
74
83
        if environ['PATH_INFO'].startswith('/static/'):