Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import difflib
- def is_func_def(line=''):
- return 'func ' in line
- def func_param(line):
- L = line.find('(')
- R = line.find(')')
- param = line[L+1:R]
- plist = param.split(',')
- typelist = []
- for p in plist:
- pl = p.strip().split(' ')
- if len(pl) > 1:
- typelist.append(pl[1])
- return '('+ ', '.join(typelist) + ')'
- def func_name(line=''):
- count = line.count('(')
- if count == 1:
- line = line.split('(')[0]
- return line.split(' ')[-1]
- if count == 3:
- line = line.split('(')[1]
- return line.split(' ')[-1]
- if count == 2:
- aline = line.split('(')[0]
- name = aline.split(' ')[-1]
- if name: return name
- bline = line.split('(')[1]
- return bline.split(' ')[-1]
- def extract_func(fname):
- fobj = open(fname, 'r')
- lines = fobj.readlines()
- lines = list(filter(lambda x: x.strip(), lines))
- funclist = []
- n = 0
- while n < len(lines):
- if not is_func_def(lines[n]): n += 1; continue
- stack = []
- begin = n
- while n < len(lines):
- for token in lines[n]:
- if '{' == token:
- stack.append(n)
- if '}' == token:
- stack.pop()
- n += 1
- if not stack: break
- func = func_name(lines[begin]) + func_param(lines[begin])
- context = lines[begin:n]
- # print(context)
- if not func:
- print(func, context)
- # 类型
- funclist.append((func, context, fname))
- n += 1
- return funclist
- def scan_dir(dir):
- funclists = []
- ignore_files = ['thrift_gen', 'test', 'clients']
- for root, dirs, files in os.walk(dir, True):
- for name in files:
- fname = os.path.join(root, name)
- if '.go' not in fname: continue
- for name in ignore_files:
- if name in fname: continue
- funclist = extract_func(fname)
- if funclist:
- funclists.extend(funclist)
- return funclists
- def count_caller(func='', block=[''], funcsets={}):
- for line in block:
- if '(' in line:
- name = func_name(line)
- if name in funcsets:
- funcsets[name] += 1
- def FuncDiff():
- dir1 = sys.argv[1]
- funclist = scan_dir(dir1)
- dir2 = sys.argv[2]
- funclist2 = scan_dir(dir2)
- funclist.extend(funclist2)
- funcsets = {}
- for item in funclist:
- (func, context, fname) = item
- if func not in funcsets:
- funcsets[func] = [(context, fname)]
- else:
- funcsets[func].append((context, fname))
- for k in funcsets:
- if k == 'init()': continue
- nlen = len(funcsets[k])
- if nlen < 2: continue
- for idx in range(nlen-1):
- (api1, fname1) = funcsets[k][idx]
- for idx2 in range(idx+1, nlen):
- (api2, fname2) = funcsets[k][idx2]
- print('//### ', fname1)
- print('//### ', fname2)
- if api1 == api2:
- print('//### same:', k)
- print('\n')
- continue
- print('//### diff:')
- for line in difflib.unified_diff(api1, api2, fromfile=fname1, tofile=fname2):
- print(line.strip())
- print('\n')
- def Counter():
- dir = sys.argv[1]
- funclists = scan_dir(dir)
- funcsets = {}
- for item in funclists:
- funcsets[item[0]] = 0
- for item in funclists:
- count_caller(item[0], item[1], funcsets)
- for k in funcsets.keys():
- print(k, "\tcalled:", funcsets[k])
- FuncDiff()
- # Counter()
Add Comment
Please, Sign In to add comment