blob: 0acb2a00426ce8d42bc287202b877e5ab8861492 [file] [log] [blame]
Paul Duffin67b9d612021-07-21 17:38:47 +01001#!/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 Das2c2219b2021-08-25 17:47:43 +000016"""Generate a set of signature patterns from the modular flags generated by a
Paul Duffin67b9d612021-07-21 17:38:47 +010017bootclasspath_fragment that can be used to select a subset of monolithic flags
18against which the modular flags can be compared.
19"""
20
21import argparse
22import csv
Spandan Das2c2219b2021-08-25 17:47:43 +000023import sys
Paul Duffin67b9d612021-07-21 17:38:47 +010024
Spandan Das2c2219b2021-08-25 17:47:43 +000025def dict_reader(csvfile):
26 return csv.DictReader(
27 csvfile, delimiter=',', quotechar='|', fieldnames=['signature']
28 )
29
Paul Duffin67b9d612021-07-21 17:38:47 +010030
31def produce_patterns_from_file(file):
32 with open(file, 'r') as f:
33 return produce_patterns_from_stream(f)
34
Spandan Das2c2219b2021-08-25 17:47:43 +000035
Paul Duffin67b9d612021-07-21 17:38:47 +010036def produce_patterns_from_stream(stream):
Paul Duffin6ffdff82021-08-09 13:47:19 +010037 # Read in all the signatures into a list and remove member names.
38 patterns = set()
39 for row in dict_reader(stream):
Paul Duffin67b9d612021-07-21 17:38:47 +010040 signature = row['signature']
Paul Duffin6ffdff82021-08-09 13:47:19 +010041 text = signature.removeprefix("L")
42 # Remove the class specific member signature
43 pieces = text.split(";->")
44 qualifiedClassName = pieces[0]
Spandan Das2c2219b2021-08-25 17:47:43 +000045 # Remove inner class names as they cannot be separated
46 # from the containing outer class.
Paul Duffin6ffdff82021-08-09 13:47:19 +010047 pieces = qualifiedClassName.split("$", maxsplit=1)
48 pattern = pieces[0]
49 patterns.add(pattern)
50
Spandan Das2c2219b2021-08-25 17:47:43 +000051 patterns = list(patterns) #pylint: disable=redefined-variable-type
Paul Duffin6ffdff82021-08-09 13:47:19 +010052 patterns.sort()
Paul Duffin67b9d612021-07-21 17:38:47 +010053 return patterns
54
Spandan Das2c2219b2021-08-25 17:47:43 +000055
Paul Duffin67b9d612021-07-21 17:38:47 +010056def main(args):
Spandan Das2c2219b2021-08-25 17:47:43 +000057 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 Duffin67b9d612021-07-21 17:38:47 +010065 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 Das2c2219b2021-08-25 17:47:43 +000077
Paul Duffin67b9d612021-07-21 17:38:47 +010078if __name__ == "__main__":
79 main(sys.argv[1:])