Skip to content

Instantly share code, notes, and snippets.

@Tatsh
Created April 15, 2015 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tatsh/6ef50cb0eea22f3dc7b7 to your computer and use it in GitHub Desktop.
Save Tatsh/6ef50cb0eea22f3dc7b7 to your computer and use it in GitHub Desktop.
Gentoo: Clean up old kernel sources in /usr/src
#!/usr/bin/env python
from os import chdir, listdir
from os.path import basename, islink, realpath
from shutil import rmtree
import re
import subprocess as sp
import sys
if __name__ == '__main__':
chdir('/usr/src')
current = None
for name in listdir('.'):
if islink(name):
current = basename(realpath(name))
cmd = ['eselect', 'kernel', 'list']
output = sp.check_output(cmd).decode('utf-8').splitlines()
for line in output:
if line.endswith('*'):
output = line
break
eselect_current = re.split(r'\s+', output)[2]
if not current or not eselect_current or current != eselect_current:
print('Invalid state. Use `eselect kernel` to set a kernel.',
file=sys.stderr)
sys.exit(1)
print('Current: {}'.format(current), file=sys.stderr)
removed = False
for name in listdir('.'):
if islink(name) or not name.startswith('linux-') or name == current:
continue
print('Removing ./{}'.format(name), file=sys.stderr)
rmtree('./{}'.format(name))
removed = True
if not removed:
print('Found nothing to clean up', file=sys.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment