-
Notifications
You must be signed in to change notification settings - Fork 2
/
find.py
118 lines (87 loc) · 2.78 KB
/
find.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# -*- coding: UTF-8 -*-
#
# 2017.08.28
import os
import re
import time
"""
Find files
"""
def findFiles(dir):
IMAGE = []
FILE = []
COLOR = []
DRAWABLE = []
startTime = time.time()
# 三个参数:分别返回 1.父目录 2.所有文件夹名字(不含路径)3.所有文件名字
for parent, dirnames, filenames in os.walk(dir):
for filename in filenames:
if "\\build" in parent or "\\.idea" in parent or "\\.gradle" in parent:
continue
ext = file_extension(filename)
if ext == ".png" or ext == ".jpg":
IMAGE.append(os.path.join(parent, filename))
elif ext == ".java" or ext == ".kt" or ext == ".xml":
FILE.append(os.path.join(parent, filename))
if "color.xml" in filename:
COLOR.append(os.path.join(parent, filename))
if "\\drawable" in parent and ext == ".xml":
DRAWABLE.append(os.path.join(parent, filename))
endTime = time.time()
print("== It take %d second to find files.." % (endTime - startTime))
return IMAGE, FILE, COLOR, DRAWABLE
def file_extension(path):
return os.path.splitext(path)[1]
def contain(target, file):
try:
fopen = open(file, 'r', encoding='UTF-8')
for line in fopen:
if target in line:
fopen.close()
return True
fopen.close()
return False
except:
return False
def containPath(path, file):
name = os.path.basename(path).split(".")[0]
return contain(name, file)
def containOne(list, content):
for item in list:
if item in content:
return True
return False
"""
Find colors
"""
def findColors(colorFiles):
startTime = time.time()
COLOR = []
for colorFile in colorFiles:
fopen = open(colorFile, 'r', encoding='UTF-8')
content = fopen.read()
pattern_key = re.compile(r'<color name="(.+?)">', re.S)
COLOR += re.findall(pattern_key, content)
fopen.close()
endTime = time.time()
print("== It take %d second to find colors.." % (endTime - startTime))
return COLOR
def findRedundantColor(colors, files):
CNT = 0
COLOR_XML_FILE = []
COLOR_REDUNDANT = []
for color in colors:
isUsed = False
for file in files:
if "color.xml" in file:
COLOR_XML_FILE.append(file)
if "color.xml" not in file and contain(color, file):
isUsed = True
#print("++ Used : %s, %s" % (color, file))
continue
if isUsed == False:
CNT += 1
print(">> Redundant : " + color)
COLOR_REDUNDANT.append(color)
return COLOR_XML_FILE, COLOR_REDUNDANT, CNT