Commits

Armin Rigo committed 4f48b52

Rewrite py.cleanup to also remove __pycache__ directories,
created by CPython 3.x.

Comments (0)

Files changed (1)

pypy/tool/py.cleanup

 #!/usr/bin/env python
-import py, sys
+import sys, os, stat, shutil
 
-def shouldremove(p):
-    return p.ext == '.pyc'
+def clean(path):
+    global count
+    try:
+        content = os.listdir(path)
+    except OSError:
+        print >> sys.stderr, "skipping", path
+        return
+    for fn in content:
+        filename = os.path.join(path, fn)
+        st = os.lstat(filename)
+        if stat.S_ISDIR(st.st_mode):
+            if fn == '__pycache__':
+                shutil.rmtree(filename)
+                count += 1
+            else:
+                clean(filename)
+        elif fn.endswith('.pyc') or fn.endswith('.pyo'):
+            os.unlink(filename)
+            count += 1
 
 count = 0
 
 for arg in sys.argv[1:] or ['.']:
-    path = py.path.local(arg)
-    print "cleaning path", path, "of .pyc files"
-    for x in path.visit(shouldremove, lambda x: x.check(dotfile=0, link=0)):
-        x.remove()
-        count += 1
+    print "cleaning path", arg, "of .pyc/.pyo/__pycache__ files"
+    clean(arg)
 
 print "%d files removed" % (count,)