stale_pyc.py 724 Bytes
Newer Older
1
#!/usr/bin/env python
2 3
from __future__ import print_function

4 5 6 7 8
import os
import sys

root = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
verbose = '-v' in sys.argv
9 10
excludes = ('.git', '.svn', '__pycache__')

11
for dirpath, dirnames, filenames in os.walk(root):
12 13 14 15 16
    for exclude in excludes:
        try:
            dirnames.remove(exclude)
        except ValueError:
            pass
17 18 19 20 21
    for filename in filenames:
        if filename.endswith('.pyc') or filename.endswith('pyo'):
            if not os.path.exists(os.path.join(dirpath, filename[:-1])):
                os.unlink(os.path.join(dirpath, filename))
                if verbose:
22
                    print(os.path.join(dirpath, filename))