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# 17*2b5878deSRich Lowe# Copyright 2009 Sun Microsystems, Inc. All rights reserved. 18cdf0c1d5Smjnelson# Use is subject to license terms. 19cdf0c1d5Smjnelson# 20cdf0c1d5Smjnelson 21cdf0c1d5Smjnelson''' 22*2b5878deSRich LoweDeal with Mercurial versioning. 23*2b5878deSRich Lowe 24*2b5878deSRich LoweAt a basic level, code to verify that the version of Mercurial in use 25*2b5878deSRich Loweis suitable for use with Cadmium, and compare that version for the 26*2b5878deSRich Lowesake of adapting to Mercurial API changes. 27cdf0c1d5Smjnelson''' 28cdf0c1d5Smjnelson 29cdf0c1d5Smjnelson# 30*2b5878deSRich Lowe# It is important that this module rely on as little of Mercurial as 31*2b5878deSRich Lowe# is possible. 32cdf0c1d5Smjnelson# 33cdf0c1d5Smjnelsonfrom mercurial import version 34cdf0c1d5Smjnelson 35cdf0c1d5Smjnelson 36cdf0c1d5Smjnelsonclass VersionMismatch(Exception): 37*2b5878deSRich Lowe "Exception used to indicate a mismatch between SCM tools and Mercurial" 38cdf0c1d5Smjnelson pass 39cdf0c1d5Smjnelson 40cdf0c1d5Smjnelson# 41cdf0c1d5Smjnelson# List of versions that are explicitly acceptable to us 42cdf0c1d5Smjnelson# 43*2b5878deSRich LoweGOOD_VERSIONS = ['1.0.2', '1.1.2'] 44cdf0c1d5Smjnelson 45cdf0c1d5Smjnelson 46cdf0c1d5Smjnelsondef check_version(): 47cdf0c1d5Smjnelson '''Check that we're running on a suitable version of Mercurial''' 48cdf0c1d5Smjnelson 49cdf0c1d5Smjnelson def versionstring(versions): 50cdf0c1d5Smjnelson '''return the list, versions, as a vaguely grammatical string''' 51cdf0c1d5Smjnelson if len(versions) > 1: 52cdf0c1d5Smjnelson return "%s or %s" % (', '.join(versions[0:-1]), versions[-1]) 53cdf0c1d5Smjnelson else: 54cdf0c1d5Smjnelson return versions[0] 55cdf0c1d5Smjnelson 56cdf0c1d5Smjnelson if version.get_version() not in GOOD_VERSIONS: 57cdf0c1d5Smjnelson raise VersionMismatch("Scm expects Mercurial version %s, " 58*2b5878deSRich Lowe "actual version is %s." % 59cdf0c1d5Smjnelson (versionstring(GOOD_VERSIONS), 60cdf0c1d5Smjnelson version.get_version())) 61*2b5878deSRich Lowe 62*2b5878deSRich Lowe 63*2b5878deSRich Lowedef _split_version(ver): 64*2b5878deSRich Lowe '''Return the Mercurial version as a list [MAJOR, MINOR, MICRO], 65*2b5878deSRich Lowe if this is not a released Mercurial return None.''' 66*2b5878deSRich Lowe 67*2b5878deSRich Lowe try: 68*2b5878deSRich Lowe l = map(int, ver.split('.')) 69*2b5878deSRich Lowe # If there's only one element, it's not really a tagged version 70*2b5878deSRich Lowe if len(l) <= 1: 71*2b5878deSRich Lowe return None 72*2b5878deSRich Lowe else: 73*2b5878deSRich Lowe return l 74*2b5878deSRich Lowe except ValueError: 75*2b5878deSRich Lowe return None 76*2b5878deSRich Lowe 77*2b5878deSRich Lowe 78*2b5878deSRich Lowedef at_least(desired): 79*2b5878deSRich Lowe '''Return boolean indicating if the running version is greater 80*2b5878deSRich Lowe than or equal to, the version specified by major, minor, micro''' 81*2b5878deSRich Lowe 82*2b5878deSRich Lowe hgver = _split_version(version.get_version()) 83*2b5878deSRich Lowe desired = map(int, desired.split('.')) 84*2b5878deSRich Lowe 85*2b5878deSRich Lowe # 86*2b5878deSRich Lowe # If _split_version() returns None, we're running on a Mercurial that 87*2b5878deSRich Lowe # has not been tagged as a release. We assume this to be newer 88*2b5878deSRich Lowe # than any released version. 89*2b5878deSRich Lowe # 90*2b5878deSRich Lowe if hgver == None: 91*2b5878deSRich Lowe return True 92*2b5878deSRich Lowe 93*2b5878deSRich Lowe # Pad our versions to the same overall length, appending 0's 94*2b5878deSRich Lowe while len(hgver) < len(desired): 95*2b5878deSRich Lowe hgver.append(0) 96*2b5878deSRich Lowe while len(desired) < len(hgver): 97*2b5878deSRich Lowe desired.append(0) 98*2b5878deSRich Lowe 99*2b5878deSRich Lowe for real, req in zip(hgver, desired): 100*2b5878deSRich Lowe if real != req: 101*2b5878deSRich Lowe return real > req 102*2b5878deSRich Lowe 103*2b5878deSRich Lowe return True 104