changeset 2:ca7580cd1f30

use PEP386 strict version numbers
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 20 Feb 2011 21:44:35 +0100
parents b55d0798d966
children 4f997989dfb1
files MoinMoin/__init__.py MoinMoin/util/_tests/test_version.py MoinMoin/util/version.py
diffstat 3 files changed, 29 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/__init__.py	Sun Feb 20 21:00:53 2011 +0100
+++ b/MoinMoin/__init__.py	Sun Feb 20 21:44:35 2011 +0100
@@ -18,5 +18,5 @@
 
 from MoinMoin.util.version import Version
 
-version = Version(2, 0, 0, 'alpha')
+version = Version(2, 0, 0, 'a0')
 
--- a/MoinMoin/util/_tests/test_version.py	Sun Feb 20 21:00:53 2011 +0100
+++ b/MoinMoin/util/_tests/test_version.py	Sun Feb 20 21:44:35 2011 +0100
@@ -15,43 +15,44 @@
         assert Version(1, 2, 3).major == 1
         assert Version(1, 2, 3).minor == 2
         assert Version(1, 2, 3).release == 3
-        assert Version(1, 2, 3, '4.5alpha6').additional == '4.5alpha6'
+        assert Version(1, 2, 3, 'a4').additional == 'a4'
         # test Version init and Version to str conversion
         assert str(Version(1)) == "1.0.0"
         assert str(Version(1, 2)) == "1.2.0"
         assert str(Version(1, 2, 3)) == "1.2.3"
-        assert str(Version(1, 2, 3, '4.5alpha6')) == "1.2.3-4.5alpha6"
+        assert str(Version(1, 2, 3, 'a4')) == "1.2.3a4"
         assert str(Version(version='1.2.3')) == "1.2.3"
-        assert str(Version(version='1.2.3-4.5alpha6')) == "1.2.3-4.5alpha6"
+        assert str(Version(version='1.2.3a4')) == "1.2.3a4"
         # test Version comparison, trivial cases
         assert Version() == Version()
         assert Version(1) == Version(1)
         assert Version(1, 2) == Version(1, 2)
         assert Version(1, 2, 3) == Version(1, 2, 3)
-        assert Version(1, 2, 3, 'foo') == Version(1, 2, 3, 'foo')
+        assert Version(1, 2, 3, 'a4') == Version(1, 2, 3, 'a4')
         assert Version(1) != Version(2)
         assert Version(1, 2) != Version(1, 3)
         assert Version(1, 2, 3) != Version(1, 2, 4)
-        assert Version(1, 2, 3, 'foo') != Version(1, 2, 3, 'bar')
+        assert Version(1, 2, 3, 'a4') != Version(1, 2, 3, 'a5')
         assert Version(1) < Version(2)
         assert Version(1, 2) < Version(1, 3)
         assert Version(1, 2, 3) < Version(1, 2, 4)
-        assert Version(1, 2, 3, 'bar') < Version(1, 2, 3, 'foo')
+        assert Version(1, 2, 3, 'a4') < Version(1, 2, 3, 'a5')
+        assert Version(1, 2, 3, 'b4') < Version(1, 2, 3, 'b5')
+        assert Version(1, 2, 3, 'c4') < Version(1, 2, 3, 'c5')
         assert Version(2) > Version(1)
         assert Version(1, 3) > Version(1, 2)
         assert Version(1, 2, 4) > Version(1, 2, 3)
-        assert Version(1, 2, 3, 'foo') > Version(1, 2, 3, 'bar')
+        assert Version(1, 2, 3, 'b1') > Version(1, 2, 3, 'a1')
+        assert Version(1, 2, 3, 'c1') > Version(1, 2, 3, 'b1')
         # test Version comparison, more delicate cases
+        assert Version(1, 2, 3) > Version(1, 2, 3, 'c1')
         assert Version(1, 12) > Version(1, 9)
         assert Version(1, 12) > Version(1, 1, 2)
-        assert Version(1, 0, 0, '0.0a2') > Version(1, 0, 0, '0.0a1')
-        assert Version(1, 0, 0, '0.0b1') > Version(1, 0, 0, '0.0a9')
-        assert Version(1, 0, 0, '0.0b2') > Version(1, 0, 0, '0.0b1')
-        assert Version(1, 0, 0, '0.0c1') > Version(1, 0, 0, '0.0b9')
-        assert Version(1, 0, 0, '1') > Version(1, 0, 0, '0.0c9')
         # test Version playing nice with tuples
-        assert Version(1, 2, 3) == (1, 2, 3, '')
+        assert Version(1, 2, 3) < (1, 2, 4)
+        assert Version(1, 2, 3, 'c99') < (1, 2, 4)
         assert Version(1, 2, 4) > (1, 2, 3)
+        assert Version(1, 2, 4) > (1, 2, 3, 'c99')
 
 
 coverage_modules = ['MoinMoin.util.version']
--- a/MoinMoin/util/version.py	Sun Feb 20 21:00:53 2011 +0100
+++ b/MoinMoin/util/version.py	Sun Feb 20 21:44:35 2011 +0100
@@ -12,17 +12,20 @@
 
 class Version(tuple):
     """
-    Version objects store versions like 1.2.3-4.5alpha6 in a structured
+    Version objects store versions like 1.2.3a4 in a structured
     way and support version comparisons and direct version component access.
     1: major version (digits only)
     2: minor version (digits only)
     3: (maintenance) release version (digits only)
-    4.5alpha6: optional additional version specification (str)
+    a4: optional additional version specification (str)
+
+    See PEP386 for more details.
+    TODO: use 3rd party code for PEP386 version numbers later.
 
     You can create a Version instance either by giving the components, like:
-        Version(1,2,3,'4.5alpha6')
+        Version(1,2,3,'a4')
     or by giving the composite version string, like:
-        Version(version="1.2.3-4.5alpha6").
+        Version(version="1.2.3a4").
 
     Version subclasses tuple, so comparisons to tuples should work.
     Also, we inherit all the comparison logic from tuple base class.
@@ -33,9 +36,7 @@
             (?P<minor>\d+)
             \.
             (?P<release>\d+)
-            (-
-             (?P<additional>.+)
-            )?""",
+            (?P<additional>[abc]\d+)?""",
             re.VERBOSE)
 
     @classmethod
@@ -44,9 +45,10 @@
         if match is None:
             raise ValueError("Unexpected version string format: %r" % version)
         v = match.groupdict()
-        return int(v['major']), int(v['minor']), int(v['release']), str(v['additional'] or '')
+        return int(v['major']), int(v['minor']), int(v['release']), str(v['additional'] or 'd0')
 
-    def __new__(cls, major=0, minor=0, release=0, additional='', version=None):
+    def __new__(cls, major=0, minor=0, release=0, additional='d0', version=None):
+        # HACK: Use "d0" for release, as "d0" > "c99".
         if version:
             major, minor, release, additional = cls.parse_version(version)
         return tuple.__new__(cls, (major, minor, release, additional))
@@ -55,11 +57,11 @@
     major = property(lambda self: self[0])
     minor = property(lambda self: self[1])
     release = property(lambda self: self[2])
-    additional = property(lambda self: self[3])
+    additional = property(lambda self: self[3] if self[3] != 'd0' else '')
 
     def __str__(self):
         version_str = "%d.%d.%d" % (self.major, self.minor, self.release)
-        if self.additional:
-            version_str += "-%s" % self.additional
+        if self.additional != 'd0':
+            version_str += self.additional
         return version_str