/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: Daniel Schierbeck
  • Date: 2007-12-06 23:37:06 UTC
  • mto: This revision was merged to the branch mainline in revision 417.
  • Revision ID: daniel.schierbeck@gmail.com-20071206233706-eeinks66w86r3gfm
Fixed bug in gmissing.

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, 93, 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)
264
 
            if revision is None:
265
 
                revids.append(br.last_revision())
266
 
            else:
267
 
                revids.append(revision[0].as_revision_id(br))
 
252
        (br, path) = branch.Branch.open_containing(location)
 
253
        if revision is None:
 
254
            revid = br.last_revision()
 
255
            if revid is None:
 
256
                return
 
257
        else:
 
258
            (revno, revid) = revision[0].in_history(br)
 
259
 
268
260
        import gtk
269
 
        pp = start_viz_window(br, revids, limit)
 
261
        pp = start_viz_window(br, revid, limit)
270
262
        pp.connect("destroy", lambda w: gtk.main_quit())
271
263
        pp.show()
272
264
        gtk.main()
289
281
    aliases = ["gblame", "gpraise"]
290
282
    
291
283
    def run(self, filename, all=False, plain=False, line='1', revision=None):
292
 
        gtk = open_display()
 
284
        gtk = self.open_display()
293
285
 
294
286
        try:
295
287
            line = int(line)
314
306
        if revision is not None:
315
307
            if len(revision) != 1:
316
308
                raise BzrCommandError("Only 1 revion may be specified.")
317
 
            revision_id = revision[0].as_revision_id(br)
 
309
            revision_id = revision[0].in_history(br).rev_id
318
310
            tree = br.repository.revision_tree(revision_id)
319
311
        else:
320
312
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
321
313
 
322
 
        window = GAnnotateWindow(all, plain, branch=br)
 
314
        window = GAnnotateWindow(all, plain)
323
315
        window.connect("destroy", lambda w: gtk.main_quit())
 
316
        window.set_title(path + " - gannotate")
324
317
        config = GAnnotateConfig(window)
325
318
        window.show()
326
319
        br.lock_read()
348
341
 
349
342
    def run(self, filename=None):
350
343
        import os
351
 
        open_display()
 
344
        self.open_display()
352
345
        from commit import CommitDialog
