1
0

check_index.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import sys
  2. import os
  3. import json
  4. import re
  5. import argparse
  6. if sys.version_info[0] >= 3:
  7. unicode = str
  8. long = int
  9. def is_bool(x, val=None):
  10. return isinstance(x, bool) and (val is None or x == val)
  11. def is_dict(x):
  12. return isinstance(x, dict)
  13. def is_list(x):
  14. return isinstance(x, list)
  15. def is_int(x, val=None):
  16. return (isinstance(x, int) or isinstance(x, long)) and (val is None or x == val)
  17. def is_string(x, val=None):
  18. return (isinstance(x, str) or isinstance(x, unicode)) and (val is None or x == val)
  19. def matches(s, pattern):
  20. return is_string(s) and bool(re.search(pattern, s))
  21. def check_list_match(match, actual, expected, check=None, check_exception=None, missing_exception=None, extra_exception=None, allow_extra=False):
  22. """
  23. Handle the common pattern of making sure every actual item "matches" some
  24. item in the expected list, and that neither list has extra items after
  25. matching is completed.
  26. @param match: Callback to check if an actual item matches an expected
  27. item. Return True if the item matches, return False if the item doesn't
  28. match.
  29. @param actual: List of actual items to search.
  30. @param expected: List of expected items to match.
  31. @param check: Optional function to check that the actual item is valid by
  32. comparing it to the expected item.
  33. @param check_exception: Optional function that returns an argument to
  34. append to any exception thrown by the check function.
  35. @param missing_exception: Optional function that returns an argument to
  36. append to the exception thrown when an item is not found.
  37. @param extra_exception: Optional function that returns an argument to
  38. append to the exception thrown when an extra item is found.
  39. @param allow_extra: Optional parameter allowing there to be extra actual
  40. items after all the expected items have been found.
  41. """
  42. assert is_list(actual)
  43. _actual = actual[:]
  44. for expected_item in expected:
  45. found = False
  46. for i, actual_item in enumerate(_actual):
  47. if match(actual_item, expected_item):
  48. if check:
  49. try:
  50. check(actual_item, expected_item)
  51. except BaseException as e:
  52. if check_exception:
  53. e.args += (check_exception(actual_item, expected_item),)
  54. raise
  55. found = True
  56. del _actual[i]
  57. break
  58. if missing_exception:
  59. assert found, missing_exception(expected_item)
  60. else:
  61. assert found
  62. if not allow_extra:
  63. if extra_exception:
  64. assert len(_actual) == 0, [extra_exception(a) for a in _actual]
  65. else:
  66. assert len(_actual) == 0
  67. def filter_list(f, l):
  68. if l is not None:
  69. l = list(filter(f, l))
  70. if l == []:
  71. l = None
  72. return l
  73. def check_cmake(cmake):
  74. assert is_dict(cmake)
  75. assert sorted(cmake.keys()) == ["generator", "paths", "version"]
  76. check_cmake_version(cmake["version"])
  77. check_cmake_paths(cmake["paths"])
  78. check_cmake_generator(cmake["generator"])
  79. def check_cmake_version(v):
  80. assert is_dict(v)
  81. assert sorted(v.keys()) == ["isDirty", "major", "minor", "patch", "string", "suffix"]
  82. assert is_string(v["string"])
  83. assert is_int(v["major"])
  84. assert is_int(v["minor"])
  85. assert is_int(v["patch"])
  86. assert is_string(v["suffix"])
  87. assert is_bool(v["isDirty"])
  88. def check_cmake_paths(v):
  89. assert is_dict(v)
  90. assert sorted(v.keys()) == ["cmake", "cpack", "ctest", "root"]
  91. assert is_string(v["cmake"])
  92. assert is_string(v["cpack"])
  93. assert is_string(v["ctest"])
  94. assert is_string(v["root"])
  95. def check_cmake_generator(g):
  96. assert is_dict(g)
  97. name = g.get("name", None)
  98. assert is_string(name)
  99. if name.startswith("Visual Studio"):
  100. assert sorted(g.keys()) == ["multiConfig", "name", "platform"]
  101. assert is_string(g["platform"])
  102. else:
  103. assert sorted(g.keys()) == ["multiConfig", "name"]
  104. assert is_bool(g["multiConfig"], matches(name, "^(Visual Studio |Xcode$|Ninja Multi-Config$)"))
  105. def check_index_object(indexEntry, kind, major, minor, check):
  106. assert is_dict(indexEntry)
  107. assert sorted(indexEntry.keys()) == ["jsonFile", "kind", "version"]
  108. assert is_string(indexEntry["kind"])
  109. assert indexEntry["kind"] == kind
  110. assert is_dict(indexEntry["version"])
  111. assert sorted(indexEntry["version"].keys()) == ["major", "minor"]
  112. assert indexEntry["version"]["major"] == major
  113. assert indexEntry["version"]["minor"] == minor
  114. assert is_string(indexEntry["jsonFile"])
  115. filepath = os.path.join(reply_dir, indexEntry["jsonFile"])
  116. with open(filepath) as f:
  117. object = json.load(f)
  118. assert is_dict(object)
  119. assert "kind" in object
  120. assert is_string(object["kind"])
  121. assert object["kind"] == kind
  122. assert "version" in object
  123. assert is_dict(object["version"])
  124. assert sorted(object["version"].keys()) == ["major", "minor"]
  125. assert object["version"]["major"] == major
  126. assert object["version"]["minor"] == minor
  127. if check:
  128. check(object)
  129. def check_index__test(indexEntry, major, minor):
  130. def check(object):
  131. assert sorted(object.keys()) == ["kind", "version"]
  132. check_index_object(indexEntry, "__test", major, minor, check)
  133. def check_error(value, error):
  134. assert is_dict(value)
  135. assert sorted(value.keys()) == ["error"]
  136. assert is_string(value["error"])
  137. assert value["error"] == error
  138. def check_error_re(value, error):
  139. assert is_dict(value)
  140. assert sorted(value.keys()) == ["error"]
  141. assert is_string(value["error"])
  142. assert re.search(error, value["error"])
  143. parser = argparse.ArgumentParser()
  144. parser.add_argument("--build-dir")
  145. parser.add_argument("--reply-index")
  146. parser.add_argument("--cxx-compiler-id")
  147. parser.add_argument("--cxx-simulate-id")
  148. args = parser.parse_args()
  149. reply_dir = os.path.dirname(args.reply_index)
  150. with open(args.reply_index) as f:
  151. index = json.load(f)