diff --git a/README b/README index 9ccb0e43343138a293cfbb994f7b5bef2373e314..759d61715eac15788694b89fc68ea9ef85085091 100644 --- a/README +++ b/README @@ -1 +1,20 @@ InstallSystems Next Generation + +INSTALLSYSTEMS VERSIONNING +__________________________ + +A valid version is an integer without dot. +A version n, may be followed by a ~, to indicate it's inferior to n +A version n, may be followed by a +, to indicate it's superior to n +Any following ascii chars after ~ or + are ignored + +Examples: + 1 < 2 + 2 > 2~dev + 2 < 2+dev + 2~dev < 2+dev + + +IMAGES VERSIONNING +__________________ +A valid version is an integer. Nothing more! \ No newline at end of file diff --git a/installsystems/image.py b/installsystems/image.py index f9859ff50efcb77e2a87250e41b7a12c209962bd..f60053ad37b15f7e289ea88eb7c89d51826f53ae 100644 --- a/installsystems/image.py +++ b/installsystems/image.py @@ -51,34 +51,14 @@ class Image(object): if re.match("^\d+$", buf) is None: raise Exception("Invalid image version %s" % buf) + @staticmethod def compare_versions(v1, v2): ''' - Compare v1 and v2 - return > 0 if v1 > v2 - return < 0 if v2 > v1 - return = 0 if v1 == v2 + For backward compatibility, image class offer a method to compare image versions + But code is now inside tools ''' - # check v1 - try: - i1 = int(v1) - except ValueError: - if isinstance(v1, basestring): - v1m = re.search("\d+", v1) - if v1m is None: - raise Exception("Invalid version %s" % v1) - i1 = int(v1m.group(0)) - # check v2 - try: - i2 = int(v2) - except ValueError: - if isinstance(v2, basestring): - v2m = re.search("\d+", v1) - if v2m is None: - raise Exception("Invalid version %s" % v2) - i2 = int(v2m.group(0)) - return i1 - i2 - + return istools.compare_versions(v1, v2) class SourceImage(Image): ''' diff --git a/installsystems/tools.py b/installsystems/tools.py index 4a978952e1b0ddb61e80880e1daa012ec45c4558..2269f97f5ac1ade06588b33d8761c387187be4cc 100644 --- a/installsystems/tools.py +++ b/installsystems/tools.py @@ -463,3 +463,33 @@ def chroot(path, shell="/bin/bash", mount=True): call(["chroot", path, shell], close_fds=True) # revert preparation of chroot unprepare_chroot(path, mount) + +def compare_versions(v1, v2): + ''' + This function compare version :param v1: and version :param v2: + Compare v1 and v2 + return > 0 if v1 > v2 + return < 0 if v2 > v1 + return = 0 if v1 == v2 + ''' + + def get_ver(version): + '''Return float version''' + if type(version) is int or type(version) is float: + return float(version) + elif isinstance(version, basestring): + iv = re.match("^(\d+)(?:([-~+])\w*)?$", version) + if iv is None: + raise TypeError('Invalid version: %s' % version) + rv = float(iv.group(1)) + if iv.group(2) == "~": + rv -= 0.1 + else: + rv += 0.1 + return rv + else: + raise TypeError('Invalid version: %s' % version) + + fv1 = get_ver(v1) + fv2 = get_ver(v2) + return fv1 - fv2