Package dogtail :: Module version
[hide private]
[frames] | no frames]

Source Code for Module dogtail.version

 1  """Handles versioning of software packages 
 2   
 3  Author: Dave Malcolm <dmalcolm@redhat.com>""" 
 4  __author__ = 'Dave Malcolm <dmalcolm@redhat.com>' 
 5   
 6  import re 
 7  import unittest 
 8   
9 -class Version:
10 """ 11 Class representing a version of a software package. 12 Stored internally as a list of subversions, from major to minor. 13 Overloaded comparison operators ought to work sanely. 14 """
15 - def __init__(self, versionList):
16 self.versionList = versionList
17
18 - def fromString(versionString):
19 """ 20 Parse a string of the form number.number.number 21 """ 22 return Version(map(int, versionString.split(".")))
23 fromString = staticmethod(fromString) 24
25 - def __str__(self):
26 return ".".join(map(str, self.versionList))
27
28 - def __getNum(self):
29 tmpList = list(self.versionList) 30 31 while len(tmpList)<5: 32 tmpList += [0] 33 34 num = 0 35 for i in range(len(tmpList)): 36 num *=1000 37 num += tmpList[i] 38 return num
39
40 - def __lt__(self, other):
41 return self.__getNum()<other.__getNum()
42 - def __le__(self, other):
43 return self.__getNum()<=other.__getNum()
44 - def __eq__(self, other):
45 return self.__getNum()==other.__getNum()
46 - def __ne__(self, other):
47 return self.__getNum()!=other.__getNum()
48 - def __gt__(self, other):
49 return self.__getNum()>other.__getNum()
50 - def __ge__(self, other):
51 return self.__getNum()>=other.__getNum()
52
53 -class VersionTests(unittest.TestCase):
54 - def testLt(self):
55 assert Version.fromString("2.0.1") < Version.fromString("2.1.0") 56 assert not Version.fromString("1.4.0") < Version.fromString("1.4.0")
57
58 - def testLe(self):
59 assert Version.fromString("2.0.1") <= Version.fromString("2.1.0") 60 assert Version.fromString("1.4.0") <= Version.fromString("1.4.0")
61
62 - def testEq(self):
63 assert not Version.fromString("2.0.1") == Version.fromString("2.1.0") 64 assert Version.fromString("1.4.0") == Version.fromString("1.4.0")
65
66 - def testNe(self):
67 assert Version.fromString("2.0.1") != Version.fromString("2.1.0") 68 assert not Version.fromString("1.4.0") != Version.fromString("1.4.0")
69
70 - def testGt(self):
71 assert Version.fromString("2.1.0") > Version.fromString("2.0.1") 72 assert not Version.fromString("1.4.0") > Version.fromString("1.4.0")
73
74 - def testGe(self):
75 assert Version.fromString("2.1.0") >= Version.fromString("2.0.1") 76 assert Version.fromString("1.4.0") >= Version.fromString("1.4.0")
77
78 - def testStr(self):
79 assert "2.0.4"==str(Version.fromString("2.0.4"))
80
81 - def testParsing(self):
82 assert Version.fromString("0") == Version([0]) 83 assert Version.fromString("0.1") == Version([0, 1]) 84 assert Version.fromString("1.4.0") == Version([1, 4, 0])
85 86 if __name__ == "__main__": 87 unittest.main() 88