353
346
        from bzrlib.errors import (BzrCommandError,
354
347
                                   NotBranchError,
361
354
            br = wt.branch
362
355
        except NoWorkingTree, e:
363
356
            from dialog import error_dialog
364
 
            error_dialog(_i18n('Directory does not have a working tree'),
365
 
                         _i18n('Operation aborted.'))
 
357
            error_dialog(_('Directory does not have a working tree'),
 
358
                         _('Operation aborted.'))
366
359
            return 1 # should this be retval=3?
367
360
 
368
361
        # It is a good habit to keep things locked for the duration, but it
385
378
    
386
379
    aliases = [ "gst" ]
387
380
    takes_args = ['PATH?']
388
 
    takes_options = ['revision']
 
381
    takes_options = []
389
382
 
390
 
    def run(self, path='.', revision=None):
 
383
    def run(self, path='.'):
391
384
        import os
392
 
        gtk = open_display()
 
385
        gtk = self.open_display()
393
386
        from status import StatusDialog
394
387
        (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)
 
388
        status = StatusDialog(wt, wt_path)
406
389
        status.connect("destroy", gtk.main_quit)
407
390
        status.run()
408
391
 
413
396
    """
414
397
    def run(self):
415
398
        (br, path) = branch.Branch.open_containing(".")
416
 
        gtk = open_display()
 
399
        gtk = self.open_display()
417
400
        from bzrlib.plugins.gtk.mergedirective import SendMergeDirectiveDialog
418
401
        from StringIO import StringIO
419
402
        dialog = SendMergeDirectiveDialog(br)
435
418
    """
436
419
    def run(self):
437
420
        (wt, path) = workingtree.WorkingTree.open_containing('.')
438
 
        open_display()
 
421
        self.open_display()
439
422
        from bzrlib.plugins.gtk.conflicts import ConflictsDialog
440
423
        dialog = ConflictsDialog(wt)
441
424
        dialog.run()
446
429
 
447
430
    """
448
431
    def run(self):
449
 
        open_display()
 
432
        self.open_display()
450
433
        from bzrlib.plugins.gtk.preferences import PreferencesWindow
451
434
        dialog = PreferencesWindow()
452
435
        dialog.run()
453
436
 
454
437
 
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
438
class cmd_gmissing(Command):
479
439
    """ GTK+ missing revisions dialog.
480
440
 
513
473
 
514
474
class cmd_ginit(GTKCommand):
515
475
    def run(self):
516
 
        open_display()
 
476
        self.open_display()
517
477
        from initialize import InitDialog
518
478
        dialog = InitDialog(os.path.abspath(os.path.curdir))
519
479
        dialog.run()
523
483
    def run(self):
524
484
        br = branch.Branch.open_containing('.')[0]
525
485
        
526
 
        gtk = open_display()
 
486
        gtk = self.open_display()
527
487
        from tags import TagsWindow
528
488
        window = TagsWindow(br)
529
489
        window.show()
538
498
    cmd_gconflicts, 
539
499
    cmd_gdiff,
540
500
    cmd_ginit,
541
 
    cmd_gmerge,
542
501
    cmd_gmissing, 
543
502
    cmd_gpreferences, 
544
503
    cmd_gpush, 
552
511
    register_command(cmd)
553
512
 
554
513
 
 
514
class cmd_commit_notify(GTKCommand):
 
515
    """Run the bzr commit notifier.
 
516
 
 
517
    This is a background program which will pop up a notification on the users
 
518
    screen when a commit occurs.
 
519
    """
 
520
 
 
521
    def run(self):
 
522
        from notify import NotifyPopupMenu
 
523
        gtk = self.open_display()
 
524
        menu = NotifyPopupMenu()
 
525
        icon = gtk.status_icon_new_from_file(os.path.join(data_path(), "bzr-icon-64.png"))
 
526
        icon.connect('popup-menu', menu.display)
 
527
 
 
528
        import cgi
 
529
        import dbus
 
530
        import dbus.service
 
531
        import pynotify
 
532
        from bzrlib.bzrdir import BzrDir
 
533
        from bzrlib import errors
 
534
        from bzrlib.osutils import format_date
 
535
        from bzrlib.transport import get_transport
 
536
        if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
 
537
            import dbus.glib
 
538
        from bzrlib.plugins.dbus import activity
 
539
        bus = dbus.SessionBus()
 
540
        # get the object so we can subscribe to callbacks from it.
 
541
        broadcast_service = bus.get_object(
 
542
            activity.Broadcast.DBUS_NAME,
 
543
            activity.Broadcast.DBUS_PATH)
 
544
 
 
545
        def catch_branch(revision_id, urls):
 
546
            # TODO: show all the urls, or perhaps choose the 'best'.
 
547
            url = urls[0]
 
548
            try:
 
549
                if isinstance(revision_id, unicode):
 
550
                    revision_id = revision_id.encode('utf8')
 
551
                transport = get_transport(url)
 
552
                a_dir = BzrDir.open_from_transport(transport)
 
553
                branch = a_dir.open_branch()
 
554
                revno = branch.revision_id_to_revno(revision_id)
 
555
                revision = branch.repository.get_revision(revision_id)
 
556
                summary = 'New revision %d in %s' % (revno, url)
 
557
                body  = 'Committer: %s\n' % revision.committer
 
558
                body += 'Date: %s\n' % format_date(revision.timestamp,
 
559
                    revision.timezone)
 
560
                body += '\n'
 
561
                body += revision.message
 
562
                body = cgi.escape(body)
 
563
                nw = pynotify.Notification(summary, body)
 
564
                def start_viz(notification=None, action=None, data=None):
 
565
                    """Start the viz program."""
 
566
                    pp = start_viz_window(branch, revision_id)
 
567
                    pp.show()
 
568
                def start_branch(notification=None, action=None, data=None):
 
569
                    """Start a Branch dialog"""
 
570
                    from bzrlib.plugins.gtk.branch import BranchDialog
 
571
                    bd = BranchDialog(remote_path=url)
 
572
                    bd.run()
 
573
                nw.add_action("inspect", "Inspect", start_viz, None)
 
574
                nw.add_action("branch", "Branch", start_branch, None)
 
575
                nw.set_timeout(5000)
 
576
                nw.show()
 
577
            except Exception, e:
 
578
                print e
 
579
                raise
 
580
        broadcast_service.connect_to_signal("Revision", catch_branch,
 
581
            dbus_interface=activity.Broadcast.DBUS_INTERFACE)
 
582
        pynotify.init("bzr commit-notify")
 
583
        gtk.main()
 
584
 
 
585
register_command(cmd_commit_notify)
 
586
 
 
587
 
555
588
class cmd_gselftest(GTKCommand):
556
589
    """Version of selftest that displays a notification at the end"""
557
590
 
648
681
register_command(cmd_test_gtk)
649
682
 
650
683
 
651
 
 
652
684
import gettext
653
685
gettext.install('olive-gtk')
654
686
 
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
687
 
659
688
class NoDisplayError(BzrCommandError):
660
689
    """gtk could not find a proper display"""