Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2021 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. |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame^] | 16 | """Generate a set of signature patterns from the modular flags generated by a |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 17 | bootclasspath_fragment that can be used to select a subset of monolithic flags |
| 18 | against which the modular flags can be compared. |
| 19 | """ |
| 20 | |
| 21 | import argparse |
| 22 | import csv |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame^] | 23 | import sys |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 24 | |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame^] | 25 | def dict_reader(csvfile): |
| 26 | return csv.DictReader( |
| 27 | csvfile, delimiter=',', quotechar='|', fieldnames=['signature'] |
| 28 | ) |
| 29 | |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 30 | |
| 31 | def produce_patterns_from_file(file): |
| 32 | with open(file, 'r') as f: |
| 33 | return produce_patterns_from_stream(f) |
| 34 | |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame^] | 35 | |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 36 | def produce_patterns_from_stream(stream): |
Paul Duffin | 6ffdff8 | 2021-08-09 13:47:19 +0100 | [diff] [blame] | 37 | # Read in all the signatures into a list and remove member names. |
| 38 | patterns = set() |
| 39 | for row in dict_reader(stream): |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 40 | signature = row['signature'] |
Paul Duffin | 6ffdff8 | 2021-08-09 13:47:19 +0100 | [diff] [blame] | 41 | text = signature.removeprefix("L") |
| 42 | # Remove the class specific member signature |
| 43 | pieces = text.split(";->") |
| 44 | qualifiedClassName = pieces[0] |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame^] | 45 | # Remove inner class names as they cannot be separated |
| 46 | # from the containing outer class. |
Paul Duffin | 6ffdff8 | 2021-08-09 13:47:19 +0100 | [diff] [blame] | 47 | pieces = qualifiedClassName.split("$", maxsplit=1) |
| 48 | pattern = pieces[0] |
| 49 | patterns.add(pattern) |
| 50 | |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame^] | 51 | patterns = list(patterns) #pylint: disable=redefined-variable-type |
Paul Duffin | 6ffdff8 | 2021-08-09 13:47:19 +0100 | [diff] [blame] | 52 | patterns.sort() |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 53 | return patterns |
| 54 | |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame^] | 55 | |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 56 | def main(args): |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame^] | 57 | args_parser = argparse.ArgumentParser( |
| 58 | description='Generate a set of signature patterns ' |
| 59 | 'that select a subset of monolithic hidden API files.' |
| 60 | ) |
| 61 | args_parser.add_argument( |
| 62 | '--flags', |
| 63 | help='The stub flags file which contains an entry for every dex member', |
| 64 | ) |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 65 | args_parser.add_argument('--output', help='Generated signature prefixes') |
| 66 | args = args_parser.parse_args(args) |
| 67 | |
| 68 | # Read in all the patterns into a list. |
| 69 | patterns = produce_patterns_from_file(args.flags) |
| 70 | |
| 71 | # Write out all the patterns. |
| 72 | with open(args.output, 'w') as outputFile: |
| 73 | for pattern in patterns: |
| 74 | outputFile.write(pattern) |
| 75 | outputFile.write("\n") |
| 76 | |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame^] | 77 | |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 78 | if __name__ == "__main__": |
| 79 | main(sys.argv[1:]) |