1#!/usr/bin/env python 2# 3# Copyright (C) 2022 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 xml.etree.ElementTree as ET 18import datetime 19import os 20import re 21 22# This script automatically generates public.xml and strings.xml for ota sample app. 23# This is used if we want to update the testing ARSC file in MDD server for tests. 24# This creates test strings that have an extra exclamation mark (!). 25 26ADSERVICES_PUBLIC_RES_FILE = '../apk/publicres/values/public.xml' 27OTA_PUBLIC_RES_FILE = '../samples/ota/res/values/public.xml' 28ADSERVICES_STRING_FILE = '../apk/res/values/strings.xml' 29OTA_STRING_FILE = '../samples/ota/res/values/strings.xml' 30COPYRIGHT_TEXT = f'''<?xml version="1.0" encoding="utf-8"?> 31<!-- Copyright (C) {datetime.date.today().year} The Android Open Source Project 32 33 Licensed under the Apache License, Version 2.0 (the "License"); 34 you may not use this file except in compliance with the License. 35 You may obtain a copy of the License at 36 37 http://www.apache.org/licenses/LICENSE-2.0 38 39 Unless required by applicable law or agreed to in writing, software 40 distributed under the License is distributed on an "AS IS" BASIS, 41 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 42 See the License for the specific language governing permissions and 43 limitations under the License. 44--> 45''' 46 47adservices_public_dict = {} 48adservices_string_dict = {} 49adservices_public_xml = ET.parse(ADSERVICES_PUBLIC_RES_FILE).getroot() 50adservices_strings_xml = ET.parse(ADSERVICES_STRING_FILE).getroot() 51for x in adservices_public_xml: 52 adservices_public_dict[x.attrib['name']] = x.attrib['id'] 53for x in adservices_strings_xml: 54 adservices_string_dict[x.attrib['name']] = x.text 55 56strings_xml = ET.Element('resources') 57strings_xml.set('xmlns:xliff', 'urn:oasis:names:tc:xliff:document:1.2') 58public_xml = ET.Element('resources') 59for x in adservices_strings_xml: 60 cur_element = ET.SubElement(strings_xml, 'string') 61 cur_element.set('name', x.attrib['name']) 62 x_str = ET.tostring(x, encoding="unicode") 63 if 'xliff:document' in x_str: 64 # Has special substitution in string 65 cur_text = re.search(r'>(.*?)</string>', x_str).group(1) 66 new_str = cur_text.replace('ns0:g','xliff:g') 67 elif '@string/' in x.text: 68 new_str = adservices_string_dict[x.text[8:]] 69 else: 70 new_str = x.text 71 new_str += "!" 72 cur_element.text = new_str 73 74 cur_element = ET.SubElement(public_xml, 'public') 75 cur_element.set('type', 'string') 76 cur_element.set('name', x.attrib['name']) 77 if x.attrib['name'] not in adservices_public_dict: 78 print(f"ERROR: {x.attrib['name']} is not in adservices strings.xml") 79 exit(0) 80 cur_element.set('id', adservices_public_dict[x.attrib['name']]) 81 82ET.indent(strings_xml, space=' ') 83if os.path.exists(OTA_STRING_FILE): 84 os.remove(OTA_STRING_FILE) 85with open(OTA_STRING_FILE, 'w') as f: 86 f.write(COPYRIGHT_TEXT) 87 s = ET.tostring(strings_xml, encoding="unicode") 88 s = s.replace('<', '<') 89 s = s.replace('>', '>') 90 f.write(s) 91 92ET.indent(public_xml, space=' ') 93if os.path.exists(OTA_PUBLIC_RES_FILE): 94 os.remove(OTA_PUBLIC_RES_FILE) 95with open(OTA_PUBLIC_RES_FILE, 'w') as f: 96 f.write(COPYRIGHT_TEXT) 97 f.write(ET.tostring(public_xml, encoding="unicode")) 98