log_parser.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import gzip
  2. import re
  3. from typing import TextIO
  4. def log_reader(fn, decompress, errors="backslashreplace"):
  5. if decompress:
  6. return gzip.open(fn, "rt", errors=errors)
  7. return open(fn, "rt", errors=errors)
  8. def parse_gtest_fails(log):
  9. ilog = iter(log)
  10. while 1:
  11. try:
  12. line = next(ilog)
  13. except StopIteration:
  14. break
  15. if line.startswith("[ RUN ]"):
  16. buf = []
  17. while 1:
  18. try:
  19. line = next(ilog)
  20. except StopIteration:
  21. break
  22. if line.startswith("[ FAILED ]"):
  23. plen = len("[ FAILED ] ")
  24. classname, method = line[plen:].split(" ")[0].split(".", maxsplit=1)
  25. yield classname, method, buf
  26. break
  27. elif line.startswith("[ OK ]"):
  28. break
  29. else:
  30. buf.append(line)
  31. def parse_yunit_fails(log):
  32. i = 0
  33. class_method = found_fail = found_exec = buf_start = None
  34. while i < len(log):
  35. line = log[i]
  36. if found_fail:
  37. if line.startswith(("[exec] ", "-----> ")):
  38. cls, method = class_method.split("::")
  39. yield cls, method, log[buf_start:i]
  40. class_method = found_fail = found_exec = buf_start = None
  41. elif found_exec:
  42. if line.startswith("[FAIL] "):
  43. found_fail = True
  44. elif line.startswith("[good] "):
  45. found_exec = class_method = buf_start = None
  46. if not found_exec and line.startswith("[exec] "):
  47. class_method = line[7:].rstrip("...")
  48. found_exec = True
  49. buf_start = i
  50. i += 1
  51. if buf_start is not None:
  52. cls, method = class_method.split("::")
  53. yield cls, method, log[buf_start:]
  54. def ctest_log_parser(fp: TextIO):
  55. start_re = re.compile(r"^\s+Start\s+\d+: ")
  56. status_re = re.compile(r"^\s*\d+/\d+ Test\s+#\d+: ([^ ]+) [.]+(\D+)")
  57. finish_re = re.compile(r"\d+% tests passed")
  58. buf = []
  59. target = reason = None
  60. while 1:
  61. line = fp.readline()
  62. if not line:
  63. break
  64. if target:
  65. if not (start_re.match(line) or status_re.match(line) or finish_re.match(line)):
  66. buf.append(line.rstrip())
  67. else:
  68. yield target, reason, buf
  69. target = reason = None
  70. buf = []
  71. if target is None:
  72. if "***" not in line:
  73. continue
  74. m = status_re.match(line)
  75. if not m:
  76. continue
  77. target = m.group(1)
  78. reason = m.group(2).replace("*", "").strip()
  79. if buf:
  80. yield target, reason, buf