Debian Codegraph @

demo/py.cleanup

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