Michał Górny avatar Michał Górny committed 8334e58

Introduce a nicer debugging.

Comments (0)

Files changed (3)

ecleankernel/cli.py

 # (c) 2010 Michał Górny <mgorny@gentoo.org>
 # Released under the terms of the 2-clause BSD license.
 
+from __future__ import print_function
+
 from optparse import OptionParser
 
 from .kernel import find_kernels
 or only those which are referenced by a bootloader (with -a).
 '''
 
+class NullDebugger(object):
+	def __init__(self):
+		self._indent = 1
+
+	def print(self, msg):
+		pass
+
+	def printf(self, fstr, *args):
+		self.print(fstr % args)
+
+	def indent(self, n = 1, heading = None):
+		if heading is not None:
+			self.print(heading)
+		self._indent += n
+
+	def outdent(self, n = 1):
+		self._indent -= n
+
+class ConsoleDebugger(NullDebugger):
+	def print(self, msg):
+		ind = '*' * self._indent
+		print('%s %s' % (ind, msg))
+
 def main(argv):
 	parser = OptionParser(description = ecleankern_desc.strip())
 	parser.add_option('-a', '--all',
 			help='Print the list of kernels to be removed and exit')
 	(opts, args) = parser.parse_args(argv[1:])
 
+	debug = ConsoleDebugger() if opts.debug else NullDebugger()
+
 	kernels = find_kernels()
 	removals = get_removal_list(kernels,
 			limit = None if opts.all else opts.num,
 			bootloader = opts.bootloader,
 			destructive = opts.destructive,
-			debug = opts.debug)
+			debug = debug)
 
 	if not removals:
 		print('No outdated kernels found.')

ecleankernel/grub.py

 # (c) 2010 Michał Górny <mgorny@gentoo.org>
 # Released under the terms of the 2-clause BSD license.
 
+from __future__ import print_function
+
 import os.path, re
 
-def get_grub_kernels(debug = False):
+def get_grub_kernels(debug):
 	kernel_re = re.compile(r'^\s*kernel\s*(\S+)',
 			re.MULTILINE | re.IGNORECASE)
 
 	f = open('/boot/grub/grub.conf')
-	if debug:
-		print('*** grub.conf found')
+	debug.print('grub.conf found')
 
 	def _get_kernels(f):
+		debug.indent(heading = 'matching grub.conf')
 		try:
 			for m in kernel_re.finditer(f.read()):
 				path = m.group(1)
-				if debug:
-					print('**** regexp matched path %s' % path)
-					print('     from line: %s' % m.group(0))
+				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, '/'))
-					print('***** 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)

ecleankernel/process.py

 # (c) 2010 Michał Górny <mgorny@gentoo.org>
 # Released under the terms of the 2-clause BSD license.
 
+from __future__ import print_function
+
 import errno, os, os.path, re
 
 from .grub import get_grub_kernels
 		if k.vmlinuz is None:
 			yield k
 
-def get_removal_list(kernels, limit = 0, bootloader = 'auto', destructive = False, debug = False):
+def get_removal_list(kernels, debug, limit = 0, bootloader = 'auto', destructive = False):
 	""" Get a list of outdated kernels to remove. With explanations. """
 
-	if debug:
-		print('* In get_removal_list()')
+	debug.indent(heading = 'In get_removal_list()')
 
 	out = RemovedKernelDict()
 	for k in remove_stray(kernels):
 			used = ()
 			for bl, getfunc in bootloaders:
 				if bootloader in ('auto', bl):
-					if debug:
-						print('** Trying bootloader %s' % bl)
+					debug.printf('Trying bootloader %s', bl)
 					try:
+						debug.indent()
 						used = getfunc(debug = debug)
+						debug.outdent()
 					except IOError as e:
 						if e.errno != errno.ENOENT:
 							raise
 			return False
 		return True
 
+	debug.outdent()
 	return list(filter(not_current, out))
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.