xref: /onnv-gate/usr/src/tools/onbld/Scm/Version.py (revision 10399:ea7df0114f8c)
17078Smjnelson#
27078Smjnelson#  This program is free software; you can redistribute it and/or modify
37078Smjnelson#  it under the terms of the GNU General Public License version 2
47078Smjnelson#  as published by the Free Software Foundation.
57078Smjnelson#
67078Smjnelson#  This program is distributed in the hope that it will be useful,
77078Smjnelson#  but WITHOUT ANY WARRANTY; without even the implied warranty of
87078Smjnelson#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
97078Smjnelson#  GNU General Public License for more details.
107078Smjnelson#
117078Smjnelson#  You should have received a copy of the GNU General Public License
127078Smjnelson#  along with this program; if not, write to the Free Software
137078Smjnelson#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
147078Smjnelson#
157078Smjnelson
167078Smjnelson#
179006Srichlowe@richlowe.net# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
187078Smjnelson# Use is subject to license terms.
197078Smjnelson#
207078Smjnelson
217078Smjnelson'''
229006Srichlowe@richlowe.netDeal with Mercurial versioning.
239006Srichlowe@richlowe.net
249006Srichlowe@richlowe.netAt a basic level, code to verify that the version of Mercurial in use
259006Srichlowe@richlowe.netis suitable for use with Cadmium, and compare that version for the
269006Srichlowe@richlowe.netsake of adapting to Mercurial API changes.
277078Smjnelson'''
287078Smjnelson
297078Smjnelson#
309006Srichlowe@richlowe.net# It is important that this module rely on as little of Mercurial as
319006Srichlowe@richlowe.net# is possible.
327078Smjnelson#
33*10399Srichlowe@richlowe.net
34*10399Srichlowe@richlowe.net#
35*10399Srichlowe@richlowe.net# Mercurial >= 1.2 has util.version(), prior versions
36*10399Srichlowe@richlowe.net# version.get_version() We discover which to use this way, rather than
37*10399Srichlowe@richlowe.net# via ImportError to account for mercurial.demandimport delaying the
38*10399Srichlowe@richlowe.net# ImportError exception.
39*10399Srichlowe@richlowe.net#
40*10399Srichlowe@richlowe.netfrom mercurial import util
41*10399Srichlowe@richlowe.netif hasattr(util, 'version'):
42*10399Srichlowe@richlowe.net    hg_version = util.version
43*10399Srichlowe@richlowe.netelse:
44*10399Srichlowe@richlowe.net    from mercurial import version
45*10399Srichlowe@richlowe.net    hg_version = version.get_version
467078Smjnelson
477078Smjnelson
487078Smjnelsonclass VersionMismatch(Exception):
499006Srichlowe@richlowe.net    "Exception used to indicate a mismatch between SCM tools and Mercurial"
507078Smjnelson    pass
517078Smjnelson
527078Smjnelson#
537078Smjnelson# List of versions that are explicitly acceptable to us
547078Smjnelson#
55*10399Srichlowe@richlowe.netGOOD_VERSIONS = ['1.1.2', '1.3.1']
567078Smjnelson
577078Smjnelson
587078Smjnelsondef check_version():
597078Smjnelson    '''Check that we're running on a suitable version of Mercurial'''
607078Smjnelson
617078Smjnelson    def versionstring(versions):
627078Smjnelson        '''return the list, versions, as a vaguely grammatical string'''
637078Smjnelson        if len(versions) > 1:
647078Smjnelson            return "%s or %s" % (', '.join(versions[0:-1]), versions[-1])
657078Smjnelson        else:
667078Smjnelson            return versions[0]
677078Smjnelson
68*10399Srichlowe@richlowe.net    if hg_version() not in GOOD_VERSIONS:
697078Smjnelson        raise VersionMismatch("Scm expects Mercurial version %s, "
709006Srichlowe@richlowe.net                              "actual version is %s." %
717078Smjnelson                              (versionstring(GOOD_VERSIONS),
72*10399Srichlowe@richlowe.net                               hg_version()))
739006Srichlowe@richlowe.net
749006Srichlowe@richlowe.net
759006Srichlowe@richlowe.netdef _split_version(ver):
769006Srichlowe@richlowe.net    '''Return the Mercurial version as a list [MAJOR, MINOR, MICRO],
779006Srichlowe@richlowe.net    if this is not a released Mercurial return None.'''
789006Srichlowe@richlowe.net
799006Srichlowe@richlowe.net    try:
809006Srichlowe@richlowe.net        l = map(int, ver.split('.'))
819006Srichlowe@richlowe.net        # If there's only one element, it's not really a tagged version
829006Srichlowe@richlowe.net        if len(l) <= 1:
839006Srichlowe@richlowe.net            return None
849006Srichlowe@richlowe.net        else:
859006Srichlowe@richlowe.net            return l
869006Srichlowe@richlowe.net    except ValueError:
879006Srichlowe@richlowe.net        return None
889006Srichlowe@richlowe.net
899006Srichlowe@richlowe.net
909006Srichlowe@richlowe.netdef at_least(desired):
919006Srichlowe@richlowe.net    '''Return boolean indicating if the running version is greater
929006Srichlowe@richlowe.net    than or equal to, the version specified by major, minor, micro'''
939006Srichlowe@richlowe.net
94*10399Srichlowe@richlowe.net    hgver = _split_version(hg_version())
959006Srichlowe@richlowe.net    desired = map(int, desired.split('.'))
969006Srichlowe@richlowe.net
979006Srichlowe@richlowe.net    #
989006Srichlowe@richlowe.net    # If _split_version() returns None, we're running on a Mercurial that
999006Srichlowe@richlowe.net    # has not been tagged as a release.  We assume this to be newer
1009006Srichlowe@richlowe.net    # than any released version.
1019006Srichlowe@richlowe.net    #
1029006Srichlowe@richlowe.net    if hgver == None:
1039006Srichlowe@richlowe.net        return True
1049006Srichlowe@richlowe.net
1059006Srichlowe@richlowe.net    # Pad our versions to the same overall length, appending 0's
1069006Srichlowe@richlowe.net    while len(hgver) < len(desired):
1079006Srichlowe@richlowe.net        hgver.append(0)
1089006Srichlowe@richlowe.net    while len(desired) < len(hgver):
1099006Srichlowe@richlowe.net        desired.append(0)
1109006Srichlowe@richlowe.net
1119006Srichlowe@richlowe.net    for real, req in zip(hgver, desired):
1129006Srichlowe@richlowe.net        if real != req:
1139006Srichlowe@richlowe.net            return real > req
1149006Srichlowe@richlowe.net
1159006Srichlowe@richlowe.net    return True
116