/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/backend/info.py

  • Committer: Jelmer Vernooij
  • Date: 2012-07-09 15:23:26 UTC
  • mto: This revision was merged to the branch mainline in revision 794.
  • Revision ID: jelmer@samba.org-20120709152326-dzxb8zoz0btull7n
Remove bzr-notify.

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 bzrlib
20
 
import bzrlib.errors as errors
21
 
 
22
 
from bzrlib.branch import Branch
23
 
from bzrlib.workingtree import WorkingTree
24
 
 
25
 
from errors import (DifferentBranchesError, NotBranchError, PrefixFormatError,
26
 
                    RevisionValueError)
27
 
 
28
 
def diff(revision=None, file_list=None, diff_options=None, prefix=None):
29
 
    """ Save the diff into a temporary file.
30
 
    
31
 
    :param revision: a list of revision numbers (one or two elements)
32
 
    
33
 
    :param file_list: list of files you want to diff
34
 
    
35
 
    :param diff_options: external diff options
36
 
    
37
 
    :param prefix: 0 - p0, 1 - p1, or specify prefixes in the form of old/:new/
38
 
    
39
 
    :return: path to the temporary file which contains the diff output (the frontend has to remove it!)
40
 
    """
41
 
    from tempfile import mkstemp
42
 
    
43
 
    from bzrlib.builtins import internal_tree_files
44
 
    from bzrlib.diff import show_diff_trees
45
 
    from bzrlib.revisionspec import RevisionSpec_int
46
 
    from bzrlib.workingtree import WorkingTree
47
 
    
48
 
    from info_helper import diff_helper
49
 
 
50
 
    if (prefix is None) or (prefix == '0'):
51
 
        # diff -p0 format
52
 
        old_label = ''
53
 
        new_label = ''
54
 
    elif prefix == '1':
55
 
        old_label = 'old/'
56
 
        new_label = 'new/'
57
 
    else:
58
 
        if not ':' in prefix:
59
 
            raise PrefixFormatError
60
 
        old_label, new_label = prefix.split(":")
61
 
    
62
 
    try:
63
 
        tree1, file_list = internal_tree_files(file_list)
64
 
        tree2 = None
65
 
        b = None
66
 
        b2 = None
67
 
    except errors.FileInWrongBranch:
68
 
        if len(file_list) != 2:
69
 
            raise DifferentBranchesError
70
 
 
71
 
        tree1, file1 = WorkingTree.open_containing(file_list[0])
72
 
        tree2, file2 = WorkingTree.open_containing(file_list[1])
73
 
    
74
 
        if file1 != "" or file2 != "":
75
 
            raise DifferentBranchesError
76
 
    
77
 
        file_list = None
78
 
    
79
 
    tmpfile = mkstemp(prefix='olive_')
80
 
    tmpfp = open(tmpfile[1], 'w')
81
 
    
82
 
    if revision is not None:
83
 
        if tree2 is not None:
84
 
            raise RevisionValueError
85
 
    
86
 
        if len(revision) >= 1:
87
 
            revision[0] = RevisionSpec_int(revision[0])
88
 
        if len(revision) == 2:
89
 
            revision[1] = RevisionSpec_int(revision[1])
90
 
        
91
 
        if (len(revision) == 1) or (revision[1].spec is None):
92
 
            ret = diff_helper(tree1, file_list, diff_options,
93
 
                              revision[0], 
94
 
                              old_label=old_label, new_label=new_label,
95
 
                              output=tmpfp)
96
 
        elif len(revision) == 2:
97
 
            ret = diff_helper(tree1, file_list, diff_options,
98
 
                              revision[0], revision[1],
99
 
                              old_label=old_label, new_label=new_label,
100
 
                              output=tmpfp)
101
 
        else:
102
 
            raise RevisionValueError
103
 
    else:
104
 
        if tree2 is not None:
105
 
            ret = show_diff_trees(tree1, tree2, tmpfp, 
106
 
                                  specific_files=file_list,
107
 
                                  external_diff_options=diff_options,
108
 
                                  old_label=old_label, new_label=new_label)
109
 
        else:
110
 
            ret = diff_helper(tree1, file_list, diff_options,
111
 
                              old_label=old_label, new_label=new_label,
112
 
                              output=tmpfp)
113
 
    
114
 
    tmpfp.close()
115
 
    
116
 
    if ret == 0:
117
 
        return False
118
 
    else:
119
 
        return tmpfile[1]
120
 
 
121
 
def get_push_location(location):
122
 
    """ Get the stored push location of a branch.
123
 
    
124
 
    :param location: the path to the branch
125
 
    
126
 
    :return: the stored location
127
 
    """
128
 
    from bzrlib.branch import Branch
129
 
    
130
 
    try:
131
 
        branch = Branch.open_containing(location)[0]
132
 
    except errors.NotBranchError:
133
 
        raise NotBranchError(location)
134
 
    except:
135
 
        raise
136
 
    
137
 
    return branch.get_push_location()
138
 
 
139
 
def info(location):
140
 
    """ Get info about branch, working tree, and repository
141
 
    
142
 
    :param location: the location of the branch/working tree/repository
143
 
    
144
 
    :return: the information in dictionary format
145
 
    
146
 
    The following informations are delivered (if available):
147
 
    ret['location']['lightcoroot']: Light checkout root
148
 
    ret['location']['sharedrepo']: Shared repository
149
 
    ret['location']['repobranch']: Repository branch
150
 
    ret['location']['cobranch']: Checkout of branch
151
 
    ret['location']['repoco']: Repository checkout
152
 
    ret['location']['coroot']: Checkout root
153
 
    ret['location']['branchroot']: Branch root
154
 
    ret['related']['parentbranch']: Parent branch
155
 
    ret['related']['publishbranch']: Publish to branch
156
 
    ret['format']['control']: Control format
157
 
    ret['format']['workingtree']: Working tree format
158
 
    ret['format']['branch']: Branch format
159
 
    ret['format']['repository']: Repository format
160
 
    ret['locking']['workingtree']: Working tree lock status
161
 
    ret['locking']['branch']: Branch lock status
162
 
    ret['locking']['repository']: Repository lock status
163
 
    ret['mrevbranch']['missing']: Missing revisions in branch
164
 
    ret['mrevworking']['missing']: Missing revisions in working tree
165
 
    ret['wtstats']['unchanged']: Unchanged files
166
 
    ret['wtstats']['modified']: Modified files
167
 
    ret['wtstats']['added']: Added files
168
 
    ret['wtstats']['removed']: Removed files
169
 
    ret['wtstats']['renamed']: Renamed files
170
 
    ret['wtstats']['unknown']: Unknown files
171
 
    ret['wtstats']['ignored']: Ingnored files
172
 
    ret['wtstats']['subdirs']: Versioned subdirectories
173
 
    ret['brstats']['revno']: Revisions in branch
174
 
    ret['brstats']['commiters']: Number of commiters
175
 
    ret['brstats']['age']: Age of branch in days
176
 
    ret['brstats']['firstrev']: Time of first revision
177
 
    ret['brstats']['lastrev']: Time of last revision
178
 
    ret['repstats']['revisions']: Revisions in repository
179
 
    ret['repstats']['size']: Size of repository in bytes
180
 
    """
181
 
    import bzrlib.bzrdir as bzrdir
182
 
    
183
 
    import info_helper
184
 
    
185
 
    ret = {}
186
 
    a_bzrdir = bzrdir.BzrDir.open_containing(location)[0]
187
 
    try:
188
 
        working = a_bzrdir.open_workingtree()
189
 
        working.lock_read()
190
 
        try:
191
 
            branch = working.branch
192
 
            repository = branch.repository
193
 
            control = working.bzrdir
194
 
 
195
 
            ret['location'] = info_helper.get_location_info(repository, branch, working)
196
 
            ret['related'] = info_helper.get_related_info(branch)
197
 
            ret['format'] = info_helper.get_format_info(control, repository, branch, working)
198
 
            ret['locking'] = info_helper.get_locking_info(repository, branch, working)
199
 
            ret['mrevbranch'] = info_helper.get_missing_revisions_branch(branch)
200
 
            ret['mrevworking'] = info_helper.get_missing_revisions_working(working)
201
 
            ret['wtstats'] = info_helper.get_working_stats(working)
202
 
            ret['brstats'] = info_helper.get_branch_stats(branch)
203
 
            ret['repstats'] = info_helper.get_repository_stats(repository)
204
 
        finally:
205
 
            working.unlock()
206
 
            return ret
207
 
        return
208
 
    except (errors.NoWorkingTree, errors.NotLocalUrl):
209
 
        pass
210
 
 
211
 
    try:
212
 
        branch = a_bzrdir.open_branch()
213
 
        branch.lock_read()
214
 
        try:
215
 
            ret['location'] = info_helper.get_location_info(repository, branch)
216
 
            ret['related'] = info_helper.get_related_info(branch)
217
 
            ret['format'] = info_helper.get_format_info(control, repository, branch)
218
 
            ret['locking'] = info_helper.get_locking_info(repository, branch)
219
 
            ret['mrevbranch'] = info_helper.get_missing_revisions_branch(branch)
220
 
            ret['brstats'] = info_helper.get_branch_stats(branch)
221
 
            ret['repstats'] = info_helper.get_repository_stats(repository)
222
 
        finally:
223
 
            branch.unlock()
224
 
            return ret
225
 
        return
226
 
    except errors.NotBranchError:
227
 
        pass
228
 
 
229
 
    try:
230
 
        repository = a_bzrdir.open_repository()
231
 
        repository.lock_read()
232
 
        try:
233
 
            ret['location'] = info_helper.get_location_info(repository)
234
 
            ret['format'] = info_helper.get_format_info(control, repository)
235
 
            ret['locking'] = info_helper.get_locking_info(repository)
236
 
            ret['repstats'] = info_helper.get_repository_stats(repository)
237
 
        finally:
238
 
            repository.unlock()
239
 
            return ret
240
 
        return
241
 
    except errors.NoRepositoryPresent:
242
 
        pass
243
 
 
244
 
def is_checkout(location):
245
 
    """ Check if the location is a checkout.
246
 
    
247
 
    :param location: the location you want to check
248
 
    
249
 
    :return: True or False respectively
250
 
    """
251
 
    try:
252
 
        branch = Branch.open_containing(location)[0]
253
 
    except errors.NotBranchError:
254
 
        raise NotBranchError
255
 
    
256
 
    try:
257
 
        working = WorkingTree.open_containing(location)[0]
258
 
    except:
259
 
        raise
260
 
    
261
 
    working_path = working.bzrdir.root_transport.base
262
 
    branch_path = branch.bzrdir.root_transport.base
263
 
    
264
 
    if working_path != branch_path:
265
 
        # lightweight checkout
266
 
        return True
267
 
    elif branch.get_bound_location():
268
 
        # checkout
269
 
        return True
270
 
    else:
271
 
        return False
272
 
 
273
 
 
274
 
def log(location, timezone='original', verbose=False, show_ids=False,
275
 
        forward=False, revision=None, log_format=None, message=None,
276
 
        long=False, short=False, line=False):
277
 
    """ Print log into a temporary file.
278
 
    
279
 
    :param location: location of local/remote branch or file
280
 
    
281
 
    :param timzone: requested timezone
282
 
    
283
 
    :param verbose: verbose output
284
 
    
285
 
    :param show_ids:
286
 
    
287
 
    :param forward: if True, start from the earliest entry
288
 
    
289
 
    :param revision: revision range as a list ([from, to])
290
 
    
291
 
    :param log_format: line, short, long
292
 
    
293
 
    :param message: show revisions whose message matches this regexp
294
 
    
295
 
    :param long: long log format
296
 
    
297
 
    :param short: short log format
298
 
    
299
 
    :param line: line log format
300
 
    
301
 
    :return: full path to the temporary file containing the log (the frontend has to remove it!)
302
 
    """
303
 
    from tempfile import mkstemp
304
 
    
305
 
    from bzrlib import bzrdir    
306
 
    from bzrlib.builtins import get_log_format
307
 
    from bzrlib.log import log_formatter, show_log
308
 
    from bzrlib.revisionspec import RevisionSpec_int
309
 
    
310
 
    assert message is None or isinstance(message, basestring), \
311
 
        "invalid message argument %r" % message
312
 
    direction = (forward and 'forward') or 'reverse'
313
 
        
314
 
    # log everything
315
 
    file_id = None
316
 
    
317
 
    # find the file id to log:
318
 
    dir, fp = bzrdir.BzrDir.open_containing(location)
319
 
    b = dir.open_branch()
320
 
    if fp != '':
321
 
        try:
322
 
            # might be a tree:
323
 
            inv = dir.open_workingtree().inventory
324
 
        except (errors.NotBranchError, errors.NotLocalUrl):
325
 
            # either no tree, or is remote.
326
 
            inv = b.basis_tree().inventory
327
 
        file_id = inv.path2id(fp)
328
 
 
329
 
    if revision is not None:
330
 
        if len(revision) >= 1:
331
 
            revision[0] = RevisionSpec_int(revision[0])
332
 
        if len(revision) == 2:
333
 
            revision[1] = RevisionSpec_int(revision[1])
334
 
    
335
 
    if revision is None:
336
 
        rev1 = None
337
 
        rev2 = None
338
 
    elif len(revision) == 1:
339
 
        rev1 = rev2 = revision[0].in_history(b).revno
340
 
    elif len(revision) == 2:
341
 
        if revision[0].spec is None:
342
 
            # missing begin-range means first revision
343
 
            rev1 = 1
344
 
        else:
345
 
            rev1 = revision[0].in_history(b).revno
346
 
 
347
 
        if revision[1].spec is None:
348
 
            # missing end-range means last known revision
349
 
            rev2 = b.revno()
350
 
        else:
351
 
            rev2 = revision[1].in_history(b).revno
352
 
    else:
353
 
        raise RevisionValueError
354
 
 
355
 
    # By this point, the revision numbers are converted to the +ve
356
 
    # form if they were supplied in the -ve form, so we can do
357
 
    # this comparison in relative safety
358
 
    if rev1 > rev2:
359
 
        (rev2, rev1) = (rev1, rev2)
360
 
 
361
 
    if (log_format == None):
362
 
        default = b.get_config().log_format()
363
 
        log_format = get_log_format(long=long, short=short, line=line, 
364
 
                                    default=default)
365
 
    
366
 
    tmpfile = mkstemp(prefix='olive_')
367
 
    tmpfp = open(tmpfile[1], 'w')
368
 
    
369
 
    lf = log_formatter(log_format,
370
 
                       show_ids=show_ids,
371
 
                       to_file=tmpfp,
372
 
                       show_timezone=timezone)
373
 
 
374
 
    show_log(b,
375
 
             lf,
376
 
             file_id,
377
 
             verbose=verbose,
378
 
             direction=direction,
379
 
             start_revision=rev1,
380
 
             end_revision=rev2,
381
 
             search=message)
382
 
    
383
 
    tmpfp.close()
384
 
    return tmpfile[1]
385
 
 
386
 
def nick(branch, nickname=None):
387
 
    """ Get or set nickname.
388
 
    
389
 
    :param branch: path to the branch
390
 
    
391
 
    :param nickname: if specified, the nickname will be set
392
 
    
393
 
    :return: nickname
394
 
    """
395
 
    try:
396
 
        branch = Branch.open_containing(branch)[0]
397
 
    except errors.NotBranchError:
398
 
        raise NotBranchError
399
 
    
400
 
    if nickname is not None:
401
 
        branch.nick = nickname
402
 
 
403
 
    return branch.nick    
404
 
 
405
 
def revno(branch):
406
 
    """ Get current revision number for specified branch
407
 
    
408
 
    :param branch: path to the branch
409
 
    
410
 
    :return: revision number
411
 
    """
412
 
    try:
413
 
        revno = Branch.open_containing(branch)[0].revno()
414
 
    except errors.NotBranchError:
415
 
        raise NotBranchError
416
 
    else:
417
 
        return revno
418
 
 
419
 
def version():
420
 
    """ Get version information from bzr
421
 
    
422
 
    :return: bzrlib version
423
 
    """
424
 
    return bzrlib.__version__
425
 
 
426
 
def whoami(branch=None, email=False):
427
 
    """ Get user's data (name and email address)
428
 
    
429
 
    :param branch: if specified, the user's data will be looked up in the branch's config
430
 
    
431
 
    :param email: if True, only the email address will be returned
432
 
    
433
 
    :return: user info (only email address if email is True)
434
 
    """
435
 
    from bzrlib.workingtree import WorkingTree
436
 
    
437
 
    if branch is not None:
438
 
        try:
439
 
            b = WorkingTree.open_containing(u'.')[0].branch
440
 
            config = bzrlib.config.BranchConfig(b)
441
 
        except NotBranchError:
442
 
            config = bzrlib.config.GlobalConfig()
443
 
    else:
444
 
        config = bzrlib.config.GlobalConfig()
445
 
        
446
 
    if email:
447
 
        return config.user_email()
448
 
    else:
449
 
        return config.username()