Cleanup signature_patterns*.py
Fix issues reported by pylint, Intellij Python checks and also try and adhere to the Google Python Style Guide. Bug: 194063708 Test: atest signature_patterns_test m out/soong/hiddenapi/hiddenapi-flags.csv /usr/bin/pylint --rcfile $ANDROID_BUILD_TOP/tools/repohooks/tools/pylintrc scripts/hiddenapi/signature_patterns*.py pyformat -s 4 --force_quote_type single -i scripts/hiddenapi/signature_patterns*.py Change-Id: I64d64e9cb269f58d65f4e10ec2f0e874154919e6
This commit is contained in:
@@ -25,82 +25,95 @@ import csv
|
||||
import sys
|
||||
|
||||
|
||||
def dict_reader(csvfile):
|
||||
def dict_reader(csv_file):
|
||||
return csv.DictReader(
|
||||
csvfile, delimiter=',', quotechar='|', fieldnames=['signature'])
|
||||
csv_file, delimiter=',', quotechar='|', fieldnames=['signature'])
|
||||
|
||||
|
||||
def dotPackageToSlashPackage(pkg):
|
||||
def dot_package_to_slash_package(pkg):
|
||||
return pkg.replace('.', '/')
|
||||
|
||||
|
||||
def slashPackageToDotPackage(pkg):
|
||||
def dot_packages_to_slash_packages(pkgs):
|
||||
return [dot_package_to_slash_package(p) for p in pkgs]
|
||||
|
||||
|
||||
def slash_package_to_dot_package(pkg):
|
||||
return pkg.replace('/', '.')
|
||||
|
||||
|
||||
def isSplitPackage(splitPackages, pkg):
|
||||
return splitPackages and (pkg in splitPackages or '*' in splitPackages)
|
||||
def slash_packages_to_dot_packages(pkgs):
|
||||
return [slash_package_to_dot_package(p) for p in pkgs]
|
||||
|
||||
|
||||
def matchedByPackagePrefixPattern(packagePrefixes, prefix):
|
||||
for packagePrefix in packagePrefixes:
|
||||
def is_split_package(split_packages, pkg):
|
||||
return split_packages and (pkg in split_packages or '*' in split_packages)
|
||||
|
||||
|
||||
def matched_by_package_prefix_pattern(package_prefixes, prefix):
|
||||
for packagePrefix in package_prefixes:
|
||||
if prefix == packagePrefix:
|
||||
return packagePrefix
|
||||
elif prefix.startswith(packagePrefix) and prefix[len(
|
||||
packagePrefix)] == '/':
|
||||
if (prefix.startswith(packagePrefix) and
|
||||
prefix[len(packagePrefix)] == '/'):
|
||||
return packagePrefix
|
||||
return False
|
||||
|
||||
|
||||
def validate_package_prefixes(splitPackages, packagePrefixes):
|
||||
def validate_package_prefixes(split_packages, package_prefixes):
|
||||
# If there are no package prefixes then there is no possible conflict
|
||||
# between them and the split packages.
|
||||
if len(packagePrefixes) == 0:
|
||||
return
|
||||
if len(package_prefixes) == 0:
|
||||
return []
|
||||
|
||||
# Check to make sure that the split packages and package prefixes do not
|
||||
# overlap.
|
||||
errors = []
|
||||
for splitPackage in splitPackages:
|
||||
if splitPackage == '*':
|
||||
for split_package in split_packages:
|
||||
if split_package == '*':
|
||||
# A package prefix matches a split package.
|
||||
packagePrefixesForOutput = ', '.join(
|
||||
map(slashPackageToDotPackage, packagePrefixes))
|
||||
package_prefixes_for_output = ', '.join(
|
||||
slash_packages_to_dot_packages(package_prefixes))
|
||||
errors.append(
|
||||
'split package "*" conflicts with all package prefixes %s\n'
|
||||
' add split_packages:[] to fix' % packagePrefixesForOutput)
|
||||
"split package '*' conflicts with all package prefixes "
|
||||
f'{package_prefixes_for_output}\n'
|
||||
' add split_packages:[] to fix')
|
||||
else:
|
||||
packagePrefix = matchedByPackagePrefixPattern(
|
||||
packagePrefixes, splitPackage)
|
||||
if packagePrefix:
|
||||
package_prefix = matched_by_package_prefix_pattern(
|
||||
package_prefixes, split_package)
|
||||
if package_prefix:
|
||||
# A package prefix matches a split package.
|
||||
splitPackageForOutput = slashPackageToDotPackage(splitPackage)
|
||||
packagePrefixForOutput = slashPackageToDotPackage(packagePrefix)
|
||||
split_package_for_output = slash_package_to_dot_package(
|
||||
split_package)
|
||||
package_prefix_for_output = slash_package_to_dot_package(
|
||||
package_prefix)
|
||||
errors.append(
|
||||
'split package %s is matched by package prefix %s' %
|
||||
(splitPackageForOutput, packagePrefixForOutput))
|
||||
f'split package {split_package_for_output} is matched by '
|
||||
f'package prefix {package_prefix_for_output}')
|
||||
return errors
|
||||
|
||||
|
||||
def validate_split_packages(splitPackages):
|
||||
def validate_split_packages(split_packages):
|
||||
errors = []
|
||||
if '*' in splitPackages and len(splitPackages) > 1:
|
||||
if '*' in split_packages and len(split_packages) > 1:
|
||||
errors.append('split packages are invalid as they contain both the'
|
||||
' wildcard (*) and specific packages, use the wildcard or'
|
||||
' specific packages, not a mixture')
|
||||
return errors
|
||||
|
||||
|
||||
def produce_patterns_from_file(file, splitPackages=None, packagePrefixes=None):
|
||||
with open(file, 'r') as f:
|
||||
return produce_patterns_from_stream(f, splitPackages, packagePrefixes)
|
||||
def produce_patterns_from_file(file,
|
||||
split_packages=None,
|
||||
package_prefixes=None):
|
||||
with open(file, 'r', encoding='utf8') as f:
|
||||
return produce_patterns_from_stream(f, split_packages, package_prefixes)
|
||||
|
||||
|
||||
def produce_patterns_from_stream(stream,
|
||||
splitPackages=None,
|
||||
packagePrefixes=None):
|
||||
splitPackages = set(splitPackages or [])
|
||||
packagePrefixes = list(packagePrefixes or [])
|
||||
split_packages=None,
|
||||
package_prefixes=None):
|
||||
split_packages = set(split_packages or [])
|
||||
package_prefixes = list(package_prefixes or [])
|
||||
# Read in all the signatures into a list and remove any unnecessary class
|
||||
# and member names.
|
||||
patterns = set()
|
||||
@@ -109,8 +122,8 @@ def produce_patterns_from_stream(stream,
|
||||
text = signature.removeprefix('L')
|
||||
# Remove the class specific member signature
|
||||
pieces = text.split(';->')
|
||||
qualifiedClassName = pieces[0]
|
||||
pieces = qualifiedClassName.rsplit('/', maxsplit=1)
|
||||
qualified_class_name = pieces[0]
|
||||
pieces = qualified_class_name.rsplit('/', maxsplit=1)
|
||||
pkg = pieces[0]
|
||||
# If the package is split across multiple modules then it cannot be used
|
||||
# to select the subset of the monolithic flags that this module
|
||||
@@ -121,9 +134,9 @@ def produce_patterns_from_stream(stream,
|
||||
# If the package is not split then every class in the package must be
|
||||
# provided by this module so there is no need to list the classes
|
||||
# explicitly so just use the package name instead.
|
||||
if isSplitPackage(splitPackages, pkg):
|
||||
if is_split_package(split_packages, pkg):
|
||||
# Remove inner class names.
|
||||
pieces = qualifiedClassName.split('$', maxsplit=1)
|
||||
pieces = qualified_class_name.split('$', maxsplit=1)
|
||||
pattern = pieces[0]
|
||||
else:
|
||||
# Add a * to ensure that the pattern matches the classes in that
|
||||
@@ -132,13 +145,14 @@ def produce_patterns_from_stream(stream,
|
||||
patterns.add(pattern)
|
||||
|
||||
# Remove any patterns that would be matched by a package prefix pattern.
|
||||
patterns = list(
|
||||
filter(lambda p: not matchedByPackagePrefixPattern(packagePrefixes, p),
|
||||
patterns))
|
||||
patterns = [
|
||||
p for p in patterns
|
||||
if not matched_by_package_prefix_pattern(package_prefixes, p)
|
||||
]
|
||||
# Add the package prefix patterns to the list. Add a ** to ensure that each
|
||||
# package prefix pattern will match the classes in that package and all
|
||||
# sub-packages.
|
||||
patterns = patterns + list(map(lambda x: x + '/**', packagePrefixes))
|
||||
patterns = patterns + [f'{p}/**' for p in package_prefixes]
|
||||
# Sort the patterns.
|
||||
patterns.sort()
|
||||
return patterns
|
||||
@@ -155,8 +169,8 @@ def main(args):
|
||||
args_parser.add_argument(
|
||||
'--split-package',
|
||||
action='append',
|
||||
help='A package that is split across multiple bootclasspath_fragment modules'
|
||||
)
|
||||
help='A package that is split across multiple bootclasspath_fragment '
|
||||
'modules')
|
||||
args_parser.add_argument(
|
||||
'--package-prefix',
|
||||
action='append',
|
||||
@@ -164,14 +178,14 @@ def main(args):
|
||||
args_parser.add_argument('--output', help='Generated signature prefixes')
|
||||
args = args_parser.parse_args(args)
|
||||
|
||||
splitPackages = set(map(dotPackageToSlashPackage, args.split_package or []))
|
||||
errors = validate_split_packages(splitPackages)
|
||||
split_packages = set(
|
||||
dot_packages_to_slash_packages(args.split_package or []))
|
||||
errors = validate_split_packages(split_packages)
|
||||
|
||||
packagePrefixes = list(
|
||||
map(dotPackageToSlashPackage, args.package_prefix or []))
|
||||
package_prefixes = dot_packages_to_slash_packages(args.package_prefix or [])
|
||||
|
||||
if not errors:
|
||||
errors = validate_package_prefixes(splitPackages, packagePrefixes)
|
||||
errors = validate_package_prefixes(split_packages, package_prefixes)
|
||||
|
||||
if errors:
|
||||
for error in errors:
|
||||
@@ -179,11 +193,11 @@ def main(args):
|
||||
sys.exit(1)
|
||||
|
||||
# Read in all the patterns into a list.
|
||||
patterns = produce_patterns_from_file(args.flags, splitPackages,
|
||||
packagePrefixes)
|
||||
patterns = produce_patterns_from_file(args.flags, split_packages,
|
||||
package_prefixes)
|
||||
|
||||
# Write out all the patterns.
|
||||
with open(args.output, 'w') as outputFile:
|
||||
with open(args.output, 'w', encoding='utf8') as outputFile:
|
||||
for pattern in patterns:
|
||||
outputFile.write(pattern)
|
||||
outputFile.write('\n')
|
||||
|
@@ -17,29 +17,29 @@
|
||||
import io
|
||||
import unittest
|
||||
|
||||
from signature_patterns import * #pylint: disable=unused-wildcard-import,wildcard-import
|
||||
import signature_patterns
|
||||
|
||||
|
||||
class TestGeneratedPatterns(unittest.TestCase):
|
||||
|
||||
csvFlags = """
|
||||
csv_flags = """
|
||||
Ljava/lang/ProcessBuilder$Redirect$1;-><init>()V,blocked
|
||||
Ljava/lang/Character$UnicodeScript;->of(I)Ljava/lang/Character$UnicodeScript;,public-api
|
||||
Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api
|
||||
Ljava/lang/Object;->toString()Ljava/lang/String;,blocked
|
||||
"""
|
||||
|
||||
def produce_patterns_from_string(self,
|
||||
csv,
|
||||
splitPackages=None,
|
||||
packagePrefixes=None):
|
||||
with io.StringIO(csv) as f:
|
||||
return produce_patterns_from_stream(f, splitPackages,
|
||||
packagePrefixes)
|
||||
@staticmethod
|
||||
def produce_patterns_from_string(csv_text,
|
||||
split_packages=None,
|
||||
package_prefixes=None):
|
||||
with io.StringIO(csv_text) as f:
|
||||
return signature_patterns.produce_patterns_from_stream(
|
||||
f, split_packages, package_prefixes)
|
||||
|
||||
def test_generate_default(self):
|
||||
patterns = self.produce_patterns_from_string(
|
||||
TestGeneratedPatterns.csvFlags)
|
||||
TestGeneratedPatterns.csv_flags)
|
||||
expected = [
|
||||
'java/lang/*',
|
||||
]
|
||||
@@ -47,7 +47,7 @@ Ljava/lang/Object;->toString()Ljava/lang/String;,blocked
|
||||
|
||||
def test_generate_split_package(self):
|
||||
patterns = self.produce_patterns_from_string(
|
||||
TestGeneratedPatterns.csvFlags, splitPackages={'java/lang'})
|
||||
TestGeneratedPatterns.csv_flags, split_packages={'java/lang'})
|
||||
expected = [
|
||||
'java/lang/Character',
|
||||
'java/lang/Object',
|
||||
@@ -57,7 +57,7 @@ Ljava/lang/Object;->toString()Ljava/lang/String;,blocked
|
||||
|
||||
def test_generate_split_package_wildcard(self):
|
||||
patterns = self.produce_patterns_from_string(
|
||||
TestGeneratedPatterns.csvFlags, splitPackages={'*'})
|
||||
TestGeneratedPatterns.csv_flags, split_packages={'*'})
|
||||
expected = [
|
||||
'java/lang/Character',
|
||||
'java/lang/Object',
|
||||
@@ -67,7 +67,7 @@ Ljava/lang/Object;->toString()Ljava/lang/String;,blocked
|
||||
|
||||
def test_generate_package_prefix(self):
|
||||
patterns = self.produce_patterns_from_string(
|
||||
TestGeneratedPatterns.csvFlags, packagePrefixes={'java/lang'})
|
||||
TestGeneratedPatterns.csv_flags, package_prefixes={'java/lang'})
|
||||
expected = [
|
||||
'java/lang/**',
|
||||
]
|
||||
@@ -75,14 +75,14 @@ Ljava/lang/Object;->toString()Ljava/lang/String;,blocked
|
||||
|
||||
def test_generate_package_prefix_top_package(self):
|
||||
patterns = self.produce_patterns_from_string(
|
||||
TestGeneratedPatterns.csvFlags, packagePrefixes={'java'})
|
||||
TestGeneratedPatterns.csv_flags, package_prefixes={'java'})
|
||||
expected = [
|
||||
'java/**',
|
||||
]
|
||||
self.assertEqual(expected, patterns)
|
||||
|
||||
def test_split_package_wildcard_conflicts_with_other_split_packages(self):
|
||||
errors = validate_split_packages({'*', 'java'})
|
||||
errors = signature_patterns.validate_split_packages({'*', 'java'})
|
||||
expected = [
|
||||
'split packages are invalid as they contain both the wildcard (*)'
|
||||
' and specific packages, use the wildcard or specific packages,'
|
||||
@@ -91,16 +91,17 @@ Ljava/lang/Object;->toString()Ljava/lang/String;,blocked
|
||||
self.assertEqual(expected, errors)
|
||||
|
||||
def test_split_package_wildcard_conflicts_with_package_prefixes(self):
|
||||
errors = validate_package_prefixes({'*'}, packagePrefixes={'java'})
|
||||
errors = signature_patterns.validate_package_prefixes(
|
||||
{'*'}, package_prefixes={'java'})
|
||||
expected = [
|
||||
'split package "*" conflicts with all package prefixes java\n'
|
||||
"split package '*' conflicts with all package prefixes java\n"
|
||||
' add split_packages:[] to fix',
|
||||
]
|
||||
self.assertEqual(expected, errors)
|
||||
|
||||
def test_split_package_conflict(self):
|
||||
errors = validate_package_prefixes({'java/split'},
|
||||
packagePrefixes={'java'})
|
||||
errors = signature_patterns.validate_package_prefixes(
|
||||
{'java/split'}, package_prefixes={'java'})
|
||||
expected = [
|
||||
'split package java.split is matched by package prefix java',
|
||||
]
|
||||
|
Reference in New Issue
Block a user