~abentley/bzrtools/bzrtools.dev

749.2.1 by Aaron Bentley
bzrtools works with next-version betas and alphas.
1
# Copyright (C) 2007, 2009, 2010, 2011 Aaron Bentley.
2
# Copyright (C) 2009 Max Bowsher.
3
#
4
#    This program is free software; you can redistribute it and/or modify
5
#    it under the terms of the GNU General Public License as published by
6
#    the Free Software Foundation; either version 2 of the License, or
7
#    (at your option) any later version.
8
#
9
#    This program is distributed in the hope that it will be useful,
10
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
#    GNU General Public License for more details.
13
#
14
#    You should have received a copy of the GNU General Public License
15
#    along with this program; if not, write to the Free Software
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
568 by Aaron Bentley
Don't check version when running non-bzrtools commands
19
import bzrlib
20
from bzrlib import commands
21
733.1.1 by Max Bowsher
Use bzrtools' preformatted version string, rather than erroneously assuming version_info is always a 3-tuple.
22
from version import version_info, __version__
568 by Aaron Bentley
Don't check version when running non-bzrtools commands
23
24
730.1.5 by Aaron Bentley
Disable version check for commands run in the test suite.
25
_testing = False
26
# True if we are currently testing commands via the test suite.
27
28
def _stop_testing():
29
    """Set the _testing flag to indicate we are no longer testing."""
30
    global _testing
31
    _testing = False
32
33
568 by Aaron Bentley
Don't check version when running non-bzrtools commands
34
class BzrToolsCommand(commands.Command):
35
36
    def run_argv_aliases(self, argv, alias_argv=None):
37
        result = check_bzrlib_version(version_info[:2])
38
        if result is not None:
39
            return result
40
        commands.Command.run_argv_aliases(self, argv, alias_argv)
41
42
43
def check_bzrlib_version(desired):
44
    """Check that bzrlib is compatible.
45
46
    If version is < bzrtools version, assume incompatible.
47
    If version == bzrtools version, assume completely compatible
48
    If version == bzrtools version + 1, assume compatible, with deprecations
49
    Otherwise, assume incompatible.
50
    """
730.1.5 by Aaron Bentley
Disable version check for commands run in the test suite.
51
    global _testing
52
    if _testing:
53
        return
568 by Aaron Bentley
Don't check version when running non-bzrtools commands
54
    desired_plus = (desired[0], desired[1]+1)
55
    bzrlib_version = bzrlib.version_info[:2]
749.2.1 by Aaron Bentley
bzrtools works with next-version betas and alphas.
56
    if bzrlib_version == desired:
57
        return
58
    if (bzrlib_version == desired_plus and
59
        bzrlib.version_info[3] not in ('final', 'candidate')):
568 by Aaron Bentley
Don't check version when running non-bzrtools commands
60
        return
61
    try:
62
        from bzrlib.trace import warning
63
    except ImportError:
64
        # get the message out any way we can
65
        from warnings import warn as warning
66
    if bzrlib_version < desired:
67
        warning('Installed Bazaar version %s is too old to be used with'
68
                ' plugin \n'
69
                '"Bzrtools" %s.' % (
733.1.1 by Max Bowsher
Use bzrtools' preformatted version string, rather than erroneously assuming version_info is always a 3-tuple.
70
                bzrlib.__version__, __version__))
568 by Aaron Bentley
Don't check version when running non-bzrtools commands
71
        # Not using BzrNewError, because it may not exist.
72
        return 3
73
    else:
74
        warning('Plugin "Bzrtools" is not up to date with installed Bazaar'
75
                ' version %s.\n'
76
                'There should be a newer version of Bzrtools available, e.g.'
77
                ' %i.%i.'
78
                % (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
79
        if bzrlib_version != desired_plus:
80
            return 3