Replace distutils.version.LooseVersion without own Version class
This commit is contained in:
parent
4ec097526d
commit
790cfe7751
2 changed files with 45 additions and 8 deletions
32
check_kernel
32
check_kernel
|
@ -1,12 +1,40 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
from __future__ import division, print_function
|
from __future__ import division, print_function
|
||||||
from distutils.version import LooseVersion
|
|
||||||
import posixpath
|
import posixpath
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
class Version(object):
|
||||||
|
"""Crude version abstraction for systems without distutils.version"""
|
||||||
|
|
||||||
|
def __init__(self, version_str):
|
||||||
|
self.version_str = version_str
|
||||||
|
|
||||||
|
def components(self):
|
||||||
|
return re.split('\.|-', self.version_str)
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.version_str == other.version_str
|
||||||
|
|
||||||
|
def __gt__(self, other):
|
||||||
|
def num_gt(str_, str_other):
|
||||||
|
m = re.search('^\d+', str_)
|
||||||
|
m_other = re.search('^\d+', str_other)
|
||||||
|
if m and m_other:
|
||||||
|
return int(m.group(0)) > int(m_other.group(0))
|
||||||
|
else:
|
||||||
|
return str_ > str_other
|
||||||
|
|
||||||
|
for self_c, other_c in zip(self.components(), other.components()):
|
||||||
|
if num_gt(self_c, other_c):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Note: not using functools.total_ordering to support Python 2.6
|
||||||
|
|
||||||
|
|
||||||
def proc_version():
|
def proc_version():
|
||||||
with open('/proc/version', 'r') as v:
|
with open('/proc/version', 'r') as v:
|
||||||
proc_version = v.next()
|
proc_version = v.next()
|
||||||
|
@ -74,7 +102,7 @@ def clean_kernel_version(version):
|
||||||
# Fedora release
|
# Fedora release
|
||||||
version = re.sub('\.fc\d+', '', version)
|
version = re.sub('\.fc\d+', '', version)
|
||||||
|
|
||||||
return LooseVersion(version)
|
return Version(version)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
|
@ -7,11 +7,10 @@ if sys.version_info[0] < 3:
|
||||||
print("This script requires Python version 3")
|
print("This script requires Python version 3")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
from distutils.version import LooseVersion
|
|
||||||
import unittest
|
import unittest
|
||||||
import unittest.mock
|
import unittest.mock
|
||||||
|
|
||||||
from check_kernel import clean_kernel_version
|
from check_kernel import clean_kernel_version, Version
|
||||||
import check_kernel
|
import check_kernel
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,25 +18,35 @@ class CleanKernelVersionTestCase(unittest.TestCase):
|
||||||
def testDebian(self):
|
def testDebian(self):
|
||||||
versions = [('3.16.7-ckt9-3~deb8u2', '3.16.7-ckt9-3~deb8u2')]
|
versions = [('3.16.7-ckt9-3~deb8u2', '3.16.7-ckt9-3~deb8u2')]
|
||||||
for dirty, clean in versions:
|
for dirty, clean in versions:
|
||||||
self.assertEqual(clean_kernel_version(dirty), LooseVersion(clean))
|
self.assertEqual(clean_kernel_version(dirty), Version(clean))
|
||||||
|
|
||||||
def testFedora(self):
|
def testFedora(self):
|
||||||
versions = [('4.0.4-301.fc22.x86_64', '4.0.4-301'),
|
versions = [('4.0.4-301.fc22.x86_64', '4.0.4-301'),
|
||||||
('4.0.0-1.fc22', '4.0.0-1')]
|
('4.0.0-1.fc22', '4.0.0-1')]
|
||||||
for dirty, clean in versions:
|
for dirty, clean in versions:
|
||||||
self.assertEqual(clean_kernel_version(dirty), LooseVersion(clean))
|
self.assertEqual(clean_kernel_version(dirty), Version(clean))
|
||||||
|
|
||||||
|
|
||||||
class RunningKernelVersionTestCase(unittest.TestCase):
|
class RunningKernelVersionTestCase(unittest.TestCase):
|
||||||
def testFedora(self):
|
def testFedora(self):
|
||||||
with unittest.mock.patch.object(check_kernel, 'proc_version', return_value='Linux version 4.0.3-301.fc22.x86_64 (mockbuild@bkernel02.phx2.fedoraproject.org) (gcc version 5.1.1 20150422 (Red Hat 5.1.1-1) (GCC) ) #1 SMP Thu May 21 13:10:33 UTC 2015'):
|
with unittest.mock.patch.object(check_kernel, 'proc_version', return_value='Linux version 4.0.3-301.fc22.x86_64 (mockbuild@bkernel02.phx2.fedoraproject.org) (gcc version 5.1.1 20150422 (Red Hat 5.1.1-1) (GCC) ) #1 SMP Thu May 21 13:10:33 UTC 2015'):
|
||||||
self.assertEqual(check_kernel.running_kernel_version(),
|
self.assertEqual(check_kernel.running_kernel_version(),
|
||||||
LooseVersion('4.0.3-301'))
|
Version('4.0.3-301'))
|
||||||
|
|
||||||
def testDebian(self):
|
def testDebian(self):
|
||||||
with unittest.mock.patch.object(check_kernel, 'proc_version', return_value='Linux version 3.16.0-4-amd64 (debian-kernel@lists.debian.org) (gcc version 4.8.4 (Debian 4.8.4-1) ) #1 SMP Debian 3.16.7-ckt9-3~deb8u1 (2015-04-24)'):
|
with unittest.mock.patch.object(check_kernel, 'proc_version', return_value='Linux version 3.16.0-4-amd64 (debian-kernel@lists.debian.org) (gcc version 4.8.4 (Debian 4.8.4-1) ) #1 SMP Debian 3.16.7-ckt9-3~deb8u1 (2015-04-24)'):
|
||||||
self.assertEqual(check_kernel.running_kernel_version(),
|
self.assertEqual(check_kernel.running_kernel_version(),
|
||||||
LooseVersion('3.16.7-ckt9-3~deb8u1'))
|
Version('3.16.7-ckt9-3~deb8u1'))
|
||||||
|
|
||||||
|
|
||||||
|
class VersionTestCase(unittest.TestCase):
|
||||||
|
def testComparingTrivial(self):
|
||||||
|
self.assertEqual(Version('1.0'), Version('1.0'))
|
||||||
|
self.assertGreater(Version('2.0'), Version('1.0'))
|
||||||
|
|
||||||
|
def testComparingNonTrivial(self):
|
||||||
|
self.assertGreater(Version('2.12'), Version('2.2'))
|
||||||
|
self.assertGreater(Version('3.12-12'), Version('3.12-2'))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue