~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/filesystem.py

  • Committer: Michael Hudson
  • Date: 2008-09-30 03:05:31 UTC
  • mfrom: (226.1.3 fix-breadcrumbs)
  • Revision ID: michael.hudson@canonical.com-20080930030531-at8220tnkx9pvqcw
small fixes to the breadcrumbs in the --prefix case.
(thanks Matt Nordhoff for the prods)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""Serve branches at urls that mimic the file system layout."""
2
2
 
3
 
import cgi
4
3
import os
5
4
import tempfile
6
5
 
7
6
from bzrlib import branch, errors, lru_cache
8
7
 
9
8
from paste.request import path_info_pop
10
 
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
11
9
from paste import httpexceptions
12
10
 
13
11
from loggerhead.apps.branch import BranchWSGIApp
14
12
from loggerhead.apps import favicon_app, static_app
 
13
from loggerhead.controllers.directory_ui import DirectoryUI
15
14
 
16
15
sql_dir = tempfile.mkdtemp(prefix='loggerhead-cache-')
17
16
 
18
17
 
19
 
class DirectoryListing(object):
20
 
 
21
 
    def __init__(self, path):
 
18
class BranchesFromFileSystemServer(object):
 
19
    def __init__(self, path, root, name=None):
22
20
        self.path = path
23
 
 
24
 
    def __call__(self, environ, start_response):
25
 
        request = WSGIRequest(environ)
26
 
        response = WSGIResponse()
27
 
        listing = [d for d in os.listdir(self.path) if not d.startswith('.')]
28
 
        response.headers['Content-Type'] = 'text/html'
29
 
        print >> response, '<html><body>'
30
 
        for d in sorted(listing):
31
 
            if os.path.isdir(os.path.join(self.path, d)):
32
 
                d = cgi.escape(d)
33
 
                print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
34
 
        print >> response, '</body></html>'
35
 
        return response(environ, start_response)
36
 
 
37
 
 
38
 
class BranchesFromFileSystemServer(object):
39
 
    def __init__(self, folder, root):
40
 
        self.folder = folder
41
21
        self.root = root
 
22
        self.name = name
42
23
 
43
24
    def app_for_branch(self, branch):
44
 
        if not self.folder:
45
 
            name = os.path.basename(os.path.abspath(self.root.folder))
 
25
        if not self.name:
 
26
            name = branch.nick
 
27
            is_root = True
46
28
        else:
47
 
            name = self.folder
 
29
            name = self.name
 
30
            is_root = False
48
31
        branch_app = BranchWSGIApp(
49
 
            branch, name, {'cachepath': sql_dir}, self.root.graph_cache)
 
32
            branch, name, {'cachepath': sql_dir}, self.root.graph_cache,
 
33
            is_root=is_root)
50
34
        return branch_app.app
51
35
 
52
36
    def app_for_non_branch(self, environ):
55
39
            raise httpexceptions.HTTPMovedPermanently(
56
40
                environ['SCRIPT_NAME'] + '/')
57
41
        elif segment == '':
58
 
            return DirectoryListing(os.path.join(self.root.folder, self.folder))
 
42
            if self.name:
 
43
                name = self.name
 
44
            else:
 
45
                name = '/'
 
46
            return DirectoryUI(environ['loggerhead.static.url'], self.path, name)
59
47
        else:
60
 
            relpath = os.path.join(self.folder, segment)
61
 
            return BranchesFromFileSystemServer(relpath, self.root)
 
48
            new_path = os.path.join(self.path, segment)
 
49
            if self.name:
 
50
                new_name = os.path.join(self.name, segment)
 
51
            else:
 
52
                new_name = '/' + segment
 
53
            return BranchesFromFileSystemServer(new_path, self.root, new_name)
62
54
 
63
55
    def __call__(self, environ, start_response):
64
 
        path = os.path.join(self.root.folder, self.folder)
65
 
        if not os.path.isdir(path):
 
56
        if not os.path.isdir(self.path):
66
57
            raise httpexceptions.HTTPNotFound()
67
58
        try:
68
 
            b = branch.Branch.open(path)
 
59
            b = branch.Branch.open(self.path)
69
60
        except errors.NotBranchError:
70
61
            return self.app_for_non_branch(environ)(environ, start_response)
71
62
        else:
88
79
            return favicon_app(environ, start_response)
89
80
        else:
90
81
            return BranchesFromFileSystemServer(
91
 
                '', self)(environ, start_response)
 
82
                self.folder, self)(environ, start_response)
 
83
 
 
84
 
 
85
class UserBranchesFromFileSystemRoot(object):
 
86
 
 
87
    def __init__(self, folder, trunk_dir):
 
88
        self.graph_cache = lru_cache.LRUCache()
 
89
        self.folder = folder
 
90
        self.trunk_dir = trunk_dir
 
91
 
 
92
    def __call__(self, environ, start_response):
 
93
        environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
 
94
        path_info= environ['PATH_INFO']
 
95
        if path_info.startswith('/static/'):
 
96
            segment = path_info_pop(environ)
 
97
            assert segment == 'static'
 
98
            return static_app(environ, start_response)
 
99
        elif path_info == '/favicon.ico':
 
100
            return favicon_app(environ, start_response)
 
101
        else:
 
102
            # segments starting with ~ are user branches
 
103
            if path_info.startswith('/~'):
 
104
                segment = path_info_pop(environ)
 
105
                new_path = os.path.join(self.folder, segment[1:])
 
106
                return BranchesFromFileSystemServer(
 
107
                    new_path, self, segment)(environ, start_response)
 
108
            else:
 
109
                new_path = os.path.join(self.folder, self.trunk_dir)
 
110
                return BranchesFromFileSystemServer(
 
111
                    new_path, self)(environ, start_response)