py.cleanup 862 B

12345678910111213141516171819202122232425262728293031
  1. #! /usr/bin/env python
  2. import sys, os, stat
  3. from bsdopendirtype import opendir
  4. def clean(path):
  5. global count
  6. try:
  7. content = opendir(path)
  8. except OSError:
  9. print >> sys.stderr, "skipping", path
  10. return
  11. for filename, smode in content:
  12. if stat.S_ISDIR(smode):
  13. clean(filename)
  14. if filename.endswith('/__pycache__'):
  15. try:
  16. os.rmdir(filename)
  17. except OSError:
  18. pass
  19. elif (filename.endswith('.pyc') or filename.endswith('.pyo') or
  20. filename.endswith('.pyc~') or filename.endswith('.pyo~')):
  21. os.unlink(filename)
  22. count += 1
  23. count = 0
  24. for arg in sys.argv[1:] or ['.']:
  25. print "cleaning path", arg, "of .pyc/.pyo/__pycache__ files"
  26. clean(arg)
  27. print "%d files removed" % (count,)