blob: 940532bc9d8802d9a68fe61c2e77a336a8adaf73 [file] [log] [blame]
Paul Duffindfa10832021-05-13 17:31:51 +01001#!/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 Duffinb5cd5222022-02-28 19:06:49 +000016"""Verify that one set of hidden API flags is a subset of another."""
Paul Duffindfa10832021-05-13 17:31:51 +010017
18import argparse
19import csv
Paul Duffinc11f6672021-07-20 00:04:21 +010020import sys
Paul Duffin53a76072021-07-21 17:27:09 +010021from itertools import chain
Paul Duffindfa10832021-05-13 17:31:51 +010022
Paul Duffinb5cd5222022-02-28 19:06:49 +000023from signature_trie import signature_trie
Paul Duffinc11f6672021-07-20 00:04:21 +010024
Spandan Das559132f2021-08-25 18:17:33 +000025
Paul Duffin181b56c2022-04-08 00:03:32 +010026def dict_reader(csv_file):
Spandan Das559132f2021-08-25 18:17:33 +000027 return csv.DictReader(
Paul Duffin181b56c2022-04-08 00:03:32 +010028 csv_file, delimiter=",", quotechar="|", fieldnames=["signature"])
Spandan Das559132f2021-08-25 18:17:33 +000029
Paul Duffindfa10832021-05-13 17:31:51 +010030
Paul Duffinc11f6672021-07-20 00:04:21 +010031def read_flag_trie_from_file(file):
Paul Duffin181b56c2022-04-08 00:03:32 +010032 with open(file, "r", encoding="utf8") as stream:
Paul Duffinc11f6672021-07-20 00:04:21 +010033 return read_flag_trie_from_stream(stream)
34
Spandan Das559132f2021-08-25 18:17:33 +000035
Paul Duffinc11f6672021-07-20 00:04:21 +010036def read_flag_trie_from_stream(stream):
Paul Duffinb5cd5222022-02-28 19:06:49 +000037 trie = signature_trie()
Paul Duffinc11f6672021-07-20 00:04:21 +010038 reader = dict_reader(stream)
39 for row in reader:
Spandan Das559132f2021-08-25 18:17:33 +000040 signature = row["signature"]
Paul Duffinc11f6672021-07-20 00:04:21 +010041 trie.add(signature, row)
42 return trie
43
Spandan Das559132f2021-08-25 18:17:33 +000044
45def extract_subset_from_monolithic_flags_as_dict_from_file(
Paul Duffin181b56c2022-04-08 00:03:32 +010046 monolithic_trie, patterns_file):
Paul Duffinb5cd5222022-02-28 19:06:49 +000047 """Extract a subset of flags from the dict of monolithic flags.
Paul Duffin53a76072021-07-21 17:27:09 +010048
Paul Duffin181b56c2022-04-08 00:03:32 +010049 :param monolithic_trie: the trie containing all the monolithic flags.
50 :param patterns_file: a file containing a list of signature patterns that
Paul Duffin67b9d612021-07-21 17:38:47 +010051 define the subset.
52 :return: the dict from signature to row.
53 """
Paul Duffin181b56c2022-04-08 00:03:32 +010054 with open(patterns_file, "r", encoding="utf8") as stream:
Spandan Das559132f2021-08-25 18:17:33 +000055 return extract_subset_from_monolithic_flags_as_dict_from_stream(
Paul Duffin181b56c2022-04-08 00:03:32 +010056 monolithic_trie, stream)
Paul Duffin67b9d612021-07-21 17:38:47 +010057
Spandan Das559132f2021-08-25 18:17:33 +000058
59def extract_subset_from_monolithic_flags_as_dict_from_stream(
Paul Duffin181b56c2022-04-08 00:03:32 +010060 monolithic_trie, stream):
Paul Duffinb5cd5222022-02-28 19:06:49 +000061 """Extract a subset of flags from the trie of monolithic flags.
Paul Duffin67b9d612021-07-21 17:38:47 +010062
Paul Duffin181b56c2022-04-08 00:03:32 +010063 :param monolithic_trie: the trie containing all the monolithic flags.
Paul Duffin67b9d612021-07-21 17:38:47 +010064 :param stream: a stream containing a list of signature patterns that define
65 the subset.
Paul Duffin53a76072021-07-21 17:27:09 +010066 :return: the dict from signature to row.
67 """
Spandan Das559132f2021-08-25 18:17:33 +000068 dict_signature_to_row = {}
Paul Duffinc11f6672021-07-20 00:04:21 +010069 for pattern in stream:
70 pattern = pattern.rstrip()
Paul Duffin181b56c2022-04-08 00:03:32 +010071 rows = monolithic_trie.get_matching_rows(pattern)
Paul Duffinc11f6672021-07-20 00:04:21 +010072 for row in rows:
Spandan Das559132f2021-08-25 18:17:33 +000073 signature = row["signature"]
74 dict_signature_to_row[signature] = row
75 return dict_signature_to_row
76
Paul Duffin53a76072021-07-21 17:27:09 +010077
Paul Duffin428c6512021-07-21 15:33:22 +010078def read_signature_csv_from_stream_as_dict(stream):
Paul Duffinb5cd5222022-02-28 19:06:49 +000079 """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 Duffin428c6512021-07-21 15:33:22 +010083
Spandan Das559132f2021-08-25 18:17:33 +000084 The whole row is stored as the value.
Paul Duffin428c6512021-07-21 15:33:22 +010085 :param stream: the csv contents to read
86 :return: the dict from signature to row.
87 """
Spandan Das559132f2021-08-25 18:17:33 +000088 dict_signature_to_row = {}
Paul Duffin428c6512021-07-21 15:33:22 +010089 reader = dict_reader(stream)
90 for row in reader:
Spandan Das559132f2021-08-25 18:17:33 +000091 signature = row["signature"]
92 dict_signature_to_row[signature] = row
93 return dict_signature_to_row
94
Paul Duffindfa10832021-05-13 17:31:51 +010095
Paul Duffin181b56c2022-04-08 00:03:32 +010096def read_signature_csv_from_file_as_dict(csv_file):
Paul Duffinb5cd5222022-02-28 19:06:49 +000097 """Read the csvFile into a dict.
98
99 The first column is assumed to be the signature and used as the
100 key.
Paul Duffin428c6512021-07-21 15:33:22 +0100101
Spandan Das559132f2021-08-25 18:17:33 +0000102 The whole row is stored as the value.
Paul Duffin181b56c2022-04-08 00:03:32 +0100103 :param csv_file: the csv file to read
Paul Duffin428c6512021-07-21 15:33:22 +0100104 :return: the dict from signature to row.
105 """
Paul Duffin181b56c2022-04-08 00:03:32 +0100106 with open(csv_file, "r", encoding="utf8") as f:
Paul Duffin428c6512021-07-21 15:33:22 +0100107 return read_signature_csv_from_stream_as_dict(f)
108
Spandan Das559132f2021-08-25 18:17:33 +0000109
Paul Duffin181b56c2022-04-08 00:03:32 +0100110def compare_signature_flags(monolithic_flags_dict, modular_flags_dict):
Spandan Das559132f2021-08-25 18:17:33 +0000111 """Compare the signature flags between the two dicts.
Paul Duffin428c6512021-07-21 15:33:22 +0100112
Paul Duffin181b56c2022-04-08 00:03:32 +0100113 :param monolithic_flags_dict: the dict containing the subset of the
114 monolithic flags that should be equal to the modular flags.
115 :param modular_flags_dict:the dict containing the flags produced by a single
Paul Duffin428c6512021-07-21 15:33:22 +0100116 bootclasspath_fragment module.
117 :return: list of mismatches., each mismatch is a tuple where the first item
118 is the signature, and the second and third items are lists of the flags from
119 modular dict, and monolithic dict respectively.
120 """
Paul Duffin181b56c2022-04-08 00:03:32 +0100121 mismatching_signatures = []
Paul Duffin53a76072021-07-21 17:27:09 +0100122 # Create a sorted set of all the signatures from both the monolithic and
123 # modular dicts.
Paul Duffin181b56c2022-04-08 00:03:32 +0100124 all_signatures = sorted(
125 set(chain(monolithic_flags_dict.keys(), modular_flags_dict.keys())))
126 for signature in all_signatures:
127 monolithic_row = monolithic_flags_dict.get(signature, {})
128 monolithic_flags = monolithic_row.get(None, [])
129 if signature in modular_flags_dict:
130 modular_row = modular_flags_dict.get(signature, {})
131 modular_flags = modular_row.get(None, [])
Paul Duffin280bae62021-07-20 18:03:53 +0100132 else:
Paul Duffin181b56c2022-04-08 00:03:32 +0100133 modular_flags = ["blocked"]
134 if monolithic_flags != modular_flags:
135 mismatching_signatures.append(
136 (signature, modular_flags, monolithic_flags))
137 return mismatching_signatures
Paul Duffindfa10832021-05-13 17:31:51 +0100138
Spandan Das559132f2021-08-25 18:17:33 +0000139
Paul Duffin428c6512021-07-21 15:33:22 +0100140def main(argv):
Spandan Das559132f2021-08-25 18:17:33 +0000141 args_parser = argparse.ArgumentParser(
142 description="Verify that sets of hidden API flags are each a subset of "
Paul Duffinb5cd5222022-02-28 19:06:49 +0000143 "the monolithic flag file.")
Spandan Das559132f2021-08-25 18:17:33 +0000144 args_parser.add_argument(
Paul Duffin0c12b782022-04-08 00:28:11 +0100145 "--monolithic-flags", help="The monolithic flag file")
146 args_parser.add_argument(
147 "--module-flags",
148 action="append",
149 help="A colon separated pair of paths. The first is a path to a "
150 "filtered set of flags, and the second is a path to a set of "
151 "signature patterns that identify the set of classes belonging to "
152 "a single bootclasspath_fragment module, ")
Paul Duffin428c6512021-07-21 15:33:22 +0100153 args = args_parser.parse_args(argv[1:])
Paul Duffindfa10832021-05-13 17:31:51 +0100154
Paul Duffinc11f6672021-07-20 00:04:21 +0100155 # Read in all the flags into the trie
Paul Duffin0c12b782022-04-08 00:28:11 +0100156 monolithic_flags_path = args.monolithic_flags
Paul Duffin181b56c2022-04-08 00:03:32 +0100157 monolithic_trie = read_flag_trie_from_file(monolithic_flags_path)
Paul Duffindfa10832021-05-13 17:31:51 +0100158
Paul Duffin53a76072021-07-21 17:27:09 +0100159 # For each subset specified on the command line, create dicts for the flags
Paul Duffinc11f6672021-07-20 00:04:21 +0100160 # provided by the subset and the corresponding flags from the complete set
161 # of flags and compare them.
Paul Duffin428c6512021-07-21 15:33:22 +0100162 failed = False
Paul Duffin0c12b782022-04-08 00:28:11 +0100163 for modular_pair in args.module_flags:
Paul Duffin181b56c2022-04-08 00:03:32 +0100164 parts = modular_pair.split(":")
165 modular_flags_path = parts[0]
166 modular_patterns_path = parts[1]
167 modular_flags_dict = read_signature_csv_from_file_as_dict(
168 modular_flags_path)
169 monolithic_flags_subset_dict = \
Spandan Das559132f2021-08-25 18:17:33 +0000170 extract_subset_from_monolithic_flags_as_dict_from_file(
Paul Duffin181b56c2022-04-08 00:03:32 +0100171 monolithic_trie, modular_patterns_path)
172 mismatching_signatures = compare_signature_flags(
173 monolithic_flags_subset_dict, modular_flags_dict)
174 if mismatching_signatures:
Paul Duffin428c6512021-07-21 15:33:22 +0100175 failed = True
176 print("ERROR: Hidden API flags are inconsistent:")
Paul Duffin181b56c2022-04-08 00:03:32 +0100177 print("< " + modular_flags_path)
178 print("> " + monolithic_flags_path)
179 for mismatch in mismatching_signatures:
Paul Duffin428c6512021-07-21 15:33:22 +0100180 signature = mismatch[0]
181 print()
Spandan Das559132f2021-08-25 18:17:33 +0000182 print("< " + ",".join([signature] + mismatch[1]))
183 print("> " + ",".join([signature] + mismatch[2]))
Paul Duffin428c6512021-07-21 15:33:22 +0100184
185 if failed:
186 sys.exit(1)
187
Spandan Das559132f2021-08-25 18:17:33 +0000188
Paul Duffin428c6512021-07-21 15:33:22 +0100189if __name__ == "__main__":
190 main(sys.argv)