splitErr.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. k = k.strip("'")
  32. err_dic[k] = {"line":line,"used":0}
  33. return err_dic
  34. def getFiles(module):
  35. rootdir = '.'
  36. route_dir = rootdir+'/'+module+'/'
  37. route_list = os.listdir(route_dir)
  38. route_file_list = []
  39. for route in route_list:
  40. if route.endswith('js'):
  41. route_file_list.append(route_dir+route)
  42. return route_file_list
  43. def addCode(fun_dic,cur_m,m):
  44. fun_dic_val = fun_dic[cur_m]
  45. fun_list = {}
  46. if "code" in fun_dic_val:
  47. fun_list = fun_dic_val["code"]
  48. for sub in m:
  49. code = sub.strip("'\" ")
  50. fun_list[code] = {}
  51. fun_dic_val["code"] = fun_list
  52. def addChild(fun_dic,cur_m,childname):
  53. fun_dic_val = fun_dic[cur_m]
  54. fun_list = {}
  55. if "child" in fun_dic_val:
  56. fun_list = fun_dic_val["child"]
  57. fun_list[childname] = {}
  58. fun_dic_val["child"] = fun_list
  59. def findFun(line,fun_dic,cur_m,filepath,all_child={}):
  60. if line.startswith("//"):
  61. return
  62. if len(cur_m) == 0:
  63. return
  64. m = []
  65. if "throwErrCode" in line:
  66. m = re.findall(r'.*throwErrCode\([^\d]*(-?[1-9]\d*)[^\d]*\).*', line)
  67. m2 = re.findall(r'.*\.err_msg\[[^\d]*(-?[1-9]\d*)[^\d]*\].*', line)
  68. m = list(set(m).union(set(m2)))
  69. m3 = re.findall(r'.*[^\d]+(1[\d]{4})[^\d]+.*', line)
  70. m = list(set(m).union(set(m3)))
  71. if len(m) > 0:
  72. addCode(fun_dic,cur_m,m)
  73. if len(all_child) > 0:
  74. for funname in all_child:
  75. name_list = funname.split(".")
  76. justname = name_list[1]
  77. if funname+"(" in line:
  78. addChild(fun_dic,cur_m,funname)
  79. elif len(justname) > 5 and justname+"(" in line:
  80. addChild(fun_dic,cur_m,funname)
  81. def getAllHttpFun(filepath,all_child={}):
  82. fun_dic = {}
  83. line_list = open(filepath).readlines()
  84. last_m = ''
  85. cur_m = ''
  86. note = ''
  87. last_line = ''
  88. for line in line_list:
  89. line = line.strip("\r\n\t ")
  90. if last_line.startswith("/*") > 0:
  91. note = line.strip(" \s*")
  92. m = re.findall(r'.*\.(.+)\s*=\s*function\*\s*\(.*', line)
  93. if len(m) > 0:
  94. last_m = cur_m
  95. cur_m = m[0].strip("'\" ")
  96. cur_m = filepath.split('/').pop().split(".")[0]+"."+cur_m
  97. fun_dic[cur_m] = {}
  98. if ':apiVer' in line:
  99. m = re.findall(r'.*regRequest\s*\((.+),\s*function\s*\*\s*\(\s*ctx\s*\).*', line)
  100. if len(m) > 0:
  101. last_m = cur_m
  102. cur_m = m[0].strip("'\" ")
  103. cur_m = cur_m.replace(':apiVer','v1')
  104. cur_m = filepath.split('/').pop().split(".")[0]+".js "+note+" "+cur_m
  105. fun_dic[cur_m] = {}
  106. findFun(line,fun_dic,cur_m,filepath,all_child)
  107. last_line = line
  108. return fun_dic
  109. def getHttpFunDic(file_dic,all_child={}):
  110. http_fun_dic = {}
  111. for k,v in file_dic.items():
  112. http_fun_dic.update(getAllHttpFun(k,all_child))
  113. return http_fun_dic
  114. def getAllIMFun(filepath,all_child={}):
  115. fun_dic = {}
  116. line_list = open(filepath).readlines()
  117. last_m = ''
  118. cur_m = ''
  119. note = ''
  120. last_line = ''
  121. for line in line_list:
  122. line = line.strip("\r\n\t ")
  123. if last_line.startswith("/**") > 0:
  124. note = line.strip(" \s*")
  125. m = re.findall(r'.*\.(.+)\s*=\s*function\*\s*\(.*', line)
  126. if len(m) > 0:
  127. last_m = cur_m
  128. cur_m = m[0].strip("'\" ")
  129. cur_m = filepath.split('/').pop().split(".")[0]+"."+cur_m
  130. fun_dic[cur_m] = {}
  131. m = re.findall(r'.*\.route\s*\((.+),\s*function\s*\*\s*\(\s*next\s*,\s*ctx\s*,\s*msg\s*,\s*cb\s*\).*', line)
  132. if len(m) > 0:
  133. last_m = cur_m
  134. cur_m = m[0].strip("'\" ")
  135. cur_m = filepath.split('/').pop().split(".")[0]+".js "+note+" "+cur_m
  136. fun_dic[cur_m] = {}
  137. findFun(line,fun_dic,cur_m,filepath,all_child)
  138. last_line = line
  139. return fun_dic
  140. def getIMFunDic(file_dic,all_child={}):
  141. im_fun_dic = {}
  142. for k,v in file_dic.items():
  143. im_fun_dic.update(getAllIMFun(k,all_child))
  144. return im_fun_dic
  145. def getAllMgrFun(filepath,all_child={}):
  146. fun_dic = {}
  147. line_list = open(filepath).readlines()
  148. last_m = ''
  149. cur_m = ''
  150. for line in line_list:
  151. line = line.strip("\r\n\t ")
  152. m = re.findall(r'.*\.(.+)\s*=\s*function\*\s*\(.*', line)
  153. if len(m) > 0:
  154. last_m = cur_m
  155. cur_m = m[0].strip("'\" ")
  156. cur_m = filepath.split('/').pop().split(".")[0]+"."+cur_m
  157. fun_dic[cur_m] = {}
  158. findFun(line,fun_dic,cur_m,filepath,all_child)
  159. return fun_dic
  160. def getMgrFunDic(file_dic,all_child={}):
  161. mgr_fun_dic = {}
  162. for k,v in file_dic.items():
  163. mgr_fun_dic.update(getAllMgrFun(k,all_child))
  164. return mgr_fun_dic
  165. def findAllCodes(nodek,nodev,all_node,has_contain_node):
  166. child_codes = {}
  167. if nodek in has_contain_node:
  168. return child_codes
  169. if "code" in nodev:
  170. child_codes = nodev["code"]
  171. if not "child" in nodev:
  172. return child_codes
  173. child_dic = nodev["child"]
  174. has_contain_node[nodek] = {}
  175. for k,v in child_dic.items():
  176. child_codes.update(findAllCodes(k,all_node[k],all_node,has_contain_node))
  177. return child_codes
  178. def findRouteChild(route_fun_dic,mgr_fun_dic):
  179. for k,v in route_fun_dic.items():
  180. has_contain_node = {}
  181. route_fun_dic[k]["code"] = findAllCodes(k,v,mgr_fun_dic,has_contain_node)
  182. def writeErr(fun_dic,err_dic,fpa):
  183. fun_dic=sorted(fun_dic.iteritems(), key=lambda d:d[0])
  184. for k,v in fun_dic:
  185. if not "code" in v:
  186. continue
  187. code_dic = v["code"]
  188. if len(code_dic) == 0:
  189. continue
  190. code_dic=sorted(code_dic.iteritems(), key=lambda d:d[0])
  191. print >> fpa,"//",k
  192. for k,v in code_dic:
  193. try:
  194. err_dic[k]["used"] = 1
  195. print >> fpa, err_dic[k]["line"]
  196. except Exception, e:
  197. print "eeeeeeeeeeeeeeee",str(e),getTraceStackMsg(),k,v
  198. print >> fpa,""
  199. def writeCommonErr(err_dic,fpa):
  200. print >> fpa, "// 公用err"
  201. print >> fpa, '''"10003":"System error.",'''
  202. err_list=sorted(err_dic.iteritems(), key=lambda d:d[0])
  203. for k,v in err_list:
  204. if v["used"] == 0:
  205. print >> fpa, err_dic[k]["line"]
  206. route_file_list = getFiles('routes')
  207. http_file_dic = {}
  208. im_file_dic = {}
  209. for routefile in route_file_list:
  210. file_name = routefile.split('/').pop()
  211. if file_name.startswith('http'):
  212. http_file_dic[routefile] = {}
  213. elif file_name.startswith('im'):
  214. im_file_dic[routefile] = {}
  215. #get function list
  216. http_fun_dic = getHttpFunDic(http_file_dic)
  217. #print json.dumps(http_fun_dic, indent=1);
  218. im_fun_dic = getIMFunDic(im_file_dic)
  219. #print json.dumps(im_fun_dic, indent=1);
  220. mgr_file_list = getFiles('manager')
  221. model_file_list = getFiles('model')
  222. mgr_file_list = list(set(mgr_file_list).union(set(model_file_list)))
  223. mgr_file_dic = {}
  224. for mgr_file in mgr_file_list:
  225. mgr_file_dic[mgr_file] = {}
  226. mgr_fun_dic = getMgrFunDic(mgr_file_dic)
  227. #print json.dumps(mgr_fun_dic, indent=1)
  228. http_fun_dic_withchild = getHttpFunDic(http_file_dic,mgr_fun_dic)
  229. print json.dumps(http_fun_dic_withchild, indent=1);
  230. im_fun_dic_withchild = getIMFunDic(im_file_dic,mgr_fun_dic)
  231. print json.dumps(im_fun_dic_withchild, indent=1);
  232. mgr_fun_dic_withchild = getMgrFunDic(mgr_file_dic,mgr_fun_dic)
  233. print json.dumps(mgr_fun_dic_withchild, indent=1)
  234. findRouteChild(http_fun_dic_withchild,mgr_fun_dic_withchild)
  235. #print json.dumps(http_fun_dic_withchild, indent=1);
  236. findRouteChild(im_fun_dic_withchild,mgr_fun_dic_withchild)
  237. #print json.dumps(im_fun_dic_withchild, indent=1)
  238. err_dic = getErrMsg()
  239. fpa=open("err_msg.txt","w")
  240. writeErr(http_fun_dic_withchild,err_dic,fpa)
  241. writeErr(im_fun_dic_withchild,err_dic,fpa)
  242. writeCommonErr(err_dic,fpa)
  243. fpa.close()