• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, 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/libs/tools/rro/generate-overlayable.py
39"""
40
41"""
42Script used to update the 'overlayable.xml' file.
43"""
44def main():
45    parser = argparse.ArgumentParser(description='Generate overlayable.xml.')
46    optional_args = parser.add_argument_group('optional arguments')
47    optional_args.add_argument('-t', '--policyType', default='system|product|signature', help='Policy type for the overlay - delimited by |')
48    optional_args.add_argument('-e', '--excludeFiles', nargs='*', help='File paths (absolute or relative to cwd) that should be excluded when generating overlayable.xml')
49    optional_args.add_argument('-o', '--outputFile', default='', help='Output file path (absolute or relative to cwd). If empty, output to stdout')
50    required_args = parser.add_argument_group('required arguments')
51    required_args.add_argument('-n', '--targetName', help='Overlayable name for the overlay.', required=True)
52    required_args.add_argument('-r', '--resourcePath', help='Path to resource directory (absolute or relative to cwd)', required=True, action='append')
53    args = parser.parse_args()
54
55    resources = set()
56    for path in args.resourcePath:
57        resources |= get_all_resources(path, args.excludeFiles)
58    generate_overlayable_file(resources, args.targetName, args.policyType, args.outputFile)
59
60def generate_overlayable_file(resources, target_name, policy_type, output_file):
61    resources = sorted(resources, key=lambda x: x.type + x.name)
62    root = etree.Element('resources')
63    root.addprevious(etree.Comment(COPYRIGHT_STR))
64    root.addprevious(etree.Comment(AUTOGENERATION_NOTICE_STR))
65    overlayable = etree.SubElement(root, 'overlayable')
66    overlayable.set('name', target_name)
67    policy = etree.SubElement(overlayable, 'policy')
68    policy.set('type', policy_type)
69    for resource in resources:
70        item = etree.SubElement(policy, 'item')
71        item.set('type', resource.type)
72        item.set('name', resource.name)
73    data = etree.ElementTree(root)
74    if not output_file:
75        print(etree.tostring(data, pretty_print=True, xml_declaration=True).decode())
76    else:
77        with open(output_file, 'wb') as f:
78            data.write(f, pretty_print=True, xml_declaration=True, encoding='utf-8')
79
80if __name__ == '__main__':
81    main()
82