1#!/usr/bin/env python3
2#  Copyright (C) 2021 The Android Open Source Project
3#
4#  Licensed under the Apache License, Version 2.0 (the "License");
5#  you may not use this file except in compliance with the License.
6#  You may obtain a copy of the License at
7#
8#       http://www.apache.org/licenses/LICENSE-2.0
9#
10#  Unless required by applicable law or agreed to in writing, software
11#  distributed under the License is distributed on an "AS IS" BASIS,
12#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#  See the License for the specific language governing permissions and
14#  limitations under the License.
15
16import argparse
17import sys
18from resource_utils import get_all_resources, get_androidx_resources, Resource
19from datetime import datetime
20import lxml.etree as etree
21if sys.version_info[0] != 3:
22    print("Must use python 3")
23    sys.exit(1)
24
25COPYRIGHT_STR = """ Copyright (C) %s The Android Open Source Project
26Licensed under the Apache License, Version 2.0 (the "License");
27you may not use this file except in compliance with the License.
28You may obtain a copy of the License at
29  http://www.apache.org/licenses/LICENSE-2.0
30Unless required by applicable law or agreed to in writing, software
31distributed under the License is distributed on an "AS IS" BASIS,
32WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33See the License for the specific language governing permissions and
34limitations under the License.""" % (datetime.today().strftime("%Y"))
35
36AUTOGENERATION_NOTICE_STR = """
37THIS FILE WAS AUTO GENERATED, DO NOT EDIT MANUALLY.
38REGENERATE USING packages/apps/Car/tests/tools/rro/generate-overlays.py
39"""
40
41"""
42Script used to update the 'overlayable.xml' file.
43"""
44def main():
45    parser = argparse.ArgumentParser(description="Generate overlayable.xml. This script assumes that all the RRO resources have the exact same name as the app resource they're overlaying.")
46    parser.add_argument('-o', '--outputFile', default='', help='Output file path. If empty, output to stdout')
47    parser.add_argument('-a', '--appResources', help="Path to the app's resource folder. If given, will be used to exclude any rro-specific resources from overlays.xml.")
48    required_args = parser.add_argument_group('Required arguments')
49    required_args.add_argument('-r', '--resourcePath', help="Path to the RRO's resource directory", required=True)
50    args = parser.parse_args()
51
52    resources = get_all_resources(args.resourcePath)
53    try:
54        resources.remove(Resource('overlays', 'xml'))
55    except KeyError:
56        pass
57
58    if args.appResources:
59        resources = resources.intersection(
60            get_all_resources(args.appResources).union(get_androidx_resources()))
61    generate_overlays_file(resources, args.outputFile)
62
63def generate_overlays_file(resources, output_file):
64    resources = sorted(resources, key=lambda x: x.type + x.name)
65    root = etree.Element('overlay')
66    root.addprevious(etree.Comment(COPYRIGHT_STR))
67    root.addprevious(etree.Comment(AUTOGENERATION_NOTICE_STR))
68    for resource in resources:
69        item = etree.SubElement(root, 'item')
70        item.set('target', f'{resource.type}/{resource.name}')
71        item.set('value', f'@{resource.type}/{resource.name}')
72    rootTree = etree.ElementTree(root)
73    if not output_file:
74        print(etree.tostring(rootTree, pretty_print=True, xml_declaration=True).decode())
75    else:
76        with open(output_file, 'wb') as f:
77            rootTree.write(f, pretty_print=True, xml_declaration=True, encoding='utf-8')
78
79if __name__ == '__main__':
80    main()
81