Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 1 | #!/usr/bin/python -B |
| 2 | |
Neil Fuller | 56166d3 | 2017-06-12 12:57:10 +0100 | [diff] [blame] | 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.""" |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 18 | |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 19 | import glob |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 20 | import os |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 21 | import re |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 22 | import subprocess |
| 23 | import sys |
| 24 | import tarfile |
| 25 | import tempfile |
| 26 | |
Neil Fuller | ad7e81c | 2017-06-15 16:45:56 +0100 | [diff] [blame] | 27 | sys.path.append('%s/external/icu/tools' % os.environ.get('ANDROID_BUILD_TOP')) |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 28 | import i18nutil |
Neil Fuller | fa89b03 | 2017-06-14 15:58:26 +0100 | [diff] [blame] | 29 | import icuutil |
Neil Fuller | 56166d3 | 2017-06-12 12:57:10 +0100 | [diff] [blame] | 30 | import tzdatautil |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 31 | |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 32 | |
| 33 | # Calculate the paths that are referred to by multiple functions. |
| 34 | android_build_top = i18nutil.GetAndroidRootOrDie() |
Neil Fuller | 56166d3 | 2017-06-12 12:57:10 +0100 | [diff] [blame] | 35 | timezone_dir = os.path.realpath('%s/system/timezone' % android_build_top) |
| 36 | i18nutil.CheckDirExists(timezone_dir, 'system/timezone') |
| 37 | |
Neil Fuller | 8cec561 | 2017-10-11 10:38:17 +0100 | [diff] [blame] | 38 | android_host_out = i18nutil.GetAndroidHostOutOrDie() |
| 39 | |
Neil Fuller | 56166d3 | 2017-06-12 12:57:10 +0100 | [diff] [blame] | 40 | zone_compactor_dir = os.path.realpath('%s/system/timezone/zone_compactor' % android_build_top) |
| 41 | i18nutil.CheckDirExists(timezone_dir, 'system/timezone/zone_zompactor') |
| 42 | |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 43 | timezone_input_tools_dir = os.path.realpath('%s/input_tools' % timezone_dir) |
Neil Fuller | 56166d3 | 2017-06-12 12:57:10 +0100 | [diff] [blame] | 44 | timezone_input_data_dir = os.path.realpath('%s/input_data' % timezone_dir) |
| 45 | |
Neil Fuller | 35467e1 | 2017-06-12 16:55:44 +0100 | [diff] [blame] | 46 | timezone_output_data_dir = '%s/output_data' % timezone_dir |
| 47 | i18nutil.CheckDirExists(timezone_output_data_dir, 'output_data') |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 48 | |
| 49 | tmp_dir = tempfile.mkdtemp('-tzdata') |
| 50 | |
Neil Fuller | a591fb0 | 2018-05-08 17:29:11 +0100 | [diff] [blame] | 51 | def 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 Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 55 | |
Neil Fuller | a591fb0 | 2018-05-08 17:29:11 +0100 | [diff] [blame] | 56 | # '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 | |
| 66 | def WriteSetupFile(zic_input_file): |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 67 | """Writes the list of zones that ZoneCompactor should process.""" |
| 68 | links = [] |
| 69 | zones = [] |
Neil Fuller | a591fb0 | 2018-05-08 17:29:11 +0100 | [diff] [blame] | 70 | 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 Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 78 | 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 Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 90 | def BuildIcuData(iana_data_tar_file): |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 91 | icu_build_dir = '%s/icu' % tmp_dir |
| 92 | |
Neil Fuller | fa89b03 | 2017-06-14 15:58:26 +0100 | [diff] [blame] | 93 | icuutil.PrepareIcuBuild(icu_build_dir) |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 94 | icuutil.MakeTzDataFiles(icu_build_dir, iana_data_tar_file) |
Neil Fuller | fa89b03 | 2017-06-14 15:58:26 +0100 | [diff] [blame] | 95 | |
| 96 | # Create ICU system image files. |
| 97 | icuutil.MakeAndCopyIcuDataFiles(icu_build_dir) |
| 98 | |
Neil Fuller | 64bd411 | 2018-11-01 18:14:07 +0000 | [diff] [blame] | 99 | icu_overlay_dir = '%s/icu_overlay' % timezone_output_data_dir |
| 100 | |
Neil Fuller | fa89b03 | 2017-06-14 15:58:26 +0100 | [diff] [blame] | 101 | # Create the ICU overlay time zone file. |
Neil Fuller | 64bd411 | 2018-11-01 18:14:07 +0000 | [diff] [blame] | 102 | icu_overlay_dat_file = '%s/icu_tzdata.dat' % icu_overlay_dir |
Neil Fuller | fa89b03 | 2017-06-14 15:58:26 +0100 | [diff] [blame] | 103 | icuutil.MakeAndCopyOverlayTzIcuData(icu_build_dir, icu_overlay_dat_file) |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 104 | |
Neil Fuller | 64bd411 | 2018-11-01 18:14:07 +0000 | [diff] [blame] | 105 | # Copy ICU license file(s) |
| 106 | icuutil.CopyLicenseFiles(icu_overlay_dir) |
| 107 | |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 108 | |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 109 | def 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 Fuller | ad7e81c | 2017-06-15 16:45:56 +0100 | [diff] [blame] | 112 | return iana_version |
| 113 | |
Neil Fuller | 8cec561 | 2017-10-11 10:38:17 +0100 | [diff] [blame] | 114 | |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 115 | def 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 Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 121 | |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 122 | |
| 123 | def BuildZic(iana_tools_dir): |
Neil Fuller | 12f6150 | 2018-11-06 07:31:05 +0000 | [diff] [blame] | 124 | iana_zic_code_tar_file = tzdatautil.GetIanaTarFile(iana_tools_dir, 'code') |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 125 | iana_zic_code_version = GetIanaVersion(iana_zic_code_tar_file) |
Neil Fuller | 12f6150 | 2018-11-06 07:31:05 +0000 | [diff] [blame] | 126 | iana_zic_data_tar_file = tzdatautil.GetIanaTarFile(iana_tools_dir, 'data') |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 127 | 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 Fuller | a591fb0 | 2018-05-08 17:29:11 +0100 | [diff] [blame] | 144 | print 'Could not find %s' % zic_binary_file |
| 145 | sys.exit(1) |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 146 | return zic_binary_file |
| 147 | |
| 148 | |
| 149 | def BuildTzdata(zic_binary_file, extracted_iana_data_dir, iana_data_version): |
Neil Fuller | a591fb0 | 2018-05-08 17:29:11 +0100 | [diff] [blame] | 150 | print 'Generating zic input file...' |
| 151 | zic_input_file = GenerateZicInputFile(extracted_iana_data_dir) |
| 152 | |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 153 | print 'Calling zic...' |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 154 | zic_output_dir = '%s/data' % tmp_dir |
| 155 | os.mkdir(zic_output_dir) |
Neil Fuller | a591fb0 | 2018-05-08 17:29:11 +0100 | [diff] [blame] | 156 | zic_cmd = [zic_binary_file, '-d', zic_output_dir, zic_input_file] |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 157 | subprocess.check_call(zic_cmd) |
| 158 | |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 159 | # ZoneCompactor |
Neil Fuller | a591fb0 | 2018-05-08 17:29:11 +0100 | [diff] [blame] | 160 | zone_compactor_setup_file = WriteSetupFile(zic_input_file) |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 161 | |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 162 | print 'Calling ZoneCompactor to update tzdata to %s...' % iana_data_version |
Neil Fuller | 8cec561 | 2017-10-11 10:38:17 +0100 | [diff] [blame] | 163 | subprocess.check_call(['make', '-C', android_build_top, '-j30', 'zone_compactor']) |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 164 | |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 165 | # Create args for ZoneCompactor |
| 166 | zone_tab_file = '%s/zone.tab' % extracted_iana_data_dir |
Neil Fuller | 8cec561 | 2017-10-11 10:38:17 +0100 | [diff] [blame] | 167 | jar_file = '%s/framework/zone_compactor.jar' % android_host_out |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 168 | header_string = 'tzdata%s' % iana_data_version |
Neil Fuller | 35467e1 | 2017-06-12 16:55:44 +0100 | [diff] [blame] | 169 | |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 170 | print 'Executing ZoneCompactor...' |
Neil Fuller | 35467e1 | 2017-06-12 16:55:44 +0100 | [diff] [blame] | 171 | iana_output_data_dir = '%s/iana' % timezone_output_data_dir |
Neil Fuller | 8cec561 | 2017-10-11 10:38:17 +0100 | [diff] [blame] | 172 | subprocess.check_call(['java', '-jar', jar_file, |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 173 | zone_compactor_setup_file, zic_output_dir, zone_tab_file, |
Neil Fuller | ad7e81c | 2017-06-15 16:45:56 +0100 | [diff] [blame] | 174 | iana_output_data_dir, header_string]) |
Neil Fuller | 56166d3 | 2017-06-12 12:57:10 +0100 | [diff] [blame] | 175 | |
| 176 | |
Neil Fuller | 8cec561 | 2017-10-11 10:38:17 +0100 | [diff] [blame] | 177 | def BuildTzlookup(iana_data_dir): |
| 178 | countryzones_source_file = '%s/android/countryzones.txt' % timezone_input_data_dir |
Neil Fuller | 35467e1 | 2017-06-12 16:55:44 +0100 | [diff] [blame] | 179 | tzlookup_dest_file = '%s/android/tzlookup.xml' % timezone_output_data_dir |
Neil Fuller | 8cec561 | 2017-10-11 10:38:17 +0100 | [diff] [blame] | 180 | |
| 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 Fuller | 56166d3 | 2017-06-12 12:57:10 +0100 | [diff] [blame] | 188 | |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 189 | |
Neil Fuller | e2d551b | 2018-10-29 15:32:06 +0000 | [diff] [blame] | 190 | def CreateDistroFiles(iana_data_version, output_distro_dir, output_version_file): |
Neil Fuller | ad7e81c | 2017-06-15 16:45:56 +0100 | [diff] [blame] | 191 | 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 Fuller | e2d551b | 2018-10-29 15:32:06 +0000 | [diff] [blame] | 197 | distro_file_pattern = '%s/*.zip' % output_distro_dir |
| 198 | existing_files = glob.glob(distro_file_pattern) |
Neil Fuller | ad7e81c | 2017-06-15 16:45:56 +0100 | [diff] [blame] | 199 | |
| 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 Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 205 | '-iana_version', iana_data_version, |
Neil Fuller | ad7e81c | 2017-06-15 16:45:56 +0100 | [diff] [blame] | 206 | '-tzdata', tzdata_file, |
| 207 | '-icu', icu_file, |
| 208 | '-tzlookup', tzlookup_file, |
Neil Fuller | e2d551b | 2018-10-29 15:32:06 +0000 | [diff] [blame] | 209 | '-output_distro_dir', output_distro_dir, |
| 210 | '-output_version_file', output_version_file]) |
Neil Fuller | ad7e81c | 2017-06-15 16:45:56 +0100 | [diff] [blame] | 211 | |
Neil Fuller | ad3c876 | 2017-10-20 15:58:57 +0100 | [diff] [blame] | 212 | def 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 Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 217 | |
| 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. |
| 220 | def main(): |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 221 | 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 Fuller | 56166d3 | 2017-06-12 12:57:10 +0100 | [diff] [blame] | 224 | |
Neil Fuller | 8cec561 | 2017-10-11 10:38:17 +0100 | [diff] [blame] | 225 | iana_input_data_dir = '%s/iana' % timezone_input_data_dir |
Neil Fuller | 12f6150 | 2018-11-06 07:31:05 +0000 | [diff] [blame] | 226 | iana_data_tar_file = tzdatautil.GetIanaTarFile(iana_input_data_dir, 'data') |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 227 | 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 Fuller | fa89b03 | 2017-06-14 15:58:26 +0100 | [diff] [blame] | 229 | |
| 230 | icu_dir = icuutil.icuDir() |
| 231 | print 'Found icu in %s ...' % icu_dir |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 232 | |
Neil Fuller | cdf5695 | 2018-02-20 17:12:37 +0000 | [diff] [blame] | 233 | 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 Fuller | 8cec561 | 2017-10-11 10:38:17 +0100 | [diff] [blame] | 241 | BuildTzlookup(iana_data_dir) |
Neil Fuller | ad7e81c | 2017-06-15 16:45:56 +0100 | [diff] [blame] | 242 | |
Neil Fuller | e2d551b | 2018-10-29 15:32:06 +0000 | [diff] [blame] | 243 | # 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 Fuller | ad7e81c | 2017-06-15 16:45:56 +0100 | [diff] [blame] | 247 | |
Neil Fuller | ad3c876 | 2017-10-20 15:58:57 +0100 | [diff] [blame] | 248 | # Update test versions of distro files too. |
| 249 | UpdateTestFiles() |
| 250 | |
Neil Fuller | ad7e81c | 2017-06-15 16:45:56 +0100 | [diff] [blame] | 251 | print 'Look in %s and %s for new files' % (timezone_output_data_dir, icu_dir) |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 252 | sys.exit(0) |
| 253 | |
| 254 | |
| 255 | if __name__ == '__main__': |
| 256 | main() |