[pypy-commit] pypy default: Rewrite py.cleanup to also remove __pycache__ directories,

arigo noreply at buildbot.pypy.org
Sat Sep 3 14:42:00 CEST 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r47043:4f48b5228d50
Date: 2011-09-03 14:41 +0200
http://bitbucket.org/pypy/pypy/changeset/4f48b5228d50/

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

diff --git a/pypy/tool/py.cleanup b/pypy/tool/py.cleanup
--- a/pypy/tool/py.cleanup
+++ b/pypy/tool/py.cleanup
@@ -1,16 +1,30 @@
 #!/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,)


More information about the pypy-commit mailing list