• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      if fields[0] == 'Link':
76        links.append('%s %s %s' % (fields[0], fields[1], fields[2]))
77        zones.append(fields[2])
78      elif fields[0] == 'Zone':
79        zones.append(fields[1])
80  zones.sort()
81
82  zone_compactor_setup_file = '%s/setup' % tmp_dir
83  setup = open(zone_compactor_setup_file, 'w')
84  for link in sorted(set(links)):
85    setup.write('%s\n' % link)
86  for zone in sorted(set(zones)):
87    setup.write('%s\n' % zone)
88  setup.close()
89  return zone_compactor_setup_file
90
91
92def BuildIcuData(iana_data_tar_file):
93  icu_build_dir = '%s/icu' % tmp_dir
94
95  icuutil.PrepareIcuBuild(icu_build_dir)
96  icuutil.MakeTzDataFiles(icu_build_dir, iana_data_tar_file)
97
98  # Create ICU system image files.
99  icuutil.MakeAndCopyIcuDataFiles(icu_build_dir)
100
101  icu_overlay_dir = '%s/icu_overlay' % timezone_output_data_dir
102
103  # Create the ICU overlay time zone file.
104  icu_overlay_dat_file = '%s/icu_tzdata.dat' % icu_overlay_dir
105  icuutil.MakeAndCopyOverlayTzIcuData(icu_build_dir, icu_overlay_dat_file)
106
107  # Copy ICU license file(s)
108  icuutil.CopyLicenseFiles(icu_overlay_dir)
109
110
111def GetIanaVersion(iana_tar_file):
112  iana_tar_filename = os.path.basename(iana_tar_file)
113  iana_version = re.search('tz(?:data|code)(.+)\\.tar\\.gz', iana_tar_filename).group(1)
114  return iana_version
115
116
117def ExtractTarFile(tar_file, dir):
118  print('Extracting %s...' % tar_file)
119  if not os.path.exists(dir):
120    os.mkdir(dir)
121  tar = tarfile.open(tar_file, 'r')
122  tar.extractall(dir)
123
124
125def BuildZic(iana_tools_dir):
126  iana_zic_code_tar_file = tzdatautil.GetIanaTarFile(iana_tools_dir, 'tzcode')
127  iana_zic_code_version = GetIanaVersion(iana_zic_code_tar_file)
128  iana_zic_data_tar_file = tzdatautil.GetIanaTarFile(iana_tools_dir, 'tzdata')
129  iana_zic_data_version = GetIanaVersion(iana_zic_data_tar_file)
130
131  print('Found IANA zic release %s/%s in %s/%s ...' \
132      % (iana_zic_code_version, iana_zic_data_version, iana_zic_code_tar_file, iana_zic_data_tar_file))
133
134  zic_build_dir = '%s/zic' % tmp_dir
135  ExtractTarFile(iana_zic_code_tar_file, zic_build_dir)
136  ExtractTarFile(iana_zic_data_tar_file, zic_build_dir)
137
138  # zic
139  print('Building zic...')
140  # VERSION_DEPS= is to stop the build process looking for files that might not
141  # be present across different versions.
142  subprocess.check_call(['make', '-C', zic_build_dir, 'zic'])
143
144  zic_binary_file = '%s/zic' % zic_build_dir
145  if not os.path.exists(zic_binary_file):
146    print('Could not find %s' % zic_binary_file)
147    sys.exit(1)
148  return zic_binary_file
149
150
151def BuildTzdata(zic_binary_file, extracted_iana_data_dir, iana_data_version):
152  print('Generating zic input file...')
153  zic_input_file = GenerateZicInputFile(extracted_iana_data_dir)
154
155  print('Calling zic...')
156  zic_output_dir = '%s/data' % tmp_dir
157  os.mkdir(zic_output_dir)
158  zic_cmd = [zic_binary_file, '-d', zic_output_dir, zic_input_file]
159  subprocess.check_call(zic_cmd)
160
161  # ZoneCompactor
162  zone_compactor_setup_file = WriteSetupFile(zic_input_file)
163
164  print('Calling ZoneCompactor to update tzdata to %s...' % iana_data_version)
165
166  tzdatautil.InvokeSoong(android_build_top, ['zone_compactor'])
167
168  # Create args for ZoneCompactor
169  zone_tab_file = '%s/zone.tab' % extracted_iana_data_dir
170  header_string = 'tzdata%s' % iana_data_version
171
172  print('Executing ZoneCompactor...')
173  command = '%s/bin/zone_compactor' % android_host_out
174  iana_output_data_dir = '%s/iana' % timezone_output_data_dir
175  subprocess.check_call([command, zone_compactor_setup_file, zic_output_dir, zone_tab_file,
176                         iana_output_data_dir, header_string])
177
178
179def BuildTzlookup(iana_data_dir):
180  countryzones_source_file = '%s/android/countryzones.txt' % timezone_input_data_dir
181  tzlookup_dest_file = '%s/android/tzlookup.xml' % timezone_output_data_dir
182
183  print('Calling TzLookupGenerator to create tzlookup.xml...')
184  tzdatautil.InvokeSoong(android_build_top, ['tzlookup_generator'])
185
186  zone_tab_file = '%s/zone.tab' % iana_data_dir
187  command = '%s/bin/tzlookup_generator' % android_host_out
188  subprocess.check_call([command, countryzones_source_file, zone_tab_file, tzlookup_dest_file])
189
190
191def BuildTelephonylookup():
192  telephonylookup_source_file = '%s/android/telephonylookup.txt' % timezone_input_data_dir
193  telephonylookup_dest_file = '%s/android/telephonylookup.xml' % timezone_output_data_dir
194
195  print('Calling TelephonyLookupGenerator to create telephonylookup.xml...')
196  tzdatautil.InvokeSoong(android_build_top, ['telephonylookup_generator'])
197
198  command = '%s/bin/telephonylookup_generator' % android_host_out
199  subprocess.check_call([command, telephonylookup_source_file, telephonylookup_dest_file])
200
201
202def CreateDistroFiles(iana_data_version, output_distro_dir, output_version_file):
203  create_distro_script = '%s/distro/tools/create-distro.py' % timezone_dir
204
205  tzdata_file = '%s/iana/tzdata' % timezone_output_data_dir
206  icu_file = '%s/icu_overlay/icu_tzdata.dat' % timezone_output_data_dir
207  tzlookup_file = '%s/android/tzlookup.xml' % timezone_output_data_dir
208  telephonylookup_file = '%s/android/telephonylookup.xml' % timezone_output_data_dir
209
210  distro_file_pattern = '%s/*.zip' % output_distro_dir
211  existing_files = glob.glob(distro_file_pattern)
212
213  print('Removing %s' % existing_files)
214  for existing_file in existing_files:
215    os.remove(existing_file)
216
217  subprocess.check_call([create_distro_script,
218      '-iana_version', iana_data_version,
219      '-tzdata', tzdata_file,
220      '-icu', icu_file,
221      '-tzlookup', tzlookup_file,
222      '-telephonylookup', telephonylookup_file,
223      '-output_distro_dir', output_distro_dir,
224      '-output_version_file', output_version_file])
225
226def UpdateTestFiles():
227  testing_data_dir = '%s/testing/data' % timezone_dir
228  update_test_files_script = '%s/create-test-data.sh' % testing_data_dir
229  subprocess.check_call([update_test_files_script], cwd=testing_data_dir)
230
231
232# Run with no arguments from any directory, with no special setup required.
233# See http://www.iana.org/time-zones/ for more about the source of this data.
234def main():
235  print('Source data file structure: %s' % timezone_input_data_dir)
236  print('Source tools file structure: %s' % timezone_input_tools_dir)
237  print('Output data file structure: %s' % timezone_output_data_dir)
238
239  iana_input_data_dir = '%s/iana' % timezone_input_data_dir
240  iana_data_tar_file = tzdatautil.GetIanaTarFile(iana_input_data_dir, 'tzdata')
241  iana_data_version = GetIanaVersion(iana_data_tar_file)
242  print('IANA time zone data release %s in %s ...' % (iana_data_version, iana_data_tar_file))
243
244  icu_dir = icuutil.icuDir()
245  print('Found icu in %s ...' % icu_dir)
246
247  BuildIcuData(iana_data_tar_file)
248
249  iana_tools_dir = '%s/iana' % timezone_input_tools_dir
250  zic_binary_file = BuildZic(iana_tools_dir)
251
252  iana_data_dir = '%s/iana_data' % tmp_dir
253  ExtractTarFile(iana_data_tar_file, iana_data_dir)
254  BuildTzdata(zic_binary_file, iana_data_dir, iana_data_version)
255  BuildTzlookup(iana_data_dir)
256  BuildTelephonylookup()
257
258  # Create a distro file and version file from the output from prior stages.
259  output_distro_dir = '%s/distro' % timezone_output_data_dir
260  output_version_file = '%s/version/tz_version' % timezone_output_data_dir
261  CreateDistroFiles(iana_data_version, output_distro_dir, output_version_file)
262
263  # Update test versions of distro files too.
264  UpdateTestFiles()
265
266  print('Look in %s and %s for new files' % (timezone_output_data_dir, icu_dir))
267  sys.exit(0)
268
269
270if __name__ == '__main__':
271  main()
272