~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
749.2.2 by Aaron Bentley
refactor version-checking code.
43
TOO_OLD = 'too_old'
44
COMPATIBLE = 'compatible'
45
MAYBE_TOO_NEW = 'maybe_too_new'
46
TOO_NEW = 'too_new'
47
48
749.2.3 by Aaron Bentley
Update compatibility-checking code.
49
def check_version_compatibility(bzrlib_version, min_version, max_version):
50
    """Check whether a bzrlib version is compatible with desired version.
51
52
    If the bzrlib_version is not less than min_version and not greater than
53
    max_version, it is considered COMPATIBLE.  If the version exceeds
54
    max_version by 1 and is not a 'candidate' or 'final' version, it is
55
    considered MAYBE_TOO_NEW.  Other values greater than max_version are
56
    considered TOO_NEW, and values lower than min_version are considered
57
    TOO_OLD.
58
    """
749.2.2 by Aaron Bentley
refactor version-checking code.
59
    bzrlib_version = bzrlib.version_info[:2]
749.2.3 by Aaron Bentley
Update compatibility-checking code.
60
    if bzrlib_version < min_version:
749.2.2 by Aaron Bentley
refactor version-checking code.
61
        return TOO_OLD
749.2.3 by Aaron Bentley
Update compatibility-checking code.
62
    if bzrlib_version <= max_version:
63
        return COMPATIBLE
64
    max_plus = (max_version[0], max_version[1] + 1)
65
    if bzrlib_version == max_plus:
66
        if bzrlib.version_info[3] not in ('final', 'candidate'):
67
            return COMPATIBLE
749.2.2 by Aaron Bentley
refactor version-checking code.
68
        return MAYBE_TOO_NEW
749.2.3 by Aaron Bentley
Update compatibility-checking code.
69
    return TOO_NEW
749.2.2 by Aaron Bentley
refactor version-checking code.
70
71
568 by Aaron Bentley
Don't check version when running non-bzrtools commands
72
def check_bzrlib_version(desired):
73
    """Check that bzrlib is compatible.
74
75
    If version is < bzrtools version, assume incompatible.
76
    If version == bzrtools version, assume completely compatible
77
    If version == bzrtools version + 1, assume compatible, with deprecations
78
    Otherwise, assume incompatible.
79
    """
730.1.5 by Aaron Bentley
Disable version check for commands run in the test suite.
80
    global _testing
81
    if _testing:
82
        return
749.2.3 by Aaron Bentley
Update compatibility-checking code.
83
    compatibility = check_version_compatibility(bzrlib.version_info, desired,
84
                                                desired)
749.2.2 by Aaron Bentley
refactor version-checking code.
85
    if compatibility == COMPATIBLE:
568 by Aaron Bentley
Don't check version when running non-bzrtools commands
86
        return
87
    try:
88
        from bzrlib.trace import warning
89
    except ImportError:
90
        # get the message out any way we can
91
        from warnings import warn as warning
749.2.2 by Aaron Bentley
refactor version-checking code.
92
    if compatibility == TOO_OLD:
93
        warning('Bazaar version %s is too old to be used with'
94
                ' plugin "Bzrtools" %s.' % (
733.1.1 by Max Bowsher
Use bzrtools' preformatted version string, rather than erroneously assuming version_info is always a 3-tuple.
95
                bzrlib.__version__, __version__))
568 by Aaron Bentley
Don't check version when running non-bzrtools commands
96
        # Not using BzrNewError, because it may not exist.
97
        return 3
98
    else:
99
        warning('Plugin "Bzrtools" is not up to date with installed Bazaar'
100
                ' version %s.\n'
101
                'There should be a newer version of Bzrtools available, e.g.'
102
                ' %i.%i.'
749.2.2 by Aaron Bentley
refactor version-checking code.
103
                % (bzrlib.__version__, bzrlib.version_info[0],
104
                   bzrlib.version_info[1]))
105
        if compatibility == TOO_NEW:
568 by Aaron Bentley
Don't check version when running non-bzrtools commands
106
            return 3