verify_overlaps.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2018 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Verify that one set of hidden API flags is a subset of another."""
  17. import argparse
  18. import csv
  19. import sys
  20. from itertools import chain
  21. from signature_trie import signature_trie
  22. def dict_reader(csv_file):
  23. return csv.DictReader(
  24. csv_file, delimiter=",", quotechar="|", fieldnames=["signature"])
  25. def read_flag_trie_from_file(file):
  26. with open(file, "r", encoding="utf8") as stream:
  27. return read_flag_trie_from_stream(stream)
  28. def read_flag_trie_from_stream(stream):
  29. trie = signature_trie()
  30. reader = dict_reader(stream)
  31. for row in reader:
  32. signature = row["signature"]
  33. trie.add(signature, row)
  34. return trie
  35. def extract_subset_from_monolithic_flags_as_dict_from_file(
  36. monolithic_trie, patterns_file):
  37. """Extract a subset of flags from the dict of monolithic flags.
  38. :param monolithic_trie: the trie containing all the monolithic flags.
  39. :param patterns_file: a file containing a list of signature patterns that
  40. define the subset.
  41. :return: the dict from signature to row.
  42. """
  43. with open(patterns_file, "r", encoding="utf8") as stream:
  44. return extract_subset_from_monolithic_flags_as_dict_from_stream(
  45. monolithic_trie, stream)
  46. def extract_subset_from_monolithic_flags_as_dict_from_stream(
  47. monolithic_trie, stream):
  48. """Extract a subset of flags from the trie of monolithic flags.
  49. :param monolithic_trie: the trie containing all the monolithic flags.
  50. :param stream: a stream containing a list of signature patterns that define
  51. the subset.
  52. :return: the dict from signature to row.
  53. """
  54. dict_signature_to_row = {}
  55. for pattern in stream:
  56. pattern = pattern.rstrip()
  57. rows = monolithic_trie.get_matching_rows(pattern)
  58. for row in rows:
  59. signature = row["signature"]
  60. dict_signature_to_row[signature] = row
  61. return dict_signature_to_row
  62. def read_signature_csv_from_stream_as_dict(stream):
  63. """Read the csv contents from the stream into a dict.
  64. The first column is assumed to be the signature and used as the
  65. key.
  66. The whole row is stored as the value.
  67. :param stream: the csv contents to read
  68. :return: the dict from signature to row.
  69. """
  70. dict_signature_to_row = {}
  71. reader = dict_reader(stream)
  72. for row in reader:
  73. signature = row["signature"]
  74. dict_signature_to_row[signature] = row
  75. return dict_signature_to_row
  76. def read_signature_csv_from_file_as_dict(csv_file):
  77. """Read the csvFile into a dict.
  78. The first column is assumed to be the signature and used as the
  79. key.
  80. The whole row is stored as the value.
  81. :param csv_file: the csv file to read
  82. :return: the dict from signature to row.
  83. """
  84. with open(csv_file, "r", encoding="utf8") as f:
  85. return read_signature_csv_from_stream_as_dict(f)
  86. def compare_signature_flags(monolithic_flags_dict, modular_flags_dict,
  87. implementation_flags):
  88. """Compare the signature flags between the two dicts.
  89. :param monolithic_flags_dict: the dict containing the subset of the
  90. monolithic flags that should be equal to the modular flags.
  91. :param modular_flags_dict:the dict containing the flags produced by a single
  92. bootclasspath_fragment module.
  93. :return: list of mismatches., each mismatch is a tuple where the first item
  94. is the signature, and the second and third items are lists of the flags from
  95. modular dict, and monolithic dict respectively.
  96. """
  97. mismatching_signatures = []
  98. # Create a sorted set of all the signatures from both the monolithic and
  99. # modular dicts.
  100. all_signatures = sorted(
  101. set(chain(monolithic_flags_dict.keys(), modular_flags_dict.keys())))
  102. for signature in all_signatures:
  103. monolithic_row = monolithic_flags_dict.get(signature, {})
  104. monolithic_flags = monolithic_row.get(None, [])
  105. if signature in modular_flags_dict:
  106. modular_row = modular_flags_dict.get(signature, {})
  107. modular_flags = modular_row.get(None, [])
  108. else:
  109. modular_flags = implementation_flags
  110. if monolithic_flags != modular_flags:
  111. mismatching_signatures.append(
  112. (signature, modular_flags, monolithic_flags))
  113. return mismatching_signatures
  114. def main(argv):
  115. args_parser = argparse.ArgumentParser(
  116. description="Verify that sets of hidden API flags are each a subset of "
  117. "the monolithic flag file. For each module this uses the provided "
  118. "signature patterns to select a subset of the monolithic flags and "
  119. "then it compares that subset against the filtered flags provided by "
  120. "the module. If the module's filtered flags does not contain flags for "
  121. "a signature then it is assumed to have been filtered out because it "
  122. "was not part of an API and so is assumed to have the implementation "
  123. "flags.")
  124. args_parser.add_argument(
  125. "--monolithic-flags", help="The monolithic flag file")
  126. args_parser.add_argument(
  127. "--module-flags",
  128. action="append",
  129. help="A colon separated pair of paths. The first is a path to a "
  130. "filtered set of flags, and the second is a path to a set of "
  131. "signature patterns that identify the set of classes belonging to "
  132. "a single bootclasspath_fragment module. Specify once for each module "
  133. "that needs to be checked.")
  134. args_parser.add_argument(
  135. "--implementation-flag",
  136. action="append",
  137. help="A flag in the set of flags that identifies a signature which is "
  138. "not part of an API, i.e. is the signature of a private implementation "
  139. "member. Specify as many times as necessary to define the "
  140. "implementation flag set. If this is not specified then the "
  141. "implementation flag set is empty.")
  142. args = args_parser.parse_args(argv[1:])
  143. # Read in all the flags into the trie
  144. monolithic_flags_path = args.monolithic_flags
  145. monolithic_trie = read_flag_trie_from_file(monolithic_flags_path)
  146. implementation_flags = args.implementation_flag or []
  147. # For each subset specified on the command line, create dicts for the flags
  148. # provided by the subset and the corresponding flags from the complete set
  149. # of flags and compare them.
  150. failed = False
  151. module_pairs = args.module_flags or []
  152. for modular_pair in module_pairs:
  153. parts = modular_pair.split(":")
  154. modular_flags_path = parts[0]
  155. modular_patterns_path = parts[1]
  156. modular_flags_dict = read_signature_csv_from_file_as_dict(
  157. modular_flags_path)
  158. monolithic_flags_subset_dict = \
  159. extract_subset_from_monolithic_flags_as_dict_from_file(
  160. monolithic_trie, modular_patterns_path)
  161. mismatching_signatures = compare_signature_flags(
  162. monolithic_flags_subset_dict, modular_flags_dict,
  163. implementation_flags)
  164. if mismatching_signatures:
  165. failed = True
  166. print("ERROR: Hidden API flags are inconsistent:")
  167. print("< " + modular_flags_path)
  168. print("> " + monolithic_flags_path)
  169. for mismatch in mismatching_signatures:
  170. signature = mismatch[0]
  171. print()
  172. print("< " + ",".join([signature] + mismatch[1]))
  173. print("> " + ",".join([signature] + mismatch[2]))
  174. if failed:
  175. sys.exit(1)
  176. if __name__ == "__main__":
  177. main(sys.argv)