Last active
August 29, 2015 13:56
-
-
Save simpleton/9211964 to your computer and use it in GitHub Desktop.
ManifestMerger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from xml.etree import ElementTree as ET | |
import glob | |
import sys | |
import os | |
class ManifestMerger(object): | |
def __init__(self, app_manifest, filenames): | |
assert len(filenames) > 0, 'No filenames!' | |
ET.register_namespace("android", "http://schemas.android.com/apk/res/android") | |
print app_manifest | |
self.app_root = ET.parse(app_manifest).getroot() | |
self.roots = [ET.parse(f).getroot() for f in filenames if not os.path.samefile(f, app_manifest)] | |
def merge(self): | |
for r in self.roots: | |
self.merge_element(self.app_root, r) | |
return ET.tostring(self.app_root) | |
def merge_element(self, one, other): | |
""" | |
This function recursively merge the xmls | |
""" | |
mapping = {el.tag: el for el in one} | |
for el in other: | |
if len(el) == 0: | |
try: | |
#These two tags didn't merge | |
if (el.tag == "activity" or el.tag == "uses-permission"): | |
one.append(el) | |
except KeyError: | |
mapping[el.tag] = el | |
one.append(el) | |
else: | |
try: | |
self.merge_element(mapping[el.tag], el) | |
except KeyError: | |
mapping[el.tag] = el | |
one.append(el) | |
if __name__ == '__main__': | |
try: | |
main_folder = str(sys.argv[1]) | |
main_manifest = str(sys.argv[2]) | |
except: | |
print """ | |
plz pass the path of trunk and app/AndroidManifest.xml | |
python ManifestMerge.py ./ app/AndroidManifest.xml | |
""" | |
exit(0) | |
r = ManifestMerger(main_folder + main_manifest, glob.glob(main_folder + "**/AndroidManifest.xml")).merge() | |
with open("MergedAndroidManifest.xml", "w") as outfile: | |
outfile.write(r) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment