1#!/usr/bin/env python 2# 3# Copyright 2019, 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. 16 17import argparse 18import os 19import sys 20from resource_utils import get_all_resources, get_resources_from_single_file, add_resource_to_set, Resource 21from git_utils import has_chassis_changes 22 23# path to 'packages/apps/Car/libs/car-ui-lib/' 24ROOT_FOLDER = os.path.dirname(os.path.abspath(__file__)) + '/../..' 25OUTPUT_FILE_PATH = ROOT_FOLDER + '/tests/apitest/' 26 27""" 28Script used to update the 'current.xml' file. This is being used as part of pre-submits to 29verify whether resources previously exposed to OEMs are being changed by a CL, potentially 30breaking existing customizations. 31 32Example usage: python auto-generate-resources.py current.xml 33""" 34def main(): 35 parser = argparse.ArgumentParser(description='Check if any existing resources are modified.') 36 parser.add_argument('--sha', help='Git hash of current changes. This script will not run if this is provided and there are no chassis changes.') 37 parser.add_argument('-f', '--file', default='current.xml', help='Name of output file.') 38 parser.add_argument('-c', '--compare', action='store_true', 39 help='Pass this flag if resources need to be compared.') 40 args = parser.parse_args() 41 42 if not has_chassis_changes(args.sha): 43 # Don't run because there were no chassis changes 44 return 45 46 output_file = args.file or 'current.xml' 47 if args.compare: 48 compare_resources(ROOT_FOLDER+'/res', OUTPUT_FILE_PATH + 'current.xml') 49 else: 50 generate_current_file(ROOT_FOLDER+'/res', output_file) 51 52def generate_current_file(res_folder, output_file='current.xml'): 53 resources = get_all_resources(res_folder) 54 resources = sorted(resources, key=lambda x: x.type + x.name) 55 56 # defer importing lxml to here so that people who aren't editing chassis don't have to have 57 # lxml installed 58 import lxml.etree as etree 59 60 root = etree.Element('resources') 61 62 root.addprevious(etree.Comment('This file is AUTO GENERATED, DO NOT EDIT MANUALLY.')) 63 for resource in resources: 64 item = etree.SubElement(root, 'public') 65 item.set('type', resource.type) 66 item.set('name', resource.name) 67 68 data = etree.ElementTree(root) 69 70 with open(OUTPUT_FILE_PATH + output_file, 'w') as f: 71 data.write(f, pretty_print=True, xml_declaration=True, encoding='utf-8') 72 73def generate_overlayable_file(res_folder): 74 resources = get_all_resources(res_folder) 75 # We need these to be able to use base layouts in RROs 76 # This should become unnecessary in S 77 add_resource_to_set(resources, Resource('layout_constraintGuide_begin', 'attr')) 78 add_resource_to_set(resources, Resource('layout_constraintGuide_end', 'attr')) 79 add_resource_to_set(resources, Resource('layout_constraintHorizontal_bias', 'attr')) 80 add_resource_to_set(resources, Resource('layout_constraintTop_toTopOf', 'attr')) 81 add_resource_to_set(resources, Resource('layout_constraintTop_toBottomOf', 'attr')) 82 add_resource_to_set(resources, Resource('layout_constraintBottom_toBottomOf', 'attr')) 83 add_resource_to_set(resources, Resource('layout_constraintBottom_toTopOf', 'attr')) 84 add_resource_to_set(resources, Resource('layout_constraintStart_toStartOf', 'attr')) 85 add_resource_to_set(resources, Resource('layout_constraintStart_toEndOf', 'attr')) 86 add_resource_to_set(resources, Resource('layout_constraintEnd_toEndOf', 'attr')) 87 add_resource_to_set(resources, Resource('layout_constraintEnd_toStartOf', 'attr')) 88 add_resource_to_set(resources, Resource('layout_constraintLeft_toLeftOf', 'attr')) 89 add_resource_to_set(resources, Resource('layout_constraintLeft_toRightOf', 'attr')) 90 add_resource_to_set(resources, Resource('layout_constraintRight_toRightOf', 'attr')) 91 add_resource_to_set(resources, Resource('layout_constraintRight_toLeftOf', 'attr')) 92 resources = sorted(resources, key=lambda x: x.type + x.name) 93 94 # defer importing lxml to here so that people who aren't editing chassis don't have to have 95 # lxml installed 96 import lxml.etree as etree 97 98 root = etree.Element('resources') 99 100 root.addprevious(etree.Comment(' Copyright (C) 2020 The Android Open Source Project\n\n' + 101 102 ' Licensed under the Apache License, Version 2.0 (the "License");\n' + 103 ' you may not use this file except in compliance with the License.\n' + 104 ' You may obtain a copy of the License at\n\n' + 105 106 ' http://www.apache.org/licenses/LICENSE-2.0\n\n' 107 108 ' Unless required by applicable law or agreed to in writing, software\n' 109 ' distributed under the License is distributed on an "AS IS" BASIS,\n' 110 ' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n' 111 ' See the License for the specific language governing permissions and\n' 112 ' limitations under the License.\n')) 113 114 overlayable = etree.SubElement(root, 'overlayable') 115 overlayable.set('name', 'CarUiLibOverlayableResources') 116 117 policy = etree.SubElement(overlayable, 'policy') 118 policy.set('type', 'public') 119 120 for resource in resources: 121 item = etree.SubElement(policy, 'item') 122 item.set('type', resource.type) 123 item.set('name', resource.name) 124 125 data = etree.ElementTree(root) 126 127 output_file=ROOT_FOLDER+'/res/values/overlayable.xml' 128 with open(output_file, 'w') as f: 129 data.write(f, pretty_print=True, xml_declaration=True, encoding='utf-8') 130 131def compare_resources(res_folder, res_public_file): 132 old_mapping = get_resources_from_single_file(res_public_file) 133 134 new_mapping = get_all_resources(res_folder) 135 136 removed = old_mapping.difference(new_mapping) 137 added = new_mapping.difference(old_mapping) 138 if len(removed) > 0: 139 print('Resources removed:\n' + '\n'.join(map(lambda x: str(x), removed))) 140 if len(added) > 0: 141 print('Resources added:\n' + '\n'.join(map(lambda x: str(x), added))) 142 143 if len(added) + len(removed) > 0: 144 print("Some resource have been modified. If this is intentional please " + 145 "run 'python auto-generate-resources.py' again and submit the new current.xml") 146 sys.exit(1) 147 148if __name__ == '__main__': 149 main() 150