ctest_utils.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import dataclasses
  2. import re
  3. from collections import defaultdict
  4. from typing import List, Dict, Tuple, Set
  5. shard_partition_re = re.compile(r"_\d+$")
  6. def get_common_shard_name(name):
  7. return shard_partition_re.sub("", name)
  8. @dataclasses.dataclass
  9. class CTestTestcaseLog:
  10. classname: str
  11. method: str
  12. fn: str
  13. url: str
  14. shard: "CTestLogShard"
  15. class CTestLogShard:
  16. def __init__(self, name, status, log_url):
  17. self.name = name
  18. self.status = status
  19. self.log_url = log_url
  20. self.testcases: List[CTestTestcaseLog] = []
  21. self.idx: Dict[Tuple[str, str], CTestTestcaseLog] = {}
  22. def add_testcase(self, classname, method, fn, url):
  23. log = CTestTestcaseLog(classname, method, fn, url, self)
  24. self.testcases.append(log)
  25. self.idx[(classname, method)] = log
  26. def get_log(self, classname, method):
  27. return self.idx.get((classname, method))
  28. def get_extra_logs(self, found_testcases: Set[Tuple[str, str]]):
  29. extra_keys = set(self.idx.keys()) - found_testcases
  30. return [self.idx[k] for k in extra_keys]
  31. @property
  32. def logs(self):
  33. return self.idx.values()
  34. @property
  35. def filename(self):
  36. return f"{self.name}.xml"
  37. class CTestLog:
  38. def __init__(self):
  39. self.name_shard = {} # type: Dict[str, CTestLogShard]
  40. self.storage = defaultdict(dict)
  41. def add_shard(self, name, status, log_url):
  42. common_name = get_common_shard_name(name)
  43. shard = self.storage[common_name][name] = self.name_shard[name] = CTestLogShard(name, status, log_url)
  44. return shard
  45. def has_error_shard(self, name):
  46. return name in self.name_shard
  47. def get_shard(self, name):
  48. return self.name_shard[name]
  49. def get_log(self, common_name, cls, name):
  50. for shard in self.storage[common_name].values():
  51. log = shard.get_log(cls, name)
  52. if log:
  53. return log
  54. return None
  55. def get_extra_tests(self, common_name, names):
  56. result = []
  57. for shard in self.storage[common_name].values():
  58. extra = shard.get_extra_logs(set(names))
  59. if extra:
  60. result.extend(extra)
  61. return result
  62. @property
  63. def has_logs(self):
  64. return len(self.name_shard) > 0