diff options
author | Andrei Onea <andreionea@google.com> | 2019-03-29 15:27:55 +0000 |
---|---|---|
committer | Andrei Onea <andreionea@google.com> | 2019-04-01 15:32:36 +0100 |
commit | a6e09b427392b4448545a92363d2e6e1967d335c (patch) | |
tree | f60c7751e63e6a0e1dfcde34cda7d96a2c7b0fd0 /tools/hiddenapi/generate_hiddenapi_lists.py | |
parent | f3736d67f85fd9a48451e165c70e16b1c3fa6856 (diff) |
Automatically greylist code in 3P packages
generate_hidden_api_lists now receives a file containing package names
which need to be greylisted (although it could be made to work with any
api list required).
Also took the opportunity to clean up the tests to reflect the more
strict code.
Bug: 129387816
Test: m appcompat
Test: frameworks/base/tools/hiddenapi/generate_hiddenapi_lists_test.py
Change-Id: I619f8581d166aa48eda572bc0053d8739d6420eb
Diffstat (limited to 'tools/hiddenapi/generate_hiddenapi_lists.py')
-rwxr-xr-x | tools/hiddenapi/generate_hiddenapi_lists.py | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/tools/hiddenapi/generate_hiddenapi_lists.py b/tools/hiddenapi/generate_hiddenapi_lists.py index 6781eba05534..c856cc36d6f6 100755 --- a/tools/hiddenapi/generate_hiddenapi_lists.py +++ b/tools/hiddenapi/generate_hiddenapi_lists.py @@ -21,6 +21,7 @@ from collections import defaultdict import os import sys import re +import functools # Names of flags recognized by the `hiddenapi` tool. FLAG_WHITELIST = "whitelist" @@ -58,6 +59,10 @@ ALL_FLAGS_SET = set(ALL_FLAGS) # script to skip any entries which do not exist any more. FLAG_IGNORE_CONFLICTS_SUFFIX = "-ignore-conflicts" +# Suffix used in command line args to express that all apis within a given set +# of packages should be assign the given flag. +FLAG_PACKAGES_SUFFIX = "-packages" + # Regex patterns of fields/methods used in serialization. These are # considered public API despite being hidden. SERIALIZATION_PATTERNS = [ @@ -91,12 +96,16 @@ def get_args(): for flag in ALL_FLAGS: ignore_conflicts_flag = flag + FLAG_IGNORE_CONFLICTS_SUFFIX + packages_flag = flag + FLAG_PACKAGES_SUFFIX parser.add_argument('--' + flag, dest=flag, nargs='*', default=[], metavar='TXT_FILE', help='lists of entries with flag "' + flag + '"') parser.add_argument('--' + ignore_conflicts_flag, dest=ignore_conflicts_flag, nargs='*', default=[], metavar='TXT_FILE', help='lists of entries with flag "' + flag + '". skip entry if missing or flag conflict.') + parser.add_argument('--' + packages_flag, dest=packages_flag, nargs='*', + default=[], metavar='TXT_FILE', + help='lists of packages to be added to ' + flag + ' list') return parser.parse_args() @@ -128,6 +137,19 @@ def write_lines(filename, lines): with open(filename, 'w') as f: f.writelines(lines) +def extract_package(signature): + """Extracts the package from a signature. + + Args: + signature (string): JNI signature of a method or field. + + Returns: + The package name of the class containing the field/method. + """ + full_class_name = signature.split(";->")[0] + package_name = full_class_name[1:full_class_name.rindex("/")] + return package_name.replace('/', '.') + class FlagsDict: def __init__(self): self._dict_keyset = set() @@ -206,7 +228,10 @@ class FlagsDict: self._dict_keyset.update([ csv[0] for csv in csv_values ]) # Check that all flags are known. - csv_flags = set(reduce(lambda x, y: set(x).union(y), [ csv[1:] for csv in csv_values ], [])) + csv_flags = set(functools.reduce( + lambda x, y: set(x).union(y), + [ csv[1:] for csv in csv_values ], + [])) self._check_flags_set(csv_flags, source) # Iterate over all CSV lines, find entry in dict and append flags to it. @@ -273,6 +298,15 @@ def main(argv): valid_entries = flags.get_valid_subset_of_unassigned_apis(read_lines(filename)) flags.assign_flag(flag, valid_entries, filename) + # All members in the specified packages will be assigned the appropriate flag. + for flag in ALL_FLAGS: + for filename in args[flag + FLAG_PACKAGES_SUFFIX]: + packages_needing_list = set(read_lines(filename)) + should_add_signature_to_list = lambda sig,lists: extract_package( + sig) in packages_needing_list and not lists + valid_entries = flags.filter_apis(should_add_signature_to_list) + flags.assign_flag(flag, valid_entries) + # Assign all remaining entries to the blacklist. flags.assign_flag(FLAG_BLACKLIST, flags.filter_apis(HAS_NO_API_LIST_ASSIGNED)) |