-
Notifications
You must be signed in to change notification settings - Fork 6
/
UnusedImage.py
70 lines (53 loc) · 1.84 KB
/
UnusedImage.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
57
58
59
60
61
62
63
64
65
66
67
68
69
# coding=utf-8
import glob
import os
import re
#***************************************************************************
#must set the imageasset path
imageSet = glob.glob('Resources/images.xcassets/*/*.imageset')
#option ignore the files
ignores = {r'loading_\d+'}
# must set the source code path
sourcePath = ''
#***************************************************************************
def findUnusedResource(imageFolder):
img_names = [os.path.basename(pic)[:-9] for pic in imageFolder]
unused_imgs = []
for i in range(0, len(imageFolder)):
pic_name = img_names[i]
if ignoreFile(pic_name):
# print 'ignore file:%s' % (imageFolder[i])
continue
result = checkContentFromFolder(pic_name,sourcePath)
if not result:
unused_imgs.append(imageFolder[i])
print 'remove %s' % (pic_name)
# print 'remove %s' % (imageFolder[i])
os.system('rm -rf "%s"' % (imageFolder[i]))
text_path = 'unused.log'
tex = '\n'.join(sorted(unused_imgs))
os.system('echo "%s" > %s' % (tex, text_path))
print 'Unused res:%d' % (len(unused_imgs))
print 'Done!'
def checkContentFromFolder(content,folder):
for dname, dirs, files in os.walk(folder):
for fname in files:
fpath = os.path.join(dname, fname)
if fpath.find("/.git/") != -1:
print fpath
continue
if fpath.find("Contents.json") != -1:
# print fpath
continue
with open(fpath,'r') as f:
s = f.read()
if content in s:
return True
return False
def ignoreFile(str):
for ignore in ignores:
if re.match(ignore, str):
return True
return False
if __name__ == '__main__':
findUnusedResource(imageSet)