| Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 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. | 
| Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 16 | """Verify that one set of hidden API flags is a subset of another.""" | 
| Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 17 |  | 
|  | 18 | import argparse | 
|  | 19 | import csv | 
| Paul Duffin | c11f667 | 2021-07-20 00:04:21 +0100 | [diff] [blame] | 20 | import sys | 
| Paul Duffin | 53a7607 | 2021-07-21 17:27:09 +0100 | [diff] [blame] | 21 | from itertools import chain | 
| Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 22 |  | 
| Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 23 | from signature_trie import signature_trie | 
| Paul Duffin | c11f667 | 2021-07-20 00:04:21 +0100 | [diff] [blame] | 24 |  | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 25 |  | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 26 | def dict_reader(csv_file): | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 27 | return csv.DictReader( | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 28 | csv_file, delimiter=",", quotechar="|", fieldnames=["signature"]) | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 29 |  | 
| Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 30 |  | 
| Paul Duffin | c11f667 | 2021-07-20 00:04:21 +0100 | [diff] [blame] | 31 | def read_flag_trie_from_file(file): | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 32 | with open(file, "r", encoding="utf8") as stream: | 
| Paul Duffin | c11f667 | 2021-07-20 00:04:21 +0100 | [diff] [blame] | 33 | return read_flag_trie_from_stream(stream) | 
|  | 34 |  | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 35 |  | 
| Paul Duffin | c11f667 | 2021-07-20 00:04:21 +0100 | [diff] [blame] | 36 | def read_flag_trie_from_stream(stream): | 
| Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 37 | trie = signature_trie() | 
| Paul Duffin | c11f667 | 2021-07-20 00:04:21 +0100 | [diff] [blame] | 38 | reader = dict_reader(stream) | 
|  | 39 | for row in reader: | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 40 | signature = row["signature"] | 
| Paul Duffin | c11f667 | 2021-07-20 00:04:21 +0100 | [diff] [blame] | 41 | trie.add(signature, row) | 
|  | 42 | return trie | 
|  | 43 |  | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 44 |  | 
|  | 45 | def extract_subset_from_monolithic_flags_as_dict_from_file( | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 46 | monolithic_trie, patterns_file): | 
| Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 47 | """Extract a subset of flags from the dict of monolithic flags. | 
| Paul Duffin | 53a7607 | 2021-07-21 17:27:09 +0100 | [diff] [blame] | 48 |  | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 49 | :param monolithic_trie: the trie containing all the monolithic flags. | 
|  | 50 | :param patterns_file: a file containing a list of signature patterns that | 
| Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 51 | define the subset. | 
|  | 52 | :return: the dict from signature to row. | 
|  | 53 | """ | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 54 | with open(patterns_file, "r", encoding="utf8") as stream: | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 55 | return extract_subset_from_monolithic_flags_as_dict_from_stream( | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 56 | monolithic_trie, stream) | 
| Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 57 |  | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 58 |  | 
|  | 59 | def extract_subset_from_monolithic_flags_as_dict_from_stream( | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 60 | monolithic_trie, stream): | 
| Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 61 | """Extract a subset of flags from the trie of monolithic flags. | 
| Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 62 |  | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 63 | :param monolithic_trie: the trie containing all the monolithic flags. | 
| Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 64 | :param stream: a stream containing a list of signature patterns that define | 
|  | 65 | the subset. | 
| Paul Duffin | 53a7607 | 2021-07-21 17:27:09 +0100 | [diff] [blame] | 66 | :return: the dict from signature to row. | 
|  | 67 | """ | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 68 | dict_signature_to_row = {} | 
| Paul Duffin | c11f667 | 2021-07-20 00:04:21 +0100 | [diff] [blame] | 69 | for pattern in stream: | 
|  | 70 | pattern = pattern.rstrip() | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 71 | rows = monolithic_trie.get_matching_rows(pattern) | 
| Paul Duffin | c11f667 | 2021-07-20 00:04:21 +0100 | [diff] [blame] | 72 | for row in rows: | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 73 | signature = row["signature"] | 
|  | 74 | dict_signature_to_row[signature] = row | 
|  | 75 | return dict_signature_to_row | 
|  | 76 |  | 
| Paul Duffin | 53a7607 | 2021-07-21 17:27:09 +0100 | [diff] [blame] | 77 |  | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 78 | def read_signature_csv_from_stream_as_dict(stream): | 
| Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 79 | """Read the csv contents from the stream into a dict. | 
|  | 80 |  | 
|  | 81 | The first column is assumed to be the signature and used as the | 
|  | 82 | key. | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 83 |  | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 84 | The whole row is stored as the value. | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 85 | :param stream: the csv contents to read | 
|  | 86 | :return: the dict from signature to row. | 
|  | 87 | """ | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 88 | dict_signature_to_row = {} | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 89 | reader = dict_reader(stream) | 
|  | 90 | for row in reader: | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 91 | signature = row["signature"] | 
|  | 92 | dict_signature_to_row[signature] = row | 
|  | 93 | return dict_signature_to_row | 
|  | 94 |  | 
| Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 95 |  | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 96 | def read_signature_csv_from_file_as_dict(csv_file): | 
| Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 97 | """Read the csvFile into a dict. | 
|  | 98 |  | 
|  | 99 | The first column is assumed to be the signature and used as the | 
|  | 100 | key. | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 101 |  | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 102 | The whole row is stored as the value. | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 103 | :param csv_file: the csv file to read | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 104 | :return: the dict from signature to row. | 
|  | 105 | """ | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 106 | with open(csv_file, "r", encoding="utf8") as f: | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 107 | return read_signature_csv_from_stream_as_dict(f) | 
|  | 108 |  | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 109 |  | 
| Paul Duffin | bd88c88 | 2022-04-07 23:32:19 +0100 | [diff] [blame] | 110 | def compare_signature_flags(monolithic_flags_dict, modular_flags_dict, | 
|  | 111 | implementation_flags): | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 112 | """Compare the signature flags between the two dicts. | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 113 |  | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 114 | :param monolithic_flags_dict: the dict containing the subset of the | 
|  | 115 | monolithic flags that should be equal to the modular flags. | 
|  | 116 | :param modular_flags_dict:the dict containing the flags produced by a single | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 117 | bootclasspath_fragment module. | 
|  | 118 | :return: list of mismatches., each mismatch is a tuple where the first item | 
|  | 119 | is the signature, and the second and third items are lists of the flags from | 
|  | 120 | modular dict, and monolithic dict respectively. | 
|  | 121 | """ | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 122 | mismatching_signatures = [] | 
| Paul Duffin | 53a7607 | 2021-07-21 17:27:09 +0100 | [diff] [blame] | 123 | # Create a sorted set of all the signatures from both the monolithic and | 
|  | 124 | # modular dicts. | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 125 | all_signatures = sorted( | 
|  | 126 | set(chain(monolithic_flags_dict.keys(), modular_flags_dict.keys()))) | 
|  | 127 | for signature in all_signatures: | 
|  | 128 | monolithic_row = monolithic_flags_dict.get(signature, {}) | 
|  | 129 | monolithic_flags = monolithic_row.get(None, []) | 
|  | 130 | if signature in modular_flags_dict: | 
|  | 131 | modular_row = modular_flags_dict.get(signature, {}) | 
|  | 132 | modular_flags = modular_row.get(None, []) | 
| Paul Duffin | 280bae6 | 2021-07-20 18:03:53 +0100 | [diff] [blame] | 133 | else: | 
| Paul Duffin | bd88c88 | 2022-04-07 23:32:19 +0100 | [diff] [blame] | 134 | modular_flags = implementation_flags | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 135 | if monolithic_flags != modular_flags: | 
|  | 136 | mismatching_signatures.append( | 
|  | 137 | (signature, modular_flags, monolithic_flags)) | 
|  | 138 | return mismatching_signatures | 
| Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 139 |  | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 140 |  | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 141 | def main(argv): | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 142 | args_parser = argparse.ArgumentParser( | 
|  | 143 | description="Verify that sets of hidden API flags are each a subset of " | 
| Paul Duffin | bd88c88 | 2022-04-07 23:32:19 +0100 | [diff] [blame] | 144 | "the monolithic flag file. For each module this uses the provided " | 
|  | 145 | "signature patterns to select a subset of the monolithic flags and " | 
|  | 146 | "then it compares that subset against the filtered flags provided by " | 
|  | 147 | "the module. If the module's filtered flags does not contain flags for " | 
|  | 148 | "a signature then it is assumed to have been filtered out because it " | 
|  | 149 | "was not part of an API and so is assumed to have the implementation " | 
|  | 150 | "flags.") | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 151 | args_parser.add_argument( | 
| Paul Duffin | 0c12b78 | 2022-04-08 00:28:11 +0100 | [diff] [blame] | 152 | "--monolithic-flags", help="The monolithic flag file") | 
|  | 153 | args_parser.add_argument( | 
|  | 154 | "--module-flags", | 
|  | 155 | action="append", | 
|  | 156 | help="A colon separated pair of paths. The first is a path to a " | 
|  | 157 | "filtered set of flags, and the second is a path to a set of " | 
|  | 158 | "signature patterns that identify the set of classes belonging to " | 
| Paul Duffin | bd88c88 | 2022-04-07 23:32:19 +0100 | [diff] [blame] | 159 | "a single bootclasspath_fragment module. Specify once for each module " | 
|  | 160 | "that needs to be checked.") | 
|  | 161 | args_parser.add_argument( | 
|  | 162 | "--implementation-flag", | 
|  | 163 | action="append", | 
|  | 164 | help="A flag in the set of flags that identifies a signature which is " | 
|  | 165 | "not part of an API, i.e. is the signature of a private implementation " | 
|  | 166 | "member. Specify as many times as necessary to define the " | 
|  | 167 | "implementation flag set. If this is not specified then the " | 
|  | 168 | "implementation flag set is empty.") | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 169 | args = args_parser.parse_args(argv[1:]) | 
| Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 170 |  | 
| Paul Duffin | c11f667 | 2021-07-20 00:04:21 +0100 | [diff] [blame] | 171 | # Read in all the flags into the trie | 
| Paul Duffin | 0c12b78 | 2022-04-08 00:28:11 +0100 | [diff] [blame] | 172 | monolithic_flags_path = args.monolithic_flags | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 173 | monolithic_trie = read_flag_trie_from_file(monolithic_flags_path) | 
| Paul Duffin | dfa1083 | 2021-05-13 17:31:51 +0100 | [diff] [blame] | 174 |  | 
| Paul Duffin | bd88c88 | 2022-04-07 23:32:19 +0100 | [diff] [blame] | 175 | implementation_flags = args.implementation_flag or [] | 
|  | 176 |  | 
| Paul Duffin | 53a7607 | 2021-07-21 17:27:09 +0100 | [diff] [blame] | 177 | # For each subset specified on the command line, create dicts for the flags | 
| Paul Duffin | c11f667 | 2021-07-20 00:04:21 +0100 | [diff] [blame] | 178 | # provided by the subset and the corresponding flags from the complete set | 
|  | 179 | # of flags and compare them. | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 180 | failed = False | 
| Paul Duffin | bd88c88 | 2022-04-07 23:32:19 +0100 | [diff] [blame] | 181 | module_pairs = args.module_flags or [] | 
|  | 182 | for modular_pair in module_pairs: | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 183 | parts = modular_pair.split(":") | 
|  | 184 | modular_flags_path = parts[0] | 
|  | 185 | modular_patterns_path = parts[1] | 
|  | 186 | modular_flags_dict = read_signature_csv_from_file_as_dict( | 
|  | 187 | modular_flags_path) | 
|  | 188 | monolithic_flags_subset_dict = \ | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 189 | extract_subset_from_monolithic_flags_as_dict_from_file( | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 190 | monolithic_trie, modular_patterns_path) | 
|  | 191 | mismatching_signatures = compare_signature_flags( | 
| Paul Duffin | bd88c88 | 2022-04-07 23:32:19 +0100 | [diff] [blame] | 192 | monolithic_flags_subset_dict, modular_flags_dict, | 
|  | 193 | implementation_flags) | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 194 | if mismatching_signatures: | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 195 | failed = True | 
|  | 196 | print("ERROR: Hidden API flags are inconsistent:") | 
| Paul Duffin | 181b56c | 2022-04-08 00:03:32 +0100 | [diff] [blame] | 197 | print("< " + modular_flags_path) | 
|  | 198 | print("> " + monolithic_flags_path) | 
|  | 199 | for mismatch in mismatching_signatures: | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 200 | signature = mismatch[0] | 
|  | 201 | print() | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 202 | print("< " + ",".join([signature] + mismatch[1])) | 
|  | 203 | print("> " + ",".join([signature] + mismatch[2])) | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 204 |  | 
|  | 205 | if failed: | 
|  | 206 | sys.exit(1) | 
|  | 207 |  | 
| Spandan Das | 559132f | 2021-08-25 18:17:33 +0000 | [diff] [blame] | 208 |  | 
| Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 209 | if __name__ == "__main__": | 
|  | 210 | main(sys.argv) |