xref: /titanic_41/usr/src/tools/onbld/Scm/Version.py (revision 489b7c4ab76ae8df137fbfcc2214f7baa52883a0)
1#
2#  This program is free software; you can redistribute it and/or modify
3#  it under the terms of the GNU General Public License version 2
4#  as published by the Free Software Foundation.
5#
6#  This program is distributed in the hope that it will be useful,
7#  but WITHOUT ANY WARRANTY; without even the implied warranty of
8#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9#  GNU General Public License for more details.
10#
11#  You should have received a copy of the GNU General Public License
12#  along with this program; if not, write to the Free Software
13#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
14#
15
16#
17# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
18# Use is subject to license terms.
19#
20
21'''
22Deal with Mercurial versioning.
23
24At a basic level, code to verify that the version of Mercurial in use
25is suitable for use with Cadmium, and compare that version for the
26sake of adapting to Mercurial API changes.
27'''
28
29#
30# It is important that this module rely on as little of Mercurial as
31# is possible.
32#
33from mercurial import version
34
35
36class VersionMismatch(Exception):
37    "Exception used to indicate a mismatch between SCM tools and Mercurial"
38    pass
39
40#
41# List of versions that are explicitly acceptable to us
42#
43GOOD_VERSIONS = ['1.0.2', '1.1.2']
44
45
46def check_version():
47    '''Check that we're running on a suitable version of Mercurial'''
48
49    def versionstring(versions):
50        '''return the list, versions, as a vaguely grammatical string'''
51        if len(versions) > 1:
52            return "%s or %s" % (', '.join(versions[0:-1]), versions[-1])
53        else:
54            return versions[0]
55
56    if version.get_version() not in GOOD_VERSIONS:
57        raise VersionMismatch("Scm expects Mercurial version %s, "
58                              "actual version is %s." %
59                              (versionstring(GOOD_VERSIONS),
60                               version.get_version()))
61
62
63def _split_version(ver):
64    '''Return the Mercurial version as a list [MAJOR, MINOR, MICRO],
65    if this is not a released Mercurial return None.'''
66
67    try:
68        l = map(int, ver.split('.'))
69        # If there's only one element, it's not really a tagged version
70        if len(l) <= 1:
71            return None
72        else:
73            return l
74    except ValueError:
75        return None
76
77
78def at_least(desired):
79    '''Return boolean indicating if the running version is greater
80    than or equal to, the version specified by major, minor, micro'''
81
82    hgver = _split_version(version.get_version())
83    desired = map(int, desired.split('.'))
84
85    #
86    # If _split_version() returns None, we're running on a Mercurial that
87    # has not been tagged as a release.  We assume this to be newer
88    # than any released version.
89    #
90    if hgver == None:
91        return True
92
93    # Pad our versions to the same overall length, appending 0's
94    while len(hgver) < len(desired):
95        hgver.append(0)
96    while len(desired) < len(hgver):
97        desired.append(0)
98
99    for real, req in zip(hgver, desired):
100        if real != req:
101            return real > req
102
103    return True
104