1234from pathlib import Path5import sys6import xml.etree.ElementTree as ET78ANDROID_NS = "{http://schemas.android.com/apk/res/android}"9ROOT = Path(__file__).resolve().parents[1]10DEFAULT_INVENTORY = ROOT / "docs" / "android-release-manifest-inventory.txt"11DEFAULT_MERGED = ROOT / "app" / "build" / "intermediates" / "merged_manifest" / "release" / "processReleaseMainManifest" / "AndroidManifest.xml"121314def attrs(node: ET.Element) -> str:15 1617181920 items = sorted(21 (key.rsplit("}", 1)[-1], value) for key, value in node.attrib.items()22 )23 return ";".join(f"{key}={value}" for key, value in items)242526def inventory(path: Path) -> str:27 root = ET.parse(path).getroot()28 lines = []29 for permission in sorted(root.findall("uses-permission"), key=lambda node: node.get(ANDROID_NS + "name", "")):30 lines.append(31 f"permission|{permission.get(ANDROID_NS + 'name', '')}|{attrs(permission)}"32 )33 for feature in sorted(root.findall("uses-feature"), key=lambda node: node.get(ANDROID_NS + "name", "")):34 lines.append(35 f"uses-feature|{feature.get(ANDROID_NS + 'name', '')}|{attrs(feature)}"36 )37 for queries in root.findall("queries"):38 lines.append(f"queries|{attrs(queries)}")39 for child in sorted(queries, key=lambda node: (node.tag, node.get(ANDROID_NS + "name", ""))):40 lines.append(41 f"query|{child.tag}|{child.get(ANDROID_NS + 'name', '')}|{attrs(child)}"42 )43 application = root.find("application")44 if application is None:45 raise SystemExit("release manifest has no application")46 47 48 49 50 51 52 lines.append(f"application|{attrs(application)}")53 for child in sorted(application, key=lambda node: (node.tag, node.get(ANDROID_NS + "name", ""))):54 name = child.get(ANDROID_NS + "name", "")55 56 57 58 lines.append(f"component|{child.tag}|{name}|{attrs(child)}")59 for intent_filter in child.findall("intent-filter"):60 actions = sorted(action.get(ANDROID_NS + "name", "") for action in intent_filter.findall("action"))61 categories = sorted(category.get(ANDROID_NS + "name", "") for category in intent_filter.findall("category"))62 lines.append(f"intent-filter|{child.tag}|{name}|actions={','.join(actions)}|categories={','.join(categories)}")63 return "\n".join(lines) + "\n"646566USAGE = f"usage: {Path(sys.argv[0]).name} check|write [merged-manifest] [inventory]"6768if len(sys.argv) not in (2, 3, 4):69 raise SystemExit(USAGE)70MERGED = Path(sys.argv[2]) if len(sys.argv) > 2 else DEFAULT_MERGED71INVENTORY = Path(sys.argv[3]) if len(sys.argv) > 3 else DEFAULT_INVENTORY7273if not MERGED.is_file():74 raise SystemExit(f"merged release manifest not found: {MERGED}")7576if sys.argv[1] == "write":77 INVENTORY.write_text(inventory(MERGED))78elif sys.argv[1] == "check":79 if not INVENTORY.is_file():80 raise SystemExit(f"checked-in inventory not found: {INVENTORY}")81 actual = inventory(MERGED)82 expected = INVENTORY.read_text()83 if actual != expected:84 raise SystemExit("merged release manifest differs from checked-in inventory")85else:86 raise SystemExit(f"unknown command: {sys.argv[1]}\n{USAGE}")