/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: 2006-05-19 16:56:46 UTC
  • mfrom: (0.1.25 gannotate)
  • Revision ID: jelmer@samba.org-20060519165646-0d867938fdbc9097
Merge in Dan Loda's gannotate plugin and put it in annotate/

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
1
3
# This program is free software; you can redistribute it and/or modify
2
4
# it under the terms of the GNU General Public License as published by
3
5
# the Free Software Foundation; either version 2 of the License, or
12
14
# along with this program; if not, write to the Free Software
13
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14
16
 
15
 
"""Graphical support for Bazaar using GTK.
16
 
 
17
 
This plugin includes:
18
 
commit-notify     Start the graphical notifier of commits.
19
 
gannotate         GTK+ annotate. 
20
 
gbranch           GTK+ branching. 
21
 
gcheckout         GTK+ checkout. 
22
 
gcommit           GTK+ commit dialog 
23
 
gconflicts        GTK+ push. 
24
 
gdiff             Show differences in working tree in a GTK+ Window. 
25
 
ginit             Initialise a new branch.
26
 
gmissing          GTK+ missing revisions dialog. 
27
 
gpreferences      GTK+ preferences dialog. 
28
 
gpush             GTK+ push. 
29
 
gstatus           GTK+ status dialog 
30
 
gtags             Manage branch tags.
31
 
visualise         Graphically visualise this branch. 
32
 
"""
33
 
 
34
 
import bzrlib
35
 
 
36
 
__version__ = '0.19.0'
37
 
version_info = tuple(int(n) for n in __version__.split('.'))
38
 
 
39
 
 
40
 
def check_bzrlib_version(desired):
41
 
    """Check that bzrlib is compatible.
42
 
 
43
 
    If version is < bzr-gtk version, assume incompatible.
44
 
    If version == bzr-gtk version, assume completely compatible
45
 
    If version == bzr-gtk version + 1, assume compatible, with deprecations
46
 
    Otherwise, assume incompatible.
47
 
    """
48
 
    desired_plus = (desired[0], desired[1]+1)
49
 
    bzrlib_version = bzrlib.version_info[:2]
50
 
    if bzrlib_version == desired or (bzrlib_version == desired_plus and
51
 
                                     bzrlib.version_info[3] == 'dev'):
52
 
        return
53
 
    try:
54
 
        from bzrlib.trace import warning
55
 
    except ImportError:
56
 
        # get the message out any way we can
57
 
        from warnings import warn as warning
58
 
    if bzrlib_version < desired:
59
 
        from bzrlib.errors import BzrError
60
 
        warning('Installed bzr version %s is too old to be used with bzr-gtk'
61
 
                ' %s.' % (bzrlib.__version__, __version__))
62
 
        raise BzrError('Version mismatch: %r' % version_info)
63
 
    else:
