xref: /titanic_50/usr/src/tools/onbld/Scm/Version.py (revision 8703921742d9c7d4d3724f89a39ff0e2725cbe7b)
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*87039217SRichard Lowe# Copyright 2008, 2010, Richard Lowe
21*87039217SRichard 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#
42*87039217SRichard Lowe# This code needs to remain, even though versions prior to 1.2 aren't
43*87039217SRichard Lowe# supported, to allow us to produce the error message which states that they
44*87039217SRichard Lowe# are not supported.
45*87039217SRichard 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*87039217SRichard LoweGOOD_VERSIONS = ['1.3.1', '1.4.2', '1.5.4', '1.6.2', '1.6.3']
62cdf0c1d5Smjnelson
63cdf0c1d5Smjnelson
64cdf0c1d5Smjnelsondef check_version():
65cdf0c1d5Smjnelson    '''Check that we're running on a suitable version of Mercurial'''
66cdf0c1d5Smjnelson
67cdf0c1d5Smjnelson    def versionstring(versions):
68cdf0c1d5Smjnelson        '''return the list, versions, as a vaguely grammatical string'''
69cdf0c1d5Smjnelson        if len(versions) > 1:
70cdf0c1d5Smjnelson            return "%s or %s" % (', '.join(versions[0:-1]), versions[-1])
71cdf0c1d5Smjnelson        else:
72cdf0c1d5Smjnelson            return versions[0]
73cdf0c1d5Smjnelson
74c959a081SRichard Lowe    if hg_version() not in GOOD_VERSIONS:
75cdf0c1d5Smjnelson        raise VersionMismatch("Scm expects Mercurial version %s, "
762b5878deSRich Lowe                              "actual version is %s." %
77cdf0c1d5Smjnelson                              (versionstring(GOOD_VERSIONS),
78c959a081SRichard Lowe                               hg_version()))
792b5878deSRich Lowe
802b5878deSRich Lowe
812b5878deSRich Lowedef _split_version(ver):
822b5878deSRich Lowe    '''Return the Mercurial version as a list [MAJOR, MINOR, MICRO],
832b5878deSRich Lowe    if this is not a released Mercurial return None.'''
842b5878deSRich Lowe
852b5878deSRich Lowe    try:
862b5878deSRich Lowe        l = map(int, ver.split('.'))
872b5878deSRich Lowe        # If there's only one element, it's not really a tagged version
882b5878deSRich Lowe        if len(l) <= 1:
892b5878deSRich Lowe            return None
902b5878deSRich Lowe        else:
912b5878deSRich Lowe            return l
922b5878deSRich Lowe    except ValueError:
932b5878deSRich Lowe        return None
942b5878deSRich Lowe
952b5878deSRich Lowe
962b5878deSRich Lowedef at_least(desired):
972b5878deSRich Lowe    '''Return boolean indicating if the running version is greater
982b5878deSRich Lowe    than or equal to, the version specified by major, minor, micro'''
992b5878deSRich Lowe
100c959a081SRichard Lowe    hgver = _split_version(hg_version())
1012b5878deSRich Lowe    desired = map(int, desired.split('.'))
1022b5878deSRich Lowe
1032b5878deSRich Lowe    #
1042b5878deSRich Lowe    # If _split_version() returns None, we're running on a Mercurial that
1052b5878deSRich Lowe    # has not been tagged as a release.  We assume this to be newer
1062b5878deSRich Lowe    # than any released version.
1072b5878deSRich Lowe    #
1082b5878deSRich Lowe    if hgver == None:
1092b5878deSRich Lowe        return True
1102b5878deSRich Lowe
1112b5878deSRich Lowe    # Pad our versions to the same overall length, appending 0's
1122b5878deSRich Lowe    while len(hgver) < len(desired):
1132b5878deSRich Lowe        hgver.append(0)
1142b5878deSRich Lowe    while len(desired) < len(hgver):
1152b5878deSRich Lowe        desired.append(0)
1162b5878deSRich Lowe
1172b5878deSRich Lowe    for real, req in zip(hgver, desired):
1182b5878deSRich Lowe        if real != req:
1192b5878deSRich Lowe            return real > req
1202b5878deSRich Lowe
1212b5878deSRich Lowe    return True
122