/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: John Arbash Meinel
  • Date: 2007-11-02 22:08:54 UTC
  • mto: (330.3.3 trunk)
  • mto: This revision was merged to the branch mainline in revision 368.
  • Revision ID: john@arbash-meinel.com-20071102220854-thegr866sz1tbwob
Finish connecting the 'Commit all changes' radio buttons.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
"""Graphical support for Bazaar using GTK.
16
16
 
17
17
This plugin includes:
 
18
commit-notify     Start the graphical notifier of commits.
18
19
gannotate         GTK+ annotate. 
19
20
gbranch           GTK+ branching. 
20
21
gcheckout         GTK+ checkout. 
22
23
gconflicts        GTK+ conflicts. 
23
24
gdiff             Show differences in working tree in a GTK+ Window. 
24
25
ginit             Initialise a new branch.
25
 
gmerge            GTK+ merge dialog
26
26
gmissing          GTK+ missing revisions dialog. 
27
27
gpreferences      GTK+ preferences dialog. 
28
28
gpush             GTK+ push.
36
36
 
37
37
import bzrlib
38
38
 
39
 
version_info = (0, 95, 0, 'dev', 1)
 
39
version_info = (0, 92, 0, 'dev', 0)
40
40
 
41
41
if version_info[3] == 'final':
42
42
    version_string = '%d.%d.%d' % version_info[:3]
44
44
    version_string = '%d.%d.%d%s%d' % version_info
45
45
__version__ = version_string
46
46
 
47
 
required_bzrlib = (1, 3)
48
 
 
49
47
def check_bzrlib_version(desired):
50
48
    """Check that bzrlib is compatible.
51
49
 
52
50
    If version is < bzr-gtk version, assume incompatible.
 
51
    If version == bzr-gtk version, assume completely compatible
 
52
    If version == bzr-gtk version + 1, assume compatible, with deprecations
 
53
    Otherwise, assume incompatible.
53
54
    """
 
55
    desired_plus = (desired[0], desired[1]+1)
54
56
    bzrlib_version = bzrlib.version_info[:2]
 
57
    if bzrlib_version == desired or (bzrlib_version == desired_plus and
 
58
                                     bzrlib.version_info[3] == 'dev'):
 
59
        return
55
60
    try:
56
61
        from bzrlib.trace import warning
57
62
    except ImportError:
62
67
        warning('Installed Bazaar version %s is too old to be used with bzr-gtk'
63
68
                ' %s.' % (bzrlib.__version__, __version__))
64
69
        raise BzrError('Version mismatch: %r, %r' % (version_info, bzrlib.version_info) )
 
70
    else:
 
71
        warning('bzr-gtk is not up to date with installed bzr version %s.'
 
72
                ' \nThere should be a newer version available, e.g. %i.%i.' 
 
73
                % (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
65
74
 
66
75
 
67
76
if version_info[2] == "final":
68
 
    check_bzrlib_version(required_bzrlib)
 
77
    check_bzrlib_version(version_info[:2])
69
78
 
70
79
from bzrlib.trace import warning
71
80
if __name__ != 'bzrlib.plugins.gtk':
77
86
    branch,
78
87
    builtins,
79
88
    errors,
80
 
    merge_directive,
81
89
    workingtree,
82
90
    )
83
91
""")
104
112
    bzrlib.ui.ui_factory = GtkUIFactory()
105
113
 
106
114
 
107
 
def data_basedirs():
108
 
    return [os.path.dirname(__file__),
109
 
             "/usr/share/bzr-gtk", 
110
 
             "/usr/local/share/bzr-gtk"]
111
 
 
112
 
 
113
 
def data_path(*args):
114
 
    for basedir in data_basedirs():
115
 
        path = os.path.join(basedir, *args)
116
 
        if os.path.exists(path):
117
 
            return path
118
 
    return None
119
 
 
120
 
 
121
 
def icon_path(*args):
122
 
    return data_path(os.path.join('icons', *args))
123
 
 
124
 
 
125
 
def open_display():
126
 
    pygtk = import_pygtk()
127
 
    try:
128
 
        import gtk
129
 
    except RuntimeError, e:
130
 
        if str(e) == "could not open display":
131
 
            raise NoDisplayError
132
 
    set_ui_factory()
133
 
    return gtk
134
 
 
 
115
def data_path():
 
116
    return os.path.dirname(__file__)
 
117
 
135
118
 
136
119
class GTKCommand(Command):
137
120
    """Abstract class providing GTK specific run commands."""
138
121
 
 
122
    def open_display(self):
 
123
        pygtk = import_pygtk()
 
124
        try:
 
125
            import gtk
 
126
        except RuntimeError, e:
 
127
            if str(e) == "could not open display":
 
128
                raise NoDisplayError
 
129
        set_ui_factory()
 
130
        return gtk
 
131
 
139
132
    def run(self):
140
 
        open_display()
 
133
        self.open_display()
141
134
        dialog = self.get_gtk_dialog(os.path.abspath('.'))
142
135
        dialog.run()
143
136
 
171
164
 
172
165
    def run(self, location="."):
173
166
        (br, path) = branch.Branch.open_containing(location)
174
 
        open_display()
 
167
        self.open_display()
175
168
        from push import PushDialog
176
169
        dialog = PushDialog(br.repository, br.last_revision(), br)
177
170
        dialog.run()
196
189
            if revision is not None:
197
190
                if len(revision) == 1:
198
191
                    tree1 = wt
199
 
                    revision_id = revision[0].as_revision_id(tree1.branch)
 
192
                    revision_id = revision[0].in_history(branch).rev_id
200
193
                    tree2 = branch.repository.revision_tree(revision_id)
201
194
                elif len(revision) == 2:
202
 
                    revision_id_0 = revision[0].as_revision_id(branch)
 
195
                    revision_id_0 = revision[0].in_history(branch).rev_id
203
196
                    tree2 = branch.repository.revision_tree(revision_id_0)
204
 
                    revision_id_1 = revision[1].as_revision_id(branch)
 
197
                    revision_id_1 = revision[1].in_history(branch).rev_id
205
198
                    tree1 = branch.repository.revision_tree(revision_id_1)
206
199
            else:
207
200
                tree1 = wt
229
222
            wt.unlock()
230
223
 
231
224
 
232
 
def start_viz_window(branch, revisions, limit=None):
 
225
def start_viz_window(branch, revision, limit=None):
233
226
    """Start viz on branch with revision revision.
234
227
    
235
228
    :return: The viz window object.
236
229
    """
237
 
    from viz import BranchWindow
238
 
    return BranchWindow(branch, revisions, limit)
 
230
    from viz.branchwin import BranchWindow
 
231
    return BranchWindow(branch, revision, limit)
239
232
 
240
233
 
241
234
class cmd_visualise(Command):
251
244
        "revision",
252
245
        Option('limit', "Maximum number of revisions to display.",
253
246
               int, 'count')]
254
 
    takes_args = [ "locations*" ]
 
247
    takes_args = [ "location?" ]
255
248
    aliases = [ "visualize", "vis", "viz" ]
256
249
 
257
 
    def run(self, locations_list, revision=None, limit=None):
 
250
    def run(self, location=".", revision=None, limit=None):
258
251
        set_ui_factory()
259
 
        if locations_list is None:
260
 
            locations_list = ["."]
261
 
        revids = []
262
 
        for location in locations_list:
263
 
            (br, path) = branch.Branch.open_containing(location)
 
252
        (br, path) = branch.Branch.open_containing(location)
 
253
        br.lock_read()
 
254
        try:
264
255
            if revision is None:
265
 
                revids.append(br.last_revision())
 
256
                revid = br.last_revision()
 
257
                if revid is None:
 
258
                    return
266
259
            else:
267
 
                revids.append(revision[0].as_revision_id(br))
268
 
        import gtk
269
 
        pp = start_viz_window(br, revids, limit)
270
 
        pp.connect("destroy", lambda w: gtk.main_quit())
271
 
        pp.show()
272
 
        gtk.main()
 
260
                (revno, revid) = revision[0].in_history(br)
 
261
 
 
262
            import gtk
 
263
            pp = start_viz_window(br, revid, limit)
 
264
            pp.connect("destroy", lambda w: gtk.main_quit())
 
265
            pp.show()
 
266
            gtk.main()
 
267
        finally:
 
268
            br.unlock()
273
269
 
274
270
 
275
271
class cmd_gannotate(GTKCommand):
289
285
    aliases = ["gblame", "gpraise"]
290
286
    
291
287
    def run(self, filename, all=False, plain=False, line='1', revision=None):
292
 
        gtk = open_display()
 
288
        gtk = self.open_display()
293
289
 
294
290
        try:
295
291
            line = int(line)
314
310
        if revision is not None:
315
311
            if len(revision) != 1:
316
312
                raise BzrCommandError("Only 1 revion may be specified.")
317
 
            revision_id = revision[0].as_revision_id(br)
 
313
            revision_id = revision[0].in_history(br).rev_id
318
314
            tree = br.repository.revision_tree(revision_id)
319
315
        else:
320
316
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
321
317
 
322
 
        window = GAnnotateWindow(all, plain, branch=br)
 
318
        window = GAnnotateWindow(all, plain)
323
319
        window.connect("destroy", lambda w: gtk.main_quit())
 
320
        window.set_title(path + " - gannotate")
324
321
        config = GAnnotateConfig(window)
325
322
        window.show()
326
323
        br.lock_read()
348
345
 
349
346
    def run(self, filename=None):
350
347
        import os
351
 
        open_display()
 
348
        self.open_display()
352
349
        from commit import CommitDialog
