Skip to content

Commit

Permalink
fix amperka#46, amperka#88, amperka#103: Correct version parsing with…
Browse files Browse the repository at this point in the history
… build number and a trash
  • Loading branch information
nkrkv committed Sep 24, 2013
1 parent 8a681dc commit 2ed47f2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 22 additions & 8 deletions ino/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
15 changes: 11 additions & 4 deletions tests/environment_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 2ed47f2

Please sign in to comment.