/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: 2008-07-17 11:51:03 UTC
  • Revision ID: jelmer@samba.org-20080717115103-djh5sb0pvpse2zkb
Add note about glade.

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
 
gannotate         GTK+ annotate.
19
 
gbranch           GTK+ branching.
20
 
gcheckout         GTK+ checkout.
 
18
gannotate         GTK+ annotate. 
 
19
gbranch           GTK+ branching. 
 
20
gcheckout         GTK+ checkout. 
21
21
gcommit           GTK+ commit dialog.
22
 
gconflicts        GTK+ conflicts.
23
 
gdiff             Show differences in working tree in a GTK+ Window.
 
22
gconflicts        GTK+ conflicts. 
 
23
gdiff             Show differences in working tree in a GTK+ Window. 
24
24
ginit             Initialise a new branch.
25
 
gloom             GTK+ loom browse dialog
26
 
gmerge            GTK+ merge dialog
27
 
gmissing          GTK+ missing revisions dialog.
28
 
gpreferences      GTK+ preferences dialog.
 
25
gmissing          GTK+ missing revisions dialog. 
 
26
gpreferences      GTK+ preferences dialog. 
29
27
gpush             GTK+ push.
30
28
gsend             GTK+ send merge directive.
31
29
gstatus           GTK+ status dialog.
32
30
gtags             Manage branch tags.
33
 
visualise         Graphically visualise this branch.
 
31
visualise         Graphically visualise this branch. 
34
32
"""
35
33
 
36
 
from __future__ import absolute_import
37
 
 
38
 
import os
39
34
import sys
40
35
 
41
 
if getattr(sys, "frozen", None) is not None: # we run bzr.exe
42
 
 
43
 
    # FIXME: Unless a better packaging solution is found, the following
44
 
    # provides a workaround for https://bugs.launchpad.net/bzr/+bug/388790 Also
45
 
    # see https://code.edge.launchpad.net/~vila/bzr-gtk/388790-windows-setup
46
 
    # for more details about while it's needed.
47
 
 
48
 
    # NOTE: _lib must be ahead of bzrlib or sax.saxutils (in olive) fails
49
 
    here = os.path.dirname(__file__)
50
 
    sys.path.insert(0, os.path.join(here, '_lib'))
51
 
    sys.path.append(os.path.join(here, '_lib/gtk-2.0'))
52
 
 
53
 
 
54
36
import bzrlib
55
 
import bzrlib.api
56
 
from bzrlib.commands import plugin_cmds
57
37
 
58
 
from bzrlib.plugins.gtk.info import (
59
 
    bzr_plugin_version as version_info,
60
 
    bzr_compatible_versions,
61
 
    )
 
38
version_info = (0, 95, 0, 'dev', 1)
62
39
 
63
40
if version_info[3] == 'final':
64
41
    version_string = '%d.%d.%d' % version_info[:3]
66
43
    version_string = '%d.%d.%d%s%d' % version_info
67
44
__version__ = version_string
68
45
 
69
 
bzrlib.api.require_any_api(bzrlib, bzr_compatible_versions)
70
 
 
 
46
required_bzrlib = (1, 3)
 
47
 
 
48
def check_bzrlib_version(desired):
 
49
    """Check that bzrlib is compatible.
 
50
 
 
51
    If version is < bzr-gtk version, assume incompatible.
 
52
    """
 
53
    bzrlib_version = bzrlib.version_info[:2]
 
54
    try:
 
55
        from bzrlib.trace import warning
 
56
    except ImportError:
 
57
        # get the message out any way we can
 
58
        from warnings import warn as warning
 
59
    if bzrlib_version < desired:
 
60
        from bzrlib.errors import BzrError
 
61
        warning('Installed Bazaar version %s is too old to be used with bzr-gtk'
 
62
                ' %s.' % (bzrlib.__version__, __version__))
 
63
        raise BzrError('Version mismatch: %r, %r' % (version_info, bzrlib.version_info) )
 
64
 
 
65
 
 
66
if version_info[2] == "final":
 
67
    check_bzrlib_version(required_bzrlib)
 
68
 
 
69
from bzrlib.trace import warning
71
70
if __name__ != 'bzrlib.plugins.gtk':
72
 
    from bzrlib.trace import warning
73
71
    warning("Not running as bzrlib.plugins.gtk, things may break.")
74
72
 
 
73
from bzrlib.lazy_import import lazy_import
 
74
lazy_import(globals(), """
 
75
from bzrlib import (
 
76
    branch,
 
77
    builtins,
 
78
    errors,
 
79
    merge_directive,
 
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
 
75
98
 
76
99
def set_ui_factory():
77
 
    from bzrlib.plugins.gtk.ui import GtkUIFactory
 
100
    import_pygtk()
 
101
    from ui import GtkUIFactory
78
102
    import bzrlib.ui
79
103
    bzrlib.ui.ui_factory = GtkUIFactory()
80
104
 
81
105
 
82
 
def data_basedirs():
83
 
    return [os.path.dirname(__file__),
 
106
def data_path():
 
107
    return os.path.dirname(__file__)
 
108
 
 
109
 
 
110
def icon_path(*args):
 
111
    basedirs = [os.path.join(data_path()),
84
112
             "/usr/share/bzr-gtk", 
85
113
             "/usr/local/share/bzr-gtk"]
86
 
 
87
 
 
88
 
def data_path(*args):
89
 
    for basedir in data_basedirs():
90
 
        path = os.path.join(basedir, *args)
 
114
    for basedir in basedirs:
 
115
        path = os.path.join(basedir, 'icons', *args)
91
116
        if os.path.exists(path):
92
117
            return path
93
118
    return None
94
119
 
95
120
 
96
 
def icon_path(*args):
97
 
    return data_path(os.path.join('icons', *args))
98
 
 
99
 
 
100
 
commands = {
101
 
    "gannotate": ["gblame", "gpraise"],
102
 
    "gbranch": [],
103
 
    "gcheckout": [],
104
 
    "gcommit": ["gci"],
105
 
    "gconflicts": [],
106
 
    "gdiff": [],
107
 
    "ginit": [],
108
 
    "gmerge": [],
109
 
    "gmissing": [],
110
 
    "gpreferences": [],
111
 
    "gpush": [],
112
 
    "gsend": [],
113
 
    "gstatus": ["gst"],
114
 
    "gtags": [],
115
 
    "visualise": ["visualize", "vis", "viz", 'glog'],
116
 
    }
117
 
 
118
 
try:
119
 
    from bzrlib.plugins import loom
120
 
except ImportError:
121
 
    pass # Loom plugin doesn't appear to be present
122
 
else:
123
 
    commands["gloom"] = []
124
 
 
125
 
for cmd, aliases in commands.iteritems():
126
 
    plugin_cmds.register_lazy("cmd_%s" % cmd, aliases,
127
 
                              "bzrlib.plugins.gtk.commands")
128
 
 
129
 
def save_commit_messages(*args):
130
 
    from bzrlib.plugins.gtk import commitmsgs
131
 
    commitmsgs.save_commit_messages(*args)
132
 
 
133
 
try:
134
 
    from bzrlib.hooks import install_lazy_named_hook
135
 
except ImportError:
136
 
    from bzrlib.branch import Branch
137
 
    Branch.hooks.install_named_hook('post_uncommit',
138
 
                                    save_commit_messages,
139
 
                                    "Saving commit messages for gcommit")
140
 
else:
141
 
    install_lazy_named_hook("bzrlib.branch", "Branch.hooks",
142
 
        'post_uncommit', save_commit_messages, "Saving commit messages for gcommit")
143
 
 
144
 
try:
145
 
    from bzrlib.registry import register_lazy
146
 
except ImportError:
147
 
    from bzrlib import config
148
 
    option_registry = getattr(config, "option_registry", None)
149
 
    if option_registry is not None:
150
 
        config.option_registry.register_lazy('nautilus_integration',
151
 
                'bzrlib.plugins.gtk.config', 'opt_nautilus_integration')
152
 
else:
153
 
    register_lazy("bzrlib.config", "option_registry",
154
 
        'nautilus_integration', 'bzrlib.plugins.gtk.config',
155
 
        'opt_nautilus_integration')
156
 
 
157
 
 
158
 
def load_tests(basic_tests, module, loader):
159
 
    testmod_names = [
160
 
        'tests',
161
 
        ]
 
121
def open_display():
 
122
    pygtk = import_pygtk()
 
123
    try:
 
124
        import gtk
 
125
    except RuntimeError, e:
 
126
        if str(e) == "could not open display":
 
127
            raise NoDisplayError
 
128
    set_ui_factory()
 
129
    return gtk
 
130
 
 
131
 
 
132
class GTKCommand(Command):
 
133
    """Abstract class providing GTK specific run commands."""
 
134
 
 
135
    def run(self):
 
136
        open_display()
 
137
        dialog = self.get_gtk_dialog(os.path.abspath('.'))
 
138
        dialog.run()
 
139
 
 
140
 
 
141
class cmd_gbranch(GTKCommand):
 
142
    """GTK+ branching.
 
143
    
 
144
    """
 
145
 
 
146
    def get_gtk_dialog(self, path):
 
147
        from bzrlib.plugins.gtk.branch import BranchDialog
 
148
        return BranchDialog(path)
 
149
 
 
150
 
 
151
class cmd_gcheckout(GTKCommand):
 
152
    """ GTK+ checkout.
 
153
    
 
154
    """
 
155
    
 
156
    def get_gtk_dialog(self, path):
 
157
        from bzrlib.plugins.gtk.checkout import CheckoutDialog
 
158
        return CheckoutDialog(path)
 
159
 
 
160
 
 
161
 
 
162
class cmd_gpush(GTKCommand):
 
163
    """ GTK+ push.
 
164
    
 
165
    """
 
166
    takes_args = [ "location?" ]
 
167
 
 
168
    def run(self, location="."):
 
169
        (br, path) = branch.Branch.open_containing(location)
 
170
        open_display()
 
171
        from push import PushDialog
 
172
        dialog = PushDialog(br.repository, br.last_revision(), br)
 
173
        dialog.run()
 
174
 
 
175
 
 
176
 
 
177
class cmd_gdiff(GTKCommand):
 
178
    """Show differences in working tree in a GTK+ Window.
 
179
    
 
180
    Otherwise, all changes for the tree are listed.
 
181
    """
 
182
    takes_args = ['filename?']
 
183
    takes_options = ['revision']
 
184
 
 
185
    @display_command
 
186
    def run(self, revision=None, filename=None):
 
187
        set_ui_factory()
 
188
        wt = workingtree.WorkingTree.open_containing(".")[0]
 
189
        wt.lock_read()
 
190
        try:
 
191
            branch = wt.branch
 
192
            if revision is not None:
 
193
                if len(revision) == 1:
 
194
                    tree1 = wt
 
195
                    revision_id = revision[0].as_revision_id(tree1.branch)
 
196
                    tree2 = branch.repository.revision_tree(revision_id)
 
197
                elif len(revision) == 2:
 
198
                    revision_id_0 = revision[0].as_revision_id(branch)
 
199
                    tree2 = branch.repository.revision_tree(revision_id_0)
 
200
                    revision_id_1 = revision[1].as_revision_id(branch)
 
201
                    tree1 = branch.repository.revision_tree(revision_id_1)
 
202
            else:
 
203
                tree1 = wt
 
204
                tree2 = tree1.basis_tree()
 
205
 
 
206
            from diff import DiffWindow
 
207
            import gtk
 
208
            window = DiffWindow()
 
209
            window.connect("destroy", gtk.main_quit)
 
210
            window.set_diff("Working Tree", tree1, tree2)
 
211
            if filename is not None:
 
212
                tree_filename = wt.relpath(filename)
 
213
                try:
 
214
                    window.set_file(tree_filename)
 
215
                except NoSuchFile:
 
216
                    if (tree1.path2id(tree_filename) is None and 
 
217
                        tree2.path2id(tree_filename) is None):
 
218
                        raise NotVersionedError(filename)
 
219
                    raise BzrCommandError('No changes found for file "%s"' % 
 
220
                                          filename)
 
221
            window.show()
 
222
 
 
223
            gtk.main()
 
224
        finally:
 
225
            wt.unlock()
 
226
 
 
227
 
 
228
def start_viz_window(branch, revisions, limit=None):
 
229
    """Start viz on branch with revision revision.
 
230
    
 
231
    :return: The viz window object.
 
232
    """
 
233
    from viz import BranchWindow
 
234
    return BranchWindow(branch, revisions, limit)
 
235
 
 
236
 
 
237
class cmd_visualise(Command):
 
238
    """Graphically visualise this branch.
 
239
 
 
240
    Opens a graphical window to allow you to see the history of the branch
 
241
    and relationships between revisions in a visual manner,
 
242
 
 
243
    The default starting point is latest revision on the branch, you can
 
244
    specify a starting point with -r revision.
 
245
    """
 
246
    takes_options = [
 
247
        "revision",
 
248
        Option('limit', "Maximum number of revisions to display.",
 
249
               int, 'count')]
 
250
    takes_args = [ "locations*" ]
 
251
    aliases = [ "visualize", "vis", "viz" ]
 
252
 
 
253
    def run(self, locations_list, revision=None, limit=None):
 
254
        set_ui_factory()
 
255
        if locations_list is None:
 
256
            locations_list = ["."]
 
257
        revids = []
 
258
        for location in locations_list:
 
259
            (br, path) = branch.Branch.open_containing(location)
 
260
            if revision is None:
 
261
                revids.append(br.last_revision())
 
262
            else:
 
263
                revids.append(revision[0].as_revision_id(br))
 
264
        import gtk
 
265
        pp = start_viz_window(br, revids, limit)
 
266
        pp.connect("destroy", lambda w: gtk.main_quit())
 
267
        pp.show()
 
268
        gtk.main()
 
269
 
 
270
 
 
271
class cmd_gannotate(GTKCommand):
 
272
    """GTK+ annotate.
 
273
    
 
274
    Browse changes to FILENAME line by line in a GTK+ window.
 
275
    """
 
276
 
 
277
    takes_args = ["filename", "line?"]
 
278
    takes_options = [
 
279
        Option("all", help="Show annotations on all lines."),
 
280
        Option("plain", help="Don't highlight annotation lines."),
 
281
        Option("line", type=int, argname="lineno",
 
282
               help="Jump to specified line number."),
 
283
        "revision",
 
284
    ]
 
285
    aliases = ["gblame", "gpraise"]
 
286
    
 
287
    def run(self, filename, all=False, plain=False, line='1', revision=None):
 
288
        gtk = open_display()
 
289
 
 
290
        try:
 
291
            line = int(line)
 
292
        except ValueError:
 
293
            raise BzrCommandError('Line argument ("%s") is not a number.' % 
 
294
                                  line)
 
295
 
 
296
        from annotate.gannotate import GAnnotateWindow
 
297
        from annotate.config import GAnnotateConfig
 
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)
 
307
 
 
308
        if file_id is None:
 
309
            raise NotVersionedError(filename)
 
310
        if revision is not None:
 
311
            if len(revision) != 1:
 
312
                raise BzrCommandError("Only 1 revion may be specified.")
 
313
            revision_id = revision[0].as_revision_id(br)
 
314
            tree = br.repository.revision_tree(revision_id)
 
315
        else:
 
316
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
 
317
 
 
318
        window = GAnnotateWindow(all, plain, branch=br)
 
319
        window.connect("destroy", lambda w: gtk.main_quit())
 
320
        config = GAnnotateConfig(window)
 
321
        window.show()
 
322
        br.lock_read()
 
323
        if wt is not None:
 
324
            wt.lock_read()
 
325
        try:
 
326
            window.annotate(tree, br, file_id)
 
327
            window.jump_to_line(line)
 
328
            gtk.main()
 
329
        finally:
 
330
            br.unlock()
 
331
            if wt is not None:
 
332
                wt.unlock()
 
333
 
 
334
 
 
335
 
 
336
class cmd_gcommit(GTKCommand):
 
337
    """GTK+ commit dialog
 
338
 
 
339
    Graphical user interface for committing revisions"""
 
340
 
 
341
    aliases = [ "gci" ]
 
342
    takes_args = []
 
343
    takes_options = []
 
344
 
 
345
    def run(self, filename=None):
 
346
        import os
 
347
        open_display()
 
348
        from commit import CommitDialog
 
349
        from bzrlib.errors import (BzrCommandError,
 
350
                                   NotBranchError,
 
351
                                   NoWorkingTree)
 
352
 
 
353
        wt = None
 
354
        br = None
 
355
        try:
 
356
            (wt, path) = workingtree.WorkingTree.open_containing(filename)
 
357
            br = wt.branch
 
358
        except NoWorkingTree, e:
 
359
            from dialog import error_dialog
 
360
            error_dialog(_i18n('Directory does not have a working tree'),
 
361
                         _i18n('Operation aborted.'))
 
362
            return 1 # should this be retval=3?
 
363
 
 
364
        # It is a good habit to keep things locked for the duration, but it
 
365
        # could cause difficulties if someone wants to do things in another
 
366
        # window... We could lock_read() until we actually go to commit
 
367
        # changes... Just a thought.
 
368
        wt.lock_write()
 
369
        try:
 
370
            dlg = CommitDialog(wt)
 
371
            return dlg.run()
 
372
        finally:
 
373
            wt.unlock()
 
374
 
 
375
 
 
376
class cmd_gstatus(GTKCommand):
 
377
    """GTK+ status dialog
 
378
 
 
379
    Graphical user interface for showing status 
 
380
    information."""
 
381
    
 
382
    aliases = [ "gst" ]
 
383
    takes_args = ['PATH?']
 
384
    takes_options = ['revision']
 
385
 
 
386
    def run(self, path='.', revision=None):
 
387
        import os
 
388
        gtk = open_display()
 
389
        from status import StatusDialog
 
390
        (wt, wt_path) = workingtree.WorkingTree.open_containing(path)
 
391
        
 
392
        if revision is not None:
 
393
            try:
 
394
                revision_id = revision[0].as_revision_id(wt.branch)
 
395
            except:
 
396
                from bzrlib.errors import BzrError
 
397
                raise BzrError('Revision %r doesn\'t exist' % revision[0].user_spec )
 
398
        else:
 
399
            revision_id = None
 
400
 
 
401
        status = StatusDialog(wt, wt_path, revision_id)
 
402
        status.connect("destroy", gtk.main_quit)
 
403
        status.run()
 
404
 
 
405
 
 
406
class cmd_gsend(GTKCommand):
 
407
    """GTK+ send merge directive.
 
408
 
 
409
    """
 
410
    def run(self):
 
411
        (br, path) = branch.Branch.open_containing(".")
 
412
        gtk = open_display()
 
413
        from bzrlib.plugins.gtk.mergedirective import SendMergeDirectiveDialog
 
414
        from StringIO import StringIO
 
415
        dialog = SendMergeDirectiveDialog(br)
 
416
        if dialog.run() == gtk.RESPONSE_OK:
 
417
            outf = StringIO()
 
418
            outf.writelines(dialog.get_merge_directive().to_lines())
 
419
            mail_client = br.get_config().get_mail_client()
 
420
            mail_client.compose_merge_request(dialog.get_mail_to(), "[MERGE]", 
 
421
                outf.getvalue())
 
422
 
 
423
            
 
424
 
 
425
 
 
426
class cmd_gconflicts(GTKCommand):
 
427
    """GTK+ conflicts.
 
428
    
 
429
    Select files from the list of conflicts and run an external utility to
 
430
    resolve them.
 
431
    """
 
432
    def run(self):
 
433
        (wt, path) = workingtree.WorkingTree.open_containing('.')
 
434
        open_display()
 
435
        from bzrlib.plugins.gtk.conflicts import ConflictsDialog
 
436
        dialog = ConflictsDialog(wt)
 
437
        dialog.run()
 
438
 
 
439
 
 
440
class cmd_gpreferences(GTKCommand):
 
441
    """ GTK+ preferences dialog.
 
442
 
 
443
    """
 
444
    def run(self):
 
445
        open_display()
 
446
        from bzrlib.plugins.gtk.preferences import PreferencesWindow
 
447
        dialog = PreferencesWindow()
 
448
        dialog.run()
 
449
 
 
450
 
 
451
class cmd_gmissing(Command):
 
452
    """ GTK+ missing revisions dialog.
 
453
 
 
454
    """
 
455
    takes_args = ["other_branch?"]
 
456
    def run(self, other_branch=None):
 
457
        pygtk = import_pygtk()
 
458
        try:
 
459
            import gtk
 
460
        except RuntimeError, e:
 
461
            if str(e) == "could not open display":
 
462
                raise NoDisplayError
 
463
 
 
464
        from bzrlib.plugins.gtk.missing import MissingWindow
 
465
        from bzrlib.branch import Branch
 
466
 
 
467
        local_branch = Branch.open_containing(".")[0]
 
468
        if other_branch is None:
 
469
            other_branch = local_branch.get_parent()
 
470
            
 
471
            if other_branch is None:
 
472
                raise errors.BzrCommandError("No peer location known or specified.")
 
473
        remote_branch = Branch.open_containing(other_branch)[0]
 
474
        set_ui_factory()
 
475
        local_branch.lock_read()
 
476
        try:
 
477
            remote_branch.lock_read()
 
478
            try:
 
479
                dialog = MissingWindow(local_branch, remote_branch)
 
480
                dialog.run()
 
481
            finally:
 
482
                remote_branch.unlock()
 
483
        finally:
 
484
            local_branch.unlock()
 
485
 
 
486
 
 
487
class cmd_ginit(GTKCommand):
 
488
    def run(self):
 
489
        open_display()
 
490
        from initialize import InitDialog
 
491
        dialog = InitDialog(os.path.abspath(os.path.curdir))
 
492
        dialog.run()
 
493
 
 
494
 
 
495
class cmd_gtags(GTKCommand):
 
496
    def run(self):
 
497
        br = branch.Branch.open_containing('.')[0]
 
498
        
 
499
        gtk = open_display()
 
500
        from tags import TagsWindow
 
501
        window = TagsWindow(br)
 
502
        window.show()
 
503
        gtk.main()
 
504
 
 
505
 
 
506
commands = [
 
507
    cmd_gannotate, 
 
508
    cmd_gbranch,
 
509
    cmd_gcheckout, 
 
510
    cmd_gcommit, 
 
511
    cmd_gconflicts, 
 
512
    cmd_gdiff,
 
513
    cmd_ginit,
 
514
    cmd_gmissing, 
 
515
    cmd_gpreferences, 
 
516
    cmd_gpush, 
 
517
    cmd_gsend,
 
518
    cmd_gstatus,
 
519
    cmd_gtags,
 
520
    cmd_visualise
 
521
    ]
 
522
 
 
523
for cmd in commands:
 
524
    register_command(cmd)
 
525
 
 
526
 
 
527
class cmd_gselftest(GTKCommand):
 
528
    """Version of selftest that displays a notification at the end"""
 
529
 
 
530
    takes_args = builtins.cmd_selftest.takes_args
 
531
    takes_options = builtins.cmd_selftest.takes_options
 
532
    _see_also = ['selftest']
 
533
 
 
534
    def run(self, *args, **kwargs):
 
535
        import cgi
 
536
        import sys
 
537
        default_encoding = sys.getdefaultencoding()
 
538
        # prevent gtk from blowing up later
 
539
        gtk = import_pygtk()
 
540
        # prevent gtk from messing with default encoding
 
541
        import pynotify
 
542
        if sys.getdefaultencoding() != default_encoding:
 
543
            reload(sys)
 
544
            sys.setdefaultencoding(default_encoding)
 
545
        result = builtins.cmd_selftest().run(*args, **kwargs)
 
546
        if result == 0:
 
547
            summary = 'Success'
 
548
            body = 'Selftest succeeded in "%s"' % os.getcwd()
 
549
        if result == 1:
 
550
            summary = 'Failure'
 
551
            body = 'Selftest failed in "%s"' % os.getcwd()
 
552
        pynotify.init("bzr gselftest")
 
553
        note = pynotify.Notification(cgi.escape(summary), cgi.escape(body))
 
554
        note.set_timeout(pynotify.EXPIRES_NEVER)
 
555
        note.show()
 
556
 
 
557
 
 
558
register_command(cmd_gselftest)
 
559
 
 
560
 
 
561
class cmd_test_gtk(GTKCommand):
 
562
    """Version of selftest that just runs the gtk test suite."""
 
563
 
 
564
    takes_options = ['verbose',
 
565
                     Option('one', short_name='1',
 
566
                            help='Stop when one test fails.'),
 
567
                     Option('benchmark', help='Run the benchmarks.'),
 
568
                     Option('lsprof-timed',
 
569
                     help='Generate lsprof output for benchmarked'
 
570
                          ' sections of code.'),
 
571
                     Option('list-only',
 
572
                     help='List the tests instead of running them.'),
 
573
                     Option('randomize', type=str, argname="SEED",
 
574
                     help='Randomize the order of tests using the given'
 
575
                          ' seed or "now" for the current time.'),
 
576
                    ]
 
577
    takes_args = ['testspecs*']
 
578
 
 
579
    def run(self, verbose=None, one=False, benchmark=None,
 
580
            lsprof_timed=None, list_only=False, randomize=None,
 
581
            testspecs_list=None):
 
582
        from bzrlib import __path__ as bzrlib_path
 
583
        from bzrlib.tests import selftest
 
584
 
 
585
        print '%10s: %s' % ('bzrlib', bzrlib_path[0])
 
586
        if benchmark:
 
587
            print 'No benchmarks yet'
 
588
            return 3
 
589
 
 
590
            test_suite_factory = bench_suite
 
591
            if verbose is None:
 
592
                verbose = True
 
593
            # TODO: should possibly lock the history file...
 
594
            benchfile = open(".perf_history", "at", buffering=1)
 
595
        else:
 
596
            test_suite_factory = test_suite
 
597
            if verbose is None:
 
598
                verbose = False
 
599
            benchfile = None
 
600
 
 
601
        if testspecs_list is not None:
 
602
            pattern = '|'.join(testspecs_list)
 
603
        else:
 
604
            pattern = ".*"
 
605
 
 
606
        try:
 
607
            result = selftest(verbose=verbose,
 
608
                              pattern=pattern,
 
609
                              stop_on_failure=one,
 
610
                              test_suite_factory=test_suite_factory,
 
611
                              lsprof_timed=lsprof_timed,
 
612
                              bench_history=benchfile,
 
613
                              list_only=list_only,
 
614
                              random_seed=randomize,
 
615
                             )
 
616
        finally:
 
617
            if benchfile is not None:
 
618
                benchfile.close()
 
619
 
 
620
register_command(cmd_test_gtk)
 
621
 
 
622
 
 
623
 
 
624
import gettext
 
625
gettext.install('olive-gtk')
 
626
 
 
627
# Let's create a specialized alias to protect '_' from being erased by other
 
628
# uses of '_' as an anonymous variable (think pdb for one).
 
629
_i18n = gettext.gettext
 
630
 
 
631
class NoDisplayError(BzrCommandError):
 
632
    """gtk could not find a proper display"""
 
633
 
 
634
    def __str__(self):
 
635
        return "No DISPLAY. Unable to run GTK+ application."
 
636
 
 
637
 
 
638
def test_suite():
 
639
    from unittest import TestSuite
 
640
    import tests
162
641
    import sys
163
642
    default_encoding = sys.getdefaultencoding()
164
643
    try:
165
 
        result = basic_tests
 
644
        result = TestSuite()
166
645
        try:
167
 
            import gi.repository.Gtk
168
 
        except ImportError:
169
 
            return basic_tests
170
 
        basic_tests.addTest(loader.loadTestsFromModuleNames(
171
 
                ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
 
646
            import_pygtk()
 
647
        except errors.BzrCommandError:
 
648
            return result
 
649
        result.addTest(tests.test_suite())
172
650
    finally:
173
651
        if sys.getdefaultencoding() != default_encoding:
174
652
            reload(sys)
175
653
            sys.setdefaultencoding(default_encoding)
176
 
    return basic_tests
177
 
 
 
654
    return result