1#!/usr/bin/python -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 the timezone data files used by Android.""" 18 19from __future__ import print_function 20 21import glob 22import os 23import re 24import subprocess 25import sys 26import tarfile 27import tempfile 28 29sys.path.append('%s/external/icu/tools' % os.environ.get('ANDROID_BUILD_TOP')) 30import i18nutil 31import icuutil 32import tzdatautil 33 34 35# Calculate the paths that are referred to by multiple functions. 36android_build_top = i18nutil.GetAndroidRootOrDie() 37timezone_dir = os.path.realpath('%s/system/timezone' % android_build_top) 38i18nutil.CheckDirExists(timezone_dir, 'system/timezone') 39 40android_host_out = i18nutil.GetAndroidHostOutOrDie() 41 42zone_compactor_dir = os.path.realpath('%s/system/timezone/input_tools/android' % android_build_top) 43i18nutil.CheckDirExists(zone_compactor_dir, 'system/timezone/input_tools/android') 44 45timezone_input_tools_dir = os.path.realpath('%s/input_tools' % timezone_dir) 46timezone_input_data_dir = os.path.realpath('%s/input_data' % timezone_dir) 47 48timezone_output_data_dir = '%s/output_data' % timezone_dir 49i18nutil.CheckDirExists(timezone_output_data_dir, 'output_data') 50 51tmp_dir = tempfile.mkdtemp('-tzdata') 52 53def GenerateZicInputFile(extracted_iana_data_dir): 54 # Android APIs assume DST means "summer time" so we follow the rearguard format 55 # introduced in 2018e. 56 zic_input_file_name = 'rearguard.zi' 57 58 # 'NDATA=' is used to remove unnecessary rules files. 59 subprocess.check_call(['make', '-C', extracted_iana_data_dir, 'NDATA=', zic_input_file_name]) 60 61 zic_input_file = '%s/%s' % (extracted_iana_data_dir, zic_input_file_name) 62 if not os.path.exists(zic_input_file): 63 print('Could not find %s' % zic_input_file) 64 sys.exit(1) 65 return zic_input_file 66 67 68def WriteSetupFile(zic_input_file): 69 """Writes the list of zones that ZoneCompactor should process.""" 70 links = [] 71 zones = [] 72 for line in open(zic_input_file): 73 fields = line.split() 74 if fields: 75 line_type = fields[0] 76 if line_type == 'Link': 77 # Each "Link" line requires the creation of a link from an old tz ID to 78 # a new tz ID, and implies the existence of a zone with the old tz ID. 79 # 80 # IANA terminology: 81 # TARGET = the new tz ID, LINK-NAME = the old tz ID 82 target = fields[1] 83 link_name = fields[2] 84 links.append('Link %s %s' % (target, link_name)) 85 zones.append('Zone %s' % link_name) 86 elif line_type == 'Zone': 87 # Each "Zone" line indicates the existence of a tz ID. 88 # 89 # IANA terminology: 90 # NAME is the tz ID, other fields like STDOFF, RULES, FORMAT,[UNTIL] are 91 # ignored. 92 name = fields[1] 93 zones.append('Zone %s' % name) 94 95 zone_compactor_setup_file = '%s/setup' % tmp_dir 96 setup = open(zone_compactor_setup_file, 'w') 97 98 # Ordering requirement from ZoneCompactor: Links must come first. 99 for link in sorted(set(links)): 100 setup.write('%s\n' % link) 101 for zone in sorted(set(zones)): 102 setup.write('%s\n' % zone) 103 setup.close() 104 return zone_compactor_setup_file 105 106 107def BuildIcuData(iana_data_tar_file): 108 icu_build_dir = '%s/icu' % tmp_dir 109 110 icuutil.PrepareIcuBuild(icu_build_dir) 111 icuutil.MakeTzDataFiles(icu_build_dir, iana_data_tar_file) 112 113 # Create ICU system image files. 114 icuutil.MakeAndCopyIcuDataFiles(icu_build_dir) 115 116 icu_overlay_dir = '%s/icu_overlay' % timezone_output_data_dir 117 118 # Create the ICU overlay time zone file. 119 icu_overlay_dat_file = '%s/icu_tzdata.dat' % icu_overlay_dir 120 icuutil.MakeAndCopyOverlayTzIcuData(icu_build_dir, icu_overlay_dat_file) 121 122 # Copy ICU license file(s) 123 icuutil.CopyLicenseFiles(icu_overlay_dir) 124 125 126def GetIanaVersion(iana_tar_file): 127 iana_tar_filename = os.path.basename(iana_tar_file) 128 iana_version = re.search('tz(?:data|code)(.+)\\.tar\\.gz', iana_tar_filename).group(1) 129 return iana_version 130 131 132def ExtractTarFile(tar_file, dir): 133 print('Extracting %s...' % tar_file) 134 if not os.path.exists(dir): 135 os.mkdir(dir) 136 tar = tarfile.open(tar_file, 'r') 137 tar.extractall(dir) 138 139 140def BuildZic(iana_tools_dir): 141 iana_zic_code_tar_file = tzdatautil.GetIanaTarFile(iana_tools_dir, 'tzcode') 142 iana_zic_code_version = GetIanaVersion(iana_zic_code_tar_file) 143 iana_zic_data_tar_file = tzdatautil.GetIanaTarFile(iana_tools_dir, 'tzdata') 144 iana_zic_data_version = GetIanaVersion(iana_zic_data_tar_file) 145 146 print('Found IANA zic release %s/%s in %s/%s ...' \ 147 % (iana_zic_code_version, iana_zic_data_version, iana_zic_code_tar_file, iana_zic_data_tar_file)) 148 149 zic_build_dir = '%s/zic' % tmp_dir 150 ExtractTarFile(iana_zic_code_tar_file, zic_build_dir) 151 ExtractTarFile(iana_zic_data_tar_file, zic_build_dir) 152 153 # zic 154 print('Building zic...') 155 # VERSION_DEPS= is to stop the build process looking for files that might not 156 # be present across different versions. 157 subprocess.check_call(['make', '-C', zic_build_dir, 'zic']) 158 159 zic_binary_file = '%s/zic' % zic_build_dir 160 if not os.path.exists(zic_binary_file): 161 print('Could not find %s' % zic_binary_file) 162 sys.exit(1) 163 return zic_binary_file 164 165 166def BuildTzdata(zic_binary_file, extracted_iana_data_dir, iana_data_version): 167 print('Generating zic input file...') 168 zic_input_file = GenerateZicInputFile(extracted_iana_data_dir) 169 170 print('Calling zic...') 171 zic_output_dir = '%s/data' % tmp_dir 172 os.mkdir(zic_output_dir) 173 zic_cmd = [zic_binary_file, '-d', zic_output_dir, zic_input_file] 174 subprocess.check_call(zic_cmd) 175 176 # ZoneCompactor 177 zone_compactor_setup_file = WriteSetupFile(zic_input_file) 178 179 print('Calling ZoneCompactor to update tzdata to %s...' % iana_data_version) 180 181 tzdatautil.InvokeSoong(android_build_top, ['zone_compactor']) 182 183 # Create args for ZoneCompactor 184 header_string = 'tzdata%s' % iana_data_version 185 186 print('Executing ZoneCompactor...') 187 command = '%s/bin/zone_compactor' % android_host_out 188 iana_output_data_dir = '%s/iana' % timezone_output_data_dir 189 subprocess.check_call([command, zone_compactor_setup_file, zic_output_dir, iana_output_data_dir, 190 header_string]) 191 192 193def BuildTzlookup(iana_data_dir): 194 countryzones_source_file = '%s/android/countryzones.txt' % timezone_input_data_dir 195 tzlookup_dest_file = '%s/android/tzlookup.xml' % timezone_output_data_dir 196 197 print('Calling TzLookupGenerator to create tzlookup.xml...') 198 tzdatautil.InvokeSoong(android_build_top, ['tzlookup_generator']) 199 200 zone_tab_file = '%s/zone.tab' % iana_data_dir 201 backward_file = '%s/backward' % iana_data_dir 202 command = '%s/bin/tzlookup_generator' % android_host_out 203 subprocess.check_call([command, countryzones_source_file, zone_tab_file, backward_file, 204 tzlookup_dest_file]) 205 206 207def BuildTelephonylookup(): 208 telephonylookup_source_file = '%s/android/telephonylookup.txt' % timezone_input_data_dir 209 telephonylookup_dest_file = '%s/android/telephonylookup.xml' % timezone_output_data_dir 210 211 print('Calling TelephonyLookupGenerator to create telephonylookup.xml...') 212 tzdatautil.InvokeSoong(android_build_top, ['telephonylookup_generator']) 213 214 command = '%s/bin/telephonylookup_generator' % android_host_out 215 subprocess.check_call([command, telephonylookup_source_file, telephonylookup_dest_file]) 216 217 218def CreateDistroFiles(iana_data_version, output_distro_dir, output_version_file): 219 create_distro_script = '%s/distro/tools/create-distro.py' % timezone_dir 220 221 tzdata_file = '%s/iana/tzdata' % timezone_output_data_dir 222 icu_file = '%s/icu_overlay/icu_tzdata.dat' % timezone_output_data_dir 223 tzlookup_file = '%s/android/tzlookup.xml' % timezone_output_data_dir 224 telephonylookup_file = '%s/android/telephonylookup.xml' % timezone_output_data_dir 225 226 distro_file_pattern = '%s/*.zip' % output_distro_dir 227 existing_files = glob.glob(distro_file_pattern) 228 229 print('Removing %s' % existing_files) 230 for existing_file in existing_files: 231 os.remove(existing_file) 232 233 subprocess.check_call([create_distro_script, 234 '-iana_version', iana_data_version, 235 '-tzdata', tzdata_file, 236 '-icu', icu_file, 237 '-tzlookup', tzlookup_file, 238 '-telephonylookup', telephonylookup_file, 239 '-output_distro_dir', output_distro_dir, 240 '-output_version_file', output_version_file]) 241 242def UpdateTestFiles(): 243 testing_data_dir = '%s/testing/data' % timezone_dir 244 update_test_files_script = '%s/create-test-data.sh' % testing_data_dir 245 subprocess.check_call([update_test_files_script], cwd=testing_data_dir) 246 247 248# Run with no arguments from any directory, with no special setup required. 249# See http://www.iana.org/time-zones/ for more about the source of this data. 250def main(): 251 print('Source data file structure: %s' % timezone_input_data_dir) 252 print('Source tools file structure: %s' % timezone_input_tools_dir) 253 print('Output data file structure: %s' % timezone_output_data_dir) 254 255 iana_input_data_dir = '%s/iana' % timezone_input_data_dir 256 iana_data_tar_file = tzdatautil.GetIanaTarFile(iana_input_data_dir, 'tzdata') 257 iana_data_version = GetIanaVersion(iana_data_tar_file) 258 print('IANA time zone data release %s in %s ...' % (iana_data_version, iana_data_tar_file)) 259 260 icu_dir = icuutil.icuDir() 261 print('Found icu in %s ...' % icu_dir) 262 263 BuildIcuData(iana_data_tar_file) 264 265 iana_tools_dir = '%s/iana' % timezone_input_tools_dir 266 zic_binary_file = BuildZic(iana_tools_dir) 267 268 iana_data_dir = '%s/iana_data' % tmp_dir 269 ExtractTarFile(iana_data_tar_file, iana_data_dir) 270 BuildTzdata(zic_binary_file, iana_data_dir, iana_data_version) 271 BuildTzlookup(iana_data_dir) 272 BuildTelephonylookup() 273 274 # Create a distro file and version file from the output from prior stages. 275 output_distro_dir = '%s/distro' % timezone_output_data_dir 276 output_version_file = '%s/version/tz_version' % timezone_output_data_dir 277 CreateDistroFiles(iana_data_version, output_distro_dir, output_version_file) 278 279 # Update test versions of distro files too. 280 UpdateTestFiles() 281 282 print('Look in %s and %s for new files' % (timezone_output_data_dir, icu_dir)) 283 sys.exit(0) 284 285 286if __name__ == '__main__': 287 main() 288