Fix getting kernel version on older Debian systems

master
Mike Gerber 10 years ago
parent 68f7bfb274
commit 49945b1a2b

@ -15,8 +15,16 @@ class Version(object):
def __str__(self): def __str__(self):
return self.version_str return self.version_str
def __repr__(self):
return 'Version(\'%s\')' % self.version_str
def components(self): def components(self):
return re.split('\.|-', self.version_str) # Split the version into components, by word boundaries, or a
# change between numbers and non-numbers and vice-versa. re.split()
# does not work on zero-length delimiters, so we have to use a
# sub+split.
return re.sub(r'\b|(?<=\d)(?=\D)|(?<=\D)(?=\d)', '\n',
self.version_str).split('\n')
def __eq__(self, other): def __eq__(self, other):
return self.version_str == other.version_str return self.version_str == other.version_str
@ -46,13 +54,22 @@ def proc_version():
def running_kernel_version(): def running_kernel_version():
m = re.findall('(?<=Debian )\S+', proc_version()) proc_version_ = proc_version()
if re.search('Debian', proc_version_):
# Remove gcc version first
proc_version_ = re.sub('\(gcc[^(]+\(Debian [^)]+\)\s+\)', '',
proc_version_)
# Then look for the Debian kernel version
m = re.search('(?<=Debian )(\S+)', proc_version_)
if m: if m:
# Note: it's the _second_ match version_str = m.group(1).strip('()')
version = clean_kernel_version(m[1]) version = clean_kernel_version(version_str)
return version return version
m = re.search('(?<=Linux version )\S+', proc_version()) else:
m = re.search('(?<=Linux version )(\S+)', proc_version_)
if m: if m:
version = clean_kernel_version(m.group(0)) version = clean_kernel_version(m.group(0))
return version return version

@ -38,6 +38,10 @@ class RunningKernelVersionTestCase(unittest.TestCase):
self.assertEqual(check_kernel.running_kernel_version(), self.assertEqual(check_kernel.running_kernel_version(),
Version('3.16.7-ckt9-3~deb8u1')) Version('3.16.7-ckt9-3~deb8u1'))
with unittest.mock.patch.object(check_kernel, 'proc_version', return_value='Linux version 2.6.32-5-amd64 (Debian 2.6.32-48squeeze11) (ben@decadent.org.uk) (gcc version 4.3.5 (Debian 4.3.5-4) ) #1 SMP Wed Feb 18 13:14:10 UTC 2015'):
self.assertEqual(check_kernel.running_kernel_version(),
Version('2.6.32-48squeeze11'))
class VersionTestCase(unittest.TestCase): class VersionTestCase(unittest.TestCase):
def testStr(self): def testStr(self):
@ -45,11 +49,14 @@ class VersionTestCase(unittest.TestCase):
def testComparingTrivial(self): def testComparingTrivial(self):
self.assertEqual(Version('1.0'), Version('1.0')) self.assertEqual(Version('1.0'), Version('1.0'))
self.assertEqual(Version('2.6.32-5foo1'), Version('2.6.32-5foo1'))
self.assertGreater(Version('2.0'), Version('1.0')) self.assertGreater(Version('2.0'), Version('1.0'))
def testComparingNonTrivial(self): def testComparingNonTrivial(self):
self.assertGreater(Version('2.12'), Version('2.2')) self.assertGreater(Version('2.12'), Version('2.2'))
self.assertGreater(Version('3.12-12'), Version('3.12-2')) self.assertGreater(Version('3.12-12'), Version('3.12-2'))
self.assertGreater(Version('2.6.32-5foo1'), Version('2.6.32-5foo0'))
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save