xref: /titanic_50/usr/src/tools/onbld/Scm/Version.py (revision 036abaca93ddab92ba33036159c30112ab844810)
1cdf0c1d5Smjnelson#
2cdf0c1d5Smjnelson#  This program is free software; you can redistribute it and/or modify
3cdf0c1d5Smjnelson#  it under the terms of the GNU General Public License version 2
4cdf0c1d5Smjnelson#  as published by the Free Software Foundation.
5cdf0c1d5Smjnelson#
6cdf0c1d5Smjnelson#  This program is distributed in the hope that it will be useful,
7cdf0c1d5Smjnelson#  but WITHOUT ANY WARRANTY; without even the implied warranty of
8cdf0c1d5Smjnelson#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9cdf0c1d5Smjnelson#  GNU General Public License for more details.
10cdf0c1d5Smjnelson#
11cdf0c1d5Smjnelson#  You should have received a copy of the GNU General Public License
12cdf0c1d5Smjnelson#  along with this program; if not, write to the Free Software
13cdf0c1d5Smjnelson#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
14cdf0c1d5Smjnelson#
15cdf0c1d5Smjnelson
16cdf0c1d5Smjnelson#
172b5878deSRich Lowe# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
18cdf0c1d5Smjnelson# Use is subject to license terms.
19cdf0c1d5Smjnelson#
20*036abacaSRichard Lowe# Copyright 2008, 2011, Richard Lowe
2187039217SRichard Lowe#
22cdf0c1d5Smjnelson
23cdf0c1d5Smjnelson'''
242b5878deSRich LoweDeal with Mercurial versioning.
252b5878deSRich Lowe
262b5878deSRich LoweAt a basic level, code to verify that the version of Mercurial in use
272b5878deSRich Loweis suitable for use with Cadmium, and compare that version for the
282b5878deSRich Lowesake of adapting to Mercurial API changes.
29cdf0c1d5Smjnelson'''
30cdf0c1d5Smjnelson
31cdf0c1d5Smjnelson#
322b5878deSRich Lowe# It is important that this module rely on as little of Mercurial as
332b5878deSRich Lowe# is possible.
34cdf0c1d5Smjnelson#
35c959a081SRichard Lowe
36c959a081SRichard Lowe#
37c959a081SRichard Lowe# Mercurial >= 1.2 has util.version(), prior versions
38c959a081SRichard Lowe# version.get_version() We discover which to use this way, rather than
39c959a081SRichard Lowe# via ImportError to account for mercurial.demandimport delaying the
40c959a081SRichard Lowe# ImportError exception.
41c959a081SRichard Lowe#
4287039217SRichard Lowe# This code needs to remain, even though versions prior to 1.2 aren't
4387039217SRichard Lowe# supported, to allow us to produce the error message which states that they
4487039217SRichard Lowe# are not supported.
4587039217SRichard Lowe#
46c959a081SRichard Lowefrom mercurial import util
47c959a081SRichard Loweif hasattr(util, 'version'):
48c959a081SRichard Lowe    hg_version = util.version
49c959a081SRichard Loweelse:
50cdf0c1d5Smjnelson    from mercurial import version
51c959a081SRichard Lowe    hg_version = version.get_version
52cdf0c1d5Smjnelson
53cdf0c1d5Smjnelson
54cdf0c1d5Smjnelsonclass VersionMismatch(Exception):
552b5878deSRich Lowe    "Exception used to indicate a mismatch between SCM tools and Mercurial"
56cdf0c1d5Smjnelson    pass
57cdf0c1d5Smjnelson
58cdf0c1d5Smjnelson#
59cdf0c1d5Smjnelson# List of versions that are explicitly acceptable to us
60cdf0c1d5Smjnelson#
61*036abacaSRichard LoweGOOD_VERSIONS = ['1.3.1', '1.4.2', '1.5.4', '1.6.2', '1.6.3', '1.7.5',
62*036abacaSRichard Lowe                 '1.8', '1.8.1']
63cdf0c1d5Smjnelson
64cdf0c1d5Smjnelson
65cdf0c1d5Smjnelsondef check_version():
66cdf0c1d5Smjnelson    '''Check that we're running on a suitable version of Mercurial'''
67cdf0c1d5Smjnelson
68cdf0c1d5Smjnelson    def versionstring(versions):
69cdf0c1d5Smjnelson        '''return the list, versions, as a vaguely grammatical string'''
70cdf0c1d5Smjnelson        if len(versions) > 1:
71cdf0c1d5Smjnelson            return "%s or %s" % (', '.join(versions[0:-1]), versions[-1])
72cdf0c1d5Smjnelson        else:
73cdf0c1d5Smjnelson            return versions[0]
74cdf0c1d5Smjnelson
75c959a081SRichard Lowe    if hg_version() not in GOOD_VERSIONS:
76cdf0c1d5Smjnelson        raise VersionMismatch("Scm expects Mercurial version %s, "
772b5878deSRich Lowe                              "actual version is %s." %
78cdf0c1d5Smjnelson                              (versionstring(GOOD_VERSIONS),
79c959a081SRichard Lowe                               hg_version()))
802b5878deSRich Lowe
812b5878deSRich Lowe
822b5878deSRich Lowedef _split_version(ver):
832b5878deSRich Lowe    '''Return the Mercurial version as a list [MAJOR, MINOR, MICRO],
842b5878deSRich Lowe    if this is not a released Mercurial return None.'''
852b5878deSRich Lowe
862b5878deSRich Lowe    try:
872b5878deSRich Lowe        l = map(int, ver.split('.'))
882b5878deSRich Lowe        # If there's only one element, it's not really a tagged version
892b5878deSRich Lowe        if len(l) <= 1:
902b5878deSRich Lowe            return None
912b5878deSRich Lowe        else:
922b5878deSRich Lowe            return l
932b5878deSRich Lowe    except ValueError:
942b5878deSRich Lowe        return None
952b5878deSRich Lowe
962b5878deSRich Lowe
972b5878deSRich Lowedef at_least(desired):
982b5878deSRich Lowe    '''Return boolean indicating if the running version is greater
992b5878deSRich Lowe    than or equal to, the version specified by major, minor, micro'''
1002b5878deSRich Lowe
101c959a081SRichard Lowe    hgver = _split_version(hg_version())
1022b5878deSRich Lowe    desired = map(int, desired.split('.'))
1032b5878deSRich Lowe
1042b5878deSRich Lowe    #
1052b5878deSRich Lowe    # If _split_version() returns None, we're running on a Mercurial that
1062b5878deSRich Lowe    # has not been tagged as a release.  We assume this to be newer
1072b5878deSRich Lowe    # than any released version.
1082b5878deSRich Lowe    #
1092b5878deSRich Lowe    if hgver == None:
1102b5878deSRich Lowe        return True
1112b5878deSRich Lowe
1122b5878deSRich Lowe    # Pad our versions to the same overall length, appending 0's
1132b5878deSRich Lowe    while len(hgver) < len(desired):
1142b5878deSRich Lowe        hgver.append(0)
1152b5878deSRich Lowe    while len(desired) < len(hgver):
1162b5878deSRich Lowe        desired.append(0)
1172b5878deSRich Lowe
1182b5878deSRich Lowe    for real, req in zip(hgver, desired):
1192b5878deSRich Lowe        if real != req:
1202b5878deSRich Lowe            return real > req
1212b5878deSRich Lowe
1222b5878deSRich Lowe    return True
123