From 68f16e5d2365f617dc54ffc1b62d6836c3eb71b3 Mon Sep 17 00:00:00 2001 From: Baligh Uddin Date: Wed, 19 Feb 2020 21:39:33 -0800 Subject: Add --logging_parent flag to apexer. BUG: 148198056 Change-Id: I626d80dbe34ab8f0f3205d65a1df24efe76af009 --- apexer/apexer.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'apexer/apexer.py') diff --git a/apexer/apexer.py b/apexer/apexer.py index 9d0997c..aec0a3e 100644 --- a/apexer/apexer.py +++ b/apexer/apexer.py @@ -33,6 +33,13 @@ import uuid import xml.etree.ElementTree as ET from apex_manifest import ValidateApexManifest from apex_manifest import ApexManifestError +from manifest import 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 +from xml.dom import minidom tool_path_list = None BLOCK_SIZE = 4096 @@ -56,6 +63,10 @@ def ParseArgs(argv): '--android_manifest', help='path to the AndroidManifest file. If omitted, a default one is created and used' ) + parser.add_argument( + '--logging_parent', + help=('specify logging parent as an additional tag.' + 'This value is ignored if the logging_parent meta-data tag is present.')) parser.add_argument( '--assets_dir', help='an assets directory to be included in the APEX' @@ -333,6 +344,54 @@ def GenerateBuildInfo(args): return build_info +def AddLoggingParent(android_manifest, logging_parent_value): + """Add logging parent as an additional tag. + + Args: + android_manifest: A string representing AndroidManifest.xml + logging_parent_value: A string representing the logging + parent value. + Raises: + RuntimeError: Invalid manifest + Returns: + A path to modified AndroidManifest.xml + """ + doc = minidom.parse(android_manifest) + manifest = parse_manifest(doc) + logging_parent_key = 'android.content.pm.LOGGING_PARENT' + elems = get_children_with_tag(manifest, 'application') + application = elems[0] if len(elems) == 1 else None + if len(elems) > 1: + raise RuntimeError('found multiple tags') + elif not elems: + application = doc.createElement('application') + indent = get_indent(manifest.firstChild, 1) + first = manifest.firstChild + manifest.insertBefore(doc.createTextNode(indent), first) + manifest.insertBefore(application, first) + + indent = get_indent(application.firstChild, 2) + last = application.lastChild + if last is not None and last.nodeType != minidom.Node.TEXT_NODE: + last = None + + if not find_child_with_attribute(application, 'meta-data', android_ns, + 'name', logging_parent_key): + ul = doc.createElement('meta-data') + ul.setAttributeNS(android_ns, 'android:name', logging_parent_key) + ul.setAttributeNS(android_ns, 'android:value', logging_parent_value) + application.insertBefore(doc.createTextNode(indent), last) + application.insertBefore(ul, last) + last = application.lastChild + + if last and last.nodeType != minidom.Node.TEXT_NODE: + indent = get_indent(application.previousSibling, 1) + application.appendChild(doc.createTextNode(indent)) + + with tempfile.NamedTemporaryFile(delete=False) as temp: + write_xml(temp, doc) + return temp.name + def CreateApex(args, work_dir): if not ValidateArgs(args): return False @@ -496,6 +555,11 @@ def CreateApex(args, work_dir): ValidateAndroidManifest(manifest_apex.name, args.android_manifest) shutil.copyfile(args.android_manifest, android_manifest_file) + # If logging parent is specified, add it to the AndroidManifest. + if args.logging_parent != "": + android_manifest_file = AddLoggingParent(android_manifest_file, + args.logging_parent) + # copy manifest to the content dir so that it is also accessible # without mounting the image copyfile(args.manifest, os.path.join(content_dir, 'apex_manifest.pb')) -- cgit v1.2.3