-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_with_mapping_file.py
executable file
·113 lines (100 loc) · 3.65 KB
/
rename_with_mapping_file.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
#!/usr/bin/env python
import sys
import os
import re
import shutil
dir_name = sys.argv[1]
mapping_file = sys.argv[2]
new_dir_name = sys.argv[3]
# get file list
file_list = []
for root, dirs , files in os.walk(dir_name, True):
for file in files:
file_list.append("%s/%s"%(root,file))
# convert to package name list
pkg_name_list= []
pkg_name_list_str = ""
for file in file_list:
if not file.endswith('.class'):
continue
file = os.path.relpath(file, dir_name)
file = file.split('.')[0]
file = file.replace('/', '.')
pkg_name_list.append(file)
pkg_name_list_str += file + '\n'
# construct map: proguard package -> unproguard package
pkg_name_map = {}
mapping_lines = open(mapping_file, 'r').readlines()
current_process_line_num = 0
processed_line_num_delta = 0
total_mapping_line_num = len(mapping_lines)
print "will construct file name mapping, this process may be very slow ..."
for line in mapping_lines:
# show progress
current_process_line_num += 1
processed_line_num_delta += 1
if processed_line_num_delta > total_mapping_line_num/20.0:
processed_line_num_delta = 0
progress = round(current_process_line_num * 100.0 / total_mapping_line_num)
#print "current progress is: " + str(progress) + "%"
# execlude unused lines
if line.startswith(' '):
continue
# line's format
#com.uc.external.barcode.oned.Code39Reader -> com.uc.external.barcode.b.c:
simple_name = line.split()[-1][:-1]
if pkg_name_list_str.find(simple_name) == -1:
continue
for n in pkg_name_list:
if line.find("-> " + n + ":") != -1:
v = line.split()[0]
pkg_name_map[n] = v
break
print "construct file name mapping complete!"
def get_recovered_name(package_name, mapping_str):
# for regex special char
package_name = package_name.replace('$', '\$')
pattern_str = "(?P<origin>[\w.\$0-9]+)\ ->\ %s"%package_name
r = re.search(pattern_str, mapping_str)
# if failed, fallback to origin proguarded package name
if r == None:
return package_name
return r.group("origin")
# rename according to mapping file
current_process_file_num = 0
processed_file_num_delta = 0
print "will rename class file using mapping file, this process may be very slow ..."
for file in file_list:
# show progress every 10% add
current_process_file_num += 1
processed_file_num_delta += 1
if processed_file_num_delta >= len(file_list)/10.0:
processed_file_num_delta = 0
progress = round(current_process_file_num * 100.0 / len(file_list))
#print "current progress is: " + str(progress) + "%"
old_file = file
if not file.endswith('.class'):
continue
file = os.path.relpath(file, dir_name)
file = file.split('.')[0]
file = file.replace('/', '.')
new_file = ""
if pkg_name_map.has_key(file):
new_file = pkg_name_map[file]
new_file = new_file.replace('.', '/')
new_file = new_file + ".class"
new_file = os.path.join(new_dir_name, new_file)
else:
#print "cannot file mapping file: " + old_file
new_file = os.path.relpath(old_file, dir_name)
new_file = os.path.join(new_dir_name, new_file)
#recovered_package_name = get_recovered_name(file, mapping_str)
#new_file = recovered_package_name.replace('.', '/')
#new_file = new_file + ".class"
#new_file = os.path.join(new_dir_name, new_file)
new_file_dir_name = os.path.dirname(new_file)
if not os.path.exists(new_file_dir_name):
os.makedirs(new_file_dir_name)
#print "will copy:", old_file, new_file
shutil.copy(old_file, new_file)
print "rename class file name complete!"