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