Fix manifest_fixer.py warnings

Fix a few warnings showing up in manifest_*.py

Test: manifest_fixer_test.py
Test: manifest_check_test.py
Flag: EXEMPT refactor
Change-Id: I144100d6d4aa7f5dff7eec79b4bd2d9d5da52153
This commit is contained in:
Colin Cross
2024-09-11 11:34:35 -07:00
parent aa8ceca851
commit 6cb462b38c
5 changed files with 74 additions and 68 deletions

View File

@@ -25,7 +25,7 @@ android_ns = 'http://schemas.android.com/apk/res/android'
def get_children_with_tag(parent, tag_name):
children = []
for child in parent.childNodes:
for child in parent.childNodes:
if child.nodeType == minidom.Node.ELEMENT_NODE and \
child.tagName == tag_name:
children.append(child)

View File

@@ -122,7 +122,7 @@ def enforce_uses_libraries(manifest, required, optional, missing_optional, relax
# handles module names specified in Android.bp properties. However not all
# <uses-library> entries in the manifest correspond to real modules: some of
# the optional libraries may be missing at build time. Therefor this script
# accepts raw module names as spelled in Android.bp/Amdroid.mk and trims the
# accepts raw module names as spelled in Android.bp/Android.mk and trims the
# optional namespace part manually.
required = trim_namespace_parts(required)
optional = trim_namespace_parts(optional)
@@ -266,7 +266,7 @@ def extract_target_sdk_version(manifest, is_apk=False):
manifest: manifest (either parsed XML or aapt dump of APK)
is_apk: if the manifest comes from an APK or an XML file
"""
if is_apk: #pylint: disable=no-else-return
if is_apk: #pylint: disable=no-else-return
return extract_target_sdk_version_apk(manifest)
else:
return extract_target_sdk_version_xml(manifest)
@@ -376,7 +376,7 @@ def main():
# Create a status file that is empty on success, or contains an
# error message on failure. When exceptions are suppressed,
# dexpreopt command command will check file size to determine if
# dexpreopt command will check file size to determine if
# the check has failed.
if args.enforce_uses_libraries_status:
with open(args.enforce_uses_libraries_status, 'w') as f:
@@ -386,7 +386,7 @@ def main():
if args.extract_target_sdk_version:
try:
print(extract_target_sdk_version(manifest, is_apk))
except: #pylint: disable=bare-except
except: #pylint: disable=bare-except
# Failed; don't crash, return "any" SDK version. This will
# result in dexpreopt not adding any compatibility libraries.
print(10000)

View File

@@ -44,8 +44,8 @@ def required_apk(value):
class EnforceUsesLibrariesTest(unittest.TestCase):
"""Unit tests for add_extract_native_libs function."""
def run_test(self, xml, apk, uses_libraries=[], optional_uses_libraries=[],
missing_optional_uses_libraries=[]): #pylint: disable=dangerous-default-value
def run_test(self, xml, apk, uses_libraries=(), optional_uses_libraries=(),
missing_optional_uses_libraries=()): #pylint: disable=dangerous-default-value
doc = minidom.parseString(xml)
try:
relax = False
@@ -114,14 +114,14 @@ class EnforceUsesLibrariesTest(unittest.TestCase):
self.assertFalse(matches)
def test_missing_uses_library(self):
xml = self.xml_tmpl % ('')
apk = self.apk_tmpl % ('')
xml = self.xml_tmpl % ''
apk = self.apk_tmpl % ''
matches = self.run_test(xml, apk, uses_libraries=['foo'])
self.assertFalse(matches)
def test_missing_optional_uses_library(self):
xml = self.xml_tmpl % ('')
apk = self.apk_tmpl % ('')
xml = self.xml_tmpl % ''
apk = self.apk_tmpl % ''
matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
self.assertFalse(matches)
@@ -256,12 +256,12 @@ class ExtractTargetSdkVersionTest(unittest.TestCase):
"targetSdkVersion:'%s'\n"
"uses-permission: name='android.permission.ACCESS_NETWORK_STATE'\n")
def test_targert_sdk_version_28(self):
def test_target_sdk_version_28(self):
xml = self.xml_tmpl % '28'
apk = self.apk_tmpl % '28'
self.run_test(xml, apk, '28')
def test_targert_sdk_version_29(self):
def test_target_sdk_version_29(self):
xml = self.xml_tmpl % '29'
apk = self.apk_tmpl % '29'
self.run_test(xml, apk, '29')

View File

@@ -48,9 +48,9 @@ def parse_args():
parser.add_argument('--library', dest='library', action='store_true',
help='manifest is for a static library')
parser.add_argument('--uses-library', dest='uses_libraries', action='append',
help='specify additional <uses-library> tag to add. android:requred is set to true')
help='specify additional <uses-library> tag to add. android:required is set to true')
parser.add_argument('--optional-uses-library', dest='optional_uses_libraries', action='append',
help='specify additional <uses-library> tag to add. android:requred is set to false')
help='specify additional <uses-library> tag to add. android:required is set to false')
parser.add_argument('--uses-non-sdk-api', dest='uses_non_sdk_api', action='store_true',
help='manifest is for a package built against the platform')
parser.add_argument('--logging-parent', dest='logging_parent', default='',
@@ -125,7 +125,7 @@ def raise_min_sdk_version(doc, min_sdk_version, target_sdk_version, library):
if library:
# TODO(b/117122200): libraries shouldn't set targetSdkVersion at all, but
# ManifestMerger treats minSdkVersion="Q" as targetSdkVersion="Q" if it
# is empty. Set it to something low so that it will be overriden by the
# is empty. Set it to something low so that it will be overridden by the
# main manifest, but high enough that it doesn't cause implicit
# permissions grants.
target_attr.value = '16'
@@ -325,6 +325,7 @@ def set_has_code_to_false(doc):
attr.value = 'false'
application.setAttributeNode(attr)
def set_test_only_flag_to_true(doc):
manifest = parse_manifest(doc)
elems = get_children_with_tag(manifest, 'application')
@@ -346,6 +347,7 @@ def set_test_only_flag_to_true(doc):
attr.value = 'true'
application.setAttributeNode(attr)
def set_max_sdk_version(doc, max_sdk_version):
"""Replace the maxSdkVersion attribute value for permission and
uses-permission tags if the value was originally set to 'current'.
@@ -364,6 +366,7 @@ def set_max_sdk_version(doc, max_sdk_version):
if max_attr and max_attr.value == 'current':
max_attr.value = max_sdk_version
def override_placeholder_version(doc, new_version):
"""Replace the versionCode attribute value if it\'s currently
set to the placeholder version of 0.
@@ -374,9 +377,10 @@ def override_placeholder_version(doc, new_version):
"""
manifest = parse_manifest(doc)
version = manifest.getAttribute("android:versionCode")
if (version == '0'):
if version == '0':
manifest.setAttribute("android:versionCode", new_version)
def main():
"""Program entry point."""
try:
@@ -427,5 +431,6 @@ def main():
print('error: ' + str(err), file=sys.stderr)
sys.exit(-1)
if __name__ == '__main__':
main()

View File

@@ -20,12 +20,13 @@ import io
import sys
import unittest
from xml.dom import minidom
import xml.etree.ElementTree as ET
import xml.etree.ElementTree as ElementTree
import manifest_fixer
sys.dont_write_bytecode = True
class CompareVersionGtTest(unittest.TestCase):
"""Unit tests for compare_version_gt function."""
@@ -69,25 +70,24 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
'%s'
'</manifest>\n')
# pylint: disable=redefined-builtin
def uses_sdk(self, min=None, target=None, extra=''):
def uses_sdk(self, min_sdk=None, target_sdk=None, extra=''):
attrs = ''
if min:
attrs += ' android:minSdkVersion="%s"' % (min)
if target:
attrs += ' android:targetSdkVersion="%s"' % (target)
if min_sdk:
attrs += ' android:minSdkVersion="%s"' % min_sdk
if target_sdk:
attrs += ' android:targetSdkVersion="%s"' % target_sdk
if extra:
attrs += ' ' + extra
return ' <uses-sdk%s/>\n' % (attrs)
return ' <uses-sdk%s/>\n' % attrs
def assert_xml_equal(self, output, expected):
self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def test_no_uses_sdk(self):
"""Tests inserting a uses-sdk element into a manifest."""
manifest_input = self.manifest_tmpl % ''
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='28')
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assert_xml_equal(output, expected)
@@ -95,7 +95,7 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
"""Tests inserting a minSdkVersion attribute into a uses-sdk element."""
manifest_input = self.manifest_tmpl % ' <uses-sdk extra="foo"/>\n'
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28',
expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='28',
extra='extra="foo"')
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assert_xml_equal(output, expected)
@@ -103,64 +103,64 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
def test_raise_min(self):
"""Tests inserting a minSdkVersion attribute into a uses-sdk element."""
manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='27')
expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='28')
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assert_xml_equal(output, expected)
def test_raise(self):
"""Tests raising a minSdkVersion attribute."""
manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='27')
expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='28')
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assert_xml_equal(output, expected)
def test_no_raise_min(self):
"""Tests a minSdkVersion that doesn't need raising."""
manifest_input = self.manifest_tmpl % self.uses_sdk(min='28')
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='28')
expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='27')
output = self.raise_min_sdk_version_test(manifest_input, '27', '27', False)
self.assert_xml_equal(output, expected)
def test_raise_codename(self):
"""Tests raising a minSdkVersion attribute to a codename."""
manifest_input = self.manifest_tmpl % self.uses_sdk(min='28')
expected = self.manifest_tmpl % self.uses_sdk(min='P', target='P')
manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='28')
expected = self.manifest_tmpl % self.uses_sdk(min_sdk='P', target_sdk='P')
output = self.raise_min_sdk_version_test(manifest_input, 'P', 'P', False)
self.assert_xml_equal(output, expected)
def test_no_raise_codename(self):
"""Tests a minSdkVersion codename that doesn't need raising."""
manifest_input = self.manifest_tmpl % self.uses_sdk(min='P')
expected = self.manifest_tmpl % self.uses_sdk(min='P', target='28')
manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='P')
expected = self.manifest_tmpl % self.uses_sdk(min_sdk='P', target_sdk='28')
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assert_xml_equal(output, expected)
def test_target(self):
"""Tests an existing targetSdkVersion is preserved."""
manifest_input = self.manifest_tmpl % self.uses_sdk(min='26', target='27')
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='26', target_sdk='27')
expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='27')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
self.assert_xml_equal(output, expected)
def test_no_target(self):
"""Tests inserting targetSdkVersion when minSdkVersion exists."""
manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='29')
manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='27')
expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='29')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
self.assert_xml_equal(output, expected)
def test_target_no_min(self):
""""Tests inserting targetSdkVersion when minSdkVersion exists."""
manifest_input = self.manifest_tmpl % self.uses_sdk(target='27')
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
manifest_input = self.manifest_tmpl % self.uses_sdk(target_sdk='27')
expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='27')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
self.assert_xml_equal(output, expected)
@@ -168,23 +168,23 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
"""Tests inserting targetSdkVersion when minSdkVersion does not exist."""
manifest_input = self.manifest_tmpl % ''
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='29')
expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='29')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
self.assert_xml_equal(output, expected)
def test_library_no_target(self):
"""Tests inserting targetSdkVersion when minSdkVersion exists."""
manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='16')
manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='27')
expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='16')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True)
self.assert_xml_equal(output, expected)
def test_library_target_no_min(self):
"""Tests inserting targetSdkVersion when minSdkVersion exists."""
manifest_input = self.manifest_tmpl % self.uses_sdk(target='27')
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
manifest_input = self.manifest_tmpl % self.uses_sdk(target_sdk='27')
expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='27')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True)
self.assert_xml_equal(output, expected)
@@ -192,7 +192,7 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
"""Tests inserting targetSdkVersion when minSdkVersion does not exist."""
manifest_input = self.manifest_tmpl % ''
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='16')
expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='16')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True)
self.assert_xml_equal(output, expected)
@@ -233,7 +233,7 @@ class AddLoggingParentTest(unittest.TestCase):
"""Unit tests for add_logging_parent function."""
def assert_xml_equal(self, output, expected):
self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def add_logging_parent_test(self, input_manifest, logging_parent=None):
doc = minidom.parseString(input_manifest)
@@ -253,8 +253,8 @@ class AddLoggingParentTest(unittest.TestCase):
attrs = ''
if logging_parent:
meta_text = ('<meta-data android:name="android.content.pm.LOGGING_PARENT" '
'android:value="%s"/>\n') % (logging_parent)
attrs += ' <application>\n %s </application>\n' % (meta_text)
'android:value="%s"/>\n') % logging_parent
attrs += ' <application>\n %s </application>\n' % meta_text
return attrs
@@ -277,7 +277,7 @@ class AddUsesLibrariesTest(unittest.TestCase):
"""Unit tests for add_uses_libraries function."""
def assert_xml_equal(self, output, expected):
self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest, new_uses_libraries):
doc = minidom.parseString(input_manifest)
@@ -366,7 +366,7 @@ class AddUsesNonSdkApiTest(unittest.TestCase):
"""Unit tests for add_uses_libraries function."""
def assert_xml_equal(self, output, expected):
self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest):
doc = minidom.parseString(input_manifest)
@@ -403,7 +403,7 @@ class UseEmbeddedDexTest(unittest.TestCase):
"""Unit tests for add_use_embedded_dex function."""
def assert_xml_equal(self, output, expected):
self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest):
doc = minidom.parseString(input_manifest)
@@ -442,7 +442,7 @@ class AddExtractNativeLibsTest(unittest.TestCase):
"""Unit tests for add_extract_native_libs function."""
def assert_xml_equal(self, output, expected):
self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest, value):
doc = minidom.parseString(input_manifest)
@@ -487,7 +487,7 @@ class AddNoCodeApplicationTest(unittest.TestCase):
"""Unit tests for set_has_code_to_false function."""
def assert_xml_equal(self, output, expected):
self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest):
doc = minidom.parseString(input_manifest)
@@ -515,7 +515,7 @@ class AddNoCodeApplicationTest(unittest.TestCase):
self.assert_xml_equal(output, expected)
def test_has_application_has_code_false(self):
""" Do nothing if there's already an application elemeent. """
""" Do nothing if there's already an application element. """
manifest_input = self.manifest_tmpl % ' <application android:hasCode="false"/>\n'
output = self.run_test(manifest_input)
self.assert_xml_equal(output, manifest_input)
@@ -532,7 +532,7 @@ class AddTestOnlyApplicationTest(unittest.TestCase):
"""Unit tests for set_test_only_flag_to_true function."""
def assert_xml_equal(self, output, expected):
self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest):
doc = minidom.parseString(input_manifest)
@@ -576,7 +576,7 @@ class SetMaxSdkVersionTest(unittest.TestCase):
"""Unit tests for set_max_sdk_version function."""
def assert_xml_equal(self, output, expected):
self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest, max_sdk_version):
doc = minidom.parseString(input_manifest)
@@ -591,15 +591,15 @@ class SetMaxSdkVersionTest(unittest.TestCase):
'%s'
'</manifest>\n')
def permission(self, max=None):
if max is None:
def permission(self, max_sdk=None):
if max_sdk is None:
return ' <permission/>'
return ' <permission android:maxSdkVersion="%s"/>\n' % max
return ' <permission android:maxSdkVersion="%s"/>\n' % max_sdk
def uses_permission(self, max=None):
if max is None:
def uses_permission(self, max_sdk=None):
if max_sdk is None:
return ' <uses-permission/>'
return ' <uses-permission android:maxSdkVersion="%s"/>\n' % max
return ' <uses-permission android:maxSdkVersion="%s"/>\n' % max_sdk
def test_permission_no_max_sdk_version(self):
"""Tests if permission has no maxSdkVersion attribute"""
@@ -643,11 +643,12 @@ class SetMaxSdkVersionTest(unittest.TestCase):
output = self.run_test(manifest_input, '9000')
self.assert_xml_equal(output, expected)
class OverrideDefaultVersionTest(unittest.TestCase):
"""Unit tests for override_default_version function."""
def assert_xml_equal(self, output, expected):
self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest, version):
doc = minidom.parseString(input_manifest)