blob: 2f4ef68803ab34b9f0992f684a9195e868fc4b19 [file] [log] [blame]
Neil Fuller86e72c52017-06-12 15:36:06 +01001#!/usr/bin/python -B
2
Neil Fuller56166d32017-06-12 12:57:10 +01003# 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."""
Neil Fuller86e72c52017-06-12 15:36:06 +010018
Neil Fuller86e72c52017-06-12 15:36:06 +010019import glob
Neil Fuller86e72c52017-06-12 15:36:06 +010020import os
Neil Fullercdf56952018-02-20 17:12:37 +000021import re
Neil Fuller86e72c52017-06-12 15:36:06 +010022import subprocess
23import sys
24import tarfile
25import tempfile
26
Neil Fullerad7e81c2017-06-15 16:45:56 +010027sys.path.append('%s/external/icu/tools' % os.environ.get('ANDROID_BUILD_TOP'))
Neil Fuller86e72c52017-06-12 15:36:06 +010028import i18nutil
Neil Fullerfa89b032017-06-14 15:58:26 +010029import icuutil
Neil Fuller56166d32017-06-12 12:57:10 +010030import tzdatautil
Neil Fuller86e72c52017-06-12 15:36:06 +010031
Neil Fuller86e72c52017-06-12 15:36:06 +010032
33# Calculate the paths that are referred to by multiple functions.
34android_build_top = i18nutil.GetAndroidRootOrDie()
Neil Fuller56166d32017-06-12 12:57:10 +010035timezone_dir = os.path.realpath('%s/system/timezone' % android_build_top)
36i18nutil.CheckDirExists(timezone_dir, 'system/timezone')
37
Neil Fuller8cec5612017-10-11 10:38:17 +010038android_host_out = i18nutil.GetAndroidHostOutOrDie()
39
Neil Fuller56166d32017-06-12 12:57:10 +010040zone_compactor_dir = os.path.realpath('%s/system/timezone/zone_compactor' % android_build_top)
41i18nutil.CheckDirExists(timezone_dir, 'system/timezone/zone_zompactor')
42
Neil Fullercdf56952018-02-20 17:12:37 +000043timezone_input_tools_dir = os.path.realpath('%s/input_tools' % timezone_dir)
Neil Fuller56166d32017-06-12 12:57:10 +010044timezone_input_data_dir = os.path.realpath('%s/input_data' % timezone_dir)
45
Neil Fuller35467e12017-06-12 16:55:44 +010046timezone_output_data_dir = '%s/output_data' % timezone_dir
47i18nutil.CheckDirExists(timezone_output_data_dir, 'output_data')
Neil Fuller86e72c52017-06-12 15:36:06 +010048
49tmp_dir = tempfile.mkdtemp('-tzdata')
50
Neil Fullera591fb02018-05-08 17:29:11 +010051def GenerateZicInputFile(extracted_iana_data_dir):
52 # Android APIs assume DST means "summer time" so we follow the rearguard format
53 # introduced in 2018e.
54 zic_input_file_name = 'rearguard.zi'
Neil Fuller86e72c52017-06-12 15:36:06 +010055
Neil Fullera591fb02018-05-08 17:29:11 +010056 # 'NDATA=' is used to remove unnecessary rules files.
57 subprocess.check_call(['make', '-C', extracted_iana_data_dir, 'NDATA=', zic_input_file_name])
58
59 zic_input_file = '%s/%s' % (extracted_iana_data_dir, zic_input_file_name)
60 if not os.path.exists(zic_input_file):
61 print 'Could not find %s' % zic_input_file
62 sys.exit(1)
63 return zic_input_file
64
65
66def WriteSetupFile(zic_input_file):
Neil Fuller86e72c52017-06-12 15:36:06 +010067 """Writes the list of zones that ZoneCompactor should process."""
68 links = []
69 zones = []
Neil Fullera591fb02018-05-08 17:29:11 +010070 for line in open(zic_input_file):
71 fields = line.split()
72 if fields:
73 if fields[0] == 'Link':
74 links.append('%s %s %s' % (fields[0], fields[1], fields[2]))
75 zones.append(fields[2])
76 elif fields[0] == 'Zone':
77 zones.append(fields[1])
Neil Fuller86e72c52017-06-12 15:36:06 +010078 zones.sort()
79
80 zone_compactor_setup_file = '%s/setup' % tmp_dir
81 setup = open(zone_compactor_setup_file, 'w')
82 for link in sorted(set(links)):
83 setup.write('%s\n' % link)
84 for zone in sorted(set(zones)):
85 setup.write('%s\n' % zone)
86 setup.close()
87 return zone_compactor_setup_file
88
89
Neil Fullercdf56952018-02-20 17:12:37 +000090def BuildIcuData(iana_data_tar_file):
Neil Fuller86e72c52017-06-12 15:36:06 +010091 icu_build_dir = '%s/icu' % tmp_dir
92
Neil Fullerfa89b032017-06-14 15:58:26 +010093 icuutil.PrepareIcuBuild(icu_build_dir)
Neil Fullercdf56952018-02-20 17:12:37 +000094 icuutil.MakeTzDataFiles(icu_build_dir, iana_data_tar_file)
Neil Fullerfa89b032017-06-14 15:58:26 +010095
96 # Create ICU system image files.
97 icuutil.MakeAndCopyIcuDataFiles(icu_build_dir)
98
Neil Fuller64bd4112018-11-01 18:14:07 +000099 icu_overlay_dir = '%s/icu_overlay' % timezone_output_data_dir
100
Neil Fullerfa89b032017-06-14 15:58:26 +0100101 # Create the ICU overlay time zone file.
Neil Fuller64bd4112018-11-01 18:14:07 +0000102 icu_overlay_dat_file = '%s/icu_tzdata.dat' % icu_overlay_dir
Neil Fullerfa89b032017-06-14 15:58:26 +0100103 icuutil.MakeAndCopyOverlayTzIcuData(icu_build_dir, icu_overlay_dat_file)
Neil Fuller86e72c52017-06-12 15:36:06 +0100104
Neil Fuller64bd4112018-11-01 18:14:07 +0000105 # Copy ICU license file(s)
106 icuutil.CopyLicenseFiles(icu_overlay_dir)
107
Neil Fuller86e72c52017-06-12 15:36:06 +0100108
Neil Fullercdf56952018-02-20 17:12:37 +0000109def GetIanaVersion(iana_tar_file):
110 iana_tar_filename = os.path.basename(iana_tar_file)
111 iana_version = re.search('tz(?:data|code)(.+)\\.tar\\.gz', iana_tar_filename).group(1)
Neil Fullerad7e81c2017-06-15 16:45:56 +0100112 return iana_version
113
Neil Fuller8cec5612017-10-11 10:38:17 +0100114
Neil Fullercdf56952018-02-20 17:12:37 +0000115def ExtractTarFile(tar_file, dir):
116 print 'Extracting %s...' % tar_file
117 if not os.path.exists(dir):
118 os.mkdir(dir)
119 tar = tarfile.open(tar_file, 'r')
120 tar.extractall(dir)
Neil Fuller86e72c52017-06-12 15:36:06 +0100121
Neil Fullercdf56952018-02-20 17:12:37 +0000122
123def BuildZic(iana_tools_dir):
Neil Fuller12f61502018-11-06 07:31:05 +0000124 iana_zic_code_tar_file = tzdatautil.GetIanaTarFile(iana_tools_dir, 'code')
Neil Fullercdf56952018-02-20 17:12:37 +0000125 iana_zic_code_version = GetIanaVersion(iana_zic_code_tar_file)
Neil Fuller12f61502018-11-06 07:31:05 +0000126 iana_zic_data_tar_file = tzdatautil.GetIanaTarFile(iana_tools_dir, 'data')
Neil Fullercdf56952018-02-20 17:12:37 +0000127 iana_zic_data_version = GetIanaVersion(iana_zic_data_tar_file)
128
129 print 'Found IANA zic release %s/%s in %s/%s ...' \
130 % (iana_zic_code_version, iana_zic_data_version, iana_zic_code_tar_file, iana_zic_data_tar_file)
131
132 zic_build_dir = '%s/zic' % tmp_dir
133 ExtractTarFile(iana_zic_code_tar_file, zic_build_dir)
134 ExtractTarFile(iana_zic_data_tar_file, zic_build_dir)
135
136 # zic
137 print 'Building zic...'
138 # VERSION_DEPS= is to stop the build process looking for files that might not
139 # be present across different versions.
140 subprocess.check_call(['make', '-C', zic_build_dir, 'zic'])
141
142 zic_binary_file = '%s/zic' % zic_build_dir
143 if not os.path.exists(zic_binary_file):
Neil Fullera591fb02018-05-08 17:29:11 +0100144 print 'Could not find %s' % zic_binary_file
145 sys.exit(1)
Neil Fullercdf56952018-02-20 17:12:37 +0000146 return zic_binary_file
147
148
149def BuildTzdata(zic_binary_file, extracted_iana_data_dir, iana_data_version):
Neil Fullera591fb02018-05-08 17:29:11 +0100150 print 'Generating zic input file...'
151 zic_input_file = GenerateZicInputFile(extracted_iana_data_dir)
152
Neil Fullercdf56952018-02-20 17:12:37 +0000153 print 'Calling zic...'
Neil Fuller86e72c52017-06-12 15:36:06 +0100154 zic_output_dir = '%s/data' % tmp_dir
155 os.mkdir(zic_output_dir)
Neil Fullera591fb02018-05-08 17:29:11 +0100156 zic_cmd = [zic_binary_file, '-d', zic_output_dir, zic_input_file]
Neil Fuller86e72c52017-06-12 15:36:06 +0100157 subprocess.check_call(zic_cmd)
158
Neil Fullercdf56952018-02-20 17:12:37 +0000159 # ZoneCompactor
Neil Fullera591fb02018-05-08 17:29:11 +0100160 zone_compactor_setup_file = WriteSetupFile(zic_input_file)
Neil Fuller86e72c52017-06-12 15:36:06 +0100161
Neil Fullercdf56952018-02-20 17:12:37 +0000162 print 'Calling ZoneCompactor to update tzdata to %s...' % iana_data_version
Neil Fuller8cec5612017-10-11 10:38:17 +0100163 subprocess.check_call(['make', '-C', android_build_top, '-j30', 'zone_compactor'])
Neil Fuller86e72c52017-06-12 15:36:06 +0100164
Neil Fullercdf56952018-02-20 17:12:37 +0000165 # Create args for ZoneCompactor
166 zone_tab_file = '%s/zone.tab' % extracted_iana_data_dir
Neil Fuller8cec5612017-10-11 10:38:17 +0100167 jar_file = '%s/framework/zone_compactor.jar' % android_host_out
Neil Fullercdf56952018-02-20 17:12:37 +0000168 header_string = 'tzdata%s' % iana_data_version
Neil Fuller35467e12017-06-12 16:55:44 +0100169
Neil Fullercdf56952018-02-20 17:12:37 +0000170 print 'Executing ZoneCompactor...'
Neil Fuller35467e12017-06-12 16:55:44 +0100171 iana_output_data_dir = '%s/iana' % timezone_output_data_dir
Neil Fuller8cec5612017-10-11 10:38:17 +0100172 subprocess.check_call(['java', '-jar', jar_file,
Neil Fuller86e72c52017-06-12 15:36:06 +0100173 zone_compactor_setup_file, zic_output_dir, zone_tab_file,
Neil Fullerad7e81c2017-06-15 16:45:56 +0100174 iana_output_data_dir, header_string])
Neil Fuller56166d32017-06-12 12:57:10 +0100175
176
Neil Fuller8cec5612017-10-11 10:38:17 +0100177def BuildTzlookup(iana_data_dir):
178 countryzones_source_file = '%s/android/countryzones.txt' % timezone_input_data_dir
Neil Fuller35467e12017-06-12 16:55:44 +0100179 tzlookup_dest_file = '%s/android/tzlookup.xml' % timezone_output_data_dir
Neil Fuller8cec5612017-10-11 10:38:17 +0100180
181 print 'Calling TzLookupGenerator to create tzlookup.xml...'
182 subprocess.check_call(['make', '-C', android_build_top, '-j30', 'tzlookup_generator'])
183
184 jar_file = '%s/framework/tzlookup_generator.jar' % android_host_out
185 zone_tab_file = '%s/zone.tab' % iana_data_dir
186 subprocess.check_call(['java', '-jar', jar_file,
187 countryzones_source_file, zone_tab_file, tzlookup_dest_file])
Neil Fuller56166d32017-06-12 12:57:10 +0100188
Neil Fuller86e72c52017-06-12 15:36:06 +0100189
Neil Fullere2d551b2018-10-29 15:32:06 +0000190def CreateDistroFiles(iana_data_version, output_distro_dir, output_version_file):
Neil Fullerad7e81c2017-06-15 16:45:56 +0100191 create_distro_script = '%s/distro/tools/create-distro.py' % timezone_dir
192
193 tzdata_file = '%s/iana/tzdata' % timezone_output_data_dir
194 icu_file = '%s/icu_overlay/icu_tzdata.dat' % timezone_output_data_dir
195 tzlookup_file = '%s/android/tzlookup.xml' % timezone_output_data_dir
196
Neil Fullere2d551b2018-10-29 15:32:06 +0000197 distro_file_pattern = '%s/*.zip' % output_distro_dir
198 existing_files = glob.glob(distro_file_pattern)
Neil Fullerad7e81c2017-06-15 16:45:56 +0100199
200 print 'Removing %s' % existing_files
201 for existing_file in existing_files:
202 os.remove(existing_file)
203
204 subprocess.check_call([create_distro_script,
Neil Fullercdf56952018-02-20 17:12:37 +0000205 '-iana_version', iana_data_version,
Neil Fullerad7e81c2017-06-15 16:45:56 +0100206 '-tzdata', tzdata_file,
207 '-icu', icu_file,
208 '-tzlookup', tzlookup_file,
Neil Fullere2d551b2018-10-29 15:32:06 +0000209 '-output_distro_dir', output_distro_dir,
210 '-output_version_file', output_version_file])
Neil Fullerad7e81c2017-06-15 16:45:56 +0100211
Neil Fullerad3c8762017-10-20 15:58:57 +0100212def UpdateTestFiles():
213 testing_data_dir = '%s/testing/data' % timezone_dir
214 update_test_files_script = '%s/create-test-data.sh' % testing_data_dir
215 subprocess.check_call([update_test_files_script], cwd=testing_data_dir)
216
Neil Fuller86e72c52017-06-12 15:36:06 +0100217
218# Run with no arguments from any directory, with no special setup required.
219# See http://www.iana.org/time-zones/ for more about the source of this data.
220def main():
Neil Fullercdf56952018-02-20 17:12:37 +0000221 print 'Source data file structure: %s' % timezone_input_data_dir
222 print 'Source tools file structure: %s' % timezone_input_tools_dir
223 print 'Output data file structure: %s' % timezone_output_data_dir
Neil Fuller56166d32017-06-12 12:57:10 +0100224
Neil Fuller8cec5612017-10-11 10:38:17 +0100225 iana_input_data_dir = '%s/iana' % timezone_input_data_dir
Neil Fuller12f61502018-11-06 07:31:05 +0000226 iana_data_tar_file = tzdatautil.GetIanaTarFile(iana_input_data_dir, 'data')
Neil Fullercdf56952018-02-20 17:12:37 +0000227 iana_data_version = GetIanaVersion(iana_data_tar_file)
228 print 'IANA time zone data release %s in %s ...' % (iana_data_version, iana_data_tar_file)
Neil Fullerfa89b032017-06-14 15:58:26 +0100229
230 icu_dir = icuutil.icuDir()
231 print 'Found icu in %s ...' % icu_dir
Neil Fuller86e72c52017-06-12 15:36:06 +0100232
Neil Fullercdf56952018-02-20 17:12:37 +0000233 BuildIcuData(iana_data_tar_file)
234
235 iana_tools_dir = '%s/iana' % timezone_input_tools_dir
236 zic_binary_file = BuildZic(iana_tools_dir)
237
238 iana_data_dir = '%s/iana_data' % tmp_dir
239 ExtractTarFile(iana_data_tar_file, iana_data_dir)
240 BuildTzdata(zic_binary_file, iana_data_dir, iana_data_version)
Neil Fuller8cec5612017-10-11 10:38:17 +0100241 BuildTzlookup(iana_data_dir)
Neil Fullerad7e81c2017-06-15 16:45:56 +0100242
Neil Fullere2d551b2018-10-29 15:32:06 +0000243 # Create a distro file and version file from the output from prior stages.
244 output_distro_dir = '%s/distro' % timezone_output_data_dir
245 output_version_file = '%s/version/tz_version' % timezone_output_data_dir
246 CreateDistroFiles(iana_data_version, output_distro_dir, output_version_file)
Neil Fullerad7e81c2017-06-15 16:45:56 +0100247
Neil Fullerad3c8762017-10-20 15:58:57 +0100248 # Update test versions of distro files too.
249 UpdateTestFiles()
250
Neil Fullerad7e81c2017-06-15 16:45:56 +0100251 print 'Look in %s and %s for new files' % (timezone_output_data_dir, icu_dir)
Neil Fuller86e72c52017-06-12 15:36:06 +0100252 sys.exit(0)
253
254
255if __name__ == '__main__':
256 main()