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

  • Committer: Szilveszter Farkas (Phanatic)
  • Date: 2006-07-06 17:08:45 UTC
  • mto: (0.14.1 main) (93.1.1 win32.bialix)
  • mto: This revision was merged to the branch mainline in revision 83.
  • Revision ID: Szilveszter.Farkas@gmail.com-20060706170845-da015b629f5876a4
2006-07-06  Szilveszter Farkas <Szilveszter.Farkas@gmail.com>

    * I'm not dead :)
    * backend/errors.py: added some exceptions related to fileops.move()
    * backend/fileops.py: implemented move()
    * backend/info_helper.py: added diff_helper()
    * backend/info.py: implemented diff()

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic)
 
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
 
 
24
from errors import (NotBranchError)
 
25
 
 
26
def diff(revision=None, file_list=None, diff_options=None, prefix=None):
 
27
    """ Save the diff into a temporary file.
 
28
    
 
29
    :param revision: a list of revisions (one or two elements)
 
30
    
 
31
    :param file_list: list of files you want to diff
 
32
    
 
33
    :param diff_options: external diff options
 
34
    
 
35
    :param prefix: 0 - p0, 1 - p1, or specify prefixes in the form of old/:new/
 
36
    
 
37
    :return: path to the temporary file which contains the diff output
 
38
    """
 
39
    from tempfile import mkstemp
 
40
    
 
41
    from bzrlib.builtins import internal_tree_files
 
42
    from bzrlib.diff import show_diff_trees
 
43
    from bzrlib.workingtree import WorkingTree
 
44
    
 
45
    from info_helper import diff_helper
 
46
 
 
47
    if (prefix is None) or (prefix == '0'):
 
48
        # diff -p0 format
 
49
        old_label = ''
 
50
        new_label = ''
 
51
    elif prefix == '1':
 
52
        old_label = 'old/'
 
53
        new_label = 'new/'
 
54
    else:
 
55
        if not ':' in prefix:
 
56
            raise errors.BzrError("--diff-prefix expects two values separated by a colon")
 
57
        old_label, new_label = prefix.split(":")
 
58
    
 
59
    try:
 
60
        tree1, file_list = internal_tree_files(file_list)
 
61
        tree2 = None
 
62
        b = None
 
63
        b2 = None
 
64
    except errors.FileInWrongBranch:
 
65
        if len(file_list) != 2:
 
66
            raise errors.BzrCommandError("Files are in different branches")
 
67
 
 
68
        tree1, file1 = WorkingTree.open_containing(file_list[0])
 
69
        tree2, file2 = WorkingTree.open_containing(file_list[1])
 
70
    
 
71
        if file1 != "" or file2 != "":
 
72
            # FIXME diff those two files. rbc 20051123
 
73
            raise errors.BzrCommandError("Files are in different branches")
 
74
    
 
75
        file_list = None
 
76
    
 
77
    tmpfile = mkstemp(prefix='olive_')
 
78
    tmpfp = open(tmpfile[1], 'w')
 
79
    
 
80
    if revision is not None:
 
81
        if tree2 is not None:
 
82
            raise errors.BzrCommandError("Can't specify -r with two branches")
 
83
    
 
84
        if (len(revision) == 1) or (revision[1].spec is None):
 
85
            ret = diff_helper(tree1, file_list, diff_options,
 
86
                                   revision[0], 
 
87
                                   old_label=old_label, new_label=new_label,
 
88
                                   output=tmpfp)
 
89
        elif len(revision) == 2:
 
90
            ret = diff_helper(tree1, file_list, diff_options,
 
91
                                   revision[0], revision[1],
 
92
                                   old_label=old_label, new_label=new_label,
 
93
                                   output=tmpfp)
 
94
        else:
 
95
            raise errors.BzrCommandError('bzr diff --revision takes exactly one or two revision identifiers')
 
96
    else:
 
97
        if tree2 is not None:
 
98
            ret = show_diff_trees(tree1, tree2, tmpfp, 
 
99
                                   specific_files=file_list,
 
100
                                   external_diff_options=diff_options,
 
101
                                   old_label=old_label, new_label=new_label)
 
102
        else:
 
103
            ret = diff_helper(tree1, file_list, diff_options,
 
104
                                   old_label=old_label, new_label=new_label,
 
105
                                   output=tmpfp)
 
106
    
 
107
    tmpfp.close()
 
108
    
 
109
    if ret == 0:
 
110
        return False
 
111
    else:
 
112
        return tmpfile[1]
 
113
 
 
114
def info(location):
 
115
    """ Get info about branch, working tree, and repository
 
116
    
 
117
    :param location: the location of the branch/working tree/repository
 
118
    
 
119
    :return: the information in dictionary format
 
120
    
 
121
    The following informations are delivered (if available):
 
122
    ret['location']['lightcoroot']: Light checkout root
 
123
    ret['location']['sharedrepo']: Shared repository
 
124
    ret['location']['repobranch']: Repository branch
 
125
    ret['location']['cobranch']: Checkout of branch
 
126
    ret['location']['repoco']: Repository checkout
 
127
    ret['location']['coroot']: Checkout root
 
128
    ret['location']['branchroot']: Branch root
 
129
    ret['related']['parentbranch']: Parent branch
 
130
    ret['related']['publishbranch']: Publish to branch
 
131
    ret['format']['control']: Control format
 
132
    ret['format']['workingtree']: Working tree format
 
133
    ret['format']['branch']: Branch format
 
134
    ret['format']['repository']: Repository format
 
135
    ret['locking']['workingtree']: Working tree lock status
 
136
    ret['locking']['branch']: Branch lock status
 
137
    ret['locking']['repository']: Repository lock status
 
138
    ret['mrevbranch']['missing']: Missing revisions in branch
 
139
    ret['mrevworking']['missing']: Missing revisions in working tree
 
140
    ret['wtstats']['unchanged']: Unchanged files
 
141
    ret['wtstats']['modified']: Modified files
 
142
    ret['wtstats']['added']: Added files
 
143
    ret['wtstats']['removed']: Removed files
 
144
    ret['wtstats']['renamed']: Renamed files
 
145
    ret['wtstats']['unknown']: Unknown files
 
146
    ret['wtstats']['ignored']: Ingnored files
 
147
    ret['wtstats']['subdirs']: Versioned subdirectories
 
148
    ret['brstats']['revno']: Revisions in branch
 
149
    ret['brstats']['commiters']: Number of commiters
 
150
    ret['brstats']['age']: Age of branch in days
 
151
    ret['brstats']['firstrev']: Time of first revision
 
152
    ret['brstats']['lastrev']: Time of last revision
 
153
    ret['repstats']['revisions']: Revisions in repository
 
154
    ret['repstats']['size']: Size of repository in bytes
 
155
    """
 
156
    import bzrlib.bzrdir as bzrdir
 
157
    
 
158
    import info_helper
 
159
    
 
160
    ret = {}
 
161
    a_bzrdir = bzrdir.BzrDir.open_containing(location)[0]
 
162
    try:
 
163
        working = a_bzrdir.open_workingtree()
 
164
        working.lock_read()
 
165
        try:
 
166
            branch = working.branch
 
167
            repository = branch.repository
 
168
            control = working.bzrdir
 
169
 
 
170
            ret['location'] = info_helper.get_location_info(repository, branch, working)
 
171
            ret['related'] = info_helper.get_related_info(branch)
 
172
            ret['format'] = info_helper.get_format_info(control, repository, branch, working)
 
173
            ret['locking'] = info_helper.get_locking_info(repository, branch, working)
 
174
            ret['mrevbranch'] = info_helper.get_missing_revisions_branch(branch)
 
175
            ret['mrevworking'] = info_helper.get_missing_revisions_working(working)
 
176
            ret['wtstats'] = info_helper.get_working_stats(working)
 
177
            ret['brstats'] = info_helper.get_branch_stats(branch)
 
178
            ret['repstats'] = info_helper.get_repository_stats(repository)
 
179
        finally:
 
180
            working.unlock()
 
181
            return ret
 
182
        return
 
183
    except (errors.NoWorkingTree, errors.NotLocalUrl):
 
184
        pass
 
185
 
 
186
    try:
 
187
        branch = a_bzrdir.open_branch()
 
188
        branch.lock_read()
 
189
        try:
 
190
            ret['location'] = info_helper.get_location_info(repository, branch)
 
191
            ret['related'] = info_helper.get_related_info(branch)
 
192
            ret['format'] = info_helper.get_format_info(control, repository, branch)
 
193
            ret['locking'] = info_helper.get_locking_info(repository, branch)
 
194
            ret['mrevbranch'] = info_helper.get_missing_revisions_branch(branch)
 
195
            ret['brstats'] = info_helper.get_branch_stats(branch)
 
196
            ret['repstats'] = info_helper.get_repository_stats(repository)
 
197
        finally:
 
198
            branch.unlock()
 
199
            return ret
 
200
        return
 
201
    except errors.NotBranchError:
 
202
        pass
 
203
 
 
204
    try:
 
205
        repository = a_bzrdir.open_repository()
 
206
        repository.lock_read()
 
207
        try:
 
208
            ret['location'] = info_helper.get_location_info(repository)
 
209
            ret['format'] = info_helper.get_format_info(control, repository)
 
210
            ret['locking'] = info_helper.get_locking_info(repository)
 
211
            ret['repstats'] = info_helper.get_repository_stats(repository)
 
212
        finally:
 
213
            repository.unlock()
 
214
            return ret
 
215
        return
 
216
    except errors.NoRepositoryPresent:
 
217
        pass
 
218
 
 
219
def nick(branch, nickname=None):
 
220
    """ Get or set nickname.
 
221
    
 
222
    :param branch: path to the branch
 
223
    
 
224
    :param nickname: if specified, the nickname will be set
 
225
    
 
226
    :return: nickname
 
227
    """
 
228
    try:
 
229
        branch = Branch.open_containing(branch)[0]
 
230
    except errors.NotBranchError:
 
231
        raise NotBranchError
 
232
    
 
233
    if nickname is not None:
 
234
        branch.nick = nickname
 
235
 
 
236
    return branch.nick    
 
237
 
 
238
def revno(branch):
 
239
    """ Get current revision number for specified branch
 
240
    
 
241
    :param branch: path to the branch
 
242
    
 
243
    :return: revision number
 
244
    """
 
245
    try:
 
246
        revno = Branch.open_containing(branch)[0].revno()
 
247
    except errors.NotBranchError:
 
248
        raise NotBranchError
 
249
    else:
 
250
        return revno
 
251
 
 
252
def version():
 
253
    """ Get version information from bzr
 
254
    
 
255
    :return: bzrlib version
 
256
    """
 
257
    return bzrlib.__version__
 
258
 
 
259
def whoami(branch=None, email=False):
 
260
    """ Get user's data (name and email address)
 
261
    
 
262
    :param branch: if specified, the user's data will be looked up in the branch's config
 
263
    
 
264
    :param email: if True, only the email address will be returned
 
265
    
 
266
    :return: user info (only email address if email is True)
 
267
    """
 
268
    from bzrlib.workingtree import WorkingTree
 
269
    
 
270
    if branch is not None:
 
271
        try:
 
272
            b = WorkingTree.open_containing(u'.')[0].branch
 
273
            config = bzrlib.config.BranchConfig(b)
 
274
        except NotBranchError:
 
275
            config = bzrlib.config.GlobalConfig()
 
276
    else:
 
277
        config = bzrlib.config.GlobalConfig()
 
278
        
 
279
    if email:
 
280
        return config.user_email()
 
281
    else:
 
282
        return config.username()