xref: /aosp_15_r20/prebuilts/sdk/current/aaos-libs/remove_overlayable.py (revision 344a7f5ef16c479e7a7f54ee6567a9d112f9e72b)
1#!/usr/bin/env python3
2#
3# Copyright 2024, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16from argparse import ArgumentParser as AP
17from zipfile import ZipFile, ZIP_DEFLATED
18import xml.etree.ElementTree as ET
19import re
20
21# Finds the <overlayable> element in the xml file and removes it
22# Returns the pruned XML tree as a String
23def remove_overlayables(file):
24    values = ET.parse(file)
25    resources = values.getroot()
26    overlayable = resources.find('overlayable')
27    if overlayable is not None:
28        resources.remove(overlayable)
29    return ET.tostring(resources, encoding='unicode', xml_declaration=True)
30
31def main():
32    parser = AP(description='This tool takes an AAR and removes the <overlayable> resources')
33    parser.add_argument('output')
34    parser.add_argument('soong_aar')
35    args = parser.parse_args()
36    values_path = 'res/values/values.xml'
37    overlayables_path = 'res\/values\/overlayable[0-9].xml'
38    print("input aar: " + args.soong_aar)
39
40    with ZipFile(args.output, mode='w', compression=ZIP_DEFLATED) as outaar, ZipFile(args.soong_aar) as soongaar:
41        for f in soongaar.namelist():
42            if f == values_path or re.search(overlayables_path, f):
43                print("Found resource file " + f)
44                try:
45                    file = soongaar.open(f)
46                    xml = remove_overlayables(file)
47                    outaar.writestr(f, xml)
48                except KeyError:
49                    print("Could not find overlayables in " + f)
50            else:
51                outaar.writestr(f, soongaar.read(f))
52
53if __name__ == "__main__":
54    main()
55