~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/inventory_ui.py

  • Committer: Matt Nordhoff
  • Date: 2009-06-26 09:23:47 UTC
  • Revision ID: mnordhoff@mattnordhoff.com-20090626092347-28xtb3s1oc16uoph
Remove loggerhead/main.py's shebang, since it's no longer executable anyway.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import posixpath
23
23
import urllib
24
24
 
25
 
from paste.httpexceptions import HTTPServerError
 
25
from paste.httpexceptions import HTTPNotFound
26
26
 
 
27
from bzrlib import errors
27
28
from bzrlib.revision import is_null as is_null_rev
28
29
 
29
30
from loggerhead import util
35
36
 
36
37
def dirname(path):
37
38
    if path is not None:
38
 
        while path.endswith('/'):
39
 
            path = path[:-1]
 
39
        path = path.rstrip('/')
40
40
        path = urllib.quote(posixpath.dirname(path))
41
41
    return path
42
42
 
45
45
 
46
46
    template_path = 'loggerhead.templates.inventory'
47
47
 
 
48
    def get_filelist(self, inv, path, sort_type):
 
49
        """
 
50
        return the list of all files (and their attributes) within a given
 
51
        path subtree.
 
52
 
 
53
        @param inv: The inventory.
 
54
        @param path: The path of a directory within the inventory.
 
55
        @param sort_type: How to sort the results... XXX.
 
56
        """
 
57
        file_id = inv.path2id(path)
 
58
        dir_ie = inv[file_id]
 
59
        file_list = []
 
60
 
 
61
        revid_set = set()
 
62
 
 
63
        for filename, entry in dir_ie.children.iteritems():
 
64
            revid_set.add(entry.revision)
 
65
 
 
66
        change_dict = {}
 
67
        for change in self._history.get_changes(list(revid_set)):
 
68
            change_dict[change.revid] = change
 
69
 
 
70
        for filename, entry in dir_ie.children.iteritems():
 
71
            pathname = filename
 
72
            if entry.kind == 'directory':
 
73
                pathname += '/'
 
74
            if path == '':
 
75
                absolutepath = pathname
 
76
            else:
 
77
                absolutepath = path + '/' + pathname
 
78
            revid = entry.revision
 
79
 
 
80
            file = util.Container(
 
81
                filename=filename, executable=entry.executable,
 
82
                kind=entry.kind, absolutepath=absolutepath,
 
83
                file_id=entry.file_id, size=entry.text_size, revid=revid,
 
84
                change=change_dict[revid])
 
85
            file_list.append(file)
 
86
 
 
87
        if sort_type == 'filename':
 
88
            file_list.sort(key=lambda x: x.filename.lower()) # case-insensitive
 
89
        elif sort_type == 'size':
 
90
            file_list.sort(key=lambda x: x.size)
 
91
        elif sort_type == 'date':
 
92
            file_list.sort(key=lambda x: x.change.date)
 
93
 
 
94
        # Always sort directories first.
 
95
        file_list.sort(key=lambda x: x.kind != 'directory')
 
96
 
 
97
        return file_list
 
98
 
48
99
    def get_values(self, path, kwargs, headers):
49
100
        history = self._history
50
 
        revid = self.get_revid()
51
 
 
 
101
        branch = history._branch
52
102
        try:
53
 
            rev_tree = history._branch.repository.revision_tree(revid)
54
 
        except:
55
 
            self.log.exception('Exception fetching changes')
56
 
            raise HTTPServerError('Could not fetch changes')
 
103
            revid = self.get_revid()
 
104
            rev_tree = branch.repository.revision_tree(revid)
 
105
        except errors.NoSuchRevision:
 
106
            raise HTTPNotFound()
57
107
 
58
108
        file_id = kwargs.get('file_id', None)
59
109
        start_revid = kwargs.get('start_revid', None)
60
 
        sort_type = kwargs.get('sort', None)
 
110
        sort_type = kwargs.get('sort', 'filename')
61
111
 
62
112
        # no navbar for revisions
63
113
        navigation = util.Container()
64
114
 
65
115
        if path is not None:
66
 
            if not path.startswith('/'):
67
 
                path = '/' + path
68
 
            file_id = history.get_file_id(revid, path)
 
116
            path = path.rstrip('/')
 
117
            file_id = rev_tree.path2id(path)
 
118
            if file_id is None:
 
119
                raise HTTPNotFound()
69
120
        else:
70
 
            path = rev_tree.id2path(file_id)
 
121
            if file_id is None:
 
122
                path = ''
 
123
            else:
 
124
                try:
 
125
                    path = rev_tree.id2path(file_id)
 
126
                except errors.NoSuchId:
 
127
                    raise HTTPNotFound()
71
128
 
72
129
        # Are we at the top of the tree
73
130
        if path in ['/', '']:
74
131
            updir = None
75
132
        else:
76
 
            updir = dirname(path)[1:]
 
133
            updir = dirname(path)
77
134
 
78
135
        # Directory Breadcrumbs
79
136
        directory_breadcrumbs = util.directory_breadcrumbs(
85
142
 
86
143
            change = history.get_changes([ revid ])[0]
87
144
            # If we're looking at the tip, use head: in the URL instead
88
 
            if revid == history.last_revid:
 
145
            if revid == branch.last_revision():
89
146
                revno_url = 'head:'
90
147
            else:
91
148
                revno_url = history.get_revno(revid)
92
 
            # add parent & merge-point branch-nick info, in case it's useful
93
 
            history.get_branch_nicks([ change ])
 
149
            history.add_branch_nicks(change)
94
150
 
95
151
            # Create breadcrumb trail for the path within the branch
96
152
            branch_breadcrumbs = util.branch_breadcrumbs(path, rev_tree, 'files')
97
 
            if file_id is None:
98
 
                file_id = rev_tree.inventory.root.file_id
99
 
            filelist = history.get_filelist(rev_tree.inventory, file_id, sort_type)
 
153
            filelist = self.get_filelist(rev_tree.inventory, path, sort_type)
100
154
        else:
101
 
            inv = None
102
155
            start_revid = None
103
 
            sort_type = None
104
156
            change = None
105
157
            path = "/"
106
158
            updir = None
117
169
            'path': path,
118
170
            'updir': updir,
119
171
            'filelist': filelist,
120
 
            'history': history,
121
 
            'posixpath': posixpath,
122
172
            'navigation': navigation,
123
173
            'url': self._branch.context_url,
124
174
            'start_revid': start_revid,