-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdel_styles.py
70 lines (55 loc) · 2.42 KB
/
del_styles.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
import os
import re
project_path = ""
# project_path = "D:\workplace\Kmob"
unused_styles = set()
def read_unused_styles(path):
global unused_styles
for line in open(path):
line = line.strip()
if not line == '':
unused_styles.add(line)
def delete_xml_styles(res_folder_path):
global unused_styles
size = 0
if len(unused_styles) == 0:
print 'no styles to be deleted'
else:
pattern = re.compile('<style name="([\w|\.]+)"')
for folder in os.listdir(res_folder_path):
if folder.startswith('values'):
for f in os.listdir(res_folder_path + os.sep + folder):
if f.startswith('style') or f.startswith('kmob_style'):
infile = res_folder_path + os.sep + folder + os.sep + f
outfile = res_folder_path + os.sep + folder + os.sep + "temp"
infp = open(infile, 'rb')
outfp = open(outfile, 'wb')
before = os.path.getsize(infile)
is_in_style = False
for line in infp:
if is_in_style and line.strip().endswith('</style>'):
is_in_style = False
continue
if is_in_style:
continue
else:
m = pattern.search(line)
if m:
name = m.group(1)
if name in unused_styles:
if not line.strip().endswith('</style>'):
is_in_style = True
else:
is_in_style = False
else:
outfp.write(line)
else:
outfp.write(line)
infp.close()
outfp.close()
after = os.path.getsize(outfile)
os.remove(infile)
os.rename(outfile, infile)
size = size + (before - after)
print "delete styles size=" + str(size / 1024) + "K"
return size, unused_styles