/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:37:13 UTC
  • Revision ID: jelmer@samba.org-20060519163713-be77b31c72cbc7e8
Move visualisation code to a separate directory, preparing for bundling 
the GTK+ plugins for bzr.

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
 
"""GTK+ frontends to Bazaar commands """
16
 
 
17
 
import bzrlib
18
 
 
19
 
__version__ = '0.16.0'
20
 
version_info = tuple(int(n) for n in __version__.split('.'))
21
 
 
22
 
 
23
 
def check_bzrlib_version(desired):
24
 
    """Check that bzrlib is compatible.
25
 
 
26
 
    If version is < bzr-gtk version, assume incompatible.
27
 
    If version == bzr-gtk version, assume completely compatible
28
 
    If version == bzr-gtk version + 1, assume compatible, with deprecations
29
 
    Otherwise, assume incompatible.
30
 
    """
31
 
    desired_plus = (desired[0], desired[1]+1)
32
 
    bzrlib_version = bzrlib.version_info[:2]
33
 
    if bzrlib_version == desired:
34
 
        return
35
 
    try:
36
 
        from bzrlib.trace import warning
37
 
    except ImportError:
38
 
        # get the message out any way we can
39
 
        from warnings import warn as warning
40
 
    if bzrlib_version < desired:
41
 
        warning('Installed bzr version %s is too old to be used with bzr-gtk'
42
 
                ' %s.' % (bzrlib.__version__, __version__))
43
 
        # Not using BzrNewError, because it may not exist.
44
 
        raise Exception, ('Version mismatch', version_info)
45
 
    else:
46
 
        warning('bzr-gtk is not up to date with installed bzr version %s.'
47
 
                ' \nThere should be a newer version available, e.g. %i.%i.' 
48
 
                % (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
49
 
        if bzrlib_version != desired_plus:
50
 
            raise Exception, 'Version mismatch'
51
 
 
52
 
 
53
 
check_bzrlib_version(version_info[:2])
54
 
 
55
 
from bzrlib.trace import warning
56
 
if __name__ != 'bzrlib.plugins.gtk':
57
 
    warning("Not running as bzrlib.plugins.gtk, things may break.")
58
 
 
59
 
from bzrlib.lazy_import import lazy_import
60
 
lazy_import(globals(), """
61
 
from bzrlib import (
62
 
    branch,
63
 
    errors,
64
 
    workingtree,
65
 
    )
66
 
""")
67
 
 
68
 
from bzrlib.commands import Command, register_command, display_command
69
 
from bzrlib.errors import NotVersionedError, BzrCommandError, NoSuchFile
70
 
from bzrlib.commands import Command, register_command
71
 
from bzrlib.option import Option
72
 
from bzrlib.bzrdir import BzrDir
73
 
 
74
 
import os.path
75
 
 
76
 
def import_pygtk():
77
 
    try:
78
 
        import pygtk
79
 
    except ImportError:
80
 
        raise errors.BzrCommandError("PyGTK not installed.")
81
 
    pygtk.require('2.0')
82
 
    return pygtk
83
 
 
84
 
 
85
 
def set_ui_factory():
86
 
    pygtk = import_pygtk()
87
 
    from ui import GtkUIFactory
88
 
    import bzrlib.ui
89
 
    bzrlib.ui.ui_factory = GtkUIFactory()
90
 
 
91
 
 
92
 
class cmd_gbranch(Command):
93
 
    """GTK+ branching.
94
 
    
95
 
    """
96
 
 
97
 
    def run(self):
98
 
        pygtk = import_pygtk()
99
 
        try:
100
 
            import gtk
101
 
        except RuntimeError, e:
102
 
            if str(e) == "could not open display":
103
 
                raise NoDisplayError
104
 
 
105
 
        from bzrlib.plugins.gtk.branch import BranchDialog
106
 
 
107
 
        set_ui_factory()
108
 
        dialog = BranchDialog(os.path.abspath('.'))
109
 
        dialog.run()
110
 
 
111
 
register_command(cmd_gbranch)
112
 
 
113
 
class cmd_gcheckout(Command):
114
 
    """ GTK+ checkout.
115
 
    
116
 
    """
117
 
    
118
 
    def run(self):
119
 
        pygtk = import_pygtk()
120
 
        try:
121
 
            import gtk
122
 
        except RuntimeError, e:
123
 
            if str(e) == "could not open display":
124
 
                raise NoDisplayError
125
 
 
126
 
        from bzrlib.plugins.gtk.checkout import CheckoutDialog
127
 
 
128
 
        set_ui_factory()
129
 
        dialog = CheckoutDialog(os.path.abspath('.'))
130
 
        dialog.run()
131
 
 
132
 
register_command(cmd_gcheckout)
133
 
 
134
 
class cmd_gpush(Command):
135
 
    """ GTK+ push.
136
 
    
137
 
    """
138
 
    takes_args = [ "location?" ]
139
 
 
140
 
    def run(self, location="."):
141
 
        (br, path) = branch.Branch.open_containing(location)
142
 
 
143
 
        pygtk = import_pygtk()
144
 
        try:
145
 
            import gtk
146
 
        except RuntimeError, e:
147
 
            if str(e) == "could not open display":
148
 
                raise NoDisplayError
149
 
 
150
 
        from push import PushDialog
151
 
 
152
 
        set_ui_factory()
153
 
        dialog = PushDialog(br)
154
 
        dialog.run()
155
 
 
156
 
register_command(cmd_gpush)
157
 
 
158
 
class cmd_gdiff(Command):
159
 
    """Show differences in working tree in a GTK+ Window.
160
 
    
161
 
    Otherwise, all changes for the tree are listed.
162
 
    """
163
 
    takes_args = ['filename?']
164
 
    takes_options = ['revision']
165
 
 
166
 
    @display_command
167
 
    def run(self, revision=None, filename=None):
168
 
        set_ui_factory()
169
 
        wt = workingtree.WorkingTree.open_containing(".")[0]
170
 
        wt.lock_read()
171
 
        try:
172
 
            branch = wt.branch
173
 
            if revision is not None:
174
 
                if len(revision) == 1:
175
 
                    tree1 = wt
176
 
                    revision_id = revision[0].in_history(branch).rev_id
177
 
                    tree2 = branch.repository.revision_tree(revision_id)
178
 
                elif len(revision) == 2:
179
 
                    revision_id_0 = revision[0].in_history(branch).rev_id
180
 
                    tree2 = branch.repository.revision_tree(revision_id_0)
181
 
                    revision_id_1 = revision[1].in_history(branch).rev_id
182
 
                    tree1 = branch.repository.revision_tree(revision_id_1)
183
 
            else:
184
 
                tree1 = wt
185
 
                tree2 = tree1.basis_tree()
186
 
 
187
 
            from diff import DiffWindow
188
 
            import gtk
189
 
            window = DiffWindow()
190
 
            window.connect("destroy", gtk.main_quit)
191
 
            window.set_diff("Working Tree", tree1, tree2)
192
 
            if filename is not None:
193
 
                tree_filename = wt.relpath(filename)
194
 
                try:
195
 
                    window.set_file(tree_filename)
196
 
                except NoSuchFile:
197
 
                    if (tree1.inventory.path2id(tree_filename) is None and 
198
 
                        tree2.inventory.path2id(tree_filename) is None):
199
 
                        raise NotVersionedError(filename)
200
 
                    raise BzrCommandError('No changes found for file "%s"' % 
201
 
                                          filename)
202
 
            window.show()
203
 
 
204
 
            gtk.main()
205
 
        finally:
206
 
            wt.unlock()
207
 
 
208
 
register_command(cmd_gdiff)
209
 
 
210
 
class cmd_visualise(Command):
211
 
    """Graphically visualise this branch.
212
 
 
213
 
    Opens a graphical window to allow you to see the history of the branch
214
 
    and relationships between revisions in a visual manner,
215
 
 
216
 
    The default starting point is latest revision on the branch, you can
217
 
    specify a starting point with -r revision.
218
 
    """
219
 
    takes_options = [
220
 
        "revision",
221
 
        Option('limit', "maximum number of revisions to display",
222
 
               int, 'count')]
223
 
    takes_args = [ "location?" ]
224
 
    aliases = [ "visualize", "vis", "viz" ]
225
 
 
226
 
    def run(self, location=".", revision=None, limit=None):
227
 
        set_ui_factory()
228
 
        (br, path) = branch.Branch.open_containing(location)
229
 
        br.lock_read()
230
 
        br.repository.lock_read()
231
 
        try:
232
 
            if revision is None:
233
 
                revid = br.last_revision()
234
 
                if revid is None:
235
 
                    return
236
 
            else:
237
 
                (revno, revid) = revision[0].in_history(br)
238
 
 
239
 
            from viz.branchwin import BranchWindow
240
 
            import gtk
241
 
                
242
 
            pp = BranchWindow()
243
 
            pp.set_branch(br, revid, limit)
244
 
            pp.connect("destroy", lambda w: gtk.main_quit())
245
 
            pp.show()
246
 
            gtk.main()
247
 
        finally:
248
 
            br.repository.unlock()
249
 
            br.unlock()
250
 
 
251
 
 
252
 
register_command(cmd_visualise)
253
 
 
254
 
class cmd_gannotate(Command):
255
 
    """GTK+ annotate.
256
 
    
257
 
    Browse changes to FILENAME line by line in a GTK+ window.
258
 
    """
259
 
 
260
 
    takes_args = ["filename", "line?"]
261
 
    takes_options = [
262
 
        Option("all", help="show annotations on all lines"),
263
 
        Option("plain", help="don't highlight annotation lines"),
264
 
        Option("line", type=int, argname="lineno",
265
 
               help="jump to specified line number"),
266
 
        "revision",
267
 
    ]
268
 
    aliases = ["gblame", "gpraise"]
269
 
    
270
 
    def run(self, filename, all=False, plain=False, line='1', revision=None):
271
 
        pygtk = import_pygtk()
272
 
 
273
 
        try:
274
 
            import gtk
275
 
        except RuntimeError, e:
276
 
            if str(e) == "could not open display":
277
 
                raise NoDisplayError
278
 
        set_ui_factory()
279
 
 
280
 
        try:
281
 
            line = int(line)
282
 
        except ValueError:
283
 
            raise BzrCommandError('Line argument ("%s") is not a number.' % 
284
 
                                  line)
285
 
 
286
 
        from annotate.gannotate import GAnnotateWindow
287
 
        from annotate.config import GAnnotateConfig
288
 
 
289
 
        wt, br, path = BzrDir.open_containing_tree_or_branch(filename)
290
 
        if wt is not None:
291
 
            tree = wt
292
 
        else:
293
 
            tree = br.basis_tree()
294
 
 
295
 
        file_id = tree.path2id(path)
296
 
 
297
 
        if file_id is None:
298
 
            raise NotVersionedError(filename)
299
 
        if revision is not None:
300
 
            if len(revision) != 1:
301
 
                raise BzrCommandError("Only 1 revion may be specified.")
302
 
            revision_id = revision[0].in_history(br).rev_id
303
 
            tree = br.repository.revision_tree(revision_id)
304
 
        else:
305
 
            revision_id = getattr(tree, 'get_revision_id', lambda: None)()
306
 
 
307
 
        window = GAnnotateWindow(all, plain)
308
 
        window.connect("destroy", lambda w: gtk.main_quit())
309
 
        window.set_title(path + " - gannotate")
310
 
        config = GAnnotateConfig(window)
311
 
        window.show()
312
 
        br.lock_read()
313
 
        if wt is not None:
314
 
            wt.lock_read()
315
 
        try:
316
 
            window.annotate(tree, br, file_id)
317
 
            window.jump_to_line(line)
318
 
            gtk.main()
319
 
        finally:
320
 
            br.unlock()
321
 
            if wt is not None:
322
 
                wt.unlock()
323
 
 
324
 
register_command(cmd_gannotate)
325
 
 
326
 
class cmd_gcommit(Command):
327
 
    """GTK+ commit dialog
328
 
 
329
 
    Graphical user interface for committing revisions"""
330
 
    
331
 
    aliases = [ "gci" ]
332
 
    takes_args = []
333
 
    takes_options = []
334
 
 
335
 
    def run(self, filename=None):
336
 
        import os
337
 
        pygtk = import_pygtk()
338
 
 
339
 
        try:
340
 
            import gtk
341
 
        except RuntimeError, e:
342
 
            if str(e) == "could not open display":
343
 
                raise NoDisplayError
344
 
 
345
 
        set_ui_factory()
346
 
        from commit import CommitDialog
347
 
        from bzrlib.commit import Commit
348
 
        from bzrlib.errors import (BzrCommandError,
349
 
                                   NotBranchError,
350
 
                                   NoWorkingTree,
351
 
                                   PointlessCommit,
352
 
                                   ConflictsInTree,
353
 
                                   StrictCommitFailed)
354
 
 
355
 
        wt = None
356
 
        br = None
357
 
        try:
358
 
            (wt, path) = workingtree.WorkingTree.open_containing(filename)
359
 
            br = wt.branch
360
 
        except NotBranchError, e:
361
 
            path = e.path
362
 
        except NoWorkingTree, e:
363
 
            path = e.base
364
 
            try:
365
 
                (br, path) = branch.Branch.open_containing(path)
366
 
            except NotBranchError, e:
367
 
                path = e.path
368
 
 
369
 
 
370
 
        commit = CommitDialog(wt, path, not br)
371
 
        commit.run()
372
 
 
373
 
register_command(cmd_gcommit)
374
 
 
375
 
class cmd_gstatus(Command):
376
 
    """GTK+ status dialog
377
 
 
378
 
    Graphical user interface for showing status 
379
 
    information."""
380
 
    
381
 
    aliases = [ "gst" ]
382
 
    takes_args = ['PATH?']
383
 
    takes_options = []
384
 
 
385
 
    def run(self, path='.'):
386
 
        import os
387
 
        pygtk = import_pygtk()
388
 
 
389
 
        try:
390
 
            import gtk
391
 
        except RuntimeError, e:
392
 
            if str(e) == "could not open display":
393
 
                raise NoDisplayError
394
 
 
395
 
        set_ui_factory()
396
 
        from status import StatusDialog
397
 
        (wt, wt_path) = workingtree.WorkingTree.open_containing(path)
398
 
        status = StatusDialog(wt, wt_path)
399
 
        status.connect("destroy", gtk.main_quit)
400
 
        status.run()
401
 
 
402
 
register_command(cmd_gstatus)
403
 
 
404
 
class cmd_gconflicts(Command):
405
 
    """ GTK+ push.
406
 
    
407
 
    """
408
 
    def run(self):
409
 
        (wt, path) = workingtree.WorkingTree.open_containing('.')
410
 
        
411
 
        pygtk = import_pygtk()
412
 
        try:
413
 
            import gtk
414
 
        except RuntimeError, e:
415
 
            if str(e) == "could not open display":
416
 
                raise NoDisplayError
417
 
 
418
 
        from bzrlib.plugins.gtk.conflicts import ConflictsDialog
419
 
 
420
 
        set_ui_factory()
421
 
        dialog = ConflictsDialog(wt)
422
 
        dialog.run()
423
 
 
424
 
register_command(cmd_gconflicts)
425
 
 
426
 
import gettext
427
 
gettext.install('olive-gtk')
428
 
 
429
 
class NoDisplayError(BzrCommandError):
430
 
    """gtk could not find a proper display"""
431
 
 
432
 
    def __str__(self):
433
 
        return "No DISPLAY. Unable to run GTK+ application."
434
 
 
435
 
def test_suite():
436
 
    from unittest import TestSuite
437
 
    import tests
438
 
    import sys
439
 
    default_encoding = sys.getdefaultencoding()
440
 
    try:
441
 
        result = TestSuite()
442
 
        result.addTest(tests.test_suite())
443
 
    finally:
444
 
        reload(sys)
445
 
        sys.setdefaultencoding(default_encoding)
446
 
    return result