diff options
author | Baligh Uddin <baligh@google.com> | 2020-02-19 21:39:33 -0800 |
---|---|---|
committer | Baligh Uddin <baligh@google.com> | 2020-02-21 08:37:58 -0800 |
commit | 68f16e5d2365f617dc54ffc1b62d6836c3eb71b3 (patch) | |
tree | e44a3ae21aeba9ab9e0b6d05598bfe0a31ed2044 | |
parent | a1c48d8d0938c49a53657388e53ac977b199c23f (diff) |
Add --logging_parent flag to apexer.
BUG: 148198056
Change-Id: I626d80dbe34ab8f0f3205d65a1df24efe76af009
-rw-r--r-- | apexer/Android.bp | 3 | ||||
-rw-r--r-- | apexer/apexer.py | 64 |
2 files changed, 66 insertions, 1 deletions
diff --git a/apexer/Android.bp b/apexer/Android.bp index 6f575fe..1b8a8a6 100644 --- a/apexer/Android.bp +++ b/apexer/Android.bp @@ -63,6 +63,7 @@ python_binary_host { libs: [ "apex_manifest", "apex_build_info_proto", + "manifest_utils", ], required: apexer_tools, } @@ -103,4 +104,4 @@ apex_test { ignore_system_library_special_case: true, key: "com.android.support.apexer.key", binaries: apexer_tools, -}
\ No newline at end of file +} 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 @@ -57,6 +64,10 @@ def ParseArgs(argv): 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 <meta-data> 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 <meta-data> 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 <application> 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')) |