~abentley/bzrtools/bzrtools.dev

568 by Aaron Bentley
Don't check version when running non-bzrtools commands
1
import bzrlib
2
from bzrlib import commands
3
733.1.1 by Max Bowsher
Use bzrtools' preformatted version string, rather than erroneously assuming version_info is always a 3-tuple.
4
from version import version_info, __version__
568 by Aaron Bentley
Don't check version when running non-bzrtools commands
5
6
730.1.5 by Aaron Bentley
Disable version check for commands run in the test suite.
7
_testing = False
8
# True if we are currently testing commands via the test suite.
9
10
def _stop_testing():
11
    """Set the _testing flag to indicate we are no longer testing."""
12
    global _testing
13
    _testing = False
14
15
568 by Aaron Bentley
Don't check version when running non-bzrtools commands
16
class BzrToolsCommand(commands.Command):
17
18
    def run_argv_aliases(self, argv, alias_argv=None):
19
        result = check_bzrlib_version(version_info[:2])
20
        if result is not None:
21
            return result
22
        commands.Command.run_argv_aliases(self, argv, alias_argv)
23
24
25
def check_bzrlib_version(desired):
26
    """Check that bzrlib is compatible.
27
28
    If version is < bzrtools version, assume incompatible.
29
    If version == bzrtools version, assume completely compatible
30
    If version == bzrtools version + 1, assume compatible, with deprecations
31
    Otherwise, assume incompatible.
32
    """
730.1.5 by Aaron Bentley
Disable version check for commands run in the test suite.
33
    global _testing
34
    if _testing:
35
        return
568 by Aaron Bentley
Don't check version when running non-bzrtools commands
36
    desired_plus = (desired[0], desired[1]+1)
37
    bzrlib_version = bzrlib.version_info[:2]
38
    if bzrlib_version == desired or (bzrlib_version == desired_plus and
39
                                     bzrlib.version_info[3] == 'dev'):
40
        return
41
    try:
42
        from bzrlib.trace import warning
43
    except ImportError:
44
        # get the message out any way we can
45
        from warnings import warn as warning
46
    if bzrlib_version < desired:
47
        warning('Installed Bazaar version %s is too old to be used with'
48
                ' plugin \n'
49
                '"Bzrtools" %s.' % (
733.1.1 by Max Bowsher
Use bzrtools' preformatted version string, rather than erroneously assuming version_info is always a 3-tuple.
50
                bzrlib.__version__, __version__))
568 by Aaron Bentley
Don't check version when running non-bzrtools commands
51
        # Not using BzrNewError, because it may not exist.
52
        return 3
53
    else:
54
        warning('Plugin "Bzrtools" is not up to date with installed Bazaar'
55
                ' version %s.\n'
56
                'There should be a newer version of Bzrtools available, e.g.'
57
                ' %i.%i.'
58
                % (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
59
        if bzrlib_version != desired_plus:
60
            return 3