From 2ed47f2d1ab25d999451ddbc57dba7ff6706743c Mon Sep 17 00:00:00 2001 From: Victor Nakoryakov Date: Tue, 24 Sep 2013 13:15:43 +0400 Subject: [PATCH] fix #46, #88, #103: Correct version parsing with build number and a trash --- README.rst | 2 ++ ino/environment.py | 30 ++++++++++++++++++++++-------- tests/environment_tests.py | 15 +++++++++++---- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/README.rst b/README.rst index 168b80f..fd0d86c 100644 --- a/README.rst +++ b/README.rst @@ -106,6 +106,8 @@ Changelog * Fix #93, #57, #8: Custom compile and link flags can be passed as `ino build` arguments * Fix #60, #63: Custom `make` tool command can be passed as `ino build` argument * Fix #23, #28: `make` is searched within Arduino IDE binaries as well + * Fix #88, #103: Correct version parsing for some distributions that mangle it + * Fix #46: Taking build number into account in version string 0.3.5 * Fix #62: Include `MIT-LICENSE.txt` in the tarball. diff --git a/ino/environment.py b/ino/environment.py index 05d219c..e538c99 100644 --- a/ino/environment.py +++ b/ino/environment.py @@ -23,9 +23,9 @@ from ino.exc import Abort -class Version(namedtuple('Version', 'major minor')): +class Version(namedtuple('Version', 'major minor build')): - regex = re.compile(ur'^\d+(\.\d+)?') + regex = re.compile(ur'^([^:]+:)?(\d+(\.\d+(\.\d+)?)?)') @classmethod def parse(cls, s): @@ -34,20 +34,34 @@ def parse(cls, s): # 0022ubuntu0.1 # 0022-macosx-20110822 # 1.0 - # We have to extract a 2-int-tuple (major, minor) + # 1:1.0.5+dfsg2-1 + # We have to extract a 3-int-tuple (major, minor, build) match = cls.regex.match(s) if not match: raise Abort("Could not parse Arduino library version: %s" % s) - v = match.group(0) + + # v is numbers possibly split by dots without a trash + v = match.group(2) + if v.startswith('0'): - return cls(0, int(v)) - return cls(*map(int, v.split('.'))) + # looks like old 0022 or something like that + return cls(0, int(v), 0) + + parts = map(int, v.split('.')) + + # append nulls if they were not explicit + while len(parts) < 3: + parts.append(0) + + return cls(*parts) def as_int(self): - return self.major * 100 + self.minor + if not self.major: + return self.minor + return self.major * 100 + self.minor * 10 + self.build def __str__(self): - return '%s.%s' % self + return '%s.%s.%s' % self class Environment(dict): diff --git a/tests/environment_tests.py b/tests/environment_tests.py index 761cd41..d0a08a1 100644 --- a/tests/environment_tests.py +++ b/tests/environment_tests.py @@ -7,7 +7,14 @@ class TestVersion(object): def test_parsing(self): - assert_equal(Version.parse('0022'), (0, 22)) - assert_equal(Version.parse('0022ubuntu0.1'), (0, 22)) - assert_equal(Version.parse('0022-macosx-20110822'), (0, 22)) - assert_equal(Version.parse('1.0'), (1, 0)) + assert_equal(Version.parse('0022'), (0, 22, 0)) + assert_equal(Version.parse('0022ubuntu0.1'), (0, 22, 0)) + assert_equal(Version.parse('0022-macosx-20110822'), (0, 22, 0)) + assert_equal(Version.parse('1.0'), (1, 0, 0)) + assert_equal(Version.parse('1:1.0.5+dfsg2-1'), (1, 0, 5)) + + def test_int_conversion(self): + assert_equal(Version(0, 22, 0).as_int(), 22) + assert_equal(Version(1, 0, 0).as_int(), 100) + assert_equal(Version(1, 0, 5).as_int(), 105) + assert_equal(Version(1, 5, 1).as_int(), 151)