summaryrefslogtreecommitdiff
path: root/apexer/apexer.py
diff options
context:
space:
mode:
Diffstat (limited to 'apexer/apexer.py')
-rw-r--r--apexer/apexer.py64
1 files changed, 64 insertions, 0 deletions
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'))