/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to olive/info_helper.py

Commit messages never contain config options

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
2
 
# Some parts of the code are:
3
 
# Copyright (C) 2005, 2006 by Canonical Ltd
4
 
#
5
 
# This program is free software; you can redistribute it and/or modify
6
 
# it under the terms of the GNU General Public License as published by
7
 
# the Free Software Foundation; either version 2 of the License, or
8
 
# (at your option) any later version.
9
 
#
10
 
# This program is distributed in the hope that it will be useful,
11
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
# GNU General Public License for more details.
14
 
#
15
 
# You should have received a copy of the GNU General Public License
16
 
# along with this program; if not, write to the Free Software
17
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
 
19
 
import time
20
 
import sys
21
 
 
22
 
import bzrlib
23
 
 
24
 
from bzrlib import (
25
 
    bzrdir,
26
 
    diff,
27
 
    errors,
28
 
    osutils,
29
 
    urlutils,
30
 
    )
31
 
 
32
 
from bzrlib.diff import show_diff_trees
33
 
from bzrlib.missing import find_unmerged
34
 
    
35
 
def _repo_rel_url(repo_url, inner_url):
36
 
    """Return path with common prefix of repository path removed.
37
 
 
38
 
    If path is not part of the repository, the original path is returned.
39
 
    If path is equal to the repository, the current directory marker '.' is
40
 
    returned.
41
 
    Otherwise, a relative path is returned, with trailing '/' stripped.
42
 
    """
43
 
    inner_url = urlutils.normalize_url(inner_url)
44
 
    repo_url = urlutils.normalize_url(repo_url)
45
 
    if inner_url == repo_url:
46
 
        return '.'
47
 
    result = urlutils.relative_url(repo_url, inner_url)
48
 
    if result != inner_url:
49
 
        result = result.rstrip('/')
50
 
    return result
51
 
 
52
 
def get_location_info(repository, branch=None, working=None):
53
 
    """ Get known locations for working, branch and repository.
54
 
    
55
 
    :return: a dictionary containing the needed infos
56
 
    """
57
 
    ret = {}
58
 
    repository_path = repository.bzrdir.root_transport.base
59
 
    if working and branch:
60
 
        working_path = working.bzrdir.root_transport.base
61
 
        branch_path = branch.bzrdir.root_transport.base
62
 
        if working_path != branch_path:
63
 
            # lightweight checkout
64
 
            ret['lightcoroot'] = working_path
65
 
            if repository.is_shared():
66
 
                # lightweight checkout of branch in shared repository
67
 
                ret['sharedrepo'] = repository_path
68
 
                ret['repobranch'] = _repo_rel_url(repository_path, branch_path)
69
 
            else:
70
 
                # lightweight checkout of standalone branch
71
 
                ret['cobranch'] = branch_path
72
 
        elif repository.is_shared():
73
 
            # branch with tree inside shared repository
74
 
            ret['sharedrepo'] = repository_path
75
 
            ret['repoco'] = _repo_rel_url(repository_path, branch_path)
76
 
        elif branch.get_bound_location():
77
 
            # normal checkout
78
 
            ret['coroot'] = working_path
79
 
            ret['cobranch'] = branch.get_bound_location()
80
 
        else:
81
 
            # standalone
82
 
            ret['branchroot'] = working_path
83
 
    elif branch:
84
 
        branch_path = branch.bzrdir.root_transport.base
85
 
        if repository.is_shared():
86
 
            # branch is part of shared repository
87
 
            ret['sharedrepo'] = repository_path
88
 
            ret['repobranch'] = _repo_rel_url(repository_path, branch_path)
89
 
        else:
90
 
            # standalone branch
91
 
            ret['branchroot'] = branch_path
92
 
    else:
93
 
        # shared repository
94
 
        assert repository.is_shared()
95
 
        ret['sharedrepo'] = repository_path
96
 
    
97
 
    return ret
98
 
 
99
 
def get_related_info(branch):
100
 
    """ Get parent and push location of branch.
101
 
    
102
 
    :return: a dictionary containing the needed infos
103
 
    """
104
 
    ret = {}
105
 
    if branch.get_parent() or branch.get_push_location():
106
 
        if branch.get_parent():
107
 
            ret['parentbranch'] = branch.get_parent()
108
 
        if branch.get_push_location():
109
 
            ret['publishbranch'] = branch.get_push_location()
110
 
    
111
 
    return ret
112
 
 
113
 
 
114
 
def get_format_info(control=None, repository=None, branch=None, working=None):
115
 
    """ Get known formats for control, working, branch and repository.
116
 
    
117
 
    :return: a dictionary containing the needed infos
118
 
    """
119
 
    ret = {}
120
 
    if control:
121
 
        ret['control'] = control._format.get_format_description()
122
 
    if working:
123
 
        ret['workingtree'] = working._format.get_format_description()
124
 
    if branch:
125
 
        ret['branch'] = branch._format.get_format_description()
126
 
    if repository:
127
 
        ret['repository'] = repository._format.get_format_description()
128
 
    
129
 
    return ret
130
 
 
131
 
 
132
 
def get_locking_info(repository, branch=None, working=None):
133
 
    """ Get locking status of working, branch and repository.
134
 
    
135
 
    :return: a dictionary containing the needed infos
136
 
    """
137
 
    ret = {}
138
 
    if (repository.get_physical_lock_status() or
139
 
        (branch and branch.get_physical_lock_status()) or
140
 
        (working and working.get_physical_lock_status())):
141
 
        if working:
142
 
            if working.get_physical_lock_status():
143
 
                status = 'locked'
144
 
            else:
145
 
                status = 'unlocked'
146
 
            ret['workingtree'] = status
147
 
        if branch:
148
 
            if branch.get_physical_lock_status():
149
 
                status = 'locked'
150
 
            else:
151
 
                status = 'unlocked'
152
 
            ret['branch'] = status
153
 
        if repository:
154
 
            if repository.get_physical_lock_status():
155
 
                status = 'locked'
156
 
            else:
157
 
                status = 'unlocked'
158
 
            ret['repository'] = status
159
 
    
160
 
    return ret
161
 
 
162
 
def get_missing_revisions_branch(branch):
163
 
    """ Get missing master revisions in branch.
164
 
    
165
 
    :return: a dictionary containing the needed infos
166
 
    """
167
 
    ret = {}
168
 
    # Try with inaccessible branch ?
169
 
    master = branch.get_master_branch()
170
 
    if master:
171
 
        local_extra, remote_extra = find_unmerged(branch, master)
172
 
        if remote_extra:
173
 
            ret = len(remote_extra)
174
 
    
175
 
    return ret
176
 
 
177
 
 
178
 
def get_missing_revisions_working(working):
179
 
    """ Get missing revisions in working tree.
180
 
    
181
 
    :return: a dictionary containing the needed infos
182
 
    """
183
 
    if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
184
 
        # function deprecated after 0.9
185
 
        from bzrlib.delta import compare_trees
186
 
 
187
 
    ret = {}
188
 
    branch = working.branch
189
 
    basis = working.basis_tree()
190
 
 
191
 
    if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
192
 
        delta = compare_trees(basis, working, want_unchanged=True)
193
 
    else:
194
 
        delta = working.changes_from(basis, want_unchanged=True)
195
 
 
196
 
    history = branch.revision_history()
197
 
    tree_last_id = working.last_revision()
198
 
 
199
 
    if len(history) and tree_last_id != history[-1]:
200
 
        tree_last_revno = branch.revision_id_to_revno(tree_last_id)
201
 
        missing_count = len(history) - tree_last_revno
202
 
        ret = missing_count
203
 
    
204
 
    return ret
205
 
 
206
 
 
207
 
def get_working_stats(working):
208
 
    """ Get statistics about a working tree.
209
 
    
210
 
    :return: a dictionary containing the needed infos
211
 
    """
212
 
    if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
213
 
        # function deprecated after 0.9
214
 
        from bzrlib.delta import compare_trees
215
 
    
216
 
    ret = {}
217
 
    basis = working.basis_tree()
218
 
 
219
 
    if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
220
 
        delta = compare_trees(basis, working, want_unchanged=True)
221
 
    else:
222
 
        delta = working.changes_from(basis, want_unchanged=True)
223
 
 
224
 
    ret['unchanged'] = len(delta.unchanged)
225
 
    ret['modified'] = len(delta.modified)
226
 
    ret['added'] = len(delta.added)
227
 
    ret['removed'] = len(delta.removed)
228
 
    ret['renamed'] = len(delta.renamed)
229
 
 
230
 
    ignore_cnt = unknown_cnt = 0
231
 
    for path in working.extras():
232
 
        if working.is_ignored(path):
233
 
            ignore_cnt += 1
234
 
        else:
235
 
            unknown_cnt += 1
236
 
    ret['unknown'] = unknown_cnt
237
 
    ret['ignored'] = ignore_cnt
238
 
 
239
 
    dir_cnt = 0
240
 
    for path, ie in working.iter_entries_by_dir():
241
 
        if ie.kind == 'directory':
242
 
            dir_cnt += 1
243
 
    ret['subdirs'] = dir_cnt
244
 
 
245
 
    return ret
246
 
 
247
 
def get_branch_stats(branch):
248
 
    """ Get statistics about a branch.
249
 
    
250
 
    :return: a dictionary containing the needed infos
251
 
    """
252
 
    ret = {}
253
 
    repository = branch.repository
254
 
    history = branch.revision_history()
255
 
 
256
 
    revno = len(history)
257
 
    ret['revno'] = revno 
258
 
    committers = {}
259
 
    for rev in history:
260
 
        committers[repository.get_revision(rev).committer] = True
261
 
    ret['commiters'] = len(committers)
262
 
    if revno > 0:
263
 
        firstrev = repository.get_revision(history[0])
264
 
        age = int((time.time() - firstrev.timestamp) / 3600 / 24)
265
 
        ret['age'] = '%d days'%age
266
 
        ret['firstrev'] = osutils.format_date(firstrev.timestamp,
267
 
                                              firstrev.timezone)
268
 
 
269
 
        lastrev = repository.get_revision(history[-1])
270
 
        ret['lastrev'] = osutils.format_date(lastrev.timestamp,
271
 
                                             lastrev.timezone)
272
 
 
273
 
    return ret
274
 
 
275
 
def get_repository_stats(repository):
276
 
    """ Get statistics about a repository.
277
 
    
278
 
    :return: a dictionary containing the needed infos
279
 
    """
280
 
    ret = {}
281
 
    if repository.bzrdir.root_transport.listable():
282
 
        c, t = repository._revision_store.total_size(repository.get_transaction())
283
 
        ret['revisions'] = c
284
 
        ret['size'] = '%d KiB'%t
285
 
 
286
 
    return ret
287
 
 
288
 
def diff_helper(tree, specific_files, external_diff_options, 
289
 
                    old_revision_spec=None, new_revision_spec=None,
290
 
                    old_label='a/', new_label='b/', output=None):
291
 
    """ Helper for diff.
292
 
 
293
 
    :param tree: a WorkingTree
294
 
 
295
 
    :param specific_files: the specific files to compare, or None
296
 
 
297
 
    :param external_diff_options: if non-None, run an external diff, and pass it these options
298
 
 
299
 
    :param old_revision_spec: if None, use basis tree as old revision, otherwise use the tree for the specified revision. 
300
 
 
301
 
    :param new_revision_spec:  if None, use working tree as new revision, otherwise use the tree for the specified revision.
302
 
    """
303
 
    
304
 
    if output == None:
305
 
        output = sys.stdout
306
 
    
307
 
    if old_revision_spec is None:
308
 
        old_tree = tree.basis_tree()
309
 
    else:
310
 
        old_tree = spec_tree(old_revision_spec)
311
 
 
312
 
    if new_revision_spec is None:
313
 
        new_tree = tree
314
 
    else:
315
 
        new_tree = spec_tree(new_revision_spec)
316
 
 
317
 
    return show_diff_trees(old_tree, new_tree, output, specific_files,
318
 
                           external_diff_options,
319
 
                           old_label=old_label, new_label=new_label)
320
 
 
321
 
def spec_tree(spec):
322
 
        revision_id = spec.in_store(tree.branch).rev_id
323
 
        return tree.branch.repository.revision_tree(revision_id)