/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 __init__.py

  • Committer: Jelmer Vernooij
  • Date: 2007-02-03 10:45:37 UTC
  • Revision ID: jelmer@samba.org-20070203104537-hbnjve7vmv1fm1pw
Actually add the preference tests this time (forgot to ran 'bzr add'...)

Move more files to the top-level directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This program is free software; you can redistribute it and/or modify
 
2
# it under the terms of the GNU General Public License as published by
 
3
# the Free Software Foundation; either version 2 of the License, or
 
4
# (at your option) any later version.
 
5
 
 
6
# This program is distributed in the hope that it will be useful,
 
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
9
# GNU General Public License for more details.
 
10
 
 
11
# You should have received a copy of the GNU General Public License
 
12
# along with this program; if not, write to the Free Software
 
13
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
14
 
 
15
"""GTK+ frontends to Bazaar commands """
 
16
 
 
17
import bzrlib
 
18
 
 
19
__version__ = '0.15.0'
 
20
version_info = tuple(int(n) for n in __version__.split('.'))
 
21
 
 
22
 
 
23
def check_bzrlib_version(desired):
 
24
    """Check that bzrlib is compatible.
 
25
 
 
26
    If version is < bzr-gtk version, assume incompatible.
 
27
    If version == bzr-gtk version, assume completely compatible
 
28
    If version == bzr-gtk version + 1, assume compatible, with deprecations
 
29
    Otherwise, assume incompatible.
 
30
    """
 
31
    desired_plus = (desired[0], desired[1]+1)
 
32
    bzrlib_version = bzrlib.version_info[:2]
 
33
    if bzrlib_version == desired:
 
34
        return
 
35
    try:
 
36
        from bzrlib.trace import warning
 
37
    except ImportError:
 
38
        # get the message out any way we can
 
39
        from warnings import warn as warning
 
40
    if bzrlib_version < desired:
 
41
        warning('Installed bzr version %s is too old to be used with bzr-gtk'
 
42
                ' %s.' % (bzrlib.__version__, __version__))
 
43
        # Not using BzrNewError, because it may not exist.
 
44
        raise Exception, ('Version mismatch', version_info)
 
45
    else:
 
46
        warning('bzr-gtk is not up to date with installed bzr version %s.'
 
47
                ' \nThere should be a newer version available, e.g. %i.%i.' 
 
48
                % (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
 
49
        if bzrlib_version != desired_plus:
 
50
            raise Exception, 'Version mismatch'
 
51
 
 
52
 
 
53
check_bzrlib_version(version_info[:2])
 
54
 
 
55
from bzrlib import errors
 
56
from bzrlib.commands import Command, register_command, display_command
 
57
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
 
58
from bzrlib.commands import Command, register_command
 
59
from bzrlib.option import Option
 
60
from bzrlib.branch import Branch
 
61
from bzrlib.workingtree import WorkingTree
 
62
from bzrlib.bzrdir import BzrDir
 
63
 
 
64
import os.path
 
65
 
 
66
def import_pygtk():
 
67
    try:
 
68
        import pygtk
 
69
    except ImportError:
 
70
        raise errors.BzrCommandError("PyGTK not installed.")
 
71
    pygtk.require('2.0')
 
72
    return pygtk
 
73
 
 
74
 
 
75
def set_ui_factory():
 
76
    pygtk = import_pygtk()
 
77
    from ui import GtkUIFactory
 
78
    import bzrlib.ui
 
79
    bzrlib.ui.ui_factory = GtkUIFactory()
 
80
 
 
81
 
 
82
class cmd_gbranch(Command):
 
83
    """GTK+ branching.
 
84
    
 
85
    """
 
86
 
 
87
    def run(self):
 
88
        pygtk = import_pygtk()
 
89
        try:
 
90
            import gtk
 
91
        except RuntimeError, e:
 
92
            if str(e) == "could not open display":
 
93
                raise NoDisplayError
 
94
 
 
95
        from bzrlib.plugins.gtk.branch import BranchDialog
 
96
 
 
97
        set_ui_factory()
 
98
        dialog = BranchDialog(os.path.abspath('.'))
 
99
        dialog.run()
 
100
 
 
101
register_command(cmd_gbranch)
 
102
 
 
103
class cmd_gcheckout(Command):
 
104
    """ GTK+ checkout.
 
105
    
 
106
    """
 
107
    
 
108
    def run(self):
 
109
        pygtk = import_pygtk()
 
110
        try:
 
111
            import gtk
 
112
        except RuntimeError, e:
 
113
            if str(e) == "could not open display":
 
114
                raise NoDisplayError
 
115
 
 
116
        from bzrlib.plugins.gtk.checkout import CheckoutDialog
 
117
 
 
118
        set_ui_factory()
 
119
        dialog = CheckoutDialog(os.path.abspath('.'))
 
120
        dialog.run()
 
121
 
 
122
register_command(cmd_gcheckout)
 
123
 
 
124
class cmd_gpush(Command):
 
125
    """ GTK+ push.
 
126
    
 
127
    """
 
128
    takes_args = [ "location?" ]
 
129
    
 
130
    def run(self, location="."):
 
131
        (branch, path) = Branch.open_containing(location)
 
132
        
 
133
        pygtk = import_pygtk()
 
134
        try:
 
135
            import gtk
 
136
        except RuntimeError, e:
 
137
            if str(e) == "could not open display":
 
138
                raise NoDisplayError
 
139
 
 
140
        from push import PushDialog
 
141
 
 
142
        set_ui_factory()
 
143
        dialog = PushDialog(branch)
 
144
        dialog.run()
 
145
 
 
146
register_command(cmd_gpush)
 
147
 
 
148
class cmd_gdiff(Command):
 
149
    """Show differences in working tree in a GTK+ Window.
 
150
    
 
151
    Otherwise, all changes for the tree are listed.
 
152
    """
 
153
    takes_args = ['filename?']
 
154
    takes_options = ['revision']
 
155
 
 
156
    @display_command
 
157
    def run(self, revision=None, filename=None):
 
158
        set_ui_factory()
 
159
        wt = WorkingTree.open_containing(".")[0]
 
160
        branch = wt.branch
 
161
        if revision is not None:
 
162
            if len(revision) == 1:
 
163
                tree1 = wt
 
164
                revision_id = revision[0].in_history(branch).rev_id
 
165
                tree2 = branch.repository.revision_tree(revision_id)
 
166
            elif len(revision) == 2:
 
167
                revision_id_0 = revision[0].in_history(branch).rev_id
 
168
                tree2 = branch.repository.revision_tree(revision_id_0)
 
169
                revision_id_1 = revision[1].in_history(branch).rev_id
 
170
                tree1 = branch.repository.revision_tree(revision_id_1)
 
171
        else:
 
172
            tree1 = wt
 
173
            tree2 = tree1.basis_tree()
 
174
 
 
175
        from viz.diffwin import DiffWindow
 
176
        import gtk
 
177
        window = DiffWindow()
 
178
        window.connect("destroy", lambda w: gtk.main_quit())
 
179
        window.set_diff("Working Tree", tree1, tree2)
 
180
        if filename is not None:
 
181
            tree_filename = wt.relpath(filename)
 
182
            try:
 
183
                window.set_file(tree_filename)
 
184
            except NoSuchFile:
 
185
                if (tree1.inventory.path2id(tree_filename) is None and 
 
186
                    tree2.inventory.path2id(tree_filename) is None):
 
187
                    raise NotVersionedError(filename)
 
188
                raise BzrCommandError('No changes found for file "%s"' % 
 
189
                                      filename)
 
190
        window.show()
 
191
 
 
192
        gtk.main()
 
193
 
 
194
register_command(cmd_gdiff)
 
195
 
 
196
class cmd_visualise(Command):
 
197
    """Graphically visualise this branch.
 
198
 
 
199
    Opens a graphical window to allow you to see the history of the branch
 
200
    and relationships between revisions in a visual manner,
 
201
 
 
202
    The default starting point is latest revision on the branch, you can
 
203
    specify a starting point with -r revision.
 
204
    """
 
205
    takes_options = [
 
206
        "revision",
 
207
        Option('limit', "maximum number of revisions to display",
 
208
               int, 'count')]
 
209
    takes_args = [ "location?" ]
 
210
    aliases = [ "visualize", "vis", "viz" ]
 
211
 
 
212
    def run(self, location=".", revision=None, limit=None):
 
213
        set_ui_factory()
 
214
        (branch, path) = Branch.open_containing(location)
 
215
        branch.lock_read()
 
216
        branch.repository.lock_read()
 
217
        try:
 
218
            if revision is None:
 
219
                revid = branch.last_revision()
 
220
                if revid is None:
 
221
                    return
 
222
            else:
 
223
                (revno, revid) = revision[0].in_history(branch)
 
224
 
 
225
            from viz.bzrkapp import BzrkApp
 
226
                
 
227
            app = BzrkApp()
 
228
            app.show(branch, revid, limit)
 
229
        finally:
 
230
            branch.repository.unlock()
 
231
            branch.unlock()
 
232
        app.main()
 
233
 
 
234
 
 
235
register_command(cmd_visualise)
 
236
 
 
237
class cmd_gannotate(Command):
 
238
    """GTK+ annotate.
 
239
    
 
240
    Browse changes to FILENAME line by line in a GTK+ window.
 
241
    """
 
242
 
 
243
    takes_args = ["filename", "line?"]
 
244
    takes_options = [
 
245
        Option("all", help="show annotations on all lines"),
 
246
        Option("plain", help="don't highlight annotation lines"),
 
247
        Option("line", type=int, argname="lineno",
 
248
               help="jump to specified line number"),
 
249
        "revision",
 
250
    ]
 
251
    aliases = ["gblame", "gpraise"]
 
252
    
 
253
    def run(self, filename, all=False, plain=False, line='1', revision=None):
 
254
        pygtk = import_pygtk()
 
255
 
 
256
        try:
 
257
            import gtk
 
258
        except RuntimeError, e:
 
259
            if str(e) == "could not open display":
 
260
                raise NoDisplayError
 
261
        set_ui_factory()
 
262
 
 
263
        try:
 
264
            line = int(line)
 
265
        except ValueError:
 
266
            raise BzrCommandError('Line argument ("%s") is not a number.' % 
 
267
                                  line)
 
268
 
 
269
        from annotate.gannotate import GAnnotateWindow
 
270
        from annotate.config import GAnnotateConfig
 
271
 
 
272
        try:
 
273
            (tree, path) = WorkingTree.open_containing(filename)
 
274
            branch = tree.branch
 
275
        except errors.NoWorkingTree:
 
276
            (branch, path) = Branch.open_containing(filename)
 
277
            tree = branch.basis_tree()
 
278
 
 
279
        file_id = tree.path2id(path)
 
280
 
 
281
        if file_id is None:
 
282
            raise NotVersionedError(filename)
 
283
        if revision is not None:
 
284
            if len(revision) != 1:
 
285
                raise BzrCommandError("Only 1 revion may be specified.")
 
286
            revision_id = revision[0].in_history(branch).rev_id
 
287
            tree = branch.repository.revision_tree(revision_id)
 
288
        else:
 
289
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
 
290
 
 
291
        window = GAnnotateWindow(all, plain)
 
292
        window.connect("destroy", lambda w: gtk.main_quit())
 
293
        window.set_title(path + " - gannotate")
 
294
        config = GAnnotateConfig(window)
 
295
        window.show()
 
296
        branch.lock_read()
 
297
        try:
 
298
            window.annotate(tree, branch, file_id)
 
299
        finally:
 
300
            branch.unlock()
 
301
        window.jump_to_line(line)
 
302
        
 
303
        gtk.main()
 
304
 
 
305
register_command(cmd_gannotate)
 
306
 
 
307
class cmd_gcommit(Command):
 
308
    """GTK+ commit dialog
 
309
 
 
310
    Graphical user interface for committing revisions"""
 
311
    
 
312
    takes_args = []
 
313
    takes_options = []
 
314
 
 
315
    def run(self, filename=None):
 
316
        import os
 
317
        pygtk = import_pygtk()
 
318
 
 
319
        try:
 
320
            import gtk
 
321
        except RuntimeError, e:
 
322
            if str(e) == "could not open display":
 
323
                raise NoDisplayError
 
324
 
 
325
        set_ui_factory()
 
326
        from commit import CommitDialog
 
327
        from bzrlib.commit import Commit
 
328
        from bzrlib.errors import (BzrCommandError,
 
329
                                   NotBranchError,
 
330
                                   NoWorkingTree,
 
331
                                   PointlessCommit,
 
332
                                   ConflictsInTree,
 
333
                                   StrictCommitFailed)
 
334
 
 
335
        wt = None
 
336
        branch = None
 
337
        try:
 
338
            (wt, path) = WorkingTree.open_containing(filename)
 
339
            branch = wt.branch
 
340
        except NotBranchError, e:
 
341
            path = e.path
 
342
        except NoWorkingTree, e:
 
343
            path = e.base
 
344
            try:
 
345
                (branch, path) = Branch.open_containing(path)
 
346
            except NotBranchError, e:
 
347
                path = e.path
 
348
 
 
349
 
 
350
        commit = CommitDialog(wt, path, not branch)
 
351
        commit.run()
 
352
 
 
353
register_command(cmd_gcommit)
 
354
 
 
355
class NoDisplayError(BzrCommandError):
 
356
    """gtk could not find a proper display"""
 
357
 
 
358
    def __str__(self):
 
359
        return "No DISPLAY. Unable to run GTK+ application."
 
360
 
 
361
def test_suite():
 
362
    from unittest import TestSuite
 
363
    import tests
 
364
    result = TestSuite()
 
365
    result.addTest(tests.test_suite())
 
366
    return result