Guest User

Untitled

a guest
Jul 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. import os
  2. import sys
  3. import difflib
  4.  
  5. def is_func_def(line=''):
  6. return 'func ' in line
  7.  
  8.  
  9. def func_param(line):
  10. L = line.find('(')
  11. R = line.find(')')
  12. param = line[L+1:R]
  13. plist = param.split(',')
  14. typelist = []
  15. for p in plist:
  16. pl = p.strip().split(' ')
  17. if len(pl) > 1:
  18. typelist.append(pl[1])
  19. return '('+ ', '.join(typelist) + ')'
  20.  
  21. def func_name(line=''):
  22. count = line.count('(')
  23. if count == 1:
  24. line = line.split('(')[0]
  25. return line.split(' ')[-1]
  26. if count == 3:
  27. line = line.split('(')[1]
  28. return line.split(' ')[-1]
  29. if count == 2:
  30. aline = line.split('(')[0]
  31. name = aline.split(' ')[-1]
  32. if name: return name
  33. bline = line.split('(')[1]
  34. return bline.split(' ')[-1]
  35.  
  36.  
  37. def extract_func(fname):
  38. fobj = open(fname, 'r')
  39. lines = fobj.readlines()
  40. lines = list(filter(lambda x: x.strip(), lines))
  41. funclist = []
  42. n = 0
  43. while n < len(lines):
  44. if not is_func_def(lines[n]): n += 1; continue
  45. stack = []
  46. begin = n
  47. while n < len(lines):
  48. for token in lines[n]:
  49. if '{' == token:
  50. stack.append(n)
  51. if '}' == token:
  52. stack.pop()
  53. n += 1
  54. if not stack: break
  55. func = func_name(lines[begin]) + func_param(lines[begin])
  56. context = lines[begin:n]
  57. # print(context)
  58. if not func:
  59. print(func, context)
  60. # 类型
  61. funclist.append((func, context, fname))
  62. n += 1
  63.  
  64. return funclist
  65.  
  66.  
  67.  
  68. def scan_dir(dir):
  69. funclists = []
  70. ignore_files = ['thrift_gen', 'test', 'clients']
  71. for root, dirs, files in os.walk(dir, True):
  72. for name in files:
  73. fname = os.path.join(root, name)
  74. if '.go' not in fname: continue
  75. for name in ignore_files:
  76. if name in fname: continue
  77. funclist = extract_func(fname)
  78. if funclist:
  79. funclists.extend(funclist)
  80. return funclists
  81.  
  82.  
  83. def count_caller(func='', block=[''], funcsets={}):
  84. for line in block:
  85. if '(' in line:
  86. name = func_name(line)
  87. if name in funcsets:
  88. funcsets[name] += 1
  89.  
  90.  
  91. def FuncDiff():
  92. dir1 = sys.argv[1]
  93. funclist = scan_dir(dir1)
  94. dir2 = sys.argv[2]
  95. funclist2 = scan_dir(dir2)
  96. funclist.extend(funclist2)
  97. funcsets = {}
  98. for item in funclist:
  99. (func, context, fname) = item
  100. if func not in funcsets:
  101. funcsets[func] = [(context, fname)]
  102. else:
  103. funcsets[func].append((context, fname))
  104. for k in funcsets:
  105. if k == 'init()': continue
  106. nlen = len(funcsets[k])
  107. if nlen < 2: continue
  108. for idx in range(nlen-1):
  109. (api1, fname1) = funcsets[k][idx]
  110. for idx2 in range(idx+1, nlen):
  111. (api2, fname2) = funcsets[k][idx2]
  112. print('//### ', fname1)
  113. print('//### ', fname2)
  114. if api1 == api2:
  115. print('//### same:', k)
  116. print('\n')
  117. continue
  118. print('//### diff:')
  119. for line in difflib.unified_diff(api1, api2, fromfile=fname1, tofile=fname2):
  120. print(line.strip())
  121. print('\n')
  122.  
  123.  
  124. def Counter():
  125. dir = sys.argv[1]
  126. funclists = scan_dir(dir)
  127. funcsets = {}
  128. for item in funclists:
  129. funcsets[item[0]] = 0
  130. for item in funclists:
  131. count_caller(item[0], item[1], funcsets)
  132. for k in funcsets.keys():
  133. print(k, "\tcalled:", funcsets[k])
  134.  
  135.  
  136. FuncDiff()
  137. # Counter()
Add Comment
Please, Sign In to add comment