]> code.ossystems Code Review - openembedded-core.git/commitdiff
Resurrect the old bb.vercmp as bb.utils.vercmp, and its deps
authorChris Larson <chris_larson@mentor.com>
Tue, 6 Apr 2010 22:29:19 +0000 (15:29 -0700)
committerRichard Purdie <rpurdie@linux.intel.com>
Fri, 2 Jul 2010 14:41:31 +0000 (15:41 +0100)
This is just for compatibility.  We may drop it in the future, or rewrite it,
as it's not particularly pythonic.

(Bitbake rev: c4e31d7fe1d15a1e3ef2a453e7c7812d403d22cb)

Signed-off-by: Chris Larson <chris_larson@mentor.com>
Signed-off-by: Richard Purdie <rpurdie@linux.intel.com>
bitbake/lib/bb/__init__.py
bitbake/lib/bb/utils.py

index c7cd0f62dd5b06230ba54f67c43a63472b8648dc..61973c38a997c086c7d4957e460ff5abf06d503f 100644 (file)
@@ -85,7 +85,7 @@ def fatal(*args):
 from bb.fetch import MalformedUrl, encodeurl, decodeurl
 from bb.data import VarExpandError
 from bb.utils import mkdirhier, movefile, copyfile, which
-from bb.utils import vercmp
+from bb.utils import vercmp_string as vercmp
 
 
 if __name__ == "__main__":
index 2e3937ec122806558ce892070be32328fd76d6fe..dce80b6342ad09d94891176faaed81d8065eefa7 100644 (file)
@@ -92,6 +92,149 @@ def vercmp(ta, tb):
         r = vercmp_part(ra, rb)
     return r
 
+_package_weights_ = {"pre":-2,"p":0,"alpha":-4,"beta":-3,"rc":-1}    # dicts are unordered
+_package_ends_    = ["pre", "p", "alpha", "beta", "rc", "cvs", "bk", "HEAD" ]            # so we need ordered list
+
+def relparse(myver):
+    """Parses the last elements of a version number into a triplet, that can
+    later be compared:
+
+    >>> relparse('1.2_pre3')
+    [1.2, -2, 3.0]
+    >>> relparse('1.2b')
+    [1.2, 98, 0]
+    >>> relparse('1.2')
+    [1.2, 0, 0]
+    """
+
+    number   = 0
+    p1       = 0
+    p2       = 0
+    mynewver = myver.split('_')
+    if len(mynewver)==2:
+        # an _package_weights_
+        number = float(mynewver[0])
+        match = 0
+        for x in _package_ends_:
+            elen = len(x)
+            if mynewver[1][:elen] == x:
+                match = 1
+                p1 = _package_weights_[x]
+                try:
+                    p2 = float(mynewver[1][elen:])
+                except:
+                    p2 = 0
+                break
+        if not match:
+            # normal number or number with letter at end
+            divider = len(myver)-1
+            if myver[divider:] not in "1234567890":
+                # letter at end
+                p1 = ord(myver[divider:])
+                number = float(myver[0:divider])
+            else:
+                number = float(myver)
+    else:
+        # normal number or number with letter at end
+        divider = len(myver)-1
+        if myver[divider:] not in "1234567890":
+            #letter at end
+            p1     = ord(myver[divider:])
+            number = float(myver[0:divider])
+        else:
+            number = float(myver)
+    return [number,p1,p2]
+
+__vercmp_cache__ = {}
+
+def vercmp_string(val1,val2):
+    """This takes two version strings and returns an integer to tell you whether
+    the versions are the same, val1>val2 or val2>val1.
+
+    >>> vercmp('1', '2')
+    -1.0
+    >>> vercmp('2', '1')
+    1.0
+    >>> vercmp('1', '1.0')
+    0
+    >>> vercmp('1', '1.1')
+    -1.0
+    >>> vercmp('1.1', '1_p2')
+    1.0
+    """
+
+    # quick short-circuit
+    if val1 == val2:
+        return 0
+    valkey = val1+" "+val2
+
+    # cache lookup
+    try:
+        return __vercmp_cache__[valkey]
+        try:
+            return - __vercmp_cache__[val2+" "+val1]
+        except KeyError:
+            pass
+    except KeyError:
+        pass
+
+    # consider 1_p2 vc 1.1
+    # after expansion will become (1_p2,0) vc (1,1)
+    # then 1_p2 is compared with 1 before 0 is compared with 1
+    # to solve the bug we need to convert it to (1,0_p2)
+    # by splitting _prepart part and adding it back _after_expansion
+
+    val1_prepart = val2_prepart = ''
+    if val1.count('_'):
+        val1, val1_prepart = val1.split('_', 1)
+    if val2.count('_'):
+        val2, val2_prepart = val2.split('_', 1)
+
+    # replace '-' by '.'
+    # FIXME: Is it needed? can val1/2 contain '-'?
+
+    val1 = string.split(val1,'-')
+    if len(val1) == 2:
+        val1[0] = val1[0] +"."+ val1[1]
+    val2 = string.split(val2,'-')
+    if len(val2) == 2:
+        val2[0] = val2[0] +"."+ val2[1]
+
+    val1 = string.split(val1[0],'.')
+    val2 = string.split(val2[0],'.')
+
+    # add back decimal point so that .03 does not become "3" !
+    for x in range(1,len(val1)):
+        if val1[x][0] == '0' :
+            val1[x] = '.' + val1[x]
+    for x in range(1,len(val2)):
+        if val2[x][0] == '0' :
+            val2[x] = '.' + val2[x]
+
+    # extend varion numbers
+    if len(val2) < len(val1):
+        val2.extend(["0"]*(len(val1)-len(val2)))
+    elif len(val1) < len(val2):
+        val1.extend(["0"]*(len(val2)-len(val1)))
+
+    # add back _prepart tails
+    if val1_prepart:
+        val1[-1] += '_' + val1_prepart
+    if val2_prepart:
+        val2[-1] += '_' + val2_prepart
+    # The above code will extend version numbers out so they
+    # have the same number of digits.
+    for x in range(0,len(val1)):
+        cmp1 = relparse(val1[x])
+        cmp2 = relparse(val2[x])
+        for y in range(0,3):
+            myret = cmp1[y] - cmp2[y]
+            if myret != 0:
+                __vercmp_cache__[valkey] = myret
+                return myret
+    __vercmp_cache__[valkey] = 0
+    return 0
+
 def explode_deps(s):
     """
     Take an RDEPENDS style string of format: