Add manifest_check tool
Add a tool that can check that the <uses-library> tags in an
AndroidManifest.xml file match a list provided by the build.
Bug: 132357300
Test: manifest_check_test
Change-Id: If15abf792282bef677469595e80f19923b87ab62
diff --git a/scripts/manifest_fixer.py b/scripts/manifest_fixer.py
index 83868e6..bb14851 100755
--- a/scripts/manifest_fixer.py
+++ b/scripts/manifest_fixer.py
@@ -17,30 +17,20 @@
"""A tool for inserting values from the build system into a manifest."""
from __future__ import print_function
+
import argparse
import sys
from xml.dom import minidom
-android_ns = 'http://schemas.android.com/apk/res/android'
-
-
-def get_children_with_tag(parent, tag_name):
- children = []
- for child in parent.childNodes:
- if child.nodeType == minidom.Node.ELEMENT_NODE and \
- child.tagName == tag_name:
- children.append(child)
- return children
-
-
-def find_child_with_attribute(element, tag_name, namespace_uri,
- attr_name, value):
- for child in get_children_with_tag(element, tag_name):
- attr = child.getAttributeNodeNS(namespace_uri, attr_name)
- if attr is not None and attr.value == value:
- return child
- return None
+from manifest import android_ns
+from manifest import compare_version_gt
+from manifest import ensure_manifest_android_ns
+from manifest import find_child_with_attribute
+from manifest import get_children_with_tag
+from manifest import get_indent
+from manifest import parse_manifest
+from manifest import write_xml
def parse_args():
@@ -74,76 +64,6 @@
return parser.parse_args()
-def parse_manifest(doc):
- """Get the manifest element."""
-
- manifest = doc.documentElement
- if manifest.tagName != 'manifest':
- raise RuntimeError('expected manifest tag at root')
- return manifest
-
-
-def ensure_manifest_android_ns(doc):
- """Make sure the manifest tag defines the android namespace."""
-
- manifest = parse_manifest(doc)
-
- ns = manifest.getAttributeNodeNS(minidom.XMLNS_NAMESPACE, 'android')
- if ns is None:
- attr = doc.createAttributeNS(minidom.XMLNS_NAMESPACE, 'xmlns:android')
- attr.value = android_ns
- manifest.setAttributeNode(attr)
- elif ns.value != android_ns:
- raise RuntimeError('manifest tag has incorrect android namespace ' +
- ns.value)
-
-
-def as_int(s):
- try:
- i = int(s)
- except ValueError:
- return s, False
- return i, True
-
-
-def compare_version_gt(a, b):
- """Compare two SDK versions.
-
- Compares a and b, treating codenames like 'Q' as higher
- than numerical versions like '28'.
-
- Returns True if a > b
-
- Args:
- a: value to compare
- b: value to compare
- Returns:
- True if a is a higher version than b
- """
-
- a, a_is_int = as_int(a.upper())
- b, b_is_int = as_int(b.upper())
-
- if a_is_int == b_is_int:
- # Both are codenames or both are versions, compare directly
- return a > b
- else:
- # One is a codename, the other is not. Return true if
- # b is an integer version
- return b_is_int
-
-
-def get_indent(element, default_level):
- indent = ''
- if element is not None and element.nodeType == minidom.Node.TEXT_NODE:
- text = element.nodeValue
- indent = text[:len(text)-len(text.lstrip())]
- if not indent or indent == '\n':
- # 1 indent = 4 space
- indent = '\n' + (' ' * default_level * 4)
- return indent
-
-
def raise_min_sdk_version(doc, min_sdk_version, target_sdk_version, library):
"""Ensure the manifest contains a <uses-sdk> tag with a minSdkVersion.
@@ -151,6 +71,7 @@
doc: The XML document. May be modified by this function.
min_sdk_version: The requested minSdkVersion attribute.
target_sdk_version: The requested targetSdkVersion attribute.
+ library: True if the manifest is for a library.
Raises:
RuntimeError: invalid manifest
"""
@@ -249,6 +170,7 @@
indent = get_indent(application.previousSibling, 1)
application.appendChild(doc.createTextNode(indent))
+
def add_uses_non_sdk_api(doc):
"""Add android:usesNonSdkApi=true attribute to <application>.
@@ -323,12 +245,6 @@
(attr.value, value))
-def write_xml(f, doc):
- f.write('<?xml version="1.0" encoding="utf-8"?>\n')
- for node in doc.childNodes:
- f.write(node.toxml(encoding='utf-8') + '\n')
-
-
def main():
"""Program entry point."""
try: