Commits

Michał Górny committed a92e36d

Move getting bootloader into separate function.

Comments (0)

Files changed (3)

ecleankernel/bootloader/__init__.py

+#	vim:fileencoding=utf-8
+# (c) 2011 Michał Górny <mgorny@gentoo.org>
+# Released under the terms of the 2-clause BSD license.
+
+from .grub import GRUB
+from .grub2 import GRUB2
+from .lilo import LILO
+from .yaboot import Yaboot
+from .symlinks import Symlinks
+
+bootloaders = (LILO, GRUB2, GRUB, Yaboot, Symlinks)
+
+def get_bootloader(debug, requested = None):
+	for bl in bootloaders:
+		if requested in ('auto', bl.name):
+			debug.printf('Trying bootloader %s', bl.name)
+			debug.indent()
+			try:
+				return bl(debug = debug)
+			except IOError as e:
+				if e.errno != errno.ENOENT:
+					raise
+			finally:
+				debug.outdent()

ecleankernel/cli.py

 
 from optparse import OptionParser
 
+from .bootloader import bootloaders
 from .kernel import find_kernels, Kernel
-from .process import get_removal_list, bootloaders
+from .process import get_removal_list
 
 ecleankern_desc = '''
 Remove old kernel versions, keeping either N newest kernels (with -n)

ecleankernel/process.py

 
 from __future__ import print_function
 
-import errno, os, os.path, re
-
-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
+from .bootloader import get_bootloader
 
-bootloaders = (LILO, GRUB2, GRUB, Yaboot, Symlinks)
+import errno, os, os.path, re
 
 class RemovedKernelDict(dict):
 	def add(self, k, reason):
 
 	if limit is None or limit > 0:
 		if not destructive:
-			used = ()
-			lastbl = None
-			for bl in bootloaders:
-				if bootloader in ('auto', bl.name):
-					debug.printf('Trying bootloader %s', bl.name)
-					try:
-						debug.indent()
-						blinst = bl(debug = debug)
-						used = blinst()
-						debug.outdent()
-					except IOError as e:
-						if e.errno != errno.ENOENT:
-							raise
-					else:
-						lastbl = blinst
-						break
+			lastbl = get_bootloader(requested = bootloader, debug = debug)
+			used = lastbl()
 
 			if lastbl is None:
 				raise SystemError('Unable to get kernels from bootloader config (%s)'