64
 
        warning('bzr-gtk is not up to date with installed bzr version %s.'
65
 
                ' \nThere should be a newer version available, e.g. %i.%i.' 
66
 
                % (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
67
 
 
68
 
 
69
 
## check_bzrlib_version(version_info[:2]) REMOVE
70
 
 
71
 
from bzrlib.trace import warning
72
 
if __name__ != 'bzrlib.plugins.gtk':
73
 
    warning("Not running as bzrlib.plugins.gtk, things may break.")
74
 
 
75
 
from bzrlib.lazy_import import lazy_import
76
 
lazy_import(globals(), """
77
 
from bzrlib import (
78
 
    branch,
79
 
    errors,
80
 
    workingtree,
81
 
    )
82
 
""")
83
 
 
84
 
from bzrlib.commands import Command, register_command, display_command
85
 
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
86
 
from bzrlib.option import Option
87
 
 
88
 
import os.path
89
 
 
90
 
def import_pygtk():
91
 
    try:
92
 
        import pygtk
93
 
    except ImportError:
94
 
        raise errors.BzrCommandError("PyGTK not installed.")
95
 
    pygtk.require('2.0')
96
 
    return pygtk
97
 
 
98
 
 
99
 
def set_ui_factory():
100
 
    import_pygtk()
101
 
    from ui import GtkUIFactory
102
 
    import bzrlib.ui
103
 
    bzrlib.ui.ui_factory = GtkUIFactory()
104
 
 
105
 
 
106
 
class GTKCommand(Command):
107
 
    """Abstract class providing GTK specific run commands."""
108
 
 
109
 
    def open_display(self):
110
 
        pygtk = import_pygtk()
111
 
        try:
112
 
            import gtk
113
 
        except RuntimeError, e:
114
 
            if str(e) == "could not open display":
115
 
                raise NoDisplayError
116
 
        set_ui_factory()
117
 
        return gtk
118
 
 
119
 
    def run(self):
120
 
        self.open_display()
121
 
        dialog = self.get_gtk_dialog(os.path.abspath('.'))
122
 
        dialog.run()
123
 
 
124
 
 
125
 
class cmd_gbranch(GTKCommand):
126
 
    """GTK+ branching.
127
 
    
128
 
    """
129
 
 
130
 
    def get_gtk_dialog(self, path):
131
 
        from bzrlib.plugins.gtk.branch import BranchDialog
132
 
        return BranchDialog(path)
133
 
 
134
 
 
135
 
class cmd_gcheckout(GTKCommand):
136
 
    """ GTK+ checkout.
137
 
    
138
 
    """
139
 
    
140
 
    def get_gtk_dialog(self, path):
141
 
        from bzrlib.plugins.gtk.checkout import CheckoutDialog
142
 
        return CheckoutDialog(path)
143
 
 
144
 
 
145
 
 
146
 
class cmd_gpush(GTKCommand):
147
 
    """ GTK+ push.
148
 
    
149
 
    """
150
 
    takes_args = [ "location?" ]
151
 
 
152
 
    def run(self, location="."):
153
 
        (br, path) = branch.Branch.open_containing(location)
154
 
        self.open_display()
155
 
        from push import PushDialog
156
 
        dialog = PushDialog(br.repository, br.last_revision(), br)
157
 
        dialog.run()
158
 
 
159
 
 
160
 
 
161
 
class cmd_gdiff(GTKCommand):
162
 
    """Show differences in working tree in a GTK+ Window.
163
 
    
164
 
    Otherwise, all changes for the tree are listed.
165
 
    """
166
 
    takes_args = ['filename?']
167
 
    takes_options = ['revision']
168
 
 
169
 
    @display_command
170
 
    def run(self, revision=None, filename=None):
171
 
        set_ui_factory()
172
 
        wt = workingtree.WorkingTree.open_containing(".")[0]
173
 
        wt.lock_read()
174
 
        try:
175
 
            branch = wt.branch
176
 
            if revision is not None:
177
 
                if len(revision) == 1:
178
 
                    tree1 = wt
179
 
                    revision_id = revision[0].in_history(branch).rev_id
180
 
                    tree2 = branch.repository.revision_tree(revision_id)
181
 
                elif len(revision) == 2:
182
 
                    revision_id_0 = revision[0].in_history(branch).rev_id
183
 
                    tree2 = branch.repository.revision_tree(revision_id_0)
184
 
                    revision_id_1 = revision[1].in_history(branch).rev_id
185
 
                    tree1 = branch.repository.revision_tree(revision_id_1)
186
 
            else:
187
 
                tree1 = wt
188
 
                tree2 = tree1.basis_tree()
189
 
 
190
 
            from diff import DiffWindow
191
 
            import gtk
192
 
            window = DiffWindow()
193
 
            window.connect("destroy", gtk.main_quit)
194
 
            window.set_diff("Working Tree", tree1, tree2)
195
 
            if filename is not None:
196
 
                tree_filename = wt.relpath(filename)
197
 
                try:
198
 
                    window.set_file(tree_filename)
199
 
                except NoSuchFile:
200
 
                    if (tree1.path2id(tree_filename) is None and 
201
 
                        tree2.path2id(tree_filename) is None):
202
 
                        raise NotVersionedError(filename)
203
 
                    raise BzrCommandError('No changes found for file "%s"' % 
204
 
                                          filename)
205
 
            window.show()
206
 
 
207
 
            gtk.main()
208
 
        finally:
209
 
            wt.unlock()
210
 
 
211
 
 
212
 
def start_viz_window(branch, revision, limit=None):
213
 
    """Start viz on branch with revision revision.
214
 
    
215
 
    :return: The viz window object.
216
 
    """
217
 
    from viz.branchwin import BranchWindow
218
 
    branch.lock_read()
219
 
    pp = BranchWindow()
220
 
    pp.set_branch(branch, revision, limit)
221
 
    # cleanup locks when the window is closed
222
 
    pp.connect("destroy", lambda w: branch.unlock())
223
 
    return pp
224
 
 
225
 
 
226
 
class cmd_visualise(Command):
227
 
    """Graphically visualise this branch.
228
 
 
229
 
    Opens a graphical window to allow you to see the history of the branch
230
 
    and relationships between revisions in a visual manner,
231
 
 
232
 
    The default starting point is latest revision on the branch, you can
233
 
    specify a starting point with -r revision.
234
 
    """
235
 
    takes_options = [
236
 
        "revision",
237
 
        Option('limit', "Maximum number of revisions to display.",
238
 
               int, 'count')]
239
 
    takes_args = [ "location?" ]
240
 
    aliases = [ "visualize", "vis", "viz" ]
241
 
 
242
 
    def run(self, location=".", revision=None, limit=None):
243
 
        set_ui_factory()
244
 
        (br, path) = branch.Branch.open_containing(location)
245
 
        br.lock_read()
246
 
        try:
247
 
            if revision is None:
248
 
                revid = br.last_revision()
249
 
                if revid is None:
250
 
                    return
251
 
            else:
252
 
                (revno, revid) = revision[0].in_history(br)
253
 
 
254
 
            import gtk
255
 
            pp = start_viz_window(br, revid, limit)
256
 
            pp.connect("destroy", lambda w: gtk.main_quit())
257
 
            pp.show()
258
 
            gtk.main()
259
 
        finally:
260
 
            br.unlock()
261
 
 
262
 
 
263
 
class cmd_gannotate(GTKCommand):
264
 
    """GTK+ annotate.
265
 
    
266
 
    Browse changes to FILENAME line by line in a GTK+ window.
267
 
    """
268
 
 
269
 
    takes_args = ["filename", "line?"]
270
 
    takes_options = [
271
 
        Option("all", help="Show annotations on all lines."),
272
 
        Option("plain", help="Don't highlight annotation lines."),
273
 
        Option("line", type=int, argname="lineno",
274
 
               help="Jump to specified line number."),
275
 
        "revision",
276
 
    ]
277
 
    aliases = ["gblame", "gpraise"]
278
 
    
279
 
    def run(self, filename, all=False, plain=False, line='1', revision=None):
280
 
        gtk = self.open_display()
281
 
 
282
 
        try:
283
 
            line = int(line)
284
 
        except ValueError:
285
 
            raise BzrCommandError('Line argument ("%s") is not a number.' % 
286
 
                                  line)
287
 
 
288
 
        from annotate.gannotate import GAnnotateWindow
289
 
        from annotate.config import GAnnotateConfig
290
 
        from bzrlib.bzrdir import BzrDir
291
 
 
292
 
        wt, br, path = BzrDir.open_containing_tree_or_branch(filename)
293
 
        if wt is not None:
294
 
            tree = wt
295
 
        else:
296
 
            tree = br.basis_tree()
297
 
 
298
 
        file_id = tree.path2id(path)
299
 
 
300
 
        if file_id is None:
301
 
            raise NotVersionedError(filename)
302
 
        if revision is not None:
303
 
            if len(revision) != 1:
304
 
                raise BzrCommandError("Only 1 revion may be specified.")
305
 
            revision_id = revision[0].in_history(br).rev_id
306
 
            tree = br.repository.revision_tree(revision_id)
307
 
        else:
308
 
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
309
 
 
310
 
        window = GAnnotateWindow(all, plain)
311
 
        window.connect("destroy", lambda w: gtk.main_quit())
312
 
        window.set_title(path + " - gannotate")
313
 
        config = GAnnotateConfig(window)
314
 
        window.show()
315
 
        br.lock_read()
316
 
        if wt is not None:
317
 
            wt.lock_read()
318
 
        try:
319
 
            window.annotate(tree, br, file_id)
320
 
            window.jump_to_line(line)
321
 
            gtk.main()
322
 
        finally:
323
 
            br.unlock()
324
 
            if wt is not None:
325
 
                wt.unlock()
326
 
 
327
 
 
328
 
 
329
 
class cmd_gcommit(GTKCommand):
330
 
    """GTK+ commit dialog
331
 
 
332
 
    Graphical user interface for committing revisions"""
333
 
    
334
 
    aliases = [ "gci" ]
335
 
    takes_args = []
336
 
    takes_options = []
337
 
 
338
 
    def run(self, filename=None):
339
 
        import os
340
 
        self.open_display()
341
 
        from commit import CommitDialog
342
 
        from bzrlib.errors import (BzrCommandError,
343
 
                                   NotBranchError,
344
 
                                   NoWorkingTree)
345
 
 
346
 
        wt = None
347
 
        br = None
348
 
        try:
349
 
            (wt, path) = workingtree.WorkingTree.open_containing(filename)
350
 
            br = wt.branch
351
 
        except NoWorkingTree, e:
352
 
            path = e.base
353
 
            (br, path) = branch.Branch.open_containing(path)
354
 
 
355
 
        commit = CommitDialog(wt, path, not br)
356
 
        commit.run()
357
 
 
358
 
 
359
 
 
360
 
class cmd_gstatus(GTKCommand):
361
 
    """GTK+ status dialog
362
 
 
363
 
    Graphical user interface for showing status 
364
 
    information."""
365
 
    
366
 
    aliases = [ "gst" ]
367
 
    takes_args = ['PATH?']
368
 
    takes_options = []
369
 
 
370
 
    def run(self, path='.'):
371
 
        import os
372
 
        gtk = self.open_display()
373
 
        from status import StatusDialog
374
 
        (wt, wt_path) = workingtree.WorkingTree.open_containing(path)
375
 
        status = StatusDialog(wt, wt_path)
376
 
        status.connect("destroy", gtk.main_quit)
377
 
        status.run()
378
 
 
379
 
 
380
 
 
381
 
class cmd_gconflicts(GTKCommand):
382
 
    """ GTK+ push.
383
 
    
384
 
    """
385
 
    def run(self):
386
 
        (wt, path) = workingtree.WorkingTree.open_containing('.')
387
 
        self.open_display()
388
 
        from bzrlib.plugins.gtk.conflicts import ConflictsDialog
389
 
        dialog = ConflictsDialog(wt)
390
 
        dialog.run()
391
 
 
392
 
 
393
 
 
394
 
class cmd_gpreferences(GTKCommand):
395
 
    """ GTK+ preferences dialog.
396
 
 
397
 
    """
398
 
    def run(self):
399
 
        self.open_display()
400
 
        from bzrlib.plugins.gtk.preferences import PreferencesWindow
401
 
        dialog = PreferencesWindow()
402
 
        dialog.run()
403
 
 
404
 
 
405
 
 
406
 
class cmd_gmissing(Command):
407
 
    """ GTK+ missing revisions dialog.
408
 
 
409
 
    """
410
 
    takes_args = ["other_branch?"]
411
 
    def run(self, other_branch=None):
412
 
        pygtk = import_pygtk()
413
 
        try:
414
 
            import gtk
415
 
        except RuntimeError, e:
416
 
            if str(e) == "could not open display":
417
 
                raise NoDisplayError
418
 
 
419
 
        from bzrlib.plugins.gtk.missing import MissingWindow
420
 
        from bzrlib.branch import Branch
421
 
 
422
 
        local_branch = Branch.open_containing(".")[0]
423
 
        if other_branch is None:
424
 
            other_branch = local_branch.get_parent()
425
 
            
426
 
            if other_branch is None:
427
 
                raise errors.BzrCommandError("No peer location known or specified.")
428
 
        remote_branch = Branch.open_containing(other_branch)[0]
429
 
        set_ui_factory()
430
 
        local_branch.lock_read()
431
 
        try:
432
 
            remote_branch.lock_read()
433
 
            try:
434
 
                dialog = MissingWindow(local_branch, remote_branch)
435
 
                dialog.run()
436
 
            finally:
437
 
                remote_branch.unlock()
438
 
        finally:
439
 
            local_branch.unlock()
440
 
 
441
 
 
442
 
class cmd_ginit(GTKCommand):
443
 
    def run(self):
444
 
        self.open_display()
445
 
        from initialize import InitDialog
446
 
        dialog = InitDialog(os.path.abspath(os.path.curdir))
447
 
        dialog.run()
448
 
 
449
 
 
450
 
class cmd_gtags(GTKCommand):
451
 
    def run(self):
452
 
        br = branch.Branch.open_containing('.')[0]
453
 
        
454
 
        gtk = self.open_display()
455
 
        from tags import TagsWindow
456
 
        window = TagsWindow(br)
457
 
        window.show()
458
 
        gtk.main()
459
 
 
460
 
 
461
 
commands = [
462
 
    cmd_gmissing, 
463
 
    cmd_gpreferences, 
464
 
    cmd_gconflicts, 
465
 
    cmd_gstatus,
466
 
    cmd_gcommit, 
467
 
    cmd_gannotate, 
468
 
    cmd_visualise, 
469
 
    cmd_gdiff,
470
 
    cmd_gpush, 
471
 
    cmd_gcheckout, 
472
 
    cmd_gbranch,
473
 
    cmd_ginit,
474
 
    cmd_gtags
475
 
    ]
476
 
 
477
 
for cmd in commands:
478
 
    register_command(cmd)
479
 
 
480
 
 
481
 
class cmd_commit_notify(GTKCommand):
482
 
    """Run the bzr commit notifier.
483
 
 
484
 
    This is a background program which will pop up a notification on the users
485
 
    screen when a commit occurs.
486
 
    """
487
 
 
488
 
    def run(self):
489
 
        from notify import NotifyPopupMenu
490
 
        gtk = self.open_display()
491
 
        menu = NotifyPopupMenu()
492
 
        icon = gtk.status_icon_new_from_file("bzr-icon-64.png")
493
 
        icon.connect('popup-menu', menu.display)
494
 
 
495
 
        import cgi
496
 
        import dbus
497
 
        import dbus.service
498
 
        import pynotify
499
 
        from bzrlib.bzrdir import BzrDir
500
 
        from bzrlib import errors
501
 
        from bzrlib.osutils import format_date
502
 
        from bzrlib.transport import get_transport
503
 
        if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
504
 
            import dbus.glib
505
 
        from bzrlib.plugins.dbus import activity
506
 
        bus = dbus.SessionBus()
507
 
        # get the object so we can subscribe to callbacks from it.
508
 
        broadcast_service = bus.get_object(
509
 
            activity.Broadcast.DBUS_NAME,
510
 
            activity.Broadcast.DBUS_PATH)
511
 
 
512
 
        def catch_branch(revision_id, urls):
513
 
            # TODO: show all the urls, or perhaps choose the 'best'.
514
 
            url = urls[0]
515
 
            try:
516
 
                if isinstance(revision_id, unicode):
517
 
                    revision_id = revision_id.encode('utf8')
518
 
                transport = get_transport(url)
519
 
                a_dir = BzrDir.open_from_transport(transport)
520
 
                branch = a_dir.open_branch()
521
 
                revno = branch.revision_id_to_revno(revision_id)
522
 
                revision = branch.repository.get_revision(revision_id)
523
 
                summary = 'New revision %d in %s' % (revno, url)
524
 
                body  = 'Committer: %s\n' % revision.committer
525
 
                body += 'Date: %s\n' % format_date(revision.timestamp,
526
 
                    revision.timezone)
527
 
                body += '\n'
528
 
                body += revision.message
529
 
                body = cgi.escape(body)
530
 
                nw = pynotify.Notification(summary, body)
531
 
                def start_viz(notification=None, action=None, data=None):
532
 
                    """Start the viz program."""
533
 
                    pp = start_viz_window(branch, revision_id)
534
 
                    pp.show()
535
 
                def start_branch(notification=None, action=None, data=None):
536
 
                    """Start a Branch dialog"""
537
 
                    from bzrlib.plugins.gtk.branch import BranchDialog
538
 
                    bd = BranchDialog(remote_path=url)
539
 
                    bd.run()
540
 
                nw.add_action("inspect", "Inspect", start_viz, None)
541
 
                nw.add_action("branch", "Branch", start_branch, None)
542
 
                nw.set_timeout(5000)
543
 
                nw.show()
544
 
            except Exception, e:
545
 
                print e
546
 
                raise
547
 
        broadcast_service.connect_to_signal("Revision", catch_branch,
548
 
            dbus_interface=activity.Broadcast.DBUS_INTERFACE)
549
 
        pynotify.init("bzr commit-notify")
550
 
        gtk.main()
551
 
 
552
 
register_command(cmd_commit_notify)
553
 
 
554
 
 
555
 
import gettext
556
 
gettext.install('olive-gtk')
557
 
 
558
 
 
559
 
class NoDisplayError(BzrCommandError):
560
 
    """gtk could not find a proper display"""
561
 
 
562
 
    def __str__(self):
563
 
        return "No DISPLAY. Unable to run GTK+ application."
564
 
 
565
 
 
566
 
def test_suite():
567
 
    from unittest import TestSuite
568
 
    import tests
569
 
    import sys
570
 
    default_encoding = sys.getdefaultencoding()
571
 
    try:
572
 
        result = TestSuite()
573
 
        result.addTest(tests.test_suite())
574
 
    finally:
575
 
        if sys.getdefaultencoding() != default_encoding:
576
 
            reload(sys)
577
 
            sys.setdefaultencoding(default_encoding)
578
 
    return result
 
17
"""GTK+ frontends to Bazaar commands """
 
18
import viz
 
19
import annotate