/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: Scott James Remnant
  • Date: 2005-10-17 01:07:49 UTC
  • Revision ID: scott@netsplit.com-20051017010749-15fa95fc2cf09289
Commit the first version of bzrk.

Show diffs side-by-side

added added

removed removed

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