-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
484 lines (429 loc) · 14.6 KB
/
util.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import csv
import networkx as nx
from networkx import Graph
from bs4 import BeautifulSoup
from time import sleep
import requests
import re
from networkx.algorithms.traversal.depth_first_search import dfs_tree
import os
from zss import simple_distance, Node
# import urllib
# from urllib.parse import urlparse
# from urllib.parse import parse_qs
# from urllib import request
# from urllib.request import urlretrieve
def filedircount(path):
if not os.path.exists(path):
print(path,"不存在")
return 0
else:
cnt=0
for rnames,dnames,fnames in os.walk(path):
for fname in fnames:
if "$" not in fname and fname.endswith(".smali"):
cnt+=1
for dname in dnames:
cnt+=1
return cnt
def filecount(path):
if not os.path.exists(path):
print(path,"不存在")
return 0
else:
cnt=0
for rnames,dnames,fnames in os.walk(path):
for fname in fnames:
if "$" not in fname and fname.endswith(".smali"):
cnt+=1
return cnt
def buildzsstree(code:str):
if not code :return
if code=="10":
a=Node("l_1")
t=(a)
return t
if len(code)%2!=0:return "error"
node_cnt=1
codes=getcode_of_child(code,"none","")
code=code[1:-1]
a=Node("r_"+str(node_cnt))
for i in codes:
node_cnt+=1
if i=="10":
a.addkid(Node("l_"+str(node_cnt)))
else:
a.addkid(buildzsstree(i))
return a
# def buildzsstree(code:str):
# if not code :return
# if code=="10":
# a=Node("l_1")
# t=(a)
# return t
# if len(code)%2!=0:return "error"
# node_cnt=1
# codes=getcode_of_child(code)
# code=code[1:-1]
# a=Node("r_"+str(node_cnt))
# node_cnt+=1
# for i in codes:
# if i=="10":
# node_cnt+=1
# a.addkid(Node("l_"+str(node_cnt)))
# else:
# a.addkid(buildzsstree(i))
# return a
"""
#flag 是标志位,分为file和tree,none三类
none表示不需要显示名字
"""
def getcode_of_child(code:str,flag:str,extra):
#来自文件的编码 extra为根目录的绝对路径
if flag=="file":
ans={}
if os.path.isfile(extra):
return ans
for dir in os.listdir(extra):
abpath=extra+"\\"+dir
ans[abpath]=getcode_for_file(abpath)
return ans
#来自树的编码 extra为树的Digraph对象以及根节点的列表
if flag=="tree":
TREE=extra[0]
root=extra[1]
ans={}
if not sum(1 for i in TREE.successors(root)):
return ans
for child in TREE.successors(root):
ans[child]=getcode_for_tree(TREE,child)
return ans
#不需要显示名字
if flag=="none":
# example code="111010011010010100"
# code="111010011010010100"
codes=[]
if not code or code=="10":return codes
if len(code)%2!=0:return ["error"]
code=code[1:-1]
stack=[]
tmp=""
for i in code:
if i=='1':
stack.append(i)
tmp+=i
else:
tmp+=i
stack.pop(-1)
if not stack:
codes.append(tmp)
tmp=""
return codes
def getcode_for_file(root :str):#考虑文件夹为空的情况?
#root is a path
if os.path.isfile(root) :
if '$' not in root:
return "10"
else:
return ""
tmp=[]
# flag_dir_exist=0
# if "libfromfolder" in root:
# print("root= ",root)
# input("pause")
for node in os.listdir(root):
if node.endswith("META-INF"):
continue
node=root+"\\"+node
# if os.path.isfile(node):
# tmp.append("10")
# if os.path.isdir(node):
tmp.append(getcode_for_file(node))
tmp=sorted(tmp)
ans="1"
for i in tmp:
ans+=i
ans+="0"
return ans
# def getgetcode_for_tree_withname(G: Graph,root: str)
# if not sum(1 for i in G.successors(root)):
# ans
# return "10"
def getcode_for_tree(G: Graph,root: str):
if root not in G.nodes():
return ""
if not sum(1 for i in G.successors(root)):
return "10"
tmp=[]
ans="1"
for child in G.successors(root):
tmp.append(getcode_for_tree(G,child))
tmp=sorted(tmp)
for i in tmp:
ans+=i
ans+="0"
return ans
def getPre2(community):
if community:
tmp=[]
for package in community:
tmp=package.split('.')
if len(tmp)>2:
return tmp[0]+'.'+tmp[1]
#如果按点划分之后长度为2
return community[0].split('.')[0]
def getpre2_node(node):
if node:
pre2=''
cnt=1
for i in node.split('.'):
if cnt<=2:
pre2+=i
cnt+=1
return pre2
def getpredixdic(G):
predixdic={}#dictionary for community and according predix
for c in nx.connected_components(G):
c=list(c)
#用社区里的第一个节点作为key
predixdic[c[0]]= getPredix(c)
return predixdic
def storeGraphAsCsv(G,filename):
with open(filename,'w') as f:
csvwriter=csv.writer(f)
csvwriter.writerow(["source","direction","weight"])
for edge in G.edges:
pkgname=edge[0]
cla=edge[1]
weight=G[pkgname][cla]['weight']
csvwriter.writerow([pkgname,cla,weight])
f.close()
print('图已保存为',filename)
def getDepth(community):
depth=0
for c in community:
if c.count('.')>depth:
depth=c.count('.')
return depth
def getPredix(community):
predix=community[0]
for c in community:
predix=getSharedSubstring(predix,c)
return predix
def getSharedSubstring(s1,s2):
#just for two package name,not a common string
a1=s1.split('.');a2=s2.split('.')
i=0
s=''
while i<len(a1) and i<len(a2):
if a1[i]==a2[i]:
if s:
s+='.'+a1[i]
else:
s+=a1[i]
else:
break
i+=1
return s
#以下是对url进行解析
def getContent(url,headers):
#def getContent(url,headers):
try:
res = requests.get(url,headers)
res.encoding = 'utf-8'
return res.text
except Exception:
sleep(0.1)
return ""
def getLabel(url,headers,label):
soup=BeautifulSoup(getContent(url,headers), 'html.parser')
return soup.find_all(label)
def getPackagename(manifest):
pattern_url="https?://.*[Pp][Rr][Ii][Vv][Aa][Cc][Yy].*"
packagename=''
try:
with open(manifest,'r',encoding='utf-8') as f:
#print(f.readlines())
#soup=BeautifulSoup(f.readlines())
for line in f.readlines():
pattern_package="package=\".+\""
ret=re.search(pattern_package,line)
if not ret:
pass
else:
packagename=ret.group(0).replace("\"",'').replace("package=",'')
break
return packagename
except:
print("AndroidManifest.xml解析失败")
# print(getPackagename("AndroidManifest.xml"))
# pattern_package="package=\"[^\"]+\""
# line=package="package=\"com.bdego.android\" platformBuildVersionCode=\"23\" "
# print(re.search(pattern_package,line))
'''
community=['cn.hugeterry.updatefun.UpdateFunGO',
'cn.hugeterry.updatefun.UpdateFunGO',
'cn.hugeterry.updatefun.UpdateFunGO',
'cn.hugeterry.updatefun.UpdateFunGO',
'cn.hugeterry.updatefun.UpdateFunGO',
]
print(getSharedSubstring(community[0],community[1]))
print(getPredix(community))
'''
# import matplotlib.cm as cm
# import matplotlib.pyplot as plt
# import networkx as nx
# import csv
# import time
# import numpy as np
# from collections import defaultdict
# def buildG(G, file_, delimiter_):
# #construct the weighted version of the contact graph from cgraph.dat file
# #reader = csv.reader(open("/home/kazem/Data/UCI/karate.txt"), delimiter=" ")
# reader = csv.reader(open(file_), delimiter=delimiter_)
# for line in reader:
# if line:
# if len(line) > 2:
# if float(line[2]) != 0.0:
# #line format: u,v,w
# # G.add_edge(line[0],line[1],weight=calBonus(line[0],line[1])*float(line[2]))
# if not line[0].startswith('com.yingyb.lljs') and not line[1].startswith('com.yingyb.lljs'):
# # G.add_edge(line[0],line[1],weight=float(line[2]))
# G.add_edge(line[0],line[1],weight=0.01*float(line[2]))
# else:
# #line format: u,v
# G.add_edge(line[0],line[1],weight=1.0)
# def find_communities1(G, T, r):
# #将图中数据录入到数据字典中以便使用
# weight = {j:{} for j in G.nodes()}
# for q in weight.keys():
# for m in G[q].keys():
# weight[q][m] = G[q][m]['weight']
# #建立成员标签记录
# memory = {i:{i:1} for i in G.nodes()}
# #开始遍历T次所有节点
# for t in range(T):
# listenerslist = list(G.nodes())
# #随机排列遍历顺序
# np.random.shuffle(listenerslist)
# #开始遍历节点
# for listener in listenerslist:
# #每个节点的key就是与他相连的节点标签名
# speakerlist = G[listener].keys()
# if len(speakerlist) == 0:
# continue
# labels = defaultdict(int)
# #遍历所有与其相关联的节点
# for j, speaker in enumerate(speakerlist):
# total = float(sum(memory[speaker].values()))
# #查看speaker中memory中出现概率最大的标签并记录,key是标签名,value是Listener与speaker之间的权
# #print(memory[speaker])
# labels[list(memory[speaker].keys())[np.random.multinomial(1,[freq / total for freq in memory[speaker].values()]).argmax()]] += weight[listener][speaker]
# #查看labels中值最大的标签,让其成为当前listener的一个记录
# maxlabel = max(labels, key=labels.get)
# if maxlabel in memory[listener]:
# memory[listener][maxlabel] += 1
# else:
# memory[listener][maxlabel] = 1.5
# #提取出每个节点memory中记录标签出现最多的一个
# for primary in memory:
# # print([freq/total for freq in memory[primary].values()])
# # print(np.random.multinomial(1,[freq/total for freq in memory[primary].values()]))
# # print(np.random.multinomial(1,[freq/total for freq in memory[primary].values()]).argmax() )
# # input()
# next_probs=[freq/total for freq in memory[primary].values()]
# for i in range(0,len(next_probs)):
# next_probs[i] /= sum(next_probs)
# p = list(memory[primary].keys())[np.random.multinomial(1,next_probs).argmax()]
# memory[primary]={p:memory[primary][p]}
# #如果希望将那种所属社区不明显的节点排除的就使用下面这段注释代码
# '''
# for primary, change in memory.items():
# for change_name, change_number in change.items():
# if change_number / float(T + 1) < r:
# del change[change_name]
# '''
# communities = {}
# #扫描memory中的记录标签,相同标签的节点加入同一个社区中
# for primary, change in memory.items():
# for label in change.keys():
# if label in communities:
# communities[label].add(primary)
# else:
# communities[label] = set([primary])
# freecommunities = set()
# keys = communities.keys()
# #排除相互包含的社区(上面那段注释代码不加这段也可以不加)
# '''
# for i, label0 in enumerate(keys[:-1]):
# comm0 = communities[label0]
# for label1 in keys[i+1:]:
# comm1 = communities[label1]
# if comm0.issubset(comm1):
# freecommunities.add(label0)
# elif comm0.issuperset(comm1):
# freecommunities.add(label1)
# for comm in freecommunities:
# del communities[comm]
# '''
# #返回值是个数据字典,value以集合的形式存在
# return communities
# def find_communities2(G,T,r):
# """
# Speaker-Listener Label Propagation Algorithm (SLPA)
# see http://arxiv.org/abs/1109.5720
# """
# ##Stage 1: Initialization
# memory = {i:{i:1} for i in G.nodes()}
# ##Stage 2: Evolution
# for t in range(T):
# listenersOrder = list(G.nodes())
# np.random.shuffle(listenersOrder)
# for listener in listenersOrder:
# speakers = G[listener].keys()
# if len(speakers)==0:
# continue
# labels = defaultdict(int)
# for j, speaker in enumerate(speakers):
# # Speaker Rule
# total = float(sum(memory[speaker].values()))
# labels[list(memory[speaker].keys())[np.random.multinomial(1,[freq/total for freq in memory[speaker].values()]).argmax()]] += 1
# # Listener Rule
# acceptedLabel = max(labels, key=labels.get)
# # Update listener memory
# if acceptedLabel in memory[listener]:
# memory[listener][acceptedLabel] += 1
# else:
# memory[listener][acceptedLabel] = 1
# ## Stage 3:
# for node, mem in memory.items():
# for label, freq in mem.items():
# if freq/float(T+1) < r:
# del mem[label]
# # Find nodes membership
# communities = {}
# for node, mem in memory.items():
# for label in mem.keys():
# if label in communities:
# communities[label].add(node)
# else:
# communities[label] = set([node])
# # Remove nested communities
# nestedCommunities = set()
# keys = communities.keys()
# for i, label0 in enumerate(keys[:-1]):
# comm0 = communities[label0]
# for label1 in keys[i+1:]:
# comm1 = communities[label1]
# if comm0.issubset(comm1):
# nestedCommunities.add(label0)
# elif comm0.issuperset(comm1):
# nestedCommunities.add(label1)
# for comm in nestedCommunities:
# del communities[comm]
# return communities
# s=time.time()
# G = nx.Graph() #let's create the graph first
# buildG(G, 'liuliujians_V1.0.0.txt', ',')
# print(find_communities1(G, 10, 10))
# e=time.time()