finddtran.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #!/usr/bin/python
  2. #-*- coding:utf-8 -*-
  3. import os
  4. import sys
  5. import re
  6. import json
  7. import traceback
  8. #reload(sys)
  9. #sys.setdefaultencoding('utf-8')
  10. def getTraceStackMsg():
  11. tb = sys.exc_info()[2]
  12. msg = ''
  13. for i in traceback.format_tb(tb):
  14. msg += i
  15. return msg
  16. def getErrMsg():
  17. rootdir = '.'
  18. config_path = rootdir+'/config/index.js'
  19. content = open(config_path).read()
  20. start = content.find("err_msg:")
  21. end = content.find("}",start)
  22. err_content = content[start+9:end]
  23. errstr_list = err_content.split("\n")
  24. err_dic = {}
  25. for line in errstr_list:
  26. line = line.strip("\r\s ")
  27. if len(line) == 0 or not ":" in line or line.startswith("//"):
  28. continue
  29. line_list = line.split(":")
  30. k = line_list[0].strip('''"''')
  31. err_dic[k] = {"line":line,"used":0}
  32. return err_dic
  33. def getFiles(module):
  34. rootdir = '.'
  35. route_dir = rootdir+'/'+module+'/'
  36. route_list = os.listdir(route_dir)
  37. route_file_list = []
  38. for route in route_list:
  39. if route.endswith('js'):
  40. route_file_list.append(route_dir+route)
  41. return route_file_list
  42. def addCode(fun_dic,cur_m,m):
  43. fun_dic_val = fun_dic[cur_m]
  44. fun_list = {}
  45. if "code" in fun_dic_val:
  46. fun_list = fun_dic_val["code"]
  47. for sub in m:
  48. code = sub.strip("'\" ")
  49. fun_list[code] = {}
  50. fun_dic_val["code"] = fun_list
  51. def addChild(fun_dic,cur_m,childname):
  52. fun_dic_val = fun_dic[cur_m]
  53. fun_list = {}
  54. if "child" in fun_dic_val:
  55. fun_list = fun_dic_val["child"]
  56. if "hastran" in fun_dic_val and fun_dic_val["hastran"] > 0:
  57. fun_list[childname] = {"aftertran":1}
  58. else:
  59. fun_list[childname] = {}
  60. fun_dic_val["child"] = fun_list
  61. def findFun(line,fun_dic,cur_m,filepath,all_child={}):
  62. if line.startswith("//"):
  63. return
  64. if len(cur_m) == 0:
  65. return
  66. m = []
  67. if "throwErrCode" in line:
  68. m = re.findall(r'.*throwErrCode\([^\d]*(-?[1-9]\d*)[^\d]*\).*', line)
  69. m2 = re.findall(r'.*\.err_msg\[[^\d]*(-?[1-9]\d*)[^\d]*\].*', line)
  70. m = list(set(m).union(set(m2)))
  71. m3 = re.findall(r'.*[^\d]+(1[\d]{4})[^\d]+.*', line)
  72. m = list(set(m).union(set(m3)))
  73. if len(m) > 0:
  74. addCode(fun_dic,cur_m,m)
  75. if len(all_child) > 0:
  76. for funname in all_child:
  77. name_list = funname.split(".")
  78. justname = name_list[1]
  79. if funname+"(" in line:
  80. addChild(fun_dic,cur_m,funname)
  81. elif len(justname) > 5 and justname+"(" in line:
  82. addChild(fun_dic,cur_m,funname)
  83. #记录是否包含事务
  84. m4 = re.findall(r'.*db_pool.startTransaction.*', line)
  85. if len(m4) > 0:
  86. fun_dic_val = fun_dic[cur_m]
  87. last_tran_count = 0
  88. if "hastran" in fun_dic_val:
  89. last_tran_count = fun_dic_val["hastran"]
  90. last_tran_count += 1
  91. fun_dic_val["hastran"] = last_tran_count
  92. if "corun" in fun_dic_val:
  93. del fun_dic_val["corun"]
  94. m5 = re.findall(r'.*corun tran.*', line)
  95. if len(m5) > 0:
  96. fun_dic_val = fun_dic[cur_m]
  97. fun_dic_val["corun"] = 1
  98. m6 = re.findall(r'.*db_pool.commit.*', line)
  99. m7 = re.findall(r'.*db_pool.rollback.*', line)
  100. if len(m6) > 0 or len(m7) > 0:
  101. fun_dic_val = fun_dic[cur_m]
  102. last_tran_count = fun_dic_val["hastran"]
  103. last_tran_count = last_tran_count - 1
  104. fun_dic_val["hastran"] = last_tran_count
  105. def getAllHttpFun(filepath,all_child={}):
  106. fun_dic = {}
  107. line_list = open(filepath).readlines()
  108. last_m = ''
  109. cur_m = ''
  110. note = ''
  111. last_line = ''
  112. for line in line_list:
  113. line = line.strip("\r\n\t ")
  114. if last_line.startswith("/*") > 0:
  115. note = line.strip(" \s*")
  116. m = re.findall(r'.*\.(.+)\s*=\s*function\*\s*\(.*', line)
  117. if len(m) > 0:
  118. last_m = cur_m
  119. cur_m = m[0].strip("'\" ")
  120. cur_m = filepath.split('/').pop().split(".")[0]+"."+cur_m
  121. fun_dic[cur_m] = {"name":cur_m}
  122. if ':apiVer' in line:
  123. m = re.findall(r'.*regRequest\s*\((.+),\s*function\s*\*\s*\(\s*ctx\s*\).*', line)
  124. if len(m) > 0:
  125. last_m = cur_m
  126. cur_m = m[0].strip("'\" ")
  127. cur_m = cur_m.replace(':apiVer','v1')
  128. cur_m = filepath.split('/').pop().split(".")[0]+".js "+note+" "+cur_m
  129. fun_dic[cur_m] = {"name":cur_m}
  130. findFun(line,fun_dic,cur_m,filepath,all_child)
  131. last_line = line
  132. return fun_dic
  133. def getHttpFunDic(file_dic,all_child={}):
  134. http_fun_dic = {}
  135. for k,v in file_dic.items():
  136. http_fun_dic.update(getAllHttpFun(k,all_child))
  137. return http_fun_dic
  138. def getAllIMFun(filepath,all_child={}):
  139. fun_dic = {}
  140. line_list = open(filepath).readlines()
  141. last_m = ''
  142. cur_m = ''
  143. note = ''
  144. last_line = ''
  145. for line in line_list:
  146. line = line.strip("\r\n\t ")
  147. if last_line.startswith("/**") > 0:
  148. note = line.strip(" \s*")
  149. m = re.findall(r'.*\.(.+)\s*=\s*function\*\s*\(.*', line)
  150. if len(m) > 0:
  151. last_m = cur_m
  152. cur_m = m[0].strip("'\" ")
  153. cur_m = filepath.split('/').pop().split(".")[0]+"."+cur_m
  154. fun_dic[cur_m] = {"name":cur_m}
  155. m = re.findall(r'.*\.route\s*\((.+),\s*function\s*\*\s*\(\s*next\s*,\s*ctx\s*,\s*msg\s*,\s*cb\s*\).*', line)
  156. if len(m) > 0:
  157. last_m = cur_m
  158. cur_m = m[0].strip("'\" ")
  159. cur_m = filepath.split('/').pop().split(".")[0]+".js "+note+" "+cur_m
  160. fun_dic[cur_m] = {"name":cur_m}
  161. findFun(line,fun_dic,cur_m,filepath,all_child)
  162. last_line = line
  163. return fun_dic
  164. def getIMFunDic(file_dic,all_child={}):
  165. im_fun_dic = {}
  166. for k,v in file_dic.items():
  167. im_fun_dic.update(getAllIMFun(k,all_child))
  168. return im_fun_dic
  169. def getAllMgrFun(filepath,all_child={}):
  170. fun_dic = {}
  171. line_list = open(filepath).readlines()
  172. last_m = ''
  173. cur_m = ''
  174. for line in line_list:
  175. line = line.strip("\r\n\t ")
  176. m = re.findall(r'.*\.(.+)\s*=\s*function\*\s*\(.*', line)
  177. if len(m) > 0:
  178. last_m = cur_m
  179. cur_m = m[0].strip("'\" ")
  180. cur_m = filepath.split('/').pop().split(".")[0]+"."+cur_m
  181. fun_dic[cur_m] = {"name":cur_m}
  182. findFun(line,fun_dic,cur_m,filepath,all_child)
  183. return fun_dic
  184. def getMgrFunDic(file_dic,all_child={}):
  185. mgr_fun_dic = {}
  186. for k,v in file_dic.items():
  187. mgr_fun_dic.update(getAllMgrFun(k,all_child))
  188. return mgr_fun_dic
  189. def findAllCodes(nodek,nodev,all_node,has_contain_node):
  190. child_codes = {}
  191. if nodek in has_contain_node:
  192. return child_codes
  193. if "code" in nodev:
  194. child_codes = nodev["code"]
  195. if not "child" in nodev:
  196. return child_codes
  197. child_dic = nodev["child"]
  198. has_contain_node[nodek] = {}
  199. for k,v in child_dic.items():
  200. child_codes.update(findAllCodes(k,all_node[k],all_node,has_contain_node))
  201. #del has_contain_node[nodek]
  202. return child_codes
  203. def findRouteChild(route_fun_dic,mgr_fun_dic):
  204. for k,v in route_fun_dic.items():
  205. has_contain_node = {}
  206. route_fun_dic[k]["code"] = findAllCodes(k,v,mgr_fun_dic,has_contain_node)
  207. def writeErr(fun_dic,err_dic,fpa):
  208. fun_dic=sorted(fun_dic.iteritems(), key=lambda d:d[0])
  209. for k,v in fun_dic:
  210. if not "code" in v:
  211. continue
  212. code_dic = v["code"]
  213. if len(code_dic) == 0:
  214. continue
  215. code_dic=sorted(code_dic.iteritems(), key=lambda d:d[0])
  216. print >> fpa,"//",k
  217. for k,v in code_dic:
  218. try:
  219. err_dic[k]["used"] = 1
  220. print >> fpa, err_dic[k]["line"]
  221. except Exception, e:
  222. print "eeeeeeeeeeeeeeee",str(e),getTraceStackMsg()
  223. print >> fpa,""
  224. def writeCommonErr(err_dic,fpa):
  225. print >> fpa, "// 公用err"
  226. err_list=sorted(err_dic.iteritems(), key=lambda d:d[0])
  227. for k,v in err_list:
  228. if v["used"] == 0:
  229. print >> fpa, err_dic[k]["line"]
  230. route_file_list = getFiles('routes')
  231. http_file_dic = {}
  232. im_file_dic = {}
  233. for routefile in route_file_list:
  234. file_name = routefile.split('/').pop()
  235. if file_name.startswith('http'):
  236. http_file_dic[routefile] = {}
  237. elif file_name.startswith('im'):
  238. im_file_dic[routefile] = {}
  239. #get function list
  240. http_fun_dic = getHttpFunDic(http_file_dic)
  241. #print json.dumps(http_fun_dic, indent=1);
  242. im_fun_dic = getIMFunDic(im_file_dic)
  243. #print json.dumps(im_fun_dic, indent=1);
  244. mgr_file_list = getFiles('manager')
  245. model_file_list = getFiles('model')
  246. mgr_file_list = list(set(mgr_file_list).union(set(model_file_list)))
  247. mgr_file_dic = {}
  248. for mgr_file in mgr_file_list:
  249. mgr_file_dic[mgr_file] = {}
  250. mgr_fun_dic = getMgrFunDic(mgr_file_dic)
  251. print json.dumps(mgr_fun_dic, indent=1)
  252. http_fun_dic_withchild = getHttpFunDic(http_file_dic,mgr_fun_dic)
  253. print json.dumps(http_fun_dic_withchild, indent=1);
  254. im_fun_dic_withchild = getIMFunDic(im_file_dic,mgr_fun_dic)
  255. print json.dumps(im_fun_dic_withchild, indent=1);
  256. mgr_fun_dic_withchild = getMgrFunDic(mgr_file_dic,mgr_fun_dic)
  257. print json.dumps(mgr_fun_dic_withchild, indent=1)
  258. findRouteChild(http_fun_dic_withchild,mgr_fun_dic_withchild)
  259. #print json.dumps(http_fun_dic_withchild, indent=1);
  260. findRouteChild(im_fun_dic_withchild,mgr_fun_dic_withchild)
  261. #print json.dumps(im_fun_dic_withchild, indent=1)
  262. '''
  263. err_dic = getErrMsg()
  264. fpa=open("err_msg.txt","w")
  265. writeErr(http_fun_dic_withchild,err_dic,fpa)
  266. writeErr(im_fun_dic_withchild,err_dic,fpa)
  267. writeCommonErr(err_dic,fpa)
  268. fpa.close()
  269. '''
  270. def findTranList(nodek,nodev,all_node,tran_count,has_contain_node):
  271. dtran_route_list = [] #contain this
  272. if "corun" in nodev:
  273. tran_count = 0
  274. if "hastran" in nodev:
  275. tran_count = tran_count + 1
  276. if tran_count >= 2:
  277. return [nodev["name"]+" count>2"]
  278. if nodek in has_contain_node:
  279. last_count = has_contain_node[nodek]["trancount"]
  280. if tran_count - last_count > 0:
  281. return [nodev["name"]+" recur"]
  282. else:
  283. return []
  284. if not "child" in nodev:
  285. return []
  286. child_dic = nodev["child"]
  287. has_contain_node[nodek] = {"trancount":tran_count}
  288. for k,v in child_dic.items():
  289. real_count = tran_count
  290. if "hastran" in nodev and "aftertran" not in v and real_count >= 1:
  291. real_count = real_count - 1
  292. sub_route_list = findTranList(k,all_node[k],all_node,real_count,has_contain_node)
  293. for sub_route in sub_route_list:
  294. dtran_route_list.append(nodev["name"]+"; "+sub_route)
  295. del has_contain_node[nodek]
  296. return dtran_route_list
  297. def findDubbleTran(route_fun_dic,mgr_fun_dic,fp):
  298. for k,v in route_fun_dic.items():
  299. tran_count = 0
  300. has_contain_node = {}
  301. dtran_route_list = findTranList(k,v,mgr_fun_dic,tran_count,has_contain_node)
  302. for dtran_route in dtran_route_list:
  303. print >> fp, dtran_route
  304. fpa=open("dubbletran.txt","w")
  305. findDubbleTran(http_fun_dic_withchild,mgr_fun_dic_withchild,fpa)
  306. findDubbleTran(im_fun_dic_withchild,mgr_fun_dic_withchild,fpa)
  307. fpa.close()
  308. print "find dubble tran suc"