353
350
        from bzrlib.errors import (BzrCommandError,
354
351
                                   NotBranchError,
361
358
            br = wt.branch
362
359
        except NoWorkingTree, e:
363
360
            from dialog import error_dialog
364
 
            error_dialog(_i18n('Directory does not have a working tree'),
365
 
                         _i18n('Operation aborted.'))
 
361
            error_dialog(_('Directory does not have a working tree'),
 
362
                         _('Operation aborted.'))
366
363
            return 1 # should this be retval=3?
367
364
 
368
365
        # It is a good habit to keep things locked for the duration, but it
385
382
    
386
383
    aliases = [ "gst" ]
387
384
    takes_args = ['PATH?']
388
 
    takes_options = ['revision']
 
385
    takes_options = []
389
386
 
390
 
    def run(self, path='.', revision=None):
 
387
    def run(self, path='.'):
391
388
        import os
392
 
        gtk = open_display()
 
389
        gtk = self.open_display()
393
390
        from status import StatusDialog
394
391
        (wt, wt_path) = workingtree.WorkingTree.open_containing(path)
395
 
        
396
 
        if revision is not None:
397
 
            try:
398
 
                revision_id = revision[0].as_revision_id(wt.branch)
399
 
            except:
400
 
                from bzrlib.errors import BzrError
401
 
                raise BzrError('Revision %r doesn\'t exist' % revision[0].user_spec )
402
 
        else:
403
 
            revision_id = None
404
 
 
405
 
        status = StatusDialog(wt, wt_path, revision_id)
 
392
        status = StatusDialog(wt, wt_path)
406
393
        status.connect("destroy", gtk.main_quit)
407
394
        status.run()
408
395
 
413
400
    """
414
401
    def run(self):
415
402
        (br, path) = branch.Branch.open_containing(".")
416
 
        gtk = open_display()
 
403
        gtk = self.open_display()
417
404
        from bzrlib.plugins.gtk.mergedirective import SendMergeDirectiveDialog
418
405
        from StringIO import StringIO
419
406
        dialog = SendMergeDirectiveDialog(br)
435
422
    """
436
423
    def run(self):
437
424
        (wt, path) = workingtree.WorkingTree.open_containing('.')
438
 
        open_display()
 
425
        self.open_display()
439
426
        from bzrlib.plugins.gtk.conflicts import ConflictsDialog
440
427
        dialog = ConflictsDialog(wt)
441
428
        dialog.run()
446
433
 
447
434
    """
448
435
    def run(self):
449
 
        open_display()
 
436
        self.open_display()
450
437
        from bzrlib.plugins.gtk.preferences import PreferencesWindow
451
438
        dialog = PreferencesWindow()
452
439
        dialog.run()
453
440
 
454
441
 
455
 
class cmd_gmerge(Command):
456
 
    """ GTK+ merge dialog
457
 
    
458
 
    """
459
 
    takes_args = ["merge_from_path?"]
460
 
    def run(self, merge_from_path=None):
461
 
        from bzrlib import workingtree
462
 
        from bzrlib.plugins.gtk.dialog import error_dialog
463
 
        from bzrlib.plugins.gtk.merge import MergeDialog
464
 
        
465
 
        (wt, path) = workingtree.WorkingTree.open_containing('.')
466
 
        old_tree = wt.branch.repository.revision_tree(wt.branch.last_revision())
467
 
        delta = wt.changes_from(old_tree)
468
 
        if len(delta.added) or len(delta.removed) or len(delta.renamed) or len(delta.modified):
469
 
            error_dialog(_i18n('There are local changes in the branch'),
470
 
                         _i18n('Please commit or revert the changes before merging.'))
471
 
        else:
472
 
            parent_branch_path = wt.branch.get_parent()
473
 
            merge = MergeDialog(wt, path, parent_branch_path)
474
 
            response = merge.run()
475
 
            merge.destroy()
476
 
 
477
 
 
478
442
class cmd_gmissing(Command):
479
443
    """ GTK+ missing revisions dialog.
480
444
 
513
477
 
514
478
class cmd_ginit(GTKCommand):
515
479
    def run(self):
516
 
        open_display()
 
480
        self.open_display()
517
481
        from initialize import InitDialog
518
482
        dialog = InitDialog(os.path.abspath(os.path.curdir))
519
483
        dialog.run()
523
487
    def run(self):
524
488
        br = branch.Branch.open_containing('.')[0]
525
489
        
526
 
        gtk = open_display()
 
490
        gtk = self.open_display()
527
491
        from tags import TagsWindow
528
492
        window = TagsWindow(br)
529
493
        window.show()
538
502
    cmd_gconflicts, 
539
503
    cmd_gdiff,
540
504
    cmd_ginit,
541
 
    cmd_gmerge,
542
505
    cmd_gmissing, 
543
506
    cmd_gpreferences, 
544
507
    cmd_gpush, 
552
515
    register_command(cmd)
553
516
 
554
517
 
 
518
class cmd_commit_notify(GTKCommand):
 
519
    """Run the bzr commit notifier.
 
520
 
 
521
    This is a background program which will pop up a notification on the users
 
522
    screen when a commit occurs.
 
523
    """
 
524
 
 
525
    def run(self):
 
526
        from notify import NotifyPopupMenu
 
527
        gtk = self.open_display()
 
528
        menu = NotifyPopupMenu()
 
529
        icon = gtk.status_icon_new_from_file(os.path.join(data_path(), "bzr-icon-64.png"))
 
530
        icon.connect('popup-menu', menu.display)
 
531
 
 
532
        import cgi
 
533
        import dbus
 
534
        import dbus.service
 
535
        import pynotify
 
536
        from bzrlib.bzrdir import BzrDir
 
537
        from bzrlib import errors
 
538
        from bzrlib.osutils import format_date
 
539
        from bzrlib.transport import get_transport
 
540
        if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
 
541
            import dbus.glib
 
542
        from bzrlib.plugins.dbus import activity
 
543
        bus = dbus.SessionBus()
 
544
        # get the object so we can subscribe to callbacks from it.
 
545
        broadcast_service = bus.get_object(
 
546
            activity.Broadcast.DBUS_NAME,
 
547
            activity.Broadcast.DBUS_PATH)
 
548
 
 
549
        def catch_branch(revision_id, urls):
 
550
            # TODO: show all the urls, or perhaps choose the 'best'.
 
551
            url = urls[0]
 
552
            try:
 
553
                if isinstance(revision_id, unicode):
 
554
                    revision_id = revision_id.encode('utf8')
 
555
                transport = get_transport(url)
 
556
                a_dir = BzrDir.open_from_transport(transport)
 
557
                branch = a_dir.open_branch()
 
558
                revno = branch.revision_id_to_revno(revision_id)
 
559
                revision = branch.repository.get_revision(revision_id)
 
560
                summary = 'New revision %d in %s' % (revno, url)
 
561
                body  = 'Committer: %s\n' % revision.committer
 
562
                body += 'Date: %s\n' % format_date(revision.timestamp,
 
563
                    revision.timezone)
 
564
                body += '\n'
 
565
                body += revision.message
 
566
                body = cgi.escape(body)
 
567
                nw = pynotify.Notification(summary, body)
 
568
                def start_viz(notification=None, action=None, data=None):
 
569
                    """Start the viz program."""
 
570
                    pp = start_viz_window(branch, revision_id)
 
571
                    pp.show()
 
572
                def start_branch(notification=None, action=None, data=None):
 
573
                    """Start a Branch dialog"""
 
574
                    from bzrlib.plugins.gtk.branch import BranchDialog
 
575
                    bd = BranchDialog(remote_path=url)
 
576
                    bd.run()
 
577
                nw.add_action("inspect", "Inspect", start_viz, None)
 
578
                nw.add_action("branch", "Branch", start_branch, None)
 
579
                nw.set_timeout(5000)
 
580
                nw.show()
 
581
            except Exception, e:
 
582
                print e
 
583
                raise
 
584
        broadcast_service.connect_to_signal("Revision", catch_branch,
 
585
            dbus_interface=activity.Broadcast.DBUS_INTERFACE)
 
586
        pynotify.init("bzr commit-notify")
 
587
        gtk.main()
 
588
 
 
589
register_command(cmd_commit_notify)
 
590
 
 
591
 
555
592
class cmd_gselftest(GTKCommand):
556
593
    """Version of selftest that displays a notification at the end"""
557
594
 
591
628
 
592
629
    takes_options = ['verbose',
593
630
                     Option('one', short_name='1',
594
 
                            help='Stop when one test fails.'),
595
 
                     Option('benchmark', help='Run the benchmarks.'),
 
631
                            help='stop when one test fails'),
 
632
                     Option('benchmark', help='run the benchmarks.'),
596
633
                     Option('lsprof-timed',
597
 
                     help='Generate lsprof output for benchmarked'
 
634
                     help='generate lsprof output for benchmarked'
598
635
                          ' sections of code.'),
599
636
                     Option('list-only',
600
 
                     help='List the tests instead of running them.'),
 
637
                     help='list the tests instead of running them'),
601
638
                     Option('randomize', type=str, argname="SEED",
602
 
                     help='Randomize the order of tests using the given'
603
 
                          ' seed or "now" for the current time.'),
 
639
                     help='randomize the order of tests using the given'
 
640
                          ' seed or "now" for the current time'),
604
641
                    ]
605
642
    takes_args = ['testspecs*']
606
643
 
648
685
register_command(cmd_test_gtk)
649
686
 
650
687
 
651
 
 
652
688
import gettext
653
689
gettext.install('olive-gtk')
654
690
 
655
 
# Let's create a specialized alias to protect '_' from being erased by other
656
 
# uses of '_' as an anonymous variable (think pdb for one).
657
 
_i18n = gettext.gettext
658
691
 
659
692
class NoDisplayError(BzrCommandError):
660
693
    """gtk could not find a proper display"""
670
703
    default_encoding = sys.getdefaultencoding()
671
704
    try:
672
705
        result = TestSuite()
673
 
        try:
674
 
            import_pygtk()
675
 
        except errors.BzrCommandError:
676
 
            return result
677
706
        result.addTest(tests.test_suite())
678
707
    finally:
679
708
        if sys.getdefaultencoding() != default_encoding: