David Brazdil | 8503b90 | 2018-08-30 13:35:03 +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. |
| 16 | """ |
| 17 | Generate API lists for non-SDK API enforcement. |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 18 | """ |
| 19 | import argparse |
David Brazdil | 17d16e8 | 2018-12-13 17:00:09 +0000 | [diff] [blame] | 20 | from collections import defaultdict |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 21 | import os |
| 22 | import sys |
| 23 | import re |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 24 | import functools |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 25 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 26 | # Names of flags recognized by the `hiddenapi` tool. |
| 27 | FLAG_WHITELIST = "whitelist" |
| 28 | FLAG_GREYLIST = "greylist" |
| 29 | FLAG_BLACKLIST = "blacklist" |
| 30 | FLAG_GREYLIST_MAX_O = "greylist-max-o" |
David Brazdil | 5cd148f | 2018-11-01 09:54:25 +0000 | [diff] [blame] | 31 | FLAG_GREYLIST_MAX_P = "greylist-max-p" |
David Brazdil | a4e64da | 2019-05-01 11:57:05 +0100 | [diff] [blame] | 32 | FLAG_GREYLIST_MAX_Q = "greylist-max-q" |
David Brazdil | 439d349 | 2018-12-07 11:49:55 +0000 | [diff] [blame] | 33 | FLAG_CORE_PLATFORM_API = "core-platform-api" |
Andrei Onea | 80a5660 | 2019-03-01 18:49:15 +0000 | [diff] [blame] | 34 | FLAG_PUBLIC_API = "public-api" |
| 35 | FLAG_SYSTEM_API = "system-api" |
| 36 | FLAG_TEST_API = "test-api" |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 37 | |
| 38 | # List of all known flags. |
David Brazdil | 439d349 | 2018-12-07 11:49:55 +0000 | [diff] [blame] | 39 | FLAGS_API_LIST = [ |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 40 | FLAG_WHITELIST, |
| 41 | FLAG_GREYLIST, |
| 42 | FLAG_BLACKLIST, |
| 43 | FLAG_GREYLIST_MAX_O, |
David Brazdil | 5cd148f | 2018-11-01 09:54:25 +0000 | [diff] [blame] | 44 | FLAG_GREYLIST_MAX_P, |
David Brazdil | a4e64da | 2019-05-01 11:57:05 +0100 | [diff] [blame] | 45 | FLAG_GREYLIST_MAX_Q, |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 46 | ] |
Andrei Onea | 80a5660 | 2019-03-01 18:49:15 +0000 | [diff] [blame] | 47 | ALL_FLAGS = FLAGS_API_LIST + [ |
| 48 | FLAG_CORE_PLATFORM_API, |
| 49 | FLAG_PUBLIC_API, |
| 50 | FLAG_SYSTEM_API, |
| 51 | FLAG_TEST_API, |
| 52 | ] |
David Brazdil | 439d349 | 2018-12-07 11:49:55 +0000 | [diff] [blame] | 53 | |
| 54 | FLAGS_API_LIST_SET = set(FLAGS_API_LIST) |
| 55 | ALL_FLAGS_SET = set(ALL_FLAGS) |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 56 | |
| 57 | # Suffix used in command line args to express that only known and |
| 58 | # otherwise unassigned entries should be assign the given flag. |
| 59 | # For example, the P dark greylist is checked in as it was in P, |
| 60 | # but signatures have changes since then. The flag instructs this |
| 61 | # script to skip any entries which do not exist any more. |
| 62 | FLAG_IGNORE_CONFLICTS_SUFFIX = "-ignore-conflicts" |
| 63 | |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 64 | # Suffix used in command line args to express that all apis within a given set |
| 65 | # of packages should be assign the given flag. |
| 66 | FLAG_PACKAGES_SUFFIX = "-packages" |
| 67 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 68 | # Regex patterns of fields/methods used in serialization. These are |
| 69 | # considered public API despite being hidden. |
| 70 | SERIALIZATION_PATTERNS = [ |
| 71 | r'readObject\(Ljava/io/ObjectInputStream;\)V', |
| 72 | r'readObjectNoData\(\)V', |
| 73 | r'readResolve\(\)Ljava/lang/Object;', |
| 74 | r'serialVersionUID:J', |
| 75 | r'serialPersistentFields:\[Ljava/io/ObjectStreamField;', |
| 76 | r'writeObject\(Ljava/io/ObjectOutputStream;\)V', |
| 77 | r'writeReplace\(\)Ljava/lang/Object;', |
| 78 | ] |
| 79 | |
| 80 | # Single regex used to match serialization API. It combines all the |
| 81 | # SERIALIZATION_PATTERNS into a single regular expression. |
| 82 | SERIALIZATION_REGEX = re.compile(r'.*->(' + '|'.join(SERIALIZATION_PATTERNS) + r')$') |
| 83 | |
| 84 | # Predicates to be used with filter_apis. |
David Brazdil | 439d349 | 2018-12-07 11:49:55 +0000 | [diff] [blame] | 85 | HAS_NO_API_LIST_ASSIGNED = lambda api, flags: not FLAGS_API_LIST_SET.intersection(flags) |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 86 | IS_SERIALIZATION = lambda api, flags: SERIALIZATION_REGEX.match(api) |
| 87 | |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 88 | def get_args(): |
| 89 | """Parses command line arguments. |
| 90 | |
| 91 | Returns: |
| 92 | Namespace: dictionary of parsed arguments |
| 93 | """ |
| 94 | parser = argparse.ArgumentParser() |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 95 | parser.add_argument('--output', required=True) |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 96 | parser.add_argument('--csv', nargs='*', default=[], metavar='CSV_FILE', |
| 97 | help='CSV files to be merged into output') |
| 98 | |
David Brazdil | 439d349 | 2018-12-07 11:49:55 +0000 | [diff] [blame] | 99 | for flag in ALL_FLAGS: |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 100 | ignore_conflicts_flag = flag + FLAG_IGNORE_CONFLICTS_SUFFIX |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 101 | packages_flag = flag + FLAG_PACKAGES_SUFFIX |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 102 | parser.add_argument('--' + flag, dest=flag, nargs='*', default=[], metavar='TXT_FILE', |
| 103 | help='lists of entries with flag "' + flag + '"') |
| 104 | parser.add_argument('--' + ignore_conflicts_flag, dest=ignore_conflicts_flag, nargs='*', |
| 105 | default=[], metavar='TXT_FILE', |
| 106 | help='lists of entries with flag "' + flag + |
| 107 | '". skip entry if missing or flag conflict.') |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 108 | parser.add_argument('--' + packages_flag, dest=packages_flag, nargs='*', |
| 109 | default=[], metavar='TXT_FILE', |
| 110 | help='lists of packages to be added to ' + flag + ' list') |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 111 | |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 112 | return parser.parse_args() |
| 113 | |
| 114 | def read_lines(filename): |
| 115 | """Reads entire file and return it as a list of lines. |
| 116 | |
David Brazdil | ae88d4e | 2018-09-06 14:46:55 +0100 | [diff] [blame] | 117 | Lines which begin with a hash are ignored. |
| 118 | |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 119 | Args: |
| 120 | filename (string): Path to the file to read from. |
| 121 | |
| 122 | Returns: |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 123 | Lines of the file as a list of string. |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 124 | """ |
| 125 | with open(filename, 'r') as f: |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 126 | lines = f.readlines(); |
| 127 | lines = filter(lambda line: not line.startswith('#'), lines) |
| 128 | lines = map(lambda line: line.strip(), lines) |
| 129 | return set(lines) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 130 | |
| 131 | def write_lines(filename, lines): |
| 132 | """Writes list of lines into a file, overwriting the file it it exists. |
| 133 | |
| 134 | Args: |
| 135 | filename (string): Path to the file to be writting into. |
| 136 | lines (list): List of strings to write into the file. |
| 137 | """ |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 138 | lines = map(lambda line: line + '\n', lines) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 139 | with open(filename, 'w') as f: |
| 140 | f.writelines(lines) |
| 141 | |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 142 | def extract_package(signature): |
| 143 | """Extracts the package from a signature. |
| 144 | |
| 145 | Args: |
| 146 | signature (string): JNI signature of a method or field. |
| 147 | |
| 148 | Returns: |
| 149 | The package name of the class containing the field/method. |
| 150 | """ |
| 151 | full_class_name = signature.split(";->")[0] |
| 152 | package_name = full_class_name[1:full_class_name.rindex("/")] |
| 153 | return package_name.replace('/', '.') |
| 154 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 155 | class FlagsDict: |
David Brazdil | 17d16e8 | 2018-12-13 17:00:09 +0000 | [diff] [blame] | 156 | def __init__(self): |
| 157 | self._dict_keyset = set() |
| 158 | self._dict = defaultdict(set) |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 159 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 160 | def _check_entries_set(self, keys_subset, source): |
| 161 | assert isinstance(keys_subset, set) |
| 162 | assert keys_subset.issubset(self._dict_keyset), ( |
| 163 | "Error processing: {}\n" |
| 164 | "The following entries were unexpected:\n" |
| 165 | "{}" |
| 166 | "Please visit go/hiddenapi for more information.").format( |
| 167 | source, "".join(map(lambda x: " " + str(x), keys_subset - self._dict_keyset))) |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 168 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 169 | def _check_flags_set(self, flags_subset, source): |
| 170 | assert isinstance(flags_subset, set) |
David Brazdil | 439d349 | 2018-12-07 11:49:55 +0000 | [diff] [blame] | 171 | assert flags_subset.issubset(ALL_FLAGS_SET), ( |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 172 | "Error processing: {}\n" |
| 173 | "The following flags were not recognized: \n" |
| 174 | "{}\n" |
| 175 | "Please visit go/hiddenapi for more information.").format( |
David Brazdil | 439d349 | 2018-12-07 11:49:55 +0000 | [diff] [blame] | 176 | source, "\n".join(flags_subset - ALL_FLAGS_SET)) |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 177 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 178 | def filter_apis(self, filter_fn): |
| 179 | """Returns APIs which match a given predicate. |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 180 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 181 | This is a helper function which allows to filter on both signatures (keys) and |
| 182 | flags (values). The built-in filter() invokes the lambda only with dict's keys. |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 183 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 184 | Args: |
| 185 | filter_fn : Function which takes two arguments (signature/flags) and returns a boolean. |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 186 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 187 | Returns: |
| 188 | A set of APIs which match the predicate. |
| 189 | """ |
| 190 | return set(filter(lambda x: filter_fn(x, self._dict[x]), self._dict_keyset)) |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 191 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 192 | def get_valid_subset_of_unassigned_apis(self, api_subset): |
| 193 | """Sanitizes a key set input to only include keys which exist in the dictionary |
David Brazdil | 439d349 | 2018-12-07 11:49:55 +0000 | [diff] [blame] | 194 | and have not been assigned any API list flags. |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 195 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 196 | Args: |
| 197 | entries_subset (set/list): Key set to be sanitized. |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 198 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 199 | Returns: |
| 200 | Sanitized key set. |
| 201 | """ |
| 202 | assert isinstance(api_subset, set) |
David Brazdil | 439d349 | 2018-12-07 11:49:55 +0000 | [diff] [blame] | 203 | return api_subset.intersection(self.filter_apis(HAS_NO_API_LIST_ASSIGNED)) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 204 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 205 | def generate_csv(self): |
| 206 | """Constructs CSV entries from a dictionary. |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 207 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 208 | Returns: |
| 209 | List of lines comprising a CSV file. See "parse_and_merge_csv" for format description. |
| 210 | """ |
| 211 | return sorted(map(lambda api: ",".join([api] + sorted(self._dict[api])), self._dict)) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 212 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 213 | def parse_and_merge_csv(self, csv_lines, source = "<unknown>"): |
| 214 | """Parses CSV entries and merges them into a given dictionary. |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 215 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 216 | The expected CSV format is: |
| 217 | <api signature>,<flag1>,<flag2>,...,<flagN> |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 218 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 219 | Args: |
| 220 | csv_lines (list of strings): Lines read from a CSV file. |
| 221 | source (string): Origin of `csv_lines`. Will be printed in error messages. |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 222 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 223 | Throws: |
David Brazdil | 17d16e8 | 2018-12-13 17:00:09 +0000 | [diff] [blame] | 224 | AssertionError if parsed flags are invalid. |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 225 | """ |
| 226 | # Split CSV lines into arrays of values. |
| 227 | csv_values = [ line.split(',') for line in csv_lines ] |
| 228 | |
David Brazdil | 17d16e8 | 2018-12-13 17:00:09 +0000 | [diff] [blame] | 229 | # Update the full set of API signatures. |
| 230 | self._dict_keyset.update([ csv[0] for csv in csv_values ]) |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 231 | |
| 232 | # Check that all flags are known. |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 233 | csv_flags = set(functools.reduce( |
| 234 | lambda x, y: set(x).union(y), |
| 235 | [ csv[1:] for csv in csv_values ], |
| 236 | [])) |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 237 | self._check_flags_set(csv_flags, source) |
| 238 | |
| 239 | # Iterate over all CSV lines, find entry in dict and append flags to it. |
| 240 | for csv in csv_values: |
Andrei Onea | 80a5660 | 2019-03-01 18:49:15 +0000 | [diff] [blame] | 241 | flags = csv[1:] |
| 242 | if (FLAG_PUBLIC_API in flags) or (FLAG_SYSTEM_API in flags): |
| 243 | flags.append(FLAG_WHITELIST) |
Andrei Onea | 80a5660 | 2019-03-01 18:49:15 +0000 | [diff] [blame] | 244 | self._dict[csv[0]].update(flags) |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 245 | |
| 246 | def assign_flag(self, flag, apis, source="<unknown>"): |
| 247 | """Assigns a flag to given subset of entries. |
| 248 | |
| 249 | Args: |
David Brazdil | 439d349 | 2018-12-07 11:49:55 +0000 | [diff] [blame] | 250 | flag (string): One of ALL_FLAGS. |
Andrei Onea | 80a5660 | 2019-03-01 18:49:15 +0000 | [diff] [blame] | 251 | apis (set): Subset of APIs to receive the flag. |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 252 | source (string): Origin of `entries_subset`. Will be printed in error messages. |
| 253 | |
| 254 | Throws: |
| 255 | AssertionError if parsed API signatures of flags are invalid. |
| 256 | """ |
| 257 | # Check that all APIs exist in the dict. |
| 258 | self._check_entries_set(apis, source) |
| 259 | |
| 260 | # Check that the flag is known. |
| 261 | self._check_flags_set(set([ flag ]), source) |
| 262 | |
| 263 | # Iterate over the API subset, find each entry in dict and assign the flag to it. |
| 264 | for api in apis: |
| 265 | self._dict[api].add(flag) |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 266 | |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 267 | def main(argv): |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 268 | # Parse arguments. |
| 269 | args = vars(get_args()) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 270 | |
David Brazdil | 17d16e8 | 2018-12-13 17:00:09 +0000 | [diff] [blame] | 271 | # Initialize API->flags dictionary. |
| 272 | flags = FlagsDict() |
| 273 | |
| 274 | # Merge input CSV files into the dictionary. |
| 275 | # Do this first because CSV files produced by parsing API stubs will |
| 276 | # contain the full set of APIs. Subsequent additions from text files |
| 277 | # will be able to detect invalid entries, and/or filter all as-yet |
| 278 | # unassigned entries. |
| 279 | for filename in args["csv"]: |
| 280 | flags.parse_and_merge_csv(read_lines(filename), filename) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 281 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 282 | # Combine inputs which do not require any particular order. |
| 283 | # (1) Assign serialization API to whitelist. |
| 284 | flags.assign_flag(FLAG_WHITELIST, flags.filter_apis(IS_SERIALIZATION)) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 285 | |
David Brazdil | 17d16e8 | 2018-12-13 17:00:09 +0000 | [diff] [blame] | 286 | # (2) Merge text files with a known flag into the dictionary. |
David Brazdil | 439d349 | 2018-12-07 11:49:55 +0000 | [diff] [blame] | 287 | for flag in ALL_FLAGS: |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 288 | for filename in args[flag]: |
| 289 | flags.assign_flag(flag, read_lines(filename), filename) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 290 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 291 | # Merge text files where conflicts should be ignored. |
| 292 | # This will only assign the given flag if: |
| 293 | # (a) the entry exists, and |
| 294 | # (b) it has not been assigned any other flag. |
| 295 | # Because of (b), this must run after all strict assignments have been performed. |
David Brazdil | 439d349 | 2018-12-07 11:49:55 +0000 | [diff] [blame] | 296 | for flag in ALL_FLAGS: |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 297 | for filename in args[flag + FLAG_IGNORE_CONFLICTS_SUFFIX]: |
| 298 | valid_entries = flags.get_valid_subset_of_unassigned_apis(read_lines(filename)) |
| 299 | flags.assign_flag(flag, valid_entries, filename) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 300 | |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 301 | # All members in the specified packages will be assigned the appropriate flag. |
| 302 | for flag in ALL_FLAGS: |
| 303 | for filename in args[flag + FLAG_PACKAGES_SUFFIX]: |
| 304 | packages_needing_list = set(read_lines(filename)) |
| 305 | should_add_signature_to_list = lambda sig,lists: extract_package( |
| 306 | sig) in packages_needing_list and not lists |
| 307 | valid_entries = flags.filter_apis(should_add_signature_to_list) |
| 308 | flags.assign_flag(flag, valid_entries) |
| 309 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 310 | # Assign all remaining entries to the blacklist. |
David Brazdil | 439d349 | 2018-12-07 11:49:55 +0000 | [diff] [blame] | 311 | flags.assign_flag(FLAG_BLACKLIST, flags.filter_apis(HAS_NO_API_LIST_ASSIGNED)) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 312 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 313 | # Write output. |
| 314 | write_lines(args["output"], flags.generate_csv()) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 315 | |
| 316 | if __name__ == "__main__": |
| 317 | main(sys.argv) |