#!/usr/bin/env python3 """Check the checked-in Android release manifest inventory against merged output.""" from pathlib import Path import sys import xml.etree.ElementTree as ET ANDROID_NS = "{http://schemas.android.com/apk/res/android}" ROOT = Path(__file__).resolve().parents[1] INVENTORY = ROOT / "docs" / "android-release-manifest-inventory.txt" MERGED = ROOT / "app" / "build" / "intermediates" / "merged_manifest" / "release" / "processReleaseMainManifest" / "AndroidManifest.xml" def inventory(path: Path) -> str: root = ET.parse(path).getroot() lines = [] for permission in sorted(root.findall("uses-permission"), key=lambda node: node.get(ANDROID_NS + "name", "")): lines.append(f"permission|{permission.get(ANDROID_NS + 'name', '')}") application = root.find("application") if application is None: raise SystemExit("release manifest has no application") for child in sorted(application, key=lambda node: (node.tag, node.get(ANDROID_NS + "name", ""))): name = child.get(ANDROID_NS + "name", "") exported = child.get(ANDROID_NS + "exported", "") permission = child.get(ANDROID_NS + "permission", "") lines.append(f"component|{child.tag}|{name}|exported={exported}|permission={permission}") for intent_filter in child.findall("intent-filter"): actions = sorted(action.get(ANDROID_NS + "name", "") for action in intent_filter.findall("action")) categories = sorted(category.get(ANDROID_NS + "name", "") for category in intent_filter.findall("category")) lines.append(f"intent-filter|{child.tag}|{name}|actions={','.join(actions)}|categories={','.join(categories)}") return "\n".join(lines) + "\n" if len(sys.argv) != 2: raise SystemExit(f"usage: {Path(sys.argv[0]).name} check|write") if sys.argv[1] == "write": INVENTORY.write_text(inventory(MERGED)) elif sys.argv[1] == "check": actual = inventory(MERGED) expected = INVENTORY.read_text() if actual != expected: raise SystemExit("merged release manifest differs from checked-in inventory") else: raise SystemExit(f"unknown command: {sys.argv[1]}")