/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

  • Committer: Szilveszter Farkas (Phanatic)
  • Date: 2006-09-27 19:11:59 UTC
  • mfrom: (0.8.90 merge)
  • mto: (93.1.1 win32.bialix)
  • mto: This revision was merged to the branch mainline in revision 103.
  • Revision ID: Szilveszter.Farkas@gmail.com-20060927191159-cc4e54f613575779
Merge all changes. Release 0.11.0.

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
 
 
21
import bzrlib
 
22
 
 
23
import bzrlib.osutils as osutils
 
24
 
 
25
from bzrlib.info import _repo_relpath
 
26
from bzrlib.missing import find_unmerged
 
27
 
 
28
def get_location_info(repository, branch=None, working=None):
 
29
    """ Get known locations for working, branch and repository.
 
30
    
 
31
    :return: a dictionary containing the needed infos
 
32
    """
 
33
    ret = {}
 
34
    repository_path = repository.bzrdir.root_transport.base
 
35
    if working and branch:
 
36
        working_path = working.bzrdir.root_transport.base
 
37
        branch_path = branch.bzrdir.root_transport.base
 
38
        if working_path != branch_path:
 
39
            # lightweight checkout
 
40
            ret['lightcoroot'] = working_path
 
41
            if repository.is_shared():
 
42
                # lightweight checkout of branch in shared repository
 
43
                ret['sharedrepo'] = repository_path
 
44
                ret['repobranch'] = _repo_relpath(repository_path, branch_path)
 
45
            else:
 
46
                # lightweight checkout of standalone branch
 
47
                ret['cobranch'] = branch_path
 
48
        elif repository.is_shared():
 
49
            # branch with tree inside shared repository
 
50
            ret['sharedrepo'] = repository_path
 
51
            ret['repoco'] = _repo_relpath(repository_path, branch_path)
 
52
        elif branch.get_bound_location():
 
53
            # normal checkout
 
54
            ret['coroot'] = working_path
 
55
            ret['cobranch'] = branch.get_bound_location()
 
56
        else:
 
57
            # standalone
 
58
            ret['branchroot'] = working_path
 
59
    elif branch:
 
60
        branch_path = branch.bzrdir.root_transport.base
 
61
        if repository.is_shared():
 
62
            # branch is part of shared repository
 
63
            ret['sharedrepo'] = repository_path
 
64
            ret['repobranch'] = _repo_relpath(repository_path, branch_path)
 
65
        else:
 
66
            # standalone branch
 
67
            ret['branchroot'] = branch_path
 
68
    else:
 
69
        # shared repository
 
70
        assert repository.is_shared()
 
71
        ret['sharedrepo'] = repository_path
 
72
    
 
73
    return ret
 
74
 
 
75
def get_related_info(branch):
 
76
    """ Get parent and push location of branch.
 
77
    
 
78
    :return: a dictionary containing the needed infos
 
79
    """
 
80
    ret = {}
 
81
    if branch.get_parent() or branch.get_push_location():
 
82
        if branch.get_parent():
 
83
            ret['parentbranch'] = branch.get_parent()
 
84
        if branch.get_push_location():
 
85
            ret['publishbranch'] = branch.get_push_location()
 
86
    
 
87
    return ret
 
88
 
 
89
 
 
90
def get_format_info(control=None, repository=None, branch=None, working=None):
 
91
    """ Get known formats for control, working, branch and repository.
 
92
    
 
93
    :return: a dictionary containing the needed infos
 
94
    """
 
95
    ret = {}
 
96
    if control:
 
97
        ret['control'] = control._format.get_format_description()
 
98
    if working:
 
99
        ret['workingtree'] = working._format.get_format_description()
 
100
    if branch:
 
101
        ret['branch'] = branch._format.get_format_description()
 
102
    if repository:
 
103
        ret['repository'] = repository._format.get_format_description()
 
104
    
 
105
    return ret
 
106
 
 
107
 
 
108
def get_locking_info(repository, branch=None, working=None):
 
109
    """ Get locking status of working, branch and repository.
 
110
    
 
111
    :return: a dictionary containing the needed infos
 
112
    """
 
113
    ret = {}
 
114
    if (repository.get_physical_lock_status() or
 
115
        (branch and branch.get_physical_lock_status()) or
 
116
        (working and working.get_physical_lock_status())):
 
117
        if working:
 
118
            if working.get_physical_lock_status():
 
119
                status = 'locked'
 
120
            else:
 
121
                status = 'unlocked'
 
122
            ret['workingtree'] = status
 
123
        if branch:
 
124
            if branch.get_physical_lock_status():
 
125
                status = 'locked'
 
126
            else:
 
127
                status = 'unlocked'
 
128
            ret['branch'] = status
 
129
        if repository:
 
130
            if repository.get_physical_lock_status():
 
131
                status = 'locked'
 
132
            else:
 
133
                status = 'unlocked'
 
134
            ret['repository'] = status
 
135
    
 
136
    return ret
 
137
 
 
138
def get_missing_revisions_branch(branch):
 
139
    """ Get missing master revisions in branch.
 
140
    
 
141
    :return: a dictionary containing the needed infos
 
142
    """
 
143
    ret = {}
 
144
    # Try with inaccessible branch ?
 
145
    master = branch.get_master_branch()
 
146
    if master:
 
147
        local_extra, remote_extra = find_unmerged(branch, master)
 
148
        if remote_extra:
 
149
            ret = len(remote_extra)
 
150
    
 
151
    return ret
 
152
 
 
153
 
 
154
def get_missing_revisions_working(working):
 
155
    """ Get missing revisions in working tree.
 
156
    
 
157
    :return: a dictionary containing the needed infos
 
158
    """
 
159
    if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
 
160
        # function deprecated after 0.9
 
161
        from bzrlib.delta import compare_trees
 
162
 
 
163
    ret = {}
 
164
    branch = working.branch
 
165
    basis = working.basis_tree()
 
166
    work_inv = working.inventory
 
167
    
 
168
    if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
 
169
        delta = compare_trees(basis, working, want_unchanged=True)
 
170
    else:
 
171
        delta = working.changes_from(basis, want_unchanged=True)
 
172
 
 
173
    history = branch.revision_history()
 
174
    tree_last_id = working.last_revision()
 
175
 
 
176
    if len(history) and tree_last_id != history[-1]:
 
177
        tree_last_revno = branch.revision_id_to_revno(tree_last_id)
 
178
        missing_count = len(history) - tree_last_revno
 
179
        ret = missing_count
 
180
    
 
181
    return ret
 
182
 
 
183
 
 
184
def get_working_stats(working):
 
185
    """ Get statistics about a working tree.
 
186
    
 
187
    :return: a dictionary containing the needed infos
 
188
    """
 
189
    if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
 
190
        # function deprecated after 0.9
 
191
        from bzrlib.delta import compare_trees
 
192
    
 
193
    ret = {}
 
194
    basis = working.basis_tree()
 
195
    work_inv = working.inventory
 
196
    
 
197
    if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
 
198
        delta = compare_trees(basis, working, want_unchanged=True)
 
199
    else:
 
200
        delta = working.changes_from(basis, want_unchanged=True)
 
201
 
 
202
    ret['unchanged'] = len(delta.unchanged)
 
203
    ret['modified'] = len(delta.modified)
 
204
    ret['added'] = len(delta.added)
 
205
    ret['removed'] = len(delta.removed)
 
206
    ret['renamed'] = len(delta.renamed)
 
207
 
 
208
    ignore_cnt = unknown_cnt = 0
 
209
    for path in working.extras():
 
210
        if working.is_ignored(path):
 
211
            ignore_cnt += 1
 
212
        else:
 
213
            unknown_cnt += 1
 
214
    ret['unknown'] = unknown_cnt
 
215
    ret['ignored'] = ignore_cnt
 
216
 
 
217
    dir_cnt = 0
 
218
    for file_id in work_inv:
 
219
        if work_inv.get_file_kind(file_id) == 'directory':
 
220
            dir_cnt += 1
 
221
    ret['subdirs'] = dir_cnt
 
222
 
 
223
    return ret
 
224
 
 
225
def get_branch_stats(branch):
 
226
    """ Get statistics about a branch.
 
227
    
 
228
    :return: a dictionary containing the needed infos
 
229
    """
 
230
    ret = {}
 
231
    repository = branch.repository
 
232
    history = branch.revision_history()
 
233
 
 
234
    revno = len(history)
 
235
    ret['revno'] = revno 
 
236
    committers = {}
 
237
    for rev in history:
 
238
        committers[repository.get_revision(rev).committer] = True
 
239
    ret['commiters'] = len(committers)
 
240
    if revno > 0:
 
241
        firstrev = repository.get_revision(history[0])
 
242
        age = int((time.time() - firstrev.timestamp) / 3600 / 24)
 
243
        ret['age'] = age
 
244
        ret['firstrev'] = osutils.format_date(firstrev.timestamp,
 
245
                                              firstrev.timezone)
 
246
 
 
247
        lastrev = repository.get_revision(history[-1])
 
248
        ret['lastrev'] = osutils.format_date(lastrev.timestamp,
 
249
                                             lastrev.timezone)
 
250
 
 
251
    return ret
 
252
 
 
253
def get_repository_stats(repository):
 
254
    """ Get statistics about a repository.
 
255
    
 
256
    :return: a dictionary containing the needed infos
 
257
    """
 
258
    ret = {}
 
259
    if repository.bzrdir.root_transport.listable():
 
260
        c, t = repository._revision_store.total_size(repository.get_transaction())
 
261
        ret['revisions'] = c
 
262
        ret['size'] = t
 
263
 
 
264
    return ret
 
265
 
 
266
def diff_helper(tree, specific_files, external_diff_options, 
 
267
                    old_revision_spec=None, new_revision_spec=None,
 
268
                    old_label='a/', new_label='b/', output=None):
 
269
    """ Helper for diff.
 
270
 
 
271
    :param tree: a WorkingTree
 
272
 
 
273
    :param specific_files: the specific files to compare, or None
 
274
 
 
275
    :param external_diff_options: if non-None, run an external diff, and pass it these options
 
276
 
 
277
    :param old_revision_spec: if None, use basis tree as old revision, otherwise use the tree for the specified revision. 
 
278
 
 
279
    :param new_revision_spec:  if None, use working tree as new revision, otherwise use the tree for the specified revision.
 
280
    """
 
281
    import sys
 
282
    
 
283
    from bzrlib.diff import show_diff_trees
 
284
    
 
285
    if output == None:
 
286
        output = sys.stdout
 
287
    
 
288
    def spec_tree(spec):
 
289
        revision_id = spec.in_store(tree.branch).rev_id
 
290
        return tree.branch.repository.revision_tree(revision_id)
 
291
    
 
292
    if old_revision_spec is None:
 
293
        old_tree = tree.basis_tree()
 
294
    else:
 
295
        old_tree = spec_tree(old_revision_spec)
 
296
 
 
297
    if new_revision_spec is None:
 
298
        new_tree = tree
 
299
    else:
 
300
        new_tree = spec_tree(new_revision_spec)
 
301
 
 
302
    return show_diff_trees(old_tree, new_tree, output, specific_files,
 
303
                           external_diff_options,
 
304
                           old_label=old_label, new_label=new_label)