Commit 0af3cac3 authored by Sebastien Luttringer's avatar Sebastien Luttringer
Browse files

introduce ~ and + in installsystems versions

parent 5aba87f6
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
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
+4 −24
Original line number Diff line number Diff line
@@ -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):
    '''
+30 −0
Original line number Diff line number Diff line
@@ -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