Support Debian

master
Mike Gerber 10 years ago
parent 5a9997058d
commit 08d16574fc

@ -14,24 +14,45 @@ def proc_version():
def running_kernel_version(): def running_kernel_version():
m = re.findall('(?<=Debian )\S+', proc_version())
if m:
# Note: it's the _second_ match
version = clean_kernel_version(m[1])
return version
m = re.search('(?<=Linux version )\S+', proc_version()) 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))
else:
version = None
return version return version
def is_debian():
return posixpath.exists('/etc/debian_version')
def is_fedora(): def is_fedora():
return posixpath.exists('/etc/fedora-release') return posixpath.exists('/etc/fedora-release')
def installed_kernel_versions(): def installed_kernel_versions():
if is_debian():
return installed_kernel_versions_debian()
if is_fedora(): if is_fedora():
return installed_kernel_versions_fedora() return installed_kernel_versions_fedora()
def installed_kernel_versions_debian():
dpkg_out = subprocess.check_output(
['dpkg-query', '--show', '--showformat', '${Package} ${Version}\n', 'linux-image*'])
dpkg_out = dpkg_out.strip()
versions = dpkg_out.split('\n')
versions = [v for v in versions if re.search('^linux-image-\d', v)]
versions = [clean_kernel_version(v.split(' ')[1]) for v in versions]
return versions
def installed_kernel_versions_fedora(): def installed_kernel_versions_fedora():
rpm_out = subprocess.check_output( rpm_out = subprocess.check_output(
['rpm', '--queryformat=%{VERSION}-%{RELEASE}\n', '-q', 'kernel']) ['rpm', '--queryformat=%{VERSION}-%{RELEASE}\n', '-q', 'kernel'])

@ -16,6 +16,11 @@ import check_kernel
class CleanKernelVersionTestCase(unittest.TestCase): class CleanKernelVersionTestCase(unittest.TestCase):
def testDebian(self):
versions = [('3.16.7-ckt9-3~deb8u2', '3.16.7-ckt9-3~deb8u2')]
for dirty, clean in versions:
self.assertEqual(clean_kernel_version(dirty), LooseVersion(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')]
@ -29,6 +34,11 @@ class RunningKernelVersionTestCase(unittest.TestCase):
self.assertEqual(check_kernel.running_kernel_version(), self.assertEqual(check_kernel.running_kernel_version(),
LooseVersion('4.0.3-301')) LooseVersion('4.0.3-301'))
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)'):
self.assertEqual(check_kernel.running_kernel_version(),
LooseVersion('3.16.7-ckt9-3~deb8u1'))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

Loading…
Cancel
Save