/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: Szilveszter Farkas (Phanatic)
  • Date: 2007-03-15 12:43:48 UTC
  • mto: (126.1.38 bzr-gtk)
  • mto: This revision was merged to the branch mainline in revision 172.
  • Revision ID: szilveszter.farkas@gmail.com-20070315124348-0nx0zb7fv4pa8xk6
Fix the indentation error in the TortoiseBZR test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 
15
15
"""GTK+ frontends to Bazaar commands """
16
16
 
 
17
import bzrlib
 
18
 
 
19
__version__ = '0.16.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.trace import warning
 
56
if __name__ != 'bzrlib.plugins.gtk':
 
57
    warning("Not running as bzrlib.plugins.gtk, things may break.")
 
58
 
 
59
from bzrlib.lazy_import import lazy_import
 
60
lazy_import(globals(), """
 
61
from bzrlib import (
 
62
    branch,
 
63
    errors,
 
64
    workingtree,
 
65
    )
 
66
""")
 
67
 
17
68
from bzrlib.commands import Command, register_command, display_command
18
69
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
19
70
from bzrlib.commands import Command, register_command
20
71
from bzrlib.option import Option
21
 
from bzrlib.branch import Branch
22
 
from bzrlib.workingtree import WorkingTree
23
72
from bzrlib.bzrdir import BzrDir
24
73
 
25
 
__version__ = '0.11.0'
 
74
import os.path
 
75
 
 
76
def import_pygtk():
 
77
    try:
 
78
        import pygtk
 
79
    except ImportError:
 
80
        raise errors.BzrCommandError("PyGTK not installed.")
 
81
    pygtk.require('2.0')
 
82
    return pygtk
 
83
 
 
84
 
 
85
def set_ui_factory():
 
86
    pygtk = import_pygtk()
 
87
    from ui import GtkUIFactory
 
88
    import bzrlib.ui
 
89
    bzrlib.ui.ui_factory = GtkUIFactory()
 
90
 
26
91
 
27
92
class cmd_gbranch(Command):
28
93
    """GTK+ branching.
30
95
    """
31
96
 
32
97
    def run(self):
33
 
        import pygtk
34
 
        pygtk.require("2.0")
 
98
        pygtk = import_pygtk()
35
99
        try:
36
100
            import gtk
37
101
        except RuntimeError, e:
38
102
            if str(e) == "could not open display":
39
103
                raise NoDisplayError
40
104
 
41
 
        from bzrlib.plugins.gtk.olive.branch import BranchDialog
 
105
        from bzrlib.plugins.gtk.branch import BranchDialog
42
106
 
43
 
        window = BranchDialog('.')
44
 
        window.display()
 
107
        set_ui_factory()
 
108
        dialog = BranchDialog(os.path.abspath('.'))
 
109
        dialog.run()
45
110
 
46
111
register_command(cmd_gbranch)
47
112
 
 
113
class cmd_gcheckout(Command):
 
114
    """ GTK+ checkout.
 
115
    
 
116
    """
 
117
    
 
118
    def run(self):
 
119
        pygtk = import_pygtk()
 
120
        try:
 
121
            import gtk
 
122
        except RuntimeError, e:
 
123
            if str(e) == "could not open display":
 
124
                raise NoDisplayError
 
125
 
 
126
        from bzrlib.plugins.gtk.checkout import CheckoutDialog
 
127
 
 
128
        set_ui_factory()
 
129
        dialog = CheckoutDialog(os.path.abspath('.'))
 
130
        dialog.run()
 
131
 
 
132
register_command(cmd_gcheckout)
 
133
 
 
134
class cmd_gpush(Command):
 
135
    """ GTK+ push.
 
136
    
 
137
    """
 
138
    takes_args = [ "location?" ]
 
139
 
 
140
    def run(self, location="."):
 
141
        (br, path) = branch.Branch.open_containing(location)
 
142
 
 
143
        pygtk = import_pygtk()
 
144
        try:
 
145
            import gtk
 
146
        except RuntimeError, e:
 
147
            if str(e) == "could not open display":
 
148
                raise NoDisplayError
 
149
 
 
150
        from push import PushDialog
 
151
 
 
152
        set_ui_factory()
 
153
        dialog = PushDialog(br)
 
154
        dialog.run()
 
155
 
 
156
register_command(cmd_gpush)
 
157
 
48
158
class cmd_gdiff(Command):
49
159
    """Show differences in working tree in a GTK+ Window.
50
160
    
55
165
 
56
166
    @display_command
57
167
    def run(self, revision=None, filename=None):
58
 
        wt = WorkingTree.open_containing(".")[0]
59
 
        branch = wt.branch
60
 
        if revision is not None:
61
 
            if len(revision) == 1:
 
168
        set_ui_factory()
 
169
        wt = workingtree.WorkingTree.open_containing(".")[0]
 
170
        wt.lock_read()
 
171
        try:
 
172
            branch = wt.branch
 
173
            if revision is not None:
 
174
                if len(revision) == 1:
 
175
                    tree1 = wt
 
176
                    revision_id = revision[0].in_history(branch).rev_id
 
177
                    tree2 = branch.repository.revision_tree(revision_id)
 
178
                elif len(revision) == 2:
 
179
                    revision_id_0 = revision[0].in_history(branch).rev_id
 
180
                    tree2 = branch.repository.revision_tree(revision_id_0)
 
181
                    revision_id_1 = revision[1].in_history(branch).rev_id
 
182
                    tree1 = branch.repository.revision_tree(revision_id_1)
 
183
            else:
62
184
                tree1 = wt
63
 
                revision_id = revision[0].in_history(branch).rev_id
64
 
                tree2 = branch.repository.revision_tree(revision_id)
65
 
            elif len(revision) == 2:
66
 
                revision_id_0 = revision[0].in_history(branch).rev_id
67
 
                tree2 = branch.repository.revision_tree(revision_id_0)
68
 
                revision_id_1 = revision[1].in_history(branch).rev_id
69
 
                tree1 = branch.repository.revision_tree(revision_id_1)
70
 
        else:
71
 
            tree1 = wt
72
 
            tree2 = tree1.basis_tree()
73
 
 
74
 
        from bzrlib.plugins.gtk.viz.diffwin import DiffWindow
75
 
        import gtk
76
 
        window = DiffWindow()
77
 
        window.connect("destroy", lambda w: gtk.main_quit())
78
 
        window.set_diff("Working Tree", tree1, tree2)
79
 
        if filename is not None:
80
 
            tree_filename = tree1.relpath(filename)
81
 
            try:
82
 
                window.set_file(tree_filename)
83
 
            except NoSuchFile:
84
 
                if (tree1.inventory.path2id(tree_filename) is None and 
85
 
                    tree2.inventory.path2id(tree_filename) is None):
86
 
                    raise NotVersionedError(filename)
87
 
                raise BzrCommandError('No changes found for file "%s"' % 
88
 
                                      filename)
89
 
        window.show()
90
 
 
91
 
        gtk.main()
 
185
                tree2 = tree1.basis_tree()
 
186
 
 
187
            from diff import DiffWindow
 
188
            import gtk
 
189
            window = DiffWindow()
 
190
            window.connect("destroy", gtk.main_quit)
 
191
            window.set_diff("Working Tree", tree1, tree2)
 
192
            if filename is not None:
 
193
                tree_filename = wt.relpath(filename)
 
194
                try:
 
195
                    window.set_file(tree_filename)
 
196
                except NoSuchFile:
 
197
                    if (tree1.inventory.path2id(tree_filename) is None and 
 
198
                        tree2.inventory.path2id(tree_filename) is None):
 
199
                        raise NotVersionedError(filename)
 
200
                    raise BzrCommandError('No changes found for file "%s"' % 
 
201
                                          filename)
 
202
            window.show()
 
203
 
 
204
            gtk.main()
 
205
        finally:
 
206
            wt.unlock()
92
207
 
93
208
register_command(cmd_gdiff)
94
209
 
109
224
    aliases = [ "visualize", "vis", "viz" ]
110
225
 
111
226
    def run(self, location=".", revision=None, limit=None):
112
 
        (branch, path) = Branch.open_containing(location)
113
 
        branch.lock_read()
114
 
        branch.repository.lock_read()
 
227
        set_ui_factory()
 
228
        (br, path) = branch.Branch.open_containing(location)
 
229
        br.lock_read()
 
230
        br.repository.lock_read()
115
231
        try:
116
232
            if revision is None:
117
 
                revid = branch.last_revision()
 
233
                revid = br.last_revision()
118
234
                if revid is None:
119
235
                    return
120
236
            else:
121
 
                (revno, revid) = revision[0].in_history(branch)
 
237
                (revno, revid) = revision[0].in_history(br)
122
238
 
123
 
            from viz.bzrkapp import BzrkApp
 
239
            from viz.branchwin import BranchWindow
 
240
            import gtk
124
241
                
125
 
            app = BzrkApp()
126
 
            app.show(branch, revid, limit)
 
242
            pp = BranchWindow()
 
243
            pp.set_branch(br, revid, limit)
 
244
            pp.connect("destroy", lambda w: gtk.main_quit())
 
245
            pp.show()
 
246
            gtk.main()
127
247
        finally:
128
 
            branch.repository.unlock()
129
 
            branch.unlock()
130
 
        app.main()
 
248
            br.repository.unlock()
 
249
            br.unlock()
131
250
 
132
251
 
133
252
register_command(cmd_visualise)
143
262
        Option("all", help="show annotations on all lines"),
144
263
        Option("plain", help="don't highlight annotation lines"),
145
264
        Option("line", type=int, argname="lineno",
146
 
               help="jump to specified line number")
 
265
               help="jump to specified line number"),
 
266
        "revision",
147
267
    ]
148
268
    aliases = ["gblame", "gpraise"]
149
269
    
150
 
    def run(self, filename, all=False, plain=False, line='1'):
151
 
        import pygtk
152
 
        pygtk.require("2.0")
 
270
    def run(self, filename, all=False, plain=False, line='1', revision=None):
 
271
        pygtk = import_pygtk()
153
272
 
154
273
        try:
155
274
            import gtk
156
275
        except RuntimeError, e:
157
276
            if str(e) == "could not open display":
158
277
                raise NoDisplayError
 
278
        set_ui_factory()
159
279
 
160
280
        try:
161
281
            line = int(line)
166
286
        from annotate.gannotate import GAnnotateWindow
167
287
        from annotate.config import GAnnotateConfig
168
288
 
169
 
        (wt, path) = WorkingTree.open_containing(filename)
170
 
        branch = wt.branch
 
289
        wt, br, path = BzrDir.open_containing_tree_or_branch(filename)
 
290
        if wt is not None:
 
291
            tree = wt
 
292
        else:
 
293
            tree = br.basis_tree()
171
294
 
172
 
        file_id = wt.path2id(path)
 
295
        file_id = tree.path2id(path)
173
296
 
174
297
        if file_id is None:
175
298
            raise NotVersionedError(filename)
 
299
        if revision is not None:
 
300
            if len(revision) != 1:
 
301
                raise BzrCommandError("Only 1 revion may be specified.")
 
302
            revision_id = revision[0].in_history(br).rev_id
 
303
            tree = br.repository.revision_tree(revision_id)
 
304
        else:
 
305
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
176
306
 
177
307
        window = GAnnotateWindow(all, plain)
178
308
        window.connect("destroy", lambda w: gtk.main_quit())
179
309
        window.set_title(path + " - gannotate")
180
310
        config = GAnnotateConfig(window)
181
311
        window.show()
182
 
        branch.lock_read()
 
312
        br.lock_read()
 
313
        if wt is not None:
 
314
            wt.lock_read()
183
315
        try:
184
 
            window.annotate(branch, file_id)
 
316
            window.annotate(tree, br, file_id)
 
317
            window.jump_to_line(line)
 
318
            gtk.main()
185
319
        finally:
186
 
            branch.unlock()
187
 
        window.jump_to_line(line)
188
 
        
189
 
        gtk.main()
 
320
            br.unlock()
 
321
            if wt is not None:
 
322
                wt.unlock()
190
323
 
191
324
register_command(cmd_gannotate)
192
325
 
195
328
 
196
329
    Graphical user interface for committing revisions"""
197
330
    
 
331
    aliases = [ "gci" ]
198
332
    takes_args = []
199
333
    takes_options = []
200
334
 
201
335
    def run(self, filename=None):
202
 
        import pygtk
203
 
        pygtk.require("2.0")
 
336
        import os
 
337
        pygtk = import_pygtk()
204
338
 
205
339
        try:
206
340
            import gtk
208
342
            if str(e) == "could not open display":
209
343
                raise NoDisplayError
210
344
 
211
 
        from olive.commit import CommitDialog
 
345
        set_ui_factory()
 
346
        from commit import CommitDialog
212
347
        from bzrlib.commit import Commit
213
 
        from bzrlib.errors import (BzrCommandError, PointlessCommit, ConflictsInTree, 
214
 
           StrictCommitFailed)
215
 
 
216
 
        (wt, path) = WorkingTree.open_containing(filename)
217
 
 
218
 
        dialog = CommitDialog(wt, path)
219
 
        dialog.display()
220
 
        gtk.main()
 
348
        from bzrlib.errors import (BzrCommandError,
 
349
                                   NotBranchError,
 
350
                                   NoWorkingTree,
 
351
                                   PointlessCommit,
 
352
                                   ConflictsInTree,
 
353
                                   StrictCommitFailed)
 
354
 
 
355
        wt = None
 
356
        br = None
 
357
        try:
 
358
            (wt, path) = workingtree.WorkingTree.open_containing(filename)
 
359
            br = wt.branch
 
360
        except NotBranchError, e:
 
361
            path = e.path
 
362
        except NoWorkingTree, e:
 
363
            path = e.base
 
364
            try:
 
365
                (br, path) = branch.Branch.open_containing(path)
 
366
            except NotBranchError, e:
 
367
                path = e.path
 
368
 
 
369
 
 
370
        commit = CommitDialog(wt, path, not br)
 
371
        commit.run()
221
372
 
222
373
register_command(cmd_gcommit)
223
374
 
 
375
class cmd_gstatus(Command):
 
376
    """GTK+ status dialog
 
377
 
 
378
    Graphical user interface for showing status 
 
379
    information."""
 
380
    
 
381
    aliases = [ "gst" ]
 
382
    takes_args = ['PATH?']
 
383
    takes_options = []
 
384
 
 
385
    def run(self, path='.'):
 
386
        import os
 
387
        pygtk = import_pygtk()
 
388
 
 
389
        try:
 
390
            import gtk
 
391
        except RuntimeError, e:
 
392
            if str(e) == "could not open display":
 
393
                raise NoDisplayError
 
394
 
 
395
        set_ui_factory()
 
396
        from status import StatusDialog
 
397
        (wt, wt_path) = workingtree.WorkingTree.open_containing(path)
 
398
        status = StatusDialog(wt, wt_path)
 
399
        status.connect("destroy", gtk.main_quit)
 
400
        status.run()
 
401
 
 
402
register_command(cmd_gstatus)
 
403
 
 
404
class cmd_gconflicts(Command):
 
405
    """ GTK+ push.
 
406
    
 
407
    """
 
408
    def run(self):
 
409
        (wt, path) = workingtree.WorkingTree.open_containing('.')
 
410
        
 
411
        pygtk = import_pygtk()
 
412
        try:
 
413
            import gtk
 
414
        except RuntimeError, e:
 
415
            if str(e) == "could not open display":
 
416
                raise NoDisplayError
 
417
 
 
418
        from bzrlib.plugins.gtk.conflicts import ConflictsDialog
 
419
 
 
420
        set_ui_factory()
 
421
        dialog = ConflictsDialog(wt)
 
422
        dialog.run()
 
423
 
 
424
register_command(cmd_gconflicts)
 
425
 
 
426
import gettext
 
427
gettext.install('olive-gtk')
 
428
 
224
429
class NoDisplayError(BzrCommandError):
225
430
    """gtk could not find a proper display"""
226
431
 
227
432
    def __str__(self):
228
 
        return "No DISPLAY. gannotate is disabled."
 
433
        return "No DISPLAY. Unable to run GTK+ application."
 
434
 
 
435
def test_suite():
 
436
    from unittest import TestSuite
 
437
    import tests
 
438
    import sys
 
439
    default_encoding = sys.getdefaultencoding()
 
440
    try:
 
441
        result = TestSuite()
 
442
        result.addTest(tests.test_suite())
 
443
    finally:
 
444
        reload(sys)
 
445
        sys.setdefaultencoding(default_encoding)
 
446
    return result