• 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/zone_compactor' % android_build_top)
43i18nutil.CheckDirExists(timezone_dir, 'system/timezone/zone_zompactor')
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, 'code')
127  iana_zic_code_version = GetIanaVersion(iana_zic_code_tar_file)
128  iana_zic_data_tar_file = tzdatautil.GetIanaTarFile(iana_tools_dir, 'data')
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  subprocess.check_call(['make', '-C', android_build_top, '-j30', 'zone_compactor'])
166
167  # Create args for ZoneCompactor
168  zone_tab_file = '%s/zone.tab' % extracted_iana_data_dir
169  jar_file = '%s/framework/zone_compactor.jar' % android_host_out
170  header_string = 'tzdata%s' % iana_data_version
171
172  print('Executing ZoneCompactor...')
173  iana_output_data_dir = '%s/iana' % timezone_output_data_dir
174  subprocess.check_call(['java', '-jar', jar_file,
175                         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  subprocess.check_call(['make', '-C', android_build_top, '-j30', 'tzlookup_generator'])
185
186  jar_file = '%s/framework/tzlookup_generator.jar' % android_host_out
187  zone_tab_file = '%s/zone.tab' % iana_data_dir
188  subprocess.check_call(['java', '-jar', jar_file,
189                         countryzones_source_file, zone_tab_file, tzlookup_dest_file])
190
191
192def CreateDistroFiles(iana_data_version, output_distro_dir, output_version_file):
193  create_distro_script = '%s/distro/tools/create-distro.py' % timezone_dir
194
195  tzdata_file = '%s/iana/tzdata' % timezone_output_data_dir
196  icu_file = '%s/icu_overlay/icu_tzdata.dat' % timezone_output_data_dir
197  tzlookup_file = '%s/android/tzlookup.xml' % timezone_output_data_dir
198
199  distro_file_pattern = '%s/*.zip' % output_distro_dir
200  existing_files = glob.glob(distro_file_pattern)
201
202  print('Removing %s' % existing_files)
203  for existing_file in existing_files:
204    os.remove(existing_file)
205
206  subprocess.check_call([create_distro_script,
207      '-iana_version', iana_data_version,
208      '-tzdata', tzdata_file,
209      '-icu', icu_file,
210      '-tzlookup', tzlookup_file,
211      '-output_distro_dir', output_distro_dir,
212      '-output_version_file', output_version_file])
213
214def UpdateTestFiles():
215  testing_data_dir = '%s/testing/data' % timezone_dir
216  update_test_files_script = '%s/create-test-data.sh' % testing_data_dir
217  subprocess.check_call([update_test_files_script], cwd=testing_data_dir)
218
219
220# Run with no arguments from any directory, with no special setup required.
221# See http://www.iana.org/time-zones/ for more about the source of this data.
222def main():
223  print('Source data file structure: %s' % timezone_input_data_dir)
224  print('Source tools file structure: %s' % timezone_input_tools_dir)
225  print('Output data file structure: %s' % timezone_output_data_dir)
226
227  iana_input_data_dir = '%s/iana' % timezone_input_data_dir
228  iana_data_tar_file = tzdatautil.GetIanaTarFile(iana_input_data_dir, 'data')
229  iana_data_version = GetIanaVersion(iana_data_tar_file)
230  print('IANA time zone data release %s in %s ...' % (iana_data_version, iana_data_tar_file))
231
232  icu_dir = icuutil.icuDir()
233  print('Found icu in %s ...' % icu_dir)
234
235  BuildIcuData(iana_data_tar_file)
236
237  iana_tools_dir = '%s/iana' % timezone_input_tools_dir
238  zic_binary_file = BuildZic(iana_tools_dir)
239
240  iana_data_dir = '%s/iana_data' % tmp_dir
241  ExtractTarFile(iana_data_tar_file, iana_data_dir)
242  BuildTzdata(zic_binary_file, iana_data_dir, iana_data_version)
243  BuildTzlookup(iana_data_dir)
244
245  # Create a distro file and version file from the output from prior stages.
246  output_distro_dir = '%s/distro' % timezone_output_data_dir
247  output_version_file = '%s/version/tz_version' % timezone_output_data_dir
248  CreateDistroFiles(iana_data_version, output_distro_dir, output_version_file)
249
250  # Update test versions of distro files too.
251  UpdateTestFiles()
252
253  print('Look in %s and %s for new files' % (timezone_output_data_dir, icu_dir))
254  sys.exit(0)
255
256
257if __name__ == '__main__':
258  main()
259