• 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      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,
148         iana_zic_data_tar_file))
149
150  zic_build_dir = '%s/zic' % tmp_dir
151  ExtractTarFile(iana_zic_code_tar_file, zic_build_dir)
152  ExtractTarFile(iana_zic_data_tar_file, zic_build_dir)
153
154  # zic
155  print('Building zic...')
156  # VERSION_DEPS= is to stop the build process looking for files that might not
157  # be present across different versions.
158  subprocess.check_call(['make', '-C', zic_build_dir, 'zic'])
159
160  zic_binary_file = '%s/zic' % zic_build_dir
161  if not os.path.exists(zic_binary_file):
162    print('Could not find %s' % zic_binary_file)
163    sys.exit(1)
164  return zic_binary_file
165
166
167def BuildTzdata(zic_binary_file, extracted_iana_data_dir, iana_data_version):
168  print('Generating zic input file...')
169  zic_input_file = GenerateZicInputFile(extracted_iana_data_dir)
170
171  print('Calling zic...')
172  zic_output_dir = '%s/data' % tmp_dir
173  os.mkdir(zic_output_dir)
174  zic_cmd = [zic_binary_file, '-d', zic_output_dir, zic_input_file]
175  subprocess.check_call(zic_cmd)
176
177  # ZoneCompactor
178  zone_compactor_setup_file = WriteSetupFile(zic_input_file)
179
180  print('Calling ZoneCompactor to update tzdata to %s...' % iana_data_version)
181
182  tzdatautil.InvokeSoong(android_build_top, ['zone_compactor'])
183
184  # Create args for ZoneCompactor
185  header_string = 'tzdata%s' % iana_data_version
186
187  print('Executing ZoneCompactor...')
188  command = '%s/bin/zone_compactor' % android_host_out
189  iana_output_data_dir = '%s/iana' % timezone_output_data_dir
190  subprocess.check_call([command, zone_compactor_setup_file, zic_output_dir, iana_output_data_dir,
191                         header_string])
192
193
194def BuildTzlookupAndTzIds(iana_data_dir):
195  countryzones_source_file = '%s/android/countryzones.txt' % timezone_input_data_dir
196  tzlookup_dest_file = '%s/android/tzlookup.xml' % timezone_output_data_dir
197  tzids_dest_file = '%s/android/tzids.prototxt' % timezone_output_data_dir
198
199  print('Calling TzLookupGenerator to create tzlookup.xml / tzids.prototxt...')
200  tzdatautil.InvokeSoong(android_build_top, ['tzlookup_generator'])
201
202  zone_tab_file = '%s/zone.tab' % iana_data_dir
203  backward_file = '%s/backward' % iana_data_dir
204  command = '%s/bin/tzlookup_generator' % android_host_out
205  subprocess.check_call([command, countryzones_source_file, zone_tab_file, backward_file,
206                         tzlookup_dest_file, tzids_dest_file])
207
208
209def BuildTelephonylookup():
210  telephonylookup_source_file = '%s/android/telephonylookup.txt' % timezone_input_data_dir
211  telephonylookup_dest_file = '%s/android/telephonylookup.xml' % timezone_output_data_dir
212
213  print('Calling TelephonyLookupGenerator to create telephonylookup.xml...')
214  tzdatautil.InvokeSoong(android_build_top, ['telephonylookup_generator'])
215
216  command = '%s/bin/telephonylookup_generator' % android_host_out
217  subprocess.check_call([command, telephonylookup_source_file, telephonylookup_dest_file])
218
219
220def CreateDistroFiles(iana_data_version, output_distro_dir, output_version_file):
221  create_distro_script = '%s/distro/tools/create-distro.py' % timezone_dir
222
223  tzdata_file = '%s/iana/tzdata' % timezone_output_data_dir
224  icu_file = '%s/icu_overlay/icu_tzdata.dat' % timezone_output_data_dir
225  tzlookup_file = '%s/android/tzlookup.xml' % timezone_output_data_dir
226  telephonylookup_file = '%s/android/telephonylookup.xml' % timezone_output_data_dir
227
228  distro_file_pattern = '%s/*.zip' % output_distro_dir
229  existing_files = glob.glob(distro_file_pattern)
230
231  print('Removing %s' % existing_files)
232  for existing_file in existing_files:
233    os.remove(existing_file)
234
235  subprocess.check_call([create_distro_script,
236      '-iana_version', iana_data_version,
237      '-tzdata', tzdata_file,
238      '-icu', icu_file,
239      '-tzlookup', tzlookup_file,
240      '-telephonylookup', telephonylookup_file,
241      '-output_distro_dir', output_distro_dir,
242      '-output_version_file', output_version_file])
243
244def UpdateTestFiles():
245  testing_data_dir = '%s/testing/data' % timezone_dir
246  update_test_files_script = '%s/create-test-data.sh' % testing_data_dir
247  subprocess.check_call([update_test_files_script], cwd=testing_data_dir)
248
249
250# Run with no arguments from any directory, with no special setup required.
251# See http://www.iana.org/time-zones/ for more about the source of this data.
252def main():
253  print('Source data file structure: %s' % timezone_input_data_dir)
254  print('Source tools file structure: %s' % timezone_input_tools_dir)
255  print('Intermediate / working dir: %s' % tmp_dir)
256  print('Output data file structure: %s' % timezone_output_data_dir)
257
258  iana_input_data_dir = '%s/iana' % timezone_input_data_dir
259  iana_data_tar_file = tzdatautil.GetIanaTarFile(iana_input_data_dir, 'tzdata')
260  iana_data_version = GetIanaVersion(iana_data_tar_file)
261  print('IANA time zone data release %s in %s ...' % (iana_data_version, iana_data_tar_file))
262
263  icu_dir = icuutil.icuDir()
264  print('Found icu in %s ...' % icu_dir)
265
266  BuildIcuData(iana_data_tar_file)
267
268  iana_tools_dir = '%s/iana' % timezone_input_tools_dir
269  zic_binary_file = BuildZic(iana_tools_dir)
270
271  iana_data_dir = '%s/iana_data' % tmp_dir
272  ExtractTarFile(iana_data_tar_file, iana_data_dir)
273  BuildTzdata(zic_binary_file, iana_data_dir, iana_data_version)
274
275  BuildTzlookupAndTzIds(iana_data_dir)
276
277  BuildTelephonylookup()
278
279  # Create a distro file and version file from the output from prior stages.
280  output_distro_dir = '%s/distro' % timezone_output_data_dir
281  output_version_file = '%s/version/tz_version' % timezone_output_data_dir
282  CreateDistroFiles(iana_data_version, output_distro_dir, output_version_file)
283
284  # Update test versions of distro files too.
285  UpdateTestFiles()
286
287  print('Look in %s and %s for new files' % (timezone_output_data_dir, icu_dir))
288  sys.exit(0)
289
290
291if __name__ == '__main__':
292  main()
293