-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpycleaner.py
56 lines (47 loc) · 1.72 KB
/
pycleaner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
import os
import fnmatch
class GlobDirectoryWalker:
# a forward iterator that traverses a directory tree
def __init__(self, directory, pattern="*"):
self.stack = [directory]
self.pattern = pattern
self.files = []
self.index = 0
def __getitem__(self, index):
while 1:
try:
file = self.files[self.index]
self.index = self.index + 1
except IndexError:
# pop next directory from stack
self.directory = self.stack.pop()
self.files = os.listdir(self.directory)
self.index = 0
else:
# got a filename
fullname = os.path.join(self.directory, file)
if os.path.isdir(fullname) and not os.path.islink(fullname):
self.stack.append(fullname)
if fnmatch.fnmatch(file, self.pattern):
return fullname
def pycCleanup(directory,path,filext='pyc'):
for filename in directory:
if filename[-3:] == filext:
print '- ' + filename
os.remove(path+os.sep+filename)
elif os.path.isdir(path+os.sep+filename):
pycCleanup(os.listdir(path+os.sep+filename),path+os.sep+filename)
def cleanup1(filext='pyc'):
directory = os.listdir('.')
print('Deleting .%s files recursively in %s.' % (filext, str(directory)))
pycCleanup(directory,'.',filext)
def cleanup2(filext='pyc'):
for file in GlobDirectoryWalker(".", "*."+filext):
print file
os.remove(file)
print "After..."
for file in GlobDirectoryWalker(".", "*."+filext):
print file
if __name__ == '__main__':
cleanup1()