callvalidator.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python
  2. # usage: txtmerge.py <input_dir> <output_file_name>
  3. # does the exact opposite of txtsplit.py
  4. from os import listdir
  5. from os.path import isfile, join
  6. import sys
  7. import re
  8. import io
  9. import time
  10. callList = []
  11. locationCallList = []
  12. def functionEmptyOrNamed(callArray):
  13. if len(callArray) == 1:
  14. return None
  15. else:
  16. return callArray[1].strip(" \t\n").lower()
  17. #endif
  18. #enddef
  19. def processLines(lines):
  20. pattern = "(gt|gs|xgt|xgs)\s*('|\")\w+('|\")(,\s*('|\")\w+('|\"))?"
  21. locationCalls = {"calllocation": lines[0].strip(' #\n').lower(), "callIds": []}
  22. for line in lines:
  23. if not line.strip(" \t").startswith("!"):
  24. match = re.search(pattern, line)
  25. if match != None:
  26. temp = match.group().strip('xgst\t\n').replace('"','').replace("'", "").replace(" ","").split(",")
  27. loc = temp[0].lower()
  28. fun = functionEmptyOrNamed(temp)
  29. call = {"location": loc, "function": fun, "valid": 0}
  30. if call not in callList:
  31. callList.append(call)
  32. locationCalls['callIds'].append(len(callList)-1)
  33. elif callList.index(call) not in locationCalls['callIds']:
  34. locationCalls['callIds'].append(callList.index(call))
  35. #endif
  36. #endif
  37. #endif
  38. #endfor
  39. locationCallList.append(locationCalls)
  40. #enddedf
  41. def validateCalls(lines):
  42. calls = [c for c in callList if c['location'] == lines[0].strip(' #\n').lower()]
  43. for call in calls:
  44. if call['function'] == None or call['function'] == 'start' or call['function'] == '':
  45. call['valid'] = 1
  46. else:
  47. findString = "if$args[0]='%s'" % call['function'].lower()
  48. findString2 = "if$args[i]='%s'" % call['function'].lower()
  49. targets = [line.replace(" ", "").lower().strip(' \n\t') for line in lines if findString in line.replace(" ", "").lower() or findString2 in line.replace(" ", "").lower()]
  50. for t in targets:
  51. if t.startswith("if$args[") or t.startswith("elseif$args["):
  52. call['valid'] = 1
  53. break;
  54. #endif
  55. #endfor
  56. #endif
  57. #endfor
  58. #enddef
  59. assert len(sys.argv) == 2, "usage:\ntest_txtmerge.py <src_input_dir>"
  60. idir = str(sys.argv[1])
  61. print("Start building call list: %s" % time.strftime("%H:%M:%S", time.localtime()))
  62. # grab the location files
  63. filelist = [f for f in listdir(idir) if ".qsrc" in f]
  64. buildlist_start = time.time()
  65. # build a list of all the calls happening
  66. for file in filelist:
  67. with io.open(join(idir, file), 'rt', encoding='utf-8') as ifile:
  68. lines = ifile.readlines()
  69. processLines(lines)
  70. print("Finished building call list: %s" % time.strftime("%H:%M:%S", time.localtime()))
  71. # validating that all the calls are for valid locations
  72. for file in filelist:
  73. with io.open(join(idir, file), 'rt', encoding='utf-8') as ifile:
  74. lines = ifile.readlines()
  75. validateCalls(lines)
  76. #endfor
  77. print("Finished validation %s" % time.strftime("%H:%M:%S", time.localtime()))
  78. # create the call validity file and a list of files that call invalid locations
  79. oname = "call_validity.txt"
  80. try:
  81. with io.open(oname, 'w', encoding='utf-8') as ofile:
  82. ofile.write("----- List of Invalid calls -----\n")
  83. ofile.write("\n")
  84. for call in callList:
  85. if call['valid'] == 0:
  86. ofile.write("\t\t '%s', '%s' : invalid call\n" % (call['location'], call['function']) )
  87. #endfor
  88. ofile.write('\n')
  89. ofile.write("----- List of Locations and invalid calls they make -----\n")
  90. ofile.write('\n')
  91. for locationCall in locationCallList:
  92. headWritten = 0
  93. for callId in locationCall["callIds"]:
  94. call = callList[callId]
  95. if call["valid"] == 0:
  96. if headWritten == 0:
  97. ofile.write("\t---- %s:\n" % locationCall["calllocation"])
  98. headWritten = 1
  99. ofile.write("\t\t '%s', '%s' : invalid call\n" % (call['location'], call['function']) )
  100. #endfor
  101. if headWritten != 0:
  102. ofile.write("\n")
  103. #endfor
  104. #endwith
  105. except IOError as e:
  106. raise SystemExit("ERROR: call validity file was not created! REASON: %s" % e.strerror)
  107. #endtry_except
  108. print("File saved finish: %s" % time.strftime("%H:%M:%S", time.localtime()))