[asterisk-dev] [Code Review]: Add the ability to parse RC and BETA in an Asterisk version.

Jeffrey Ollie jeff at ocjtech.us
Thu Aug 18 19:29:39 CDT 2011


On Thu, Aug 18, 2011 at 6:01 PM, Paul Belanger <reviewboard at asterisk.org> wrote:
>
> Ya, I figured subtracting the values would cause an issue.  I couldn't figure out the pattern.  I've been banging my head against the keyboard of the last few hours trying to fix it.  Going to take a break for a bit and see if I can figure it out.

Here's some code that I wrote a while back to compare Asterisk version
numbers.  I'll stick a version on reviewboard once I figure out my
password.

import re
import sys

class Version:
    status_map = {None:   3,
                  'rc':   2,
                  'beta': 1}

    version_re = re.compile('^((\d+(?:\.\d+)+)(?:-(rc|beta)(\d+(?:\.\d+)*))?)$')

    def __init__(self, s):
        m = self.version_re.match(s)
        if not m:
            raise ValueError('Is this an Asterisk version?')

        self.a, self.b, self.c = m.groups()[1:]

        self.cmp = [map(int, self.a.split('.'))]

        try:
            self.cmp.append(self.status_map[self.b])
        except:
            self.cmp.append(0)

        if self.c is None:
            self.cmp.append([])
        else:
            self.cmp.append(map(int, self.c.split('.')))

    def __cmp__(self, other):
        return cmp(self.cmp, other.cmp)

    def __str__(self):
        if self.b is not None:
            return '%s-%s%s' % (self.a, self.b, self.c)
        return self.a

    def __repr__(self):
        if self.b is not None:
            return '%s-%s%s' % (self.a, self.b, self.c)
        return self.a

print sys.argv[1]
a = Version(sys.argv[1])
print sys.argv[2]
b = Version(sys.argv[2])

print a.__cmp__(b)
print a < b
print a > b
print a == b

--
Jeff Ollie



More information about the asterisk-dev mailing list