~abentley/bzrtools/bzrtools.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright (C) 2007, 2009, 2010, 2011 Aaron Bentley.
# Copyright (C) 2009 Max Bowsher.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


import bzrlib
from bzrlib import commands

from version import version_info, __version__


_testing = False
# True if we are currently testing commands via the test suite.

def _stop_testing():
    """Set the _testing flag to indicate we are no longer testing."""
    global _testing
    _testing = False


class BzrToolsCommand(commands.Command):

    def run_argv_aliases(self, argv, alias_argv=None):
        result = check_bzrlib_version(version_info[:2])
        if result is not None:
            return result
        commands.Command.run_argv_aliases(self, argv, alias_argv)


def check_bzrlib_version(desired):
    """Check that bzrlib is compatible.

    If version is < bzrtools version, assume incompatible.
    If version == bzrtools version, assume completely compatible
    If version == bzrtools version + 1, assume compatible, with deprecations
    Otherwise, assume incompatible.
    """
    global _testing
    if _testing:
        return
    desired_plus = (desired[0], desired[1]+1)
    bzrlib_version = bzrlib.version_info[:2]
    if bzrlib_version == desired:
        return
    if (bzrlib_version == desired_plus and
        bzrlib.version_info[3] not in ('final', 'candidate')):
        return
    try:
        from bzrlib.trace import warning
    except ImportError:
        # get the message out any way we can
        from warnings import warn as warning
    if bzrlib_version < desired:
        warning('Installed Bazaar version %s is too old to be used with'
                ' plugin \n'
                '"Bzrtools" %s.' % (
                bzrlib.__version__, __version__))
        # Not using BzrNewError, because it may not exist.
        return 3
    else:
        warning('Plugin "Bzrtools" is not up to date with installed Bazaar'
                ' version %s.\n'
                'There should be a newer version of Bzrtools available, e.g.'
                ' %i.%i.'
                % (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
        if bzrlib_version != desired_plus:
            return 3