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, remove_layout_resources 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 = remove_layout_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 compare_resources(res_folder, res_public_file): 74 old_mapping = get_resources_from_single_file(res_public_file) 75 76 new_mapping = remove_layout_resources(get_all_resources(res_folder)) 77 78 removed = old_mapping.difference(new_mapping) 79 added = new_mapping.difference(old_mapping) 80 if len(removed) > 0: 81 print('Resources removed:\n' + '\n'.join(map(lambda x: str(x), removed))) 82 if len(added) > 0: 83 print('Resources added:\n' + '\n'.join(map(lambda x: str(x), added))) 84 85 if len(added) + len(removed) > 0: 86 print("Some resource have been modified. If this is intentional please " + 87 "run 'python auto-generate-resources.py' again and submit the new current.xml") 88 sys.exit(1) 89 90if __name__ == '__main__': 91 main() 92