• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
20
21# This script generates and updates the public.xml file for sample ota app automatically.
22# Must be run after adding new strings to sample ota app.
23
24ADSERVICES_PUBLIC_RES_FILE = '../apk/publicres/values/public.xml'
25OTA_PUBLIC_RES_FILE = '../samples/ota/res/values/public.xml'
26OTA_STRING_FILE = '../samples/ota/res/values/strings.xml'
27COPYRIGHT_TEXT = f'''<?xml version="1.0" encoding="utf-8"?>
28<!-- Copyright (C) {datetime.date.today().year} The Android Open Source Project
29
30    Licensed under the Apache License, Version 2.0 (the "License");
31    you may not use this file except in compliance with the License.
32    You may obtain a copy of the License at
33
34    http://www.apache.org/licenses/LICENSE-2.0
35
36    Unless required by applicable law or agreed to in writing, software
37    distributed under the License is distributed on an "AS IS" BASIS,
38    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39    See the License for the specific language governing permissions and
40    limitations under the License.
41-->
42'''
43
44adservices_dict = {}
45adservices_public_xml = ET.parse(ADSERVICES_PUBLIC_RES_FILE).getroot()
46for x in adservices_public_xml:
47    adservices_dict[x.attrib['name']] = x.attrib['id']
48
49ota_strings_xml = ET.parse(OTA_STRING_FILE).getroot()
50public_xml = ET.Element('resources')
51for x in ota_strings_xml:
52    cur_element = ET.SubElement(public_xml, 'public')
53    cur_element.set('type', 'string')
54    cur_element.set('name', x.attrib['name'])
55    if x.attrib['name'] not in adservices_dict:
56        print(f"ERROR: {x.attrib['name']} is not in adservices strings.xml")
57        exit(0)
58    cur_element.set('id', adservices_dict[x.attrib['name']])
59
60ET.indent(public_xml, space='    ')
61if os.path.exists(OTA_PUBLIC_RES_FILE):
62    os.remove(OTA_PUBLIC_RES_FILE)
63with open(OTA_PUBLIC_RES_FILE, 'w') as f:
64    f.write(COPYRIGHT_TEXT)
65    f.write(ET.tostring(public_xml, encoding="unicode"))
66