Commits

Michał Górny committed b73fc9e

Support -n to keep NUM newest kernels.

Comments (0)

Files changed (2)

ecleankernel/cli.py

 	parser.add_option('-d', '--destructive',
 			dest='destructive', action='store_true', default=False,
 			help='Destructive mode: remove kernels even when referenced by bootloader')
+	parser.add_option('-n', '--num',
+			dest='num', type='int', default=0,
+			help='Leave only newest NUM kernels')
 	parser.add_option('-p', '--pretend',
 			dest='pretend', action='store_true', default=False,
 			help='Print the list of kernels to be removed and exit')
 
 	kernels = find_kernels()
 	removals = get_removal_list(kernels,
-			limit = None if opts.all else 0,
+			limit = None if opts.all else opts.num,
 			bootloader = opts.bootloader,
 			destructive = opts.destructive)
 

ecleankernel/process.py

 			used = frozenset(unprefixify(realpaths))
 
 		if limit is not None:
-			raise NotImplementedError('Limiting number of removed kernels not supported yet')
-		for k in kernels:
+			def getmtime(k):
+				return os.path.getmtime(k.vmlinuz)
+
+			ordered = sorted(kernels, key = getmtime, reverse = True)
+			candidates = ordered[limit:]
+		else:
+			candidates = kernels
+
+		for k in candidates:
 			if destructive:
 				out.add(k, 'unwanted')
 			elif k.version not in used: