/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-10-30 21:15:13 UTC
  • mfrom: (326 trunk)
  • mto: (330.3.3 trunk)
  • mto: This revision was merged to the branch mainline in revision 368.
  • Revision ID: john@arbash-meinel.com-20071030211513-l8ukdfa81g1y74mi
Merge the latest trunk 326

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
# along with this program; if not, write to the Free Software
13
13
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14
14
 
15
 
"""GTK+ frontends to Bazaar commands """
 
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
gsend             GTK+ send merge directive.
 
30
gstatus           GTK+ status dialog.
 
31
gtags             Manage branch tags.
 
32
visualise         Graphically visualise this branch. 
 
33
"""
 
34
 
 
35
import sys
 
36
 
 
37
import bzrlib
 
38
 
 
39
version_info = (0, 92, 0, 'dev', 0)
 
40
 
 
41
if version_info[3] == 'final':
 
42
    version_string = '%d.%d.%d' % version_info[:3]
 
43
else:
 
44
    version_string = '%d.%d.%d%s%d' % version_info
 
45
__version__ = version_string
 
46
 
 
47
def check_bzrlib_version(desired):
 
48
    """Check that bzrlib is compatible.
 
49
 
 
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.
 
54
    """
 
55
    desired_plus = (desired[0], desired[1]+1)
 
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
 
60
    try:
 
61
        from bzrlib.trace import warning
 
62
    except ImportError:
 
63
        # get the message out any way we can
 
64
        from warnings import warn as warning
 
65
    if bzrlib_version < desired:
 
66
        from bzrlib.errors import BzrError
 
67
        warning('Installed Bazaar version %s is too old to be used with bzr-gtk'
 
68
                ' %s.' % (bzrlib.__version__, __version__))
 
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]))
 
74
 
 
75
 
 
76
if version_info[2] == "final":
 
77
    check_bzrlib_version(version_info[:2])
 
78
 
 
79
from bzrlib.trace import warning
 
80
if __name__ != 'bzrlib.plugins.gtk':
 
81
    warning("Not running as bzrlib.plugins.gtk, things may break.")
 
82
 
 
83
from bzrlib.lazy_import import lazy_import
 
84
lazy_import(globals(), """
 
85
from bzrlib import (
 
86
    branch,
 
87
    builtins,
 
88
    errors,
 
89
    workingtree,
 
90
    )
 
91
""")
16
92
 
17
93
from bzrlib.commands import Command, register_command, display_command
18
94
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
19
 
from bzrlib.commands import Command, register_command
20
95
from bzrlib.option import Option
21
 
from bzrlib.branch import Branch
22
 
from bzrlib.workingtree import WorkingTree
23
 
from bzrlib.bzrdir import BzrDir
24
 
 
25
 
__version__ = '0.13.0'
26
 
 
27
 
class cmd_gbranch(Command):
 
96
 
 
97
import os.path
 
98
 
 
99
def import_pygtk():
 
100
    try:
 
101
        import pygtk
 
102
    except ImportError:
 
103
        raise errors.BzrCommandError("PyGTK not installed.")
 
104
    pygtk.require('2.0')
 
105
    return pygtk
 
106
 
 
107
 
 
108
def set_ui_factory():
 
109
    import_pygtk()
 
110
    from ui import GtkUIFactory
 
111
    import bzrlib.ui
 
112
    bzrlib.ui.ui_factory = GtkUIFactory()
 
113
 
 
114
 
 
115
def data_path():
 
116
    return os.path.dirname(__file__)
 
117
 
 
118
 
 
119
class GTKCommand(Command):
 
120
    """Abstract class providing GTK specific run commands."""
 
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
 
 
132
    def run(self):
 
133
        self.open_display()
 
134
        dialog = self.get_gtk_dialog(os.path.abspath('.'))
 
135
        dialog.run()
 
136
 
 
137
 
 
138
class cmd_gbranch(GTKCommand):
28
139
    """GTK+ branching.
29
140
    
30
141
    """
31
142
 
32
 
    def run(self):
33
 
        import pygtk
34
 
        pygtk.require("2.0")
35
 
        try:
36
 
            import gtk
37
 
        except RuntimeError, e:
38
 
            if str(e) == "could not open display":
39
 
                raise NoDisplayError
40
 
 
41
 
        from bzrlib.plugins.gtk.olive.branch import BranchDialog
42
 
 
43
 
        window = BranchDialog('.')
44
 
        window.display()
45
 
 
46
 
register_command(cmd_gbranch)
47
 
 
48
 
class cmd_gdiff(Command):
 
143
    def get_gtk_dialog(self, path):
 
144
        from bzrlib.plugins.gtk.branch import BranchDialog
 
145
        return BranchDialog(path)
 
146
 
 
147
 
 
148
class cmd_gcheckout(GTKCommand):
 
149
    """ GTK+ checkout.
 
150
    
 
151
    """
 
152
    
 
153
    def get_gtk_dialog(self, path):
 
154
        from bzrlib.plugins.gtk.checkout import CheckoutDialog
 
155
        return CheckoutDialog(path)
 
156
 
 
157
 
 
158
 
 
159
class cmd_gpush(GTKCommand):
 
160
    """ GTK+ push.
 
161
    
 
162
    """
 
163
    takes_args = [ "location?" ]
 
164
 
 
165
    def run(self, location="."):
 
166
        (br, path) = branch.Branch.open_containing(location)
 
167
        self.open_display()
 
168
        from push import PushDialog
 
169
        dialog = PushDialog(br.repository, br.last_revision(), br)
 
170
        dialog.run()
 
171
 
 
172
 
 
173
 
 
174
class cmd_gdiff(GTKCommand):
49
175
    """Show differences in working tree in a GTK+ Window.
50
176
    
51
177
    Otherwise, all changes for the tree are listed.
55
181
 
56
182
    @display_command
57
183
    def run(self, revision=None, filename=None):
58
 
        wt = WorkingTree.open_containing(".")[0]
59
 
        branch = wt.branch
60
 
        if revision is not None:
61
 
            if len(revision) == 1:
 
184
        set_ui_factory()
 
185
        wt = workingtree.WorkingTree.open_containing(".")[0]
 
186
        wt.lock_read()
 
187
        try:
 
188
            branch = wt.branch
 
189
            if revision is not None:
 
190
                if len(revision) == 1:
 
191
                    tree1 = wt
 
192
                    revision_id = revision[0].in_history(branch).rev_id
 
193
                    tree2 = branch.repository.revision_tree(revision_id)
 
194
                elif len(revision) == 2:
 
195
                    revision_id_0 = revision[0].in_history(branch).rev_id
 
196
                    tree2 = branch.repository.revision_tree(revision_id_0)
 
197
                    revision_id_1 = revision[1].in_history(branch).rev_id
 
198
                    tree1 = branch.repository.revision_tree(revision_id_1)
 
199
            else:
62
200
                tree1 = wt
63
 
                revision_id = revision[0].in_history(branch).rev_id
64
 
                tree2 = branch.repository.revision_tree(revision_id)
65
 
            elif len(revision) == 2:
66
 
                revision_id_0 = revision[0].in_history(branch).rev_id
67
 
                tree2 = branch.repository.revision_tree(revision_id_0)
68
 
                revision_id_1 = revision[1].in_history(branch).rev_id
69
 
                tree1 = branch.repository.revision_tree(revision_id_1)
70
 
        else:
71
 
            tree1 = wt
72
 
            tree2 = tree1.basis_tree()
73
 
 
74
 
        from viz.diffwin import DiffWindow
75
 
        import gtk
76
 
        window = DiffWindow()
77
 
        window.connect("destroy", lambda w: gtk.main_quit())
78
 
        window.set_diff("Working Tree", tree1, tree2)
79
 
        if filename is not None:
80
 
            tree_filename = wt.relpath(filename)
81
 
            try:
82
 
                window.set_file(tree_filename)
83
 
            except NoSuchFile:
84
 
                if (tree1.inventory.path2id(tree_filename) is None and 
85
 
                    tree2.inventory.path2id(tree_filename) is None):
86
 
                    raise NotVersionedError(filename)
87
 
                raise BzrCommandError('No changes found for file "%s"' % 
88
 
                                      filename)
89
 
        window.show()
90
 
 
91
 
        gtk.main()
92
 
 
93
 
register_command(cmd_gdiff)
 
201
                tree2 = tree1.basis_tree()
 
202
 
 
203
            from diff import DiffWindow
 
204
            import gtk
 
205
            window = DiffWindow()
 
206
            window.connect("destroy", gtk.main_quit)
 
207
            window.set_diff("Working Tree", tree1, tree2)
 
208
            if filename is not None:
 
209
                tree_filename = wt.relpath(filename)
 
210
                try:
 
211
                    window.set_file(tree_filename)
 
212
                except NoSuchFile:
 
213
                    if (tree1.path2id(tree_filename) is None and 
 
214
                        tree2.path2id(tree_filename) is None):
 
215
                        raise NotVersionedError(filename)
 
216
                    raise BzrCommandError('No changes found for file "%s"' % 
 
217
                                          filename)
 
218
            window.show()
 
219
 
 
220
            gtk.main()
 
221
        finally:
 
222
            wt.unlock()
 
223
 
 
224
 
 
225
def start_viz_window(branch, revision, limit=None):
 
226
    """Start viz on branch with revision revision.
 
227
    
 
228
    :return: The viz window object.
 
229
    """
 
230
    from viz.branchwin import BranchWindow
 
231
    return BranchWindow(branch, revision, limit)
 
232
 
94
233
 
95
234
class cmd_visualise(Command):
96
235
    """Graphically visualise this branch.
103
242
    """
104
243
    takes_options = [
105
244
        "revision",
106
 
        Option('limit', "maximum number of revisions to display",
 
245
        Option('limit', "Maximum number of revisions to display.",
107
246
               int, 'count')]
108
247
    takes_args = [ "location?" ]
109
248
    aliases = [ "visualize", "vis", "viz" ]
110
249
 
111
250
    def run(self, location=".", revision=None, limit=None):
112
 
        (branch, path) = Branch.open_containing(location)
113
 
        branch.lock_read()
114
 
        branch.repository.lock_read()
 
251
        set_ui_factory()
 
252
        (br, path) = branch.Branch.open_containing(location)
 
253
        br.lock_read()
115
254
        try:
116
255
            if revision is None:
117
 
                revid = branch.last_revision()
 
256
                revid = br.last_revision()
118
257
                if revid is None:
119
258
                    return
120
259
            else:
121
 
                (revno, revid) = revision[0].in_history(branch)
 
260
                (revno, revid) = revision[0].in_history(br)
122
261
 
123
 
            from viz.bzrkapp import BzrkApp
124
 
                
125
 
            app = BzrkApp()
126
 
            app.show(branch, revid, limit)
 
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()
127
267
        finally:
128
 
            branch.repository.unlock()
129
 
            branch.unlock()
130
 
        app.main()
131
 
 
132
 
 
133
 
register_command(cmd_visualise)
134
 
 
135
 
class cmd_gannotate(Command):
 
268
            br.unlock()
 
269
 
 
270
 
 
271
class cmd_gannotate(GTKCommand):
136
272
    """GTK+ annotate.
137
273
    
138
274
    Browse changes to FILENAME line by line in a GTK+ window.
140
276
 
141
277
    takes_args = ["filename", "line?"]
142
278
    takes_options = [
143
 
        Option("all", help="show annotations on all lines"),
144
 
        Option("plain", help="don't highlight annotation lines"),
 
279
        Option("all", help="Show annotations on all lines."),
 
280
        Option("plain", help="Don't highlight annotation lines."),
145
281
        Option("line", type=int, argname="lineno",
146
 
               help="jump to specified line number"),
 
282
               help="Jump to specified line number."),
147
283
        "revision",
148
284
    ]
149
285
    aliases = ["gblame", "gpraise"]
150
286
    
151
287
    def run(self, filename, all=False, plain=False, line='1', revision=None):
152
 
        import pygtk
153
 
        pygtk.require("2.0")
154
 
 
155
 
        try:
156
 
            import gtk
157
 
        except RuntimeError, e:
158
 
            if str(e) == "could not open display":
159
 
                raise NoDisplayError
 
288
        gtk = self.open_display()
160
289
 
161
290
        try:
162
291
            line = int(line)
166
295
 
167
296
        from annotate.gannotate import GAnnotateWindow
168
297
        from annotate.config import GAnnotateConfig
169
 
 
170
 
        (wt, path) = WorkingTree.open_containing(filename)
171
 
        branch = wt.branch
172
 
 
173
 
        file_id = wt.path2id(path)
 
298
        from bzrlib.bzrdir import BzrDir
 
299
 
 
300
        wt, br, path = BzrDir.open_containing_tree_or_branch(filename)
 
301
        if wt is not None:
 
302
            tree = wt
 
303
        else:
 
304
            tree = br.basis_tree()
 
305
 
 
306
        file_id = tree.path2id(path)
174
307
 
175
308
        if file_id is None:
176
309
            raise NotVersionedError(filename)
177
310
        if revision is not None:
178
311
            if len(revision) != 1:
179
312
                raise BzrCommandError("Only 1 revion may be specified.")
180
 
            revision_id = revision[0].in_history(branch).rev_id
 
313
            revision_id = revision[0].in_history(br).rev_id
 
314
            tree = br.repository.revision_tree(revision_id)
181
315
        else:
182
 
            revision_id = None
 
316
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
183
317
 
184
318
        window = GAnnotateWindow(all, plain)
185
319
        window.connect("destroy", lambda w: gtk.main_quit())
186
320
        window.set_title(path + " - gannotate")
187
321
        config = GAnnotateConfig(window)
188
322
        window.show()
189
 
        branch.lock_read()
 
323
        br.lock_read()
 
324
        if wt is not None:
 
325
            wt.lock_read()
190
326
        try:
191
 
            window.annotate(branch, file_id, revision_id)
 
327
            window.annotate(tree, br, file_id)
 
328
            window.jump_to_line(line)
 
329
            gtk.main()
192
330
        finally:
193
 
            branch.unlock()
194
 
        window.jump_to_line(line)
195
 
        
196
 
        gtk.main()
197
 
 
198
 
register_command(cmd_gannotate)
199
 
 
200
 
class cmd_gcommit(Command):
 
331
            br.unlock()
 
332
            if wt is not None:
 
333
                wt.unlock()
 
334
 
 
335
 
 
336
 
 
337
class cmd_gcommit(GTKCommand):
201
338
    """GTK+ commit dialog
202
339
 
203
340
    Graphical user interface for committing revisions"""
204
 
    
 
341
 
 
342
    aliases = [ "gci" ]
205
343
    takes_args = []
206
344
    takes_options = []
207
345
 
208
346
    def run(self, filename=None):
209
347
        import os
210
 
        import pygtk
211
 
        pygtk.require("2.0")
212
 
 
213
 
        try:
214
 
            import gtk
215
 
        except RuntimeError, e:
216
 
            if str(e) == "could not open display":
217
 
                raise NoDisplayError
218
 
 
219
 
        from olive.commit import CommitDialog
220
 
        from bzrlib.commit import Commit
 
348
        self.open_display()
 
349
        from commit import CommitDialog
221
350
        from bzrlib.errors import (BzrCommandError,
222
351
                                   NotBranchError,
223
 
                                   NoWorkingTree,
224
 
                                   PointlessCommit,
225
 
                                   ConflictsInTree,
226
 
                                   StrictCommitFailed)
 
352
                                   NoWorkingTree)
227
353
 
228
354
        wt = None
229
 
        branch = None
 
355
        br = None
230
356
        try:
231
 
            (wt, path) = WorkingTree.open_containing(filename)
232
 
            branch = wt.branch
233
 
        except NotBranchError, e:
234
 
            path = e.path
 
357
            (wt, path) = workingtree.WorkingTree.open_containing(filename)
 
358
            br = wt.branch
235
359
        except NoWorkingTree, e:
236
 
            path = e.base
237
 
            try:
238
 
                (branch, path) = Branch.open_containing(path)
239
 
            except NotBranchError, e:
240
 
                path = e.path
241
 
 
242
 
        dialog = CommitDialog(wt, path, not branch)
243
 
        if dialog.display():
244
 
            dialog.window.connect("destroy", lambda w: gtk.main_quit())
245
 
            gtk.main()
246
 
 
247
 
register_command(cmd_gcommit)
 
360
            from dialog import error_dialog
 
361
            error_dialog(_('Directory does not have a working tree'),
 
362
                         _('Operation aborted.'))
 
363
            return 1 # should this be retval=3?
 
364
 
 
365
        # It is a good habit to keep things locked for the duration, but it
 
366
        # could cause difficulties if someone wants to do things in another
 
367
        # window... We could lock_read() until we actually go to commit
 
368
        # changes... Just a thought.
 
369
        wt.lock_write()
 
370
        try:
 
371
            dlg = CommitDialog(wt)
 
372
            return dlg.run()
 
373
        finally:
 
374
            wt.unlock()
 
375
 
 
376
 
 
377
class cmd_gstatus(GTKCommand):
 
378
    """GTK+ status dialog
 
379
 
 
380
    Graphical user interface for showing status 
 
381
    information."""
 
382
    
 
383
    aliases = [ "gst" ]
 
384
    takes_args = ['PATH?']
 
385
    takes_options = []
 
386
 
 
387
    def run(self, path='.'):
 
388
        import os
 
389
        gtk = self.open_display()
 
390
        from status import StatusDialog
 
391
        (wt, wt_path) = workingtree.WorkingTree.open_containing(path)
 
392
        status = StatusDialog(wt, wt_path)
 
393
        status.connect("destroy", gtk.main_quit)
 
394
        status.run()
 
395
 
 
396
 
 
397
class cmd_gsend(GTKCommand):
 
398
    """GTK+ send merge directive.
 
399
 
 
400
    """
 
401
    def run(self):
 
402
        (br, path) = branch.Branch.open_containing(".")
 
403
        gtk = self.open_display()
 
404
        from bzrlib.plugins.gtk.mergedirective import SendMergeDirectiveDialog
 
405
        from StringIO import StringIO
 
406
        dialog = SendMergeDirectiveDialog(br)
 
407
        if dialog.run() == gtk.RESPONSE_OK:
 
408
            outf = StringIO()
 
409
            outf.writelines(dialog.get_merge_directive().to_lines())
 
410
            mail_client = br.get_config().get_mail_client()
 
411
            mail_client.compose_merge_request(dialog.get_mail_to(), "[MERGE]", 
 
412
                outf.getvalue())
 
413
 
 
414
            
 
415
 
 
416
 
 
417
class cmd_gconflicts(GTKCommand):
 
418
    """GTK+ conflicts.
 
419
    
 
420
    Select files from the list of conflicts and run an external utility to
 
421
    resolve them.
 
422
    """
 
423
    def run(self):
 
424
        (wt, path) = workingtree.WorkingTree.open_containing('.')
 
425
        self.open_display()
 
426
        from bzrlib.plugins.gtk.conflicts import ConflictsDialog
 
427
        dialog = ConflictsDialog(wt)
 
428
        dialog.run()
 
429
 
 
430
 
 
431
class cmd_gpreferences(GTKCommand):
 
432
    """ GTK+ preferences dialog.
 
433
 
 
434
    """
 
435
    def run(self):
 
436
        self.open_display()
 
437
        from bzrlib.plugins.gtk.preferences import PreferencesWindow
 
438
        dialog = PreferencesWindow()
 
439
        dialog.run()
 
440
 
 
441
 
 
442
class cmd_gmissing(Command):
 
443
    """ GTK+ missing revisions dialog.
 
444
 
 
445
    """
 
446
    takes_args = ["other_branch?"]
 
447
    def run(self, other_branch=None):
 
448
        pygtk = import_pygtk()
 
449
        try:
 
450
            import gtk
 
451
        except RuntimeError, e:
 
452
            if str(e) == "could not open display":
 
453
                raise NoDisplayError
 
454
 
 
455
        from bzrlib.plugins.gtk.missing import MissingWindow
 
456
        from bzrlib.branch import Branch
 
457
 
 
458
        local_branch = Branch.open_containing(".")[0]
 
459
        if other_branch is None:
 
460
            other_branch = local_branch.get_parent()
 
461
            
 
462
            if other_branch is None:
 
463
                raise errors.BzrCommandError("No peer location known or specified.")
 
464
        remote_branch = Branch.open_containing(other_branch)[0]
 
465
        set_ui_factory()
 
466
        local_branch.lock_read()
 
467
        try:
 
468
            remote_branch.lock_read()
 
469
            try:
 
470
                dialog = MissingWindow(local_branch, remote_branch)
 
471
                dialog.run()
 
472
            finally:
 
473
                remote_branch.unlock()
 
474
        finally:
 
475
            local_branch.unlock()
 
476
 
 
477
 
 
478
class cmd_ginit(GTKCommand):
 
479
    def run(self):
 
480
        self.open_display()
 
481
        from initialize import InitDialog
 
482
        dialog = InitDialog(os.path.abspath(os.path.curdir))
 
483
        dialog.run()
 
484
 
 
485
 
 
486
class cmd_gtags(GTKCommand):
 
487
    def run(self):
 
488
        br = branch.Branch.open_containing('.')[0]
 
489
        
 
490
        gtk = self.open_display()
 
491
        from tags import TagsWindow
 
492
        window = TagsWindow(br)
 
493
        window.show()
 
494
        gtk.main()
 
495
 
 
496
 
 
497
commands = [
 
498
    cmd_gannotate, 
 
499
    cmd_gbranch,
 
500
    cmd_gcheckout, 
 
501
    cmd_gcommit, 
 
502
    cmd_gconflicts, 
 
503
    cmd_gdiff,
 
504
    cmd_ginit,
 
505
    cmd_gmissing, 
 
506
    cmd_gpreferences, 
 
507
    cmd_gpush, 
 
508
    cmd_gsend,
 
509
    cmd_gstatus,
 
510
    cmd_gtags,
 
511
    cmd_visualise
 
512
    ]
 
513
 
 
514
for cmd in commands:
 
515
    register_command(cmd)
 
516
 
 
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
 
 
592
class cmd_gselftest(GTKCommand):
 
593
    """Version of selftest that displays a notification at the end"""
 
594
 
 
595
    takes_args = builtins.cmd_selftest.takes_args
 
596
    takes_options = builtins.cmd_selftest.takes_options
 
597
    _see_also = ['selftest']
 
598
 
 
599
    def run(self, *args, **kwargs):
 
600
        import cgi
 
601
        import sys
 
602
        default_encoding = sys.getdefaultencoding()
 
603
        # prevent gtk from blowing up later
 
604
        gtk = import_pygtk()
 
605
        # prevent gtk from messing with default encoding
 
606
        import pynotify
 
607
        if sys.getdefaultencoding() != default_encoding:
 
608
            reload(sys)
 
609
            sys.setdefaultencoding(default_encoding)
 
610
        result = builtins.cmd_selftest().run(*args, **kwargs)
 
611
        if result == 0:
 
612
            summary = 'Success'
 
613
            body = 'Selftest succeeded in "%s"' % os.getcwd()
 
614
        if result == 1:
 
615
            summary = 'Failure'
 
616
            body = 'Selftest failed in "%s"' % os.getcwd()
 
617
        pynotify.init("bzr gselftest")
 
618
        note = pynotify.Notification(cgi.escape(summary), cgi.escape(body))
 
619
        note.set_timeout(pynotify.EXPIRES_NEVER)
 
620
        note.show()
 
621
 
 
622
 
 
623
register_command(cmd_gselftest)
 
624
 
 
625
 
 
626
class cmd_test_gtk(GTKCommand):
 
627
    """Version of selftest that just runs the gtk test suite."""
 
628
 
 
629
    takes_options = ['verbose',
 
630
                     Option('one', short_name='1',
 
631
                            help='stop when one test fails'),
 
632
                     Option('benchmark', help='run the benchmarks.'),
 
633
                     Option('lsprof-timed',
 
634
                     help='generate lsprof output for benchmarked'
 
635
                          ' sections of code.'),
 
636
                     Option('list-only',
 
637
                     help='list the tests instead of running them'),
 
638
                     Option('randomize', type=str, argname="SEED",
 
639
                     help='randomize the order of tests using the given'
 
640
                          ' seed or "now" for the current time'),
 
641
                    ]
 
642
    takes_args = ['testspecs*']
 
643
 
 
644
    def run(self, verbose=None, one=False, benchmark=None,
 
645
            lsprof_timed=None, list_only=False, randomize=None,
 
646
            testspecs_list=None):
 
647
        from bzrlib import __path__ as bzrlib_path
 
648
        from bzrlib.tests import selftest
 
649
 
 
650
        print '%10s: %s' % ('bzrlib', bzrlib_path[0])
 
651
        if benchmark:
 
652
            print 'No benchmarks yet'
 
653
            return 3
 
654
 
 
655
            test_suite_factory = bench_suite
 
656
            if verbose is None:
 
657
                verbose = True
 
658
            # TODO: should possibly lock the history file...
 
659
            benchfile = open(".perf_history", "at", buffering=1)
 
660
        else:
 
661
            test_suite_factory = test_suite
 
662
            if verbose is None:
 
663
                verbose = False
 
664
            benchfile = None
 
665
 
 
666
        if testspecs_list is not None:
 
667
            pattern = '|'.join(testspecs_list)
 
668
        else:
 
669
            pattern = ".*"
 
670
 
 
671
        try:
 
672
            result = selftest(verbose=verbose,
 
673
                              pattern=pattern,
 
674
                              stop_on_failure=one,
 
675
                              test_suite_factory=test_suite_factory,
 
676
                              lsprof_timed=lsprof_timed,
 
677
                              bench_history=benchfile,
 
678
                              list_only=list_only,
 
679
                              random_seed=randomize,
 
680
                             )
 
681
        finally:
 
682
            if benchfile is not None:
 
683
                benchfile.close()
 
684
 
 
685
register_command(cmd_test_gtk)
 
686
 
 
687
 
 
688
import gettext
 
689
gettext.install('olive-gtk')
 
690
 
248
691
 
249
692
class NoDisplayError(BzrCommandError):
250
693
    """gtk could not find a proper display"""
251
694
 
252
695
    def __str__(self):
253
 
        return "No DISPLAY. gannotate is disabled."
 
696
        return "No DISPLAY. Unable to run GTK+ application."
 
697
 
 
698
 
 
699
def test_suite():
 
700
    from unittest import TestSuite
 
701
    import tests
 
702
    import sys
 
703
    default_encoding = sys.getdefaultencoding()
 
704
    try:
 
705
        result = TestSuite()
 
706
        result.addTest(tests.test_suite())
 
707
    finally:
 
708
        if sys.getdefaultencoding() != default_encoding:
 
709
            reload(sys)
 
710
            sys.setdefaultencoding(default_encoding)
 
711
    return result