""" Copyright(C) 2007 INL Written by Damien Boucard <damien.boucard AT inl.fr> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>. --- Functions to compare program versions, only support decimal notation. "alpha", "beta", "rc", timestamp suffix are not supported. """ from IPy import IP def parseVersion(version): """ >>> parseVersion("10.2.03") [10, 2, 3] """ return [ int(number) for number in version.split(".") ] def compareVersion(versionA, versionB): """ >>> compareVersion("1.0", "1.3") -1 >>> compareVersion("2.0", "2.0.0") 0 >>> compareVersion("2.0", "2.0.1") -1 >>> compareVersion("1.2.3", "1.2.2") 1 """ versionA = parseVersion(versionA) versionB = parseVersion(versionB) for a, b in zip(versionA, versionB): diff = cmp(a, b) if diff: return diff a = len(versionA) b = len(versionB) if a == b: return 0 if a > b: tail = versionA[b:] diff = 1 else: tail = versionB[a:] diff = -1 if sum(tail): # cmp("2.0", "2.0.1"): sum([1])!=0 => one is greater return diff else: # cmp("2.0", "2.0.0"): sum([0])=0 => same version return 0 def IPmakenet(ip_mask): try: # Try IPy version 0.54+ constructor return IP(ip_mask, make_net=True) except TypeError: pass # Fallback for older version of IPy ip, mask = ip_mask.split('/', 1) if '.' in mask: mask_str = mask mask = IP(mask).int() else: mask_str = str(mask) mask = 2**32 - (2**(32 - int(mask))) ip = str(IP( IP(ip).int() & mask )) ip = IP('%s/%s' % (ip, mask_str)) return ip if __name__ == "__main__": from doctest import testmod from sys import exit failure, nb_test = testmod() if failure: exit(1)