Michał Górny avatar Michał Górny committed 39a45e5

Unify and reuse bootloader code.

Comments (0)

Files changed (6)

ecleankernel/bootloader/grub.py

 # (c) 2011 Michał Górny <mgorny@gentoo.org>
 # Released under the terms of the 2-clause BSD license.
 
-from __future__ import print_function
+from .lilo import LILO
 
-import os.path, re
+import os.path
 
-def get_grub_kernels(debug):
-	kernel_re = re.compile(r'^\s*kernel\s*(\([^)]+\))?(\S+)',
-			re.MULTILINE | re.IGNORECASE)
+class GRUB(LILO):
+	name = 'grub'
+	kernel_re = r'^\s*kernel\s*(\([^)]+\))?(?P<path>\S+)'
+	def_path = '/boot/grub/grub.conf'
 
-	f = open('/boot/grub/grub.conf')
-	debug.print('grub.conf found')
+	def _get_kernels(self, f):
+		debug = self._debug
 
-	def _get_kernels(f):
-		debug.indent(heading = 'matching grub.conf')
-		try:
-			for m in kernel_re.finditer(f.read()):
-				path = m.group(2)
-				debug.printf('regexp matched path %s', path)
+		for path in LILO._get_kernels(self, f):
+			if os.path.relpath(path, '/boot').startswith('..'):
+				path = os.path.join('/boot', os.path.relpath(path, '/'))
 				debug.indent()
-				debug.printf('from line: %s', m.group(0))
-				if os.path.relpath(path, '/boot').startswith('..'):
-					path = os.path.join('/boot', os.path.relpath(path, '/'))
-					debug.printf('appending /boot, path now: %s', path)
+				debug.printf('appending /boot, path now: %s', path)
 				debug.outdent()
-				yield path
-		finally:
-			debug.outdent()
-			f.close()
-
-	return _get_kernels(f)
+			yield path

ecleankernel/bootloader/grub2.py

 # (c) 2011 Michał Górny <mgorny@gentoo.org>
 # Released under the terms of the 2-clause BSD license.
 
-from __future__ import print_function
+from .grub import GRUB
 
-import os.path, re
-
-def get_grub2_kernels(debug):
-	kernel_re = re.compile(r'^\s*linux\s*(\([^)]+\))?(\S+)',
-			re.MULTILINE | re.IGNORECASE)
-
-	f = open('/boot/grub/grub.cfg')
-	debug.print('grub.cfg found')
-
-	def _get_kernels(f):
-		debug.indent(heading = 'matching grub.cfg')
-		try:
-			for m in kernel_re.finditer(f.read()):
-				path = m.group(2)
-				debug.printf('regexp matched path %s', path)
-				debug.indent()
-				debug.printf('from line: %s', m.group(0))
-				if os.path.relpath(path, '/boot').startswith('..'):
-					path = os.path.join('/boot', os.path.relpath(path, '/'))
-					debug.printf('appending /boot, path now: %s', path)
-				debug.outdent()
-				yield path
-		finally:
-			debug.outdent()
-			f.close()
-
-	return _get_kernels(f)
+class GRUB2(GRUB):
+	name = 'grub2'
+	kernel_re = r'^\s*linux\s*(\([^)]+\))?(?P<path>\S+)'
+	def_path = '/boot/grub/grub.cfg'

ecleankernel/bootloader/lilo.py

 # (c) 2011 Michał Górny <mgorny@gentoo.org>
 # Released under the terms of the 2-clause BSD license.
 
+from __future__ import print_function
+
 import re
 
-def get_lilo_kernels(debug = False):
-	kernel_re = re.compile(r'^\s*image\s*=\s*(.+)\s*$',
-			re.MULTILINE | re.IGNORECASE)
+class LILO(object):
+	name = 'lilo'
+	kernel_re = r'^\s*image\s*=\s*(?P<path>.+)\s*$'
+	def_path = '/etc/lilo.conf'
+
+	def __init__(self, debug = False):
+		self._debug = debug
+		self._kernel_re = re.compile(self.kernel_re,
+				re.MULTILINE | re.IGNORECASE)
 
-	f = open('/etc/lilo.conf')
+	def _get_kernels(self, f):
+		debug = self._debug
 
-	def _get_kernels(f):
+		debug.indent(heading = 'matching...')
 		try:
-			for m in kernel_re.finditer(f.read()):
-				yield m.group(1)
+			for m in self._kernel_re.finditer(f.read()):
+				path = m.group('path')
+				debug.printf('regexp matched path %s', path)
+				debug.indent()
+				debug.printf('from line: %s', m.group(0))
+				debug.outdent()
+				yield path
 		finally:
 			f.close()
+			debug.outdent()
+
+	def __call__(self, path = None):
+		f = open(path or self.def_path)
+		self._debug.print('%s found' % (path or self.def_path))
 
-	return _get_kernels(f)
+		return self._get_kernels(f)

ecleankernel/bootloader/symlinks.py

 
 import os.path
 
-def get_vmlinuz_symlinks(debug = False):
-	for fn in ('vmlinuz', 'vmlinux', 'kernel', 'bzImage'):
-		for suffix in ('', '.old'):
-			f = '/boot/%s%s' % (fn, suffix)
-			if os.path.exists(f):
-				yield f
+class Symlinks(object):
+	name = 'symlinks'
+
+	def __init__(self, debug = False):
+		self._debug = debug
+
+	def __call__(self, path = None):
+		for fn in ('vmlinuz', 'vmlinux', 'kernel', 'bzImage'):
+			for suffix in ('', '.old'):
+				f = '/boot/%s%s' % (fn, suffix)
+				if os.path.exists(f):
+					yield f

ecleankernel/bootloader/yaboot.py

 # (c) 2011 Michał Górny <mgorny@gentoo.org>
 # Released under the terms of the 2-clause BSD license.
 
-import re
+from .lilo import LILO
 
-def get_yaboot_kernels(debug = False):
-	kernel_re = re.compile(r'^\s*image\s*=\s*(.+)\s*$',
-			re.MULTILINE | re.IGNORECASE)
-
-	f = open('/etc/yaboot.conf')
-
-	def _get_kernels(f):
-		try:
-			for m in kernel_re.finditer(f.read()):
-				yield m.group(1)
-		finally:
-			f.close()
-
-	return _get_kernels(f)
+class Yaboot(LILO):
+	name = 'yaboot'
+	def_path = '/etc/yaboot.conf'

ecleankernel/process.py

 
 import errno, os, os.path, re
 
-from .bootloader.grub import get_grub_kernels
-from .bootloader.grub2 import get_grub2_kernels
-from .bootloader.lilo import get_lilo_kernels
-from .bootloader.yaboot import get_yaboot_kernels
-from .bootloader.symlinks import get_vmlinuz_symlinks
-
-bootloaders = (
-	('lilo', get_lilo_kernels),
-	('grub2', get_grub2_kernels),
-	('grub', get_grub_kernels),
-	('yaboot', get_yaboot_kernels),
-	('symlinks', get_vmlinuz_symlinks),
-)
+from .bootloader.grub import GRUB
+from .bootloader.grub2 import GRUB2
+from .bootloader.lilo import LILO
+from .bootloader.yaboot import Yaboot
+from .bootloader.symlinks import Symlinks
+
+bootloaders = (LILO, GRUB2, GRUB, Yaboot, Symlinks)
 
 class RemovedKernelDict(dict):
 	def add(self, k, reason):
 	if limit is None or limit > 0:
 		if not destructive:
 			used = ()
-			for bl, getfunc in bootloaders:
-				if bootloader in ('auto', bl):
-					debug.printf('Trying bootloader %s', bl)
+			for bl in bootloaders:
+				if bootloader in ('auto', bl.name):
+					debug.printf('Trying bootloader %s', bl.name)
 					try:
 						debug.indent()
-						used = getfunc(debug = debug)
+						blinst = bl(debug = debug)
+						used = blinst()
 						debug.outdent()
 					except IOError as e:
 						if e.errno != errno.ENOENT:
 							raise
 					else:
-						lastbl = bl
+						lastbl = blinst
 						break
 
 			realpaths = [os.path.realpath(x) for x in used]
 			if destructive:
 				out.add(k, 'unwanted')
 			elif k.version not in used:
-				out.add(k, 'not referenced by bootloader (%s)' % lastbl)
+				out.add(k, 'not referenced by bootloader (%s)' % lastbl.name)
 
 	current = os.uname()[2]
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.