1#!/usr/bin/python3 -B 2 3# Copyright 2017 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 17"""Generates a time zone distro file""" 18 19import argparse 20import os 21import shutil 22import subprocess 23import sys 24 25sys.path.append('%s/external/icu/tools' % os.environ.get('ANDROID_BUILD_TOP')) 26import i18nutil 27 28sys.path.append('%s/system/timezone' % os.environ.get('ANDROID_BUILD_TOP')) 29import tzdatautil 30 31android_build_top = i18nutil.GetAndroidRootOrDie() 32android_host_out_dir = i18nutil.GetAndroidHostOutOrDie() 33timezone_dir = os.path.realpath('%s/system/timezone' % android_build_top) 34i18nutil.CheckDirExists(timezone_dir, 'system/timezone') 35 36def RunCreateTimeZoneDistro(properties_file): 37 # Build the libraries needed. 38 tzdatautil.InvokeSoong(android_build_top, ['create_time_zone_distro']) 39 40 # Run the CreateTimeZoneDistro tool 41 command = '%s/bin/create_time_zone_distro' % android_host_out_dir 42 subprocess.check_call([command, properties_file]) 43 44 45def CreateTimeZoneDistro( 46 iana_version, revision, tzdata_file, icu_file, tzlookup_file, telephonylookup_file, 47 output_distro_dir, output_version_file): 48 original_cwd = os.getcwd() 49 50 i18nutil.SwitchToNewTemporaryDirectory() 51 working_dir = os.getcwd() 52 53 # Generate the properties file. 54 properties_file = '%s/distro.properties' % working_dir 55 with open(properties_file, "w") as properties: 56 properties.write('rules.version=%s\n' % iana_version) 57 properties.write('revision=%s\n' % revision) 58 properties.write('tzdata.file=%s\n' % tzdata_file) 59 properties.write('icu.file=%s\n' % icu_file) 60 properties.write('tzlookup.file=%s\n' % tzlookup_file) 61 properties.write('telephonylookup.file=%s\n' % telephonylookup_file) 62 properties.write('output.distro.dir=%s\n' % output_distro_dir) 63 properties.write('output.version.file=%s\n' % output_version_file) 64 65 RunCreateTimeZoneDistro(properties_file) 66 67 os.chdir(original_cwd) 68 69 70def main(): 71 parser = argparse.ArgumentParser() 72 parser.add_argument('-iana_version', required=True, 73 help='The IANA time zone rules release version, e.g. 2017b') 74 parser.add_argument('-revision', type=int, required = True, 75 help='The distro revision for the IANA version') 76 parser.add_argument('-tzdata', required=True, help='The location of the tzdata file to include') 77 parser.add_argument('-icu_dir', required=True, 78 help='Directory with the ICU overlay .dat and license files') 79 parser.add_argument('-tzlookup', required=True, 80 help='The location of the tzlookup.xml file to include') 81 parser.add_argument('-telephonylookup', required=True, 82 help='The location of the telephonylookup.xml file to include') 83 parser.add_argument('-output_distro_dir', required=True, 84 help='The output directory for the distro.zip') 85 parser.add_argument('-output_version_file', required=True, 86 help='The output path for the version file') 87 args = parser.parse_args() 88 89 iana_version = args.iana_version 90 revision = args.revision 91 icu_dir = args.icu_dir 92 tzdata_file = os.path.abspath(args.tzdata) 93 icu_file = os.path.abspath('%s/icu_tzdata.dat' % icu_dir) 94 icu_license = os.path.abspath('%s/LICENSE' % icu_dir) 95 tzlookup_file = os.path.abspath(args.tzlookup) 96 telephonylookup_file = os.path.abspath(args.telephonylookup) 97 output_distro_dir = os.path.abspath(args.output_distro_dir) 98 output_version_file = os.path.abspath(args.output_version_file) 99 100 if not os.path.isfile(icu_license): 101 raise Exception("ICU license file was not found in %s" % icu_dir) 102 103 print("Copying %s to %s" % (icu_license, output_distro_dir)) 104 shutil.copy(icu_license, output_distro_dir) 105 106 CreateTimeZoneDistro( 107 iana_version=iana_version, 108 revision=revision, 109 tzdata_file=tzdata_file, 110 icu_file=icu_file, 111 tzlookup_file=tzlookup_file, 112 telephonylookup_file=telephonylookup_file, 113 output_distro_dir=output_distro_dir, 114 output_version_file=output_version_file) 115 116 print('Distro file created in %s' % output_distro_dir) 117 print('Version file created as %s' % output_version_file) 118 sys.exit(0) 119 120 121if __name__ == '__main__': 122 main() 123