Loading tools/hiddenapi/generate_hiddenapi_lists.py +16 −12 Original line number Original line Diff line number Diff line Loading @@ -28,16 +28,20 @@ FLAG_GREYLIST = "greylist" FLAG_BLACKLIST = "blacklist" FLAG_BLACKLIST = "blacklist" FLAG_GREYLIST_MAX_O = "greylist-max-o" FLAG_GREYLIST_MAX_O = "greylist-max-o" FLAG_GREYLIST_MAX_P = "greylist-max-p" FLAG_GREYLIST_MAX_P = "greylist-max-p" FLAG_CORE_PLATFORM_API = "core-platform-api" # List of all known flags. # List of all known flags. FLAGS = [ FLAGS_API_LIST = [ FLAG_WHITELIST, FLAG_WHITELIST, FLAG_GREYLIST, FLAG_GREYLIST, FLAG_BLACKLIST, FLAG_BLACKLIST, FLAG_GREYLIST_MAX_O, FLAG_GREYLIST_MAX_O, FLAG_GREYLIST_MAX_P, FLAG_GREYLIST_MAX_P, ] ] FLAGS_SET = set(FLAGS) ALL_FLAGS = FLAGS_API_LIST + [ FLAG_CORE_PLATFORM_API ] FLAGS_API_LIST_SET = set(FLAGS_API_LIST) ALL_FLAGS_SET = set(ALL_FLAGS) # Suffix used in command line args to express that only known and # Suffix used in command line args to express that only known and # otherwise unassigned entries should be assign the given flag. # otherwise unassigned entries should be assign the given flag. Loading @@ -63,7 +67,7 @@ SERIALIZATION_PATTERNS = [ SERIALIZATION_REGEX = re.compile(r'.*->(' + '|'.join(SERIALIZATION_PATTERNS) + r')$') SERIALIZATION_REGEX = re.compile(r'.*->(' + '|'.join(SERIALIZATION_PATTERNS) + r')$') # Predicates to be used with filter_apis. # Predicates to be used with filter_apis. IS_UNASSIGNED = lambda api, flags: not flags HAS_NO_API_LIST_ASSIGNED = lambda api, flags: not FLAGS_API_LIST_SET.intersection(flags) IS_SERIALIZATION = lambda api, flags: SERIALIZATION_REGEX.match(api) IS_SERIALIZATION = lambda api, flags: SERIALIZATION_REGEX.match(api) def get_args(): def get_args(): Loading @@ -77,7 +81,7 @@ def get_args(): parser.add_argument('--csv', nargs='*', default=[], metavar='CSV_FILE', parser.add_argument('--csv', nargs='*', default=[], metavar='CSV_FILE', help='CSV files to be merged into output') help='CSV files to be merged into output') for flag in FLAGS: for flag in ALL_FLAGS: ignore_conflicts_flag = flag + FLAG_IGNORE_CONFLICTS_SUFFIX ignore_conflicts_flag = flag + FLAG_IGNORE_CONFLICTS_SUFFIX parser.add_argument('--' + flag, dest=flag, nargs='*', default=[], metavar='TXT_FILE', parser.add_argument('--' + flag, dest=flag, nargs='*', default=[], metavar='TXT_FILE', help='lists of entries with flag "' + flag + '"') help='lists of entries with flag "' + flag + '"') Loading Loading @@ -132,12 +136,12 @@ class FlagsDict: def _check_flags_set(self, flags_subset, source): def _check_flags_set(self, flags_subset, source): assert isinstance(flags_subset, set) assert isinstance(flags_subset, set) assert flags_subset.issubset(FLAGS_SET), ( assert flags_subset.issubset(ALL_FLAGS_SET), ( "Error processing: {}\n" "Error processing: {}\n" "The following flags were not recognized: \n" "The following flags were not recognized: \n" "{}\n" "{}\n" "Please visit go/hiddenapi for more information.").format( "Please visit go/hiddenapi for more information.").format( source, "\n".join(flags_subset - FLAGS_SET)) source, "\n".join(flags_subset - ALL_FLAGS_SET)) def filter_apis(self, filter_fn): def filter_apis(self, filter_fn): """Returns APIs which match a given predicate. """Returns APIs which match a given predicate. Loading @@ -155,7 +159,7 @@ class FlagsDict: def get_valid_subset_of_unassigned_apis(self, api_subset): def get_valid_subset_of_unassigned_apis(self, api_subset): """Sanitizes a key set input to only include keys which exist in the dictionary """Sanitizes a key set input to only include keys which exist in the dictionary and have not been assigned any flags. and have not been assigned any API list flags. Args: Args: entries_subset (set/list): Key set to be sanitized. entries_subset (set/list): Key set to be sanitized. Loading @@ -164,7 +168,7 @@ class FlagsDict: Sanitized key set. Sanitized key set. """ """ assert isinstance(api_subset, set) assert isinstance(api_subset, set) return api_subset.intersection(self.filter_apis(IS_UNASSIGNED)) return api_subset.intersection(self.filter_apis(HAS_NO_API_LIST_ASSIGNED)) def generate_csv(self): def generate_csv(self): """Constructs CSV entries from a dictionary. """Constructs CSV entries from a dictionary. Loading Loading @@ -205,7 +209,7 @@ class FlagsDict: """Assigns a flag to given subset of entries. """Assigns a flag to given subset of entries. Args: Args: flag (string): One of FLAGS. flag (string): One of ALL_FLAGS. apis (set): Subset of APIs to recieve the flag. apis (set): Subset of APIs to recieve the flag. source (string): Origin of `entries_subset`. Will be printed in error messages. source (string): Origin of `entries_subset`. Will be printed in error messages. Loading Loading @@ -242,7 +246,7 @@ def main(argv): flags.assign_flag(FLAG_WHITELIST, flags.filter_apis(IS_SERIALIZATION)) flags.assign_flag(FLAG_WHITELIST, flags.filter_apis(IS_SERIALIZATION)) # (2) Merge text files with a known flag into the dictionary. # (2) Merge text files with a known flag into the dictionary. for flag in FLAGS: for flag in ALL_FLAGS: for filename in args[flag]: for filename in args[flag]: flags.assign_flag(flag, read_lines(filename), filename) flags.assign_flag(flag, read_lines(filename), filename) Loading @@ -251,13 +255,13 @@ def main(argv): # (a) the entry exists, and # (a) the entry exists, and # (b) it has not been assigned any other flag. # (b) it has not been assigned any other flag. # Because of (b), this must run after all strict assignments have been performed. # Because of (b), this must run after all strict assignments have been performed. for flag in FLAGS: for flag in ALL_FLAGS: for filename in args[flag + FLAG_IGNORE_CONFLICTS_SUFFIX]: for filename in args[flag + FLAG_IGNORE_CONFLICTS_SUFFIX]: valid_entries = flags.get_valid_subset_of_unassigned_apis(read_lines(filename)) valid_entries = flags.get_valid_subset_of_unassigned_apis(read_lines(filename)) flags.assign_flag(flag, valid_entries, filename) flags.assign_flag(flag, valid_entries, filename) # Assign all remaining entries to the blacklist. # Assign all remaining entries to the blacklist. flags.assign_flag(FLAG_BLACKLIST, flags.filter_apis(IS_UNASSIGNED)) flags.assign_flag(FLAG_BLACKLIST, flags.filter_apis(HAS_NO_API_LIST_ASSIGNED)) # Write output. # Write output. write_lines(args["output"], flags.generate_csv()) write_lines(args["output"], flags.generate_csv()) Loading Loading
tools/hiddenapi/generate_hiddenapi_lists.py +16 −12 Original line number Original line Diff line number Diff line Loading @@ -28,16 +28,20 @@ FLAG_GREYLIST = "greylist" FLAG_BLACKLIST = "blacklist" FLAG_BLACKLIST = "blacklist" FLAG_GREYLIST_MAX_O = "greylist-max-o" FLAG_GREYLIST_MAX_O = "greylist-max-o" FLAG_GREYLIST_MAX_P = "greylist-max-p" FLAG_GREYLIST_MAX_P = "greylist-max-p" FLAG_CORE_PLATFORM_API = "core-platform-api" # List of all known flags. # List of all known flags. FLAGS = [ FLAGS_API_LIST = [ FLAG_WHITELIST, FLAG_WHITELIST, FLAG_GREYLIST, FLAG_GREYLIST, FLAG_BLACKLIST, FLAG_BLACKLIST, FLAG_GREYLIST_MAX_O, FLAG_GREYLIST_MAX_O, FLAG_GREYLIST_MAX_P, FLAG_GREYLIST_MAX_P, ] ] FLAGS_SET = set(FLAGS) ALL_FLAGS = FLAGS_API_LIST + [ FLAG_CORE_PLATFORM_API ] FLAGS_API_LIST_SET = set(FLAGS_API_LIST) ALL_FLAGS_SET = set(ALL_FLAGS) # Suffix used in command line args to express that only known and # Suffix used in command line args to express that only known and # otherwise unassigned entries should be assign the given flag. # otherwise unassigned entries should be assign the given flag. Loading @@ -63,7 +67,7 @@ SERIALIZATION_PATTERNS = [ SERIALIZATION_REGEX = re.compile(r'.*->(' + '|'.join(SERIALIZATION_PATTERNS) + r')$') SERIALIZATION_REGEX = re.compile(r'.*->(' + '|'.join(SERIALIZATION_PATTERNS) + r')$') # Predicates to be used with filter_apis. # Predicates to be used with filter_apis. IS_UNASSIGNED = lambda api, flags: not flags HAS_NO_API_LIST_ASSIGNED = lambda api, flags: not FLAGS_API_LIST_SET.intersection(flags) IS_SERIALIZATION = lambda api, flags: SERIALIZATION_REGEX.match(api) IS_SERIALIZATION = lambda api, flags: SERIALIZATION_REGEX.match(api) def get_args(): def get_args(): Loading @@ -77,7 +81,7 @@ def get_args(): parser.add_argument('--csv', nargs='*', default=[], metavar='CSV_FILE', parser.add_argument('--csv', nargs='*', default=[], metavar='CSV_FILE', help='CSV files to be merged into output') help='CSV files to be merged into output') for flag in FLAGS: for flag in ALL_FLAGS: ignore_conflicts_flag = flag + FLAG_IGNORE_CONFLICTS_SUFFIX ignore_conflicts_flag = flag + FLAG_IGNORE_CONFLICTS_SUFFIX parser.add_argument('--' + flag, dest=flag, nargs='*', default=[], metavar='TXT_FILE', parser.add_argument('--' + flag, dest=flag, nargs='*', default=[], metavar='TXT_FILE', help='lists of entries with flag "' + flag + '"') help='lists of entries with flag "' + flag + '"') Loading Loading @@ -132,12 +136,12 @@ class FlagsDict: def _check_flags_set(self, flags_subset, source): def _check_flags_set(self, flags_subset, source): assert isinstance(flags_subset, set) assert isinstance(flags_subset, set) assert flags_subset.issubset(FLAGS_SET), ( assert flags_subset.issubset(ALL_FLAGS_SET), ( "Error processing: {}\n" "Error processing: {}\n" "The following flags were not recognized: \n" "The following flags were not recognized: \n" "{}\n" "{}\n" "Please visit go/hiddenapi for more information.").format( "Please visit go/hiddenapi for more information.").format( source, "\n".join(flags_subset - FLAGS_SET)) source, "\n".join(flags_subset - ALL_FLAGS_SET)) def filter_apis(self, filter_fn): def filter_apis(self, filter_fn): """Returns APIs which match a given predicate. """Returns APIs which match a given predicate. Loading @@ -155,7 +159,7 @@ class FlagsDict: def get_valid_subset_of_unassigned_apis(self, api_subset): def get_valid_subset_of_unassigned_apis(self, api_subset): """Sanitizes a key set input to only include keys which exist in the dictionary """Sanitizes a key set input to only include keys which exist in the dictionary and have not been assigned any flags. and have not been assigned any API list flags. Args: Args: entries_subset (set/list): Key set to be sanitized. entries_subset (set/list): Key set to be sanitized. Loading @@ -164,7 +168,7 @@ class FlagsDict: Sanitized key set. Sanitized key set. """ """ assert isinstance(api_subset, set) assert isinstance(api_subset, set) return api_subset.intersection(self.filter_apis(IS_UNASSIGNED)) return api_subset.intersection(self.filter_apis(HAS_NO_API_LIST_ASSIGNED)) def generate_csv(self): def generate_csv(self): """Constructs CSV entries from a dictionary. """Constructs CSV entries from a dictionary. Loading Loading @@ -205,7 +209,7 @@ class FlagsDict: """Assigns a flag to given subset of entries. """Assigns a flag to given subset of entries. Args: Args: flag (string): One of FLAGS. flag (string): One of ALL_FLAGS. apis (set): Subset of APIs to recieve the flag. apis (set): Subset of APIs to recieve the flag. source (string): Origin of `entries_subset`. Will be printed in error messages. source (string): Origin of `entries_subset`. Will be printed in error messages. Loading Loading @@ -242,7 +246,7 @@ def main(argv): flags.assign_flag(FLAG_WHITELIST, flags.filter_apis(IS_SERIALIZATION)) flags.assign_flag(FLAG_WHITELIST, flags.filter_apis(IS_SERIALIZATION)) # (2) Merge text files with a known flag into the dictionary. # (2) Merge text files with a known flag into the dictionary. for flag in FLAGS: for flag in ALL_FLAGS: for filename in args[flag]: for filename in args[flag]: flags.assign_flag(flag, read_lines(filename), filename) flags.assign_flag(flag, read_lines(filename), filename) Loading @@ -251,13 +255,13 @@ def main(argv): # (a) the entry exists, and # (a) the entry exists, and # (b) it has not been assigned any other flag. # (b) it has not been assigned any other flag. # Because of (b), this must run after all strict assignments have been performed. # Because of (b), this must run after all strict assignments have been performed. for flag in FLAGS: for flag in ALL_FLAGS: for filename in args[flag + FLAG_IGNORE_CONFLICTS_SUFFIX]: for filename in args[flag + FLAG_IGNORE_CONFLICTS_SUFFIX]: valid_entries = flags.get_valid_subset_of_unassigned_apis(read_lines(filename)) valid_entries = flags.get_valid_subset_of_unassigned_apis(read_lines(filename)) flags.assign_flag(flag, valid_entries, filename) flags.assign_flag(flag, valid_entries, filename) # Assign all remaining entries to the blacklist. # Assign all remaining entries to the blacklist. flags.assign_flag(FLAG_BLACKLIST, flags.filter_apis(IS_UNASSIGNED)) flags.assign_flag(FLAG_BLACKLIST, flags.filter_apis(HAS_NO_API_LIST_ASSIGNED)) # Write output. # Write output. write_lines(args["output"], flags.generate_csv()) write_lines(args["output"], flags.generate_csv()) Loading