/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-01 15:50:40 UTC
  • Revision ID: jelmer@samba.org-20070201155040-3hq4mfbxs99kzazy
add framework for tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import bzrlib
18
18
 
19
 
__version__ = '0.17.0'
 
19
__version__ = '0.15.0'
20
20
version_info = tuple(int(n) for n in __version__.split('.'))
21
21
 
22
22
 
40
40
    if bzrlib_version < desired:
41
41
        warning('Installed bzr version %s is too old to be used with bzr-gtk'
42
42
                ' %s.' % (bzrlib.__version__, __version__))
43
 
        raise BzrError('Version mismatch: %r' % version_info)
 
43
        # Not using BzrNewError, because it may not exist.
 
44
        raise Exception, ('Version mismatch', version_info)
44
45
    else:
45
46
        warning('bzr-gtk is not up to date with installed bzr version %s.'
46
47
                ' \nThere should be a newer version available, e.g. %i.%i.' 
51
52
 
52
53
check_bzrlib_version(version_info[:2])
53
54
 
54
 
from bzrlib.trace import warning
55
 
if __name__ != 'bzrlib.plugins.gtk':
56
 
    warning("Not running as bzrlib.plugins.gtk, things may break.")
57
 
 
58
 
from bzrlib.lazy_import import lazy_import
59
 
lazy_import(globals(), """
60
 
from bzrlib import (
61
 
    branch,
62
 
    errors,
63
 
    workingtree,
64
 
    )
65
 
""")
66
 
 
 
55
from bzrlib import errors
67
56
from bzrlib.commands import Command, register_command, display_command
68
57
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
 
58
from bzrlib.commands import Command, register_command
69
59
from bzrlib.option import Option
 
60
from bzrlib.branch import Branch
 
61
from bzrlib.workingtree import WorkingTree
 
62
from bzrlib.bzrdir import BzrDir
70
63
 
71
64
import os.path
72
65
 
80
73
 
81
74
 
82
75
def set_ui_factory():
83
 
    import_pygtk()
84
 
    from ui import GtkUIFactory
 
76
    pygtk = import_pygtk()
 
77
    from olive.ui import GtkUIFactory
85
78
    import bzrlib.ui
86
79
    bzrlib.ui.ui_factory = GtkUIFactory()
87
80
 
88
81
 
89
 
class GTKCommand(Command):
90
 
    """Abstract class providing GTK specific run commands."""
91
 
 
92
 
    def open_display(self):
93
 
        pygtk = import_pygtk()
94
 
        try:
95
 
            import gtk
96
 
        except RuntimeError, e:
97
 
            if str(e) == "could not open display":
98
 
                raise NoDisplayError
99
 
        set_ui_factory()
100
 
        return gtk
101
 
 
102
 
    def run(self):
103
 
        self.open_display()
104
 
        dialog = self.get_gtk_dialog(os.path.abspath('.'))
105
 
        dialog.run()
106
 
 
107
 
 
108
 
class cmd_gbranch(GTKCommand):
 
82
class cmd_gbranch(Command):
109
83
    """GTK+ branching.
110
84
    
111
85
    """
112
86
 
113
 
    def get_gtk_dialog(self, path):
114
 
        from bzrlib.plugins.gtk.branch import BranchDialog
115
 
        return BranchDialog(path)
116
 
 
117
 
 
118
 
class cmd_gcheckout(GTKCommand):
119
 
    """ GTK+ checkout.
120
 
    
121
 
    """
122
 
    
123
 
    def get_gtk_dialog(self, path):
124
 
        from bzrlib.plugins.gtk.checkout import CheckoutDialog
125
 
        return CheckoutDialog(path)
126
 
 
127
 
 
128
 
 
129
 
class cmd_gpush(GTKCommand):
130
 
    """ GTK+ push.
131
 
    
132
 
    """
133
 
    takes_args = [ "location?" ]
134
 
 
135
 
    def run(self, location="."):
136
 
        (br, path) = branch.Branch.open_containing(location)
137
 
        self.open_display()
138
 
        from push import PushDialog
139
 
        dialog = PushDialog(br)
140
 
        dialog.run()
141
 
 
142
 
 
143
 
 
144
 
class cmd_gdiff(GTKCommand):
 
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.olive.branch import BranchDialog
 
96
 
 
97
        set_ui_factory()
 
98
        dialog = BranchDialog(os.path.abspath('.'))
 
99
        dialog.window.connect("destroy", lambda w: gtk.main_quit())
 
100
        dialog.display()
 
101
        
 
102
        gtk.main()
 
103
 
 
104
register_command(cmd_gbranch)
 
105
 
 
106
class cmd_gdiff(Command):
145
107
    """Show differences in working tree in a GTK+ Window.
146
108
    
147
109
    Otherwise, all changes for the tree are listed.
152
114
    @display_command
153
115
    def run(self, revision=None, filename=None):
154
116
        set_ui_factory()
155
 
        wt = workingtree.WorkingTree.open_containing(".")[0]
156
 
        wt.lock_read()
157
 
        try:
158
 
            branch = wt.branch
159
 
            if revision is not None:
160
 
                if len(revision) == 1:
161
 
                    tree1 = wt
162
 
                    revision_id = revision[0].in_history(branch).rev_id
163
 
                    tree2 = branch.repository.revision_tree(revision_id)
164
 
                elif len(revision) == 2:
165
 
                    revision_id_0 = revision[0].in_history(branch).rev_id
166
 
                    tree2 = branch.repository.revision_tree(revision_id_0)
167
 
                    revision_id_1 = revision[1].in_history(branch).rev_id
168
 
                    tree1 = branch.repository.revision_tree(revision_id_1)
169
 
            else:
 
117
        wt = WorkingTree.open_containing(".")[0]
 
118
        branch = wt.branch
 
119
        if revision is not None:
 
120
            if len(revision) == 1:
170
121
                tree1 = wt
171
 
                tree2 = tree1.basis_tree()
172
 
 
173
 
            from diff import DiffWindow
174
 
            import gtk
175
 
            window = DiffWindow()
176
 
            window.connect("destroy", gtk.main_quit)
177
 
            window.set_diff("Working Tree", tree1, tree2)
178
 
            if filename is not None:
179
 
                tree_filename = wt.relpath(filename)
180
 
                try:
181
 
                    window.set_file(tree_filename)
182
 
                except NoSuchFile:
183
 
                    if (tree1.path2id(tree_filename) is None and 
184
 
                        tree2.path2id(tree_filename) is None):
185
 
                        raise NotVersionedError(filename)
186
 
                    raise BzrCommandError('No changes found for file "%s"' % 
187
 
                                          filename)
188
 
            window.show()
189
 
 
190
 
            gtk.main()
191
 
        finally:
192
 
            wt.unlock()
193
 
 
 
122
                revision_id = revision[0].in_history(branch).rev_id
 
123
                tree2 = branch.repository.revision_tree(revision_id)
 
124
            elif len(revision) == 2:
 
125
                revision_id_0 = revision[0].in_history(branch).rev_id
 
126
                tree2 = branch.repository.revision_tree(revision_id_0)
 
127
                revision_id_1 = revision[1].in_history(branch).rev_id
 
128
                tree1 = branch.repository.revision_tree(revision_id_1)
 
129
        else:
 
130
            tree1 = wt
 
131
            tree2 = tree1.basis_tree()
 
132
 
 
133
        from viz.diffwin import DiffWindow
 
134
        import gtk
 
135
        window = DiffWindow()
 
136
        window.connect("destroy", lambda w: gtk.main_quit())
 
137
        window.set_diff("Working Tree", tree1, tree2)
 
138
        if filename is not None:
 
139
            tree_filename = wt.relpath(filename)
 
140
            try:
 
141
                window.set_file(tree_filename)
 
142
            except NoSuchFile:
 
143
                if (tree1.inventory.path2id(tree_filename) is None and 
 
144
                    tree2.inventory.path2id(tree_filename) is None):
 
145
                    raise NotVersionedError(filename)
 
146
                raise BzrCommandError('No changes found for file "%s"' % 
 
147
                                      filename)
 
148
        window.show()
 
149
 
 
150
        gtk.main()
 
151
 
 
152
register_command(cmd_gdiff)
194
153
 
195
154
class cmd_visualise(Command):
196
155
    """Graphically visualise this branch.
210
169
 
211
170
    def run(self, location=".", revision=None, limit=None):
212
171
        set_ui_factory()
213
 
        (br, path) = branch.Branch.open_containing(location)
214
 
        br.lock_read()
215
 
        br.repository.lock_read()
 
172
        (branch, path) = Branch.open_containing(location)
 
173
        branch.lock_read()
 
174
        branch.repository.lock_read()
216
175
        try:
217
176
            if revision is None:
218
 
                revid = br.last_revision()
 
177
                revid = branch.last_revision()
219
178
                if revid is None:
220
179
                    return
221
180
            else:
222
 
                (revno, revid) = revision[0].in_history(br)
 
181
                (revno, revid) = revision[0].in_history(branch)
223
182
 
224
 
            from viz.branchwin import BranchWindow
225
 
            import gtk
 
183
            from viz.bzrkapp import BzrkApp
226
184
                
227
 
            pp = BranchWindow()
228
 
            pp.set_branch(br, revid, limit)
229
 
            pp.connect("destroy", lambda w: gtk.main_quit())
230
 
            pp.show()
231
 
            gtk.main()
 
185
            app = BzrkApp()
 
186
            app.show(branch, revid, limit)
232
187
        finally:
233
 
            br.repository.unlock()
234
 
            br.unlock()
235
 
 
236
 
 
237
 
class cmd_gannotate(GTKCommand):
 
188
            branch.repository.unlock()
 
189
            branch.unlock()
 
190
        app.main()
 
191
 
 
192
 
 
193
register_command(cmd_visualise)
 
194
 
 
195
class cmd_gannotate(Command):
238
196
    """GTK+ annotate.
239
197
    
240
198
    Browse changes to FILENAME line by line in a GTK+ window.
251
209
    aliases = ["gblame", "gpraise"]
252
210
    
253
211
    def run(self, filename, all=False, plain=False, line='1', revision=None):
254
 
        gtk = self.open_display()
 
212
        pygtk = import_pygtk()
 
213
 
 
214
        try:
 
215
            import gtk
 
216
        except RuntimeError, e:
 
217
            if str(e) == "could not open display":
 
218
                raise NoDisplayError
 
219
        set_ui_factory()
255
220
 
256
221
        try:
257
222
            line = int(line)
261
226
 
262
227
        from annotate.gannotate import GAnnotateWindow
263
228
        from annotate.config import GAnnotateConfig
264
 
        from bzrlib.bzrdir import BzrDir
265
229
 
266
 
        wt, br, path = BzrDir.open_containing_tree_or_branch(filename)
267
 
        if wt is not None:
268
 
            tree = wt
269
 
        else:
270
 
            tree = br.basis_tree()
 
230
        try:
 
231
            (tree, path) = WorkingTree.open_containing(filename)
 
232
            branch = tree.branch
 
233
        except errors.NoWorkingTree:
 
234
            (branch, path) = Branch.open_containing(filename)
 
235
            tree = branch.basis_tree()
271
236
 
272
237
        file_id = tree.path2id(path)
273
238
 
276
241
        if revision is not None:
277
242
            if len(revision) != 1:
278
243
                raise BzrCommandError("Only 1 revion may be specified.")
279
 
            revision_id = revision[0].in_history(br).rev_id
280
 
            tree = br.repository.revision_tree(revision_id)
 
244
            revision_id = revision[0].in_history(branch).rev_id
 
245
            tree = branch.repository.revision_tree(revision_id)
281
246
        else:
282
247
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
283
248
 
286
251
        window.set_title(path + " - gannotate")
287
252
        config = GAnnotateConfig(window)
288
253
        window.show()
289
 
        br.lock_read()
290
 
        if wt is not None:
291
 
            wt.lock_read()
 
254
        branch.lock_read()
292
255
        try:
293
 
            window.annotate(tree, br, file_id)
294
 
            window.jump_to_line(line)
295
 
            gtk.main()
 
256
            window.annotate(tree, branch, file_id)
296
257
        finally:
297
 
            br.unlock()
298
 
            if wt is not None:
299
 
                wt.unlock()
300
 
 
301
 
 
302
 
 
303
 
class cmd_gcommit(GTKCommand):
 
258
            branch.unlock()
 
259
        window.jump_to_line(line)
 
260
        
 
261
        gtk.main()
 
262
 
 
263
register_command(cmd_gannotate)
 
264
 
 
265
class cmd_gcommit(Command):
304
266
    """GTK+ commit dialog
305
267
 
306
268
    Graphical user interface for committing revisions"""
307
269
    
308
 
    aliases = [ "gci" ]
309
270
    takes_args = []
310
271
    takes_options = []
311
272
 
312
273
    def run(self, filename=None):
313
274
        import os
314
 
        self.open_display()
315
 
        from commit import CommitDialog
 
275
        pygtk = import_pygtk()
 
276
 
 
277
        try:
 
278
            import gtk
 
279
        except RuntimeError, e:
 
280
            if str(e) == "could not open display":
 
281
                raise NoDisplayError
 
282
 
 
283
        set_ui_factory()
 
284
        from olive.commit import CommitDialog
 
285
        from bzrlib.commit import Commit
316
286
        from bzrlib.errors import (BzrCommandError,
317
287
                                   NotBranchError,
318
 
                                   NoWorkingTree)
 
288
                                   NoWorkingTree,
 
289
                                   PointlessCommit,
 
290
                                   ConflictsInTree,
 
291
                                   StrictCommitFailed)
319
292
 
320
293
        wt = None
321
 
        br = None
 
294
        branch = None
322
295
        try:
323
 
            (wt, path) = workingtree.WorkingTree.open_containing(filename)
324
 
            br = wt.branch
 
296
            (wt, path) = WorkingTree.open_containing(filename)
 
297
            branch = wt.branch
 
298
        except NotBranchError, e:
 
299
            path = e.path
325
300
        except NoWorkingTree, e:
326
301
            path = e.base
327
 
            (br, path) = branch.Branch.open_containing(path)
328
 
 
329
 
        commit = CommitDialog(wt, path, not br)
 
302
            try:
 
303
                (branch, path) = Branch.open_containing(path)
 
304
            except NotBranchError, e:
 
305
                path = e.path
 
306
 
 
307
 
 
308
        commit = CommitDialog(wt, path, not branch)
330
309
        commit.run()
331
310
 
332
 
 
333
 
 
334
 
class cmd_gstatus(GTKCommand):
335
 
    """GTK+ status dialog
336
 
 
337
 
    Graphical user interface for showing status 
338
 
    information."""
339
 
    
340
 
    aliases = [ "gst" ]
341
 
    takes_args = ['PATH?']
342
 
    takes_options = []
343
 
 
344
 
    def run(self, path='.'):
345
 
        import os
346
 
        gtk = self.open_display()
347
 
        from status import StatusDialog
348
 
        (wt, wt_path) = workingtree.WorkingTree.open_containing(path)
349
 
        status = StatusDialog(wt, wt_path)
350
 
        status.connect("destroy", gtk.main_quit)
351
 
        status.run()
352
 
 
353
 
 
354
 
 
355
 
class cmd_gconflicts(GTKCommand):
356
 
    """ GTK+ push.
357
 
    
358
 
    """
359
 
    def run(self):
360
 
        (wt, path) = workingtree.WorkingTree.open_containing('.')
361
 
        self.open_display()
362
 
        from bzrlib.plugins.gtk.conflicts import ConflictsDialog
363
 
        dialog = ConflictsDialog(wt)
364
 
        dialog.run()
365
 
 
366
 
 
367
 
 
368
 
class cmd_gpreferences(GTKCommand):
369
 
    """ GTK+ preferences dialog.
370
 
 
371
 
    """
372
 
    def run(self):
373
 
        self.open_display()
374
 
        from bzrlib.plugins.gtk.preferences import PreferencesWindow
375
 
        dialog = PreferencesWindow()
376
 
        dialog.run()
377
 
 
378
 
 
379
 
 
380
 
class cmd_gmissing(Command):
381
 
    """ GTK+ missing revisions dialog.
382
 
 
383
 
    """
384
 
    takes_args = ["other_branch?"]
385
 
    def run(self, other_branch=None):
386
 
        pygtk = import_pygtk()
387
 
        try:
388
 
            import gtk
389
 
        except RuntimeError, e:
390
 
            if str(e) == "could not open display":
391
 
                raise NoDisplayError
392
 
 
393
 
        from bzrlib.plugins.gtk.missing import MissingWindow
394
 
        from bzrlib.branch import Branch
395
 
 
396
 
        local_branch = Branch.open_containing(".")[0]
397
 
        if other_branch is None:
398
 
            other_branch = local_branch.get_parent()
399
 
            
400
 
            if other_branch is None:
401
 
                raise errors.BzrCommandError("No peer location known or specified.")
402
 
        remote_branch = Branch.open_containing(other_branch)[0]
403
 
        set_ui_factory()
404
 
        local_branch.lock_read()
405
 
        try:
406
 
            remote_branch.lock_read()
407
 
            try:
408
 
                dialog = MissingWindow(local_branch, remote_branch)
409
 
                dialog.run()
410
 
            finally:
411
 
                remote_branch.unlock()
412
 
        finally:
413
 
            local_branch.unlock()
414
 
 
415
 
 
416
 
class cmd_ginit(GTKCommand):
417
 
    def run(self):
418
 
        self.open_display()
419
 
        from initialize import InitDialog
420
 
        dialog = InitDialog(os.path.abspath(os.path.curdir))
421
 
        dialog.run()
422
 
 
423
 
 
424
 
class cmd_gtags(GTKCommand):
425
 
    def run(self):
426
 
        br = branch.Branch.open_containing('.')[0]
427
 
        
428
 
        gtk = self.open_display()
429
 
        from tags import TagsWindow
430
 
        window = TagsWindow(br)
431
 
        window.show()
432
 
        gtk.main()
433
 
 
434
 
 
435
 
commands = [
436
 
    cmd_gmissing, 
437
 
    cmd_gpreferences, 
438
 
    cmd_gconflicts, 
439
 
    cmd_gstatus,
440
 
    cmd_gcommit, 
441
 
    cmd_gannotate, 
442
 
    cmd_visualise, 
443
 
    cmd_gdiff,
444
 
    cmd_gpush, 
445
 
    cmd_gcheckout, 
446
 
    cmd_gbranch,
447
 
    cmd_ginit,
448
 
    cmd_gtags
449
 
    ]
450
 
 
451
 
for cmd in commands:
452
 
    register_command(cmd)
453
 
 
454
 
 
455
 
class cmd_commit_notify(GTKCommand):
456
 
    """Run the bzr commit notifier.
457
 
 
458
 
    This is a background program which will pop up a notification on the users
459
 
    screen when a commit occurs.
460
 
    """
461
 
 
462
 
    def run(self):
463
 
        gtk = self.open_display()
464
 
        import cgi
465
 
        import dbus
466
 
        import dbus.service
467
 
        import pynotify
468
 
        from bzrlib.bzrdir import BzrDir
469
 
        from bzrlib import errors
470
 
        from bzrlib.osutils import format_date
471
 
        from bzrlib.transport import get_transport
472
 
        if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
473
 
            import dbus.glib
474
 
        from bzrlib.plugins.dbus import activity
475
 
        bus = dbus.SessionBus()
476
 
        # get the object so we can subscribe to callbacks from it.
477
 
        broadcast_service = bus.get_object(
478
 
            activity.Broadcast.DBUS_NAME,
479
 
            activity.Broadcast.DBUS_PATH)
480
 
        def catch_branch(revision_id, urls):
481
 
            # TODO: show all the urls, or perhaps choose the 'best'.
482
 
            url = urls[0]
483
 
            try:
484
 
                if isinstance(revision_id, unicode):
485
 
                    revision_id = revision_id.encode('utf8')
486
 
                transport = get_transport(url)
487
 
                a_dir = BzrDir.open_from_transport(transport)
488
 
                branch = a_dir.open_branch()
489
 
                revno = branch.revision_id_to_revno(revision_id)
490
 
                revision = branch.repository.get_revision(revision_id)
491
 
                summary = 'New revision %d in %s' % (revno, url)
492
 
                body  = 'Committer: %s\n' % revision.committer
493
 
                body += 'Date: %s\n' % format_date(revision.timestamp,
494
 
                    revision.timezone)
495
 
                body += '\n'
496
 
                body += revision.message
497
 
                body = cgi.escape(body)
498
 
                nw = pynotify.Notification(summary, body)
499
 
                nw.set_timeout(5000)
500
 
                nw.show()
501
 
            except Exception, e:
502
 
                print e
503
 
                raise
504
 
        broadcast_service.connect_to_signal("Revision", catch_branch,
505
 
            dbus_interface=activity.Broadcast.DBUS_INTERFACE)
506
 
        pynotify.init("bzr commit-notify")
507
 
        gtk.main()
508
 
 
509
 
register_command(cmd_commit_notify)
510
 
 
511
 
 
512
 
import gettext
513
 
gettext.install('olive-gtk')
514
 
 
 
311
register_command(cmd_gcommit)
515
312
 
516
313
class NoDisplayError(BzrCommandError):
517
314
    """gtk could not find a proper display"""
519
316
    def __str__(self):
520
317
        return "No DISPLAY. Unable to run GTK+ application."
521
318
 
522
 
 
523
319
def test_suite():
524
320
    from unittest import TestSuite
525
321
    import tests
526
 
    import sys
527
 
    default_encoding = sys.getdefaultencoding()
528
 
    try:
529
 
        result = TestSuite()
530
 
        result.addTest(tests.test_suite())
531
 
    finally:
532
 
        if sys.getdefaultencoding() != default_encoding:
533
 
            reload(sys)
534
 
            sys.setdefaultencoding(default_encoding)
 
322
    result = TestSuite()
 
323
    result.addTest(tests.test_suite())
535
324
    return result
 
325