Advertisement
Guest User

Untitled

a guest
May 25th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from os import chdir, listdir
  3. from os.path import basename, islink, realpath
  4. from shutil import rmtree
  5. import re
  6. import subprocess as sp
  7. import sys
  8.  
  9.  
  10. if __name__ == '__main__':
  11. chdir('/usr/src')
  12. current = None
  13.  
  14. for name in listdir('.'):
  15. if islink(name):
  16. current = basename(realpath(name))
  17.  
  18. cmd = ['eselect', 'kernel', 'list']
  19. output = sp.check_output(cmd).decode('utf-8').splitlines()
  20. for line in output:
  21. if line.endswith('*'):
  22. output = line
  23. break
  24. eselect_current = re.split(r'\s+', output)[2]
  25.  
  26. if not current or not eselect_current or current != eselect_current:
  27. print('Invalid state. Use `eselect kernel` to set a kernel.',
  28. file=sys.stderr)
  29. sys.exit(1)
  30.  
  31. print('Current: {}'.format(current), file=sys.stderr)
  32.  
  33. removed = False
  34. for name in listdir('.'):
  35. if islink(name) or not name.startswith('linux-') or name == current:
  36. continue
  37. print('Removing ./{}'.format(name), file=sys.stderr)
  38. rmtree('./{}'.format(name))
  39. removed = True
  40.  
  41. if not removed:
  42. print('Found nothing to clean up', file=sys.stderr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement