-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSameCodeFinder.py
More file actions
302 lines (249 loc) · 9.63 KB
/
SameCodeFinder.py
File metadata and controls
302 lines (249 loc) · 9.63 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python
#coding=utf-8
#---------------------------------------------
# Name: SameCodeFinder.py
# Author: Xing Chen
# Date: 2016-12-02
# Description: SameCodeFinder is a static code
# text scanner which can find the similar or
# the same code file in a big directory.
#----------------------------------------------
import re
import os
import sys
import fileinput
import datetime
from simhash import Simhash, SimhashIndex
gb_detail = 0
gb_max_dis = 20
gb_min_linecount = 3
gb_output = "out.txt"
def main():
global gb_detail
global gb_max_dis
global gb_min_linecount
global gb_output
if len(sys.argv) <= 1:
print_help()
return
# Accoring to Argv as a Scanner Target root path
root_path= sys.argv[1]
suffix = sys.argv[2]
funciton_standard = 0
for arg in sys.argv:
if arg == "--help":
print_help()
return
elif arg == "--detail":
gb_detail = 1
elif arg == "--functions":
funciton_standard = 1
elif arg.startswith("--max-distance="):
arg_arr = arg.split("=")
gb_max_dis = int(arg_arr[1])
elif arg.startswith("--min-linecount="):
arg_arr = arg.split("=")
gb_min_linecount = int(arg_arr[1])
elif arg.startswith("--output="):
arg_arr = arg.split("=")
gb_output = int(arg_arr[1])
if not suffix:
print "You must assign a suffix. eg: \".m\" \".java\""
return
if not os.path.isdir(root_path):
print "You must assign a dir as first input"
return
if funciton_standard == 1:
print "Hashing all the functions..."
hashed_arr = hash_funcs(root_path, suffix)
else:
print "Hashing all the files..."
hashed_arr = hash_files(root_path, suffix)
if len(hashed_arr) == 0:
return
print "Ranking all the hash results..."
ranked_arr = rank_hash(hashed_arr)
print "Sorting all the ranked results..."
sorted_arr = sorted(ranked_arr, cmp=lambda x,y:cmp(x[2],y[2]))
output_file = open(gb_output, 'w+')
for obj in sorted_arr:
print >>output_file, obj
print "Finished! Result saved in %s" % (gb_output)
def hash_files(root_path, suffix):
hashed_arr = []
for file_path in scan_files(root_path, None, suffix):
single_file_name = file_name(file_path)
if gb_detail == 1:
print "Start Hash File %s" % (single_file_name)
signle_file = open(file_path, 'r')
single_file_content = signle_file.read()
single_hash_result = Simhash(get_features(single_file_content))
hashed_arr.append((single_file_name, single_hash_result))
return hashed_arr
def hash_funcs(root_path, suffix):
hashed_arr = []
if not is_suffix_supported(suffix):
print "The Funcs Standard SameCodeFinder just support Object-C and Java now. Use \".m\" or \".java\", please"
return hashed_arr
for file_path in scan_files(root_path, None, suffix):
single_file_name = file_name(file_path)
single_beauti_name = ""
single_func_content = ""
single_line_count = 0
single_bracket_count = 0
is_function_started = 0
for line in fileinput.input(file_path):
strip_line = line.strip()
## Skip comment lines
if strip_line.startswith("//") or strip_line.startswith("/*") or strip_line.startswith("*"):
continue
if not is_function_started and re.findall(grammar_regex_by_suffix(suffix), line):
## for now, SameCodeFinder support Object-C and Java only
if is_function_start_line(line, suffix):
single_beauti_name = beautify_func_name(line, suffix)
# Reset Line Content
single_func_content = ""
single_line_count = 0
single_bracket_count = 0
single_func_content = single_func_content + line
single_line_count = single_line_count + 1
single_bracket_count = single_bracket_count + count_func_bracket(line)
if single_bracket_count > 0 and single_beauti_name:
is_function_started = 1
if single_bracket_count == 0 and is_function_started == 1:
single_func_name = "%s(%s)" % (single_file_name, single_beauti_name)
if gb_detail == 1:
print "Start Hash Func %s" % (single_func_name)
single_hash_result = Simhash(get_features(single_func_content.strip()))
if single_line_count >= gb_min_linecount:
hashed_arr.append((single_func_name, single_hash_result))
is_function_started = 0
return hashed_arr
def count_func_bracket(line):
count = 0
for word in line:
if word=="{":
count=count+1
elif word=="}":
count=count-1
return count;
def rank_hash(hashed_arr):
global gb_detail
global gb_max_dis
ranked_arr = []
count = len(hashed_arr)
for i in range(0, count):
obj1 = hashed_arr[i]
name1 = obj1[0]
min_distance = 1000
same_obj2_name = ""
if gb_detail == 1:
print "Start Rank %s" % (name1)
for j in range(i + 1, count):
obj2 = hashed_arr[j]
name2 = obj2[0]
distance = obj1[1].distance(obj2[1])
if distance < min_distance:
min_distance = distance
same_obj2_name = name2
if name1 != same_obj2_name and same_obj2_name != "":
if min_distance < gb_max_dis:
ranked_arr.append((name1, same_obj2_name, min_distance))
return ranked_arr
##################################################
################ Start Util Funcs ################
def get_features(s):
width = 3
s = s.lower()
s = re.sub(r'[^\w]+', '', s)
return [s[i:i + width] for i in range(max(len(s) - width + 1, 1))]
#############################################
############### File Processor ##############
def scan_files(directory,prefix=None,postfix=None):
files_list=[]
for root, sub_dirs, files in os.walk(directory):
for special_file in files:
if postfix:
if special_file.endswith(postfix):
files_list.append(os.path.join(root,special_file))
elif prefix:
if special_file.startswith(prefix):
files_list.append(os.path.join(root,special_file))
else:
files_list.append(os.path.join(root,special_file))
return files_list
def file_name(file_path):
name_arr=file_path.split("/")
file_name=name_arr[len(name_arr) - 1]
return file_name
############################################
############# Language Diversity ###########
def grammar_regex_by_suffix(suffix):
if suffix == ".java":
return ur"(public|private)(.*)\)\s?{";
elif suffix == ".m":
return ur"(\-|\+)\s?\(.*\).*(\:\s?\(.*\).*)?{?";
return
def is_suffix_supported(suffix):
if suffix == ".m" or suffix == ".java":
return 1
else:
return 0
def is_function_start_line(line, suffix):
if suffix == ".java":
if line.strip().startswith("public") or line.strip().startswith("private"):
return 1
elif suffix == ".m":
if line.startswith("+") or line.startswith("-"):
return 1
return 0
#############################################
########## Beautify function's name #########
def beautify_func_name(line, suffix):
if suffix == ".m":
return beautify_object_c_func_name(line)
elif suffix == ".java":
return beautify_java_func_name(line)
else:
return
def beautify_java_func_name(func_name):
name_arr = func_name.split("(");
name_func_main = name_arr[0]
name_func_arr = name_func_main.split(" ")
b_name = name_func_arr[len(name_func_arr) - 1]
if b_name:
return b_name
else:
return func_name
def beautify_object_c_func_name(func_name):
has_startLeft = 0
func_new_name = ""
for char in func_name:
if char=="(":
has_startLeft = 1
elif char == ')':
has_startLeft = 0
if has_startLeft == 0 and char != ')' and char != "+" and char != "-" and char != " " and char != "{":
func_new_name = func_new_name + char;
func_new_name = func_new_name.strip()
func_new_name = func_new_name.replace(",...NS_REQUIRES_NIL_TERMINATION", "")
return func_new_name
##########################################
############ Help ########################
def print_help():
print "Usage:\n"
print "\tpython SameFileFinder.python [arg0] [arg1]\n"
print "Args:\n"
print "\t[arg0] - Target Directory of files should be scan"
print "\t[arg1] - Doc Suffix of files should be scan, eg"
print "\t\t .m - Object-C file"
print "\t\t .swift - Swift file"
print "\t\t .java - Java file\n"
print "\t--max-distance=[input] - max hamming distance to keep, default is 20"
print "\t--min-linecount=[input] - for function scan, the function would be ignore if the total line count of the function less than min-linecount"
print "\t--functions - Use Functions as code scan standard"
print "\t Attention: The \"--functions\" support just Object-C and Java now"
print "\t--detail - Show the detail of process\n"
print "\t--output=[intput] - Customize the output file, default is \"out.txt\""
################ End Util Funcs ################
main()