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 | |
| 19 | import ftplib |
| 20 | import glob |
| 21 | import httplib |
| 22 | import os |
| 23 | import re |
| 24 | import shutil |
| 25 | import subprocess |
| 26 | import sys |
| 27 | import tarfile |
| 28 | import tempfile |
| 29 | |
| 30 | sys.path.append('../../external/icu/tools') |
| 31 | import i18nutil |
Neil Fuller | 56166d3 | 2017-06-12 12:57:10 +0100 | [diff] [blame^] | 32 | import tzdatautil |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 33 | import updateicudata |
| 34 | |
| 35 | regions = ['africa', 'antarctica', 'asia', 'australasia', |
| 36 | 'etcetera', 'europe', 'northamerica', 'southamerica', |
| 37 | # These two deliberately come last so they override what came |
| 38 | # before (and each other). |
| 39 | 'backward', 'backzone' ] |
| 40 | |
| 41 | # Calculate the paths that are referred to by multiple functions. |
| 42 | android_build_top = i18nutil.GetAndroidRootOrDie() |
Neil Fuller | 56166d3 | 2017-06-12 12:57:10 +0100 | [diff] [blame^] | 43 | timezone_dir = os.path.realpath('%s/system/timezone' % android_build_top) |
| 44 | i18nutil.CheckDirExists(timezone_dir, 'system/timezone') |
| 45 | |
| 46 | zone_compactor_dir = os.path.realpath('%s/system/timezone/zone_compactor' % android_build_top) |
| 47 | i18nutil.CheckDirExists(timezone_dir, 'system/timezone/zone_zompactor') |
| 48 | |
| 49 | timezone_input_data_dir = os.path.realpath('%s/input_data' % timezone_dir) |
| 50 | |
| 51 | # TODO(nfuller): Move to {timezone_dir}/output_data. http://b/36882778 |
| 52 | timezone_output_data_dir = '%s/bionic/libc/zoneinfo' % android_build_top |
| 53 | i18nutil.CheckDirExists(timezone_output_data_dir, 'bionic/libc/zoneinfo') |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 54 | |
| 55 | tmp_dir = tempfile.mkdtemp('-tzdata') |
| 56 | |
| 57 | |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 58 | def WriteSetupFile(extracted_iana_dir): |
| 59 | """Writes the list of zones that ZoneCompactor should process.""" |
| 60 | links = [] |
| 61 | zones = [] |
| 62 | for region in regions: |
| 63 | for line in open('%s/%s' % (extracted_iana_dir, region)): |
| 64 | fields = line.split() |
| 65 | if fields: |
| 66 | if fields[0] == 'Link': |
| 67 | links.append('%s %s %s' % (fields[0], fields[1], fields[2])) |
| 68 | zones.append(fields[2]) |
| 69 | elif fields[0] == 'Zone': |
| 70 | zones.append(fields[1]) |
| 71 | zones.sort() |
| 72 | |
| 73 | zone_compactor_setup_file = '%s/setup' % tmp_dir |
| 74 | setup = open(zone_compactor_setup_file, 'w') |
| 75 | for link in sorted(set(links)): |
| 76 | setup.write('%s\n' % link) |
| 77 | for zone in sorted(set(zones)): |
| 78 | setup.write('%s\n' % zone) |
| 79 | setup.close() |
| 80 | return zone_compactor_setup_file |
| 81 | |
| 82 | |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 83 | def BuildIcuData(iana_tar_file): |
| 84 | icu_build_dir = '%s/icu' % tmp_dir |
| 85 | |
| 86 | updateicudata.PrepareIcuBuild(icu_build_dir) |
| 87 | updateicudata.MakeTzDataFiles(icu_build_dir, iana_tar_file) |
| 88 | updateicudata.MakeAndCopyIcuDataFiles(icu_build_dir) |
| 89 | |
| 90 | |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 91 | def BuildTzdata(iana_tar_file): |
| 92 | iana_tar_filename = os.path.basename(iana_tar_file) |
| 93 | new_version = re.search('(tzdata.+)\\.tar\\.gz', iana_tar_filename).group(1) |
| 94 | |
| 95 | print 'Extracting...' |
| 96 | extracted_iana_dir = '%s/extracted_iana' % tmp_dir |
| 97 | os.mkdir(extracted_iana_dir) |
| 98 | tar = tarfile.open(iana_tar_file, 'r') |
| 99 | tar.extractall(extracted_iana_dir) |
| 100 | |
| 101 | print 'Calling zic(1)...' |
| 102 | zic_output_dir = '%s/data' % tmp_dir |
| 103 | os.mkdir(zic_output_dir) |
| 104 | zic_generator_template = '%s/%%s' % extracted_iana_dir |
| 105 | zic_inputs = [ zic_generator_template % x for x in regions ] |
| 106 | zic_cmd = ['zic', '-d', zic_output_dir ] |
| 107 | zic_cmd.extend(zic_inputs) |
| 108 | subprocess.check_call(zic_cmd) |
| 109 | |
| 110 | zone_compactor_setup_file = WriteSetupFile(extracted_iana_dir) |
| 111 | |
| 112 | print 'Calling ZoneCompactor to update tzdata to %s...' % new_version |
| 113 | class_files_dir = '%s/classes' % tmp_dir |
| 114 | os.mkdir(class_files_dir) |
| 115 | |
| 116 | subprocess.check_call(['javac', '-d', class_files_dir, |
| 117 | '%s/main/java/ZoneCompactor.java' % zone_compactor_dir]) |
| 118 | |
| 119 | zone_tab_file = '%s/zone.tab' % extracted_iana_dir |
| 120 | subprocess.check_call(['java', '-cp', class_files_dir, 'ZoneCompactor', |
| 121 | zone_compactor_setup_file, zic_output_dir, zone_tab_file, |
Neil Fuller | 56166d3 | 2017-06-12 12:57:10 +0100 | [diff] [blame^] | 122 | timezone_output_data_dir, new_version]) |
| 123 | |
| 124 | |
| 125 | def BuildTzlookup(): |
| 126 | # We currently just copy a manually-maintained xml file. |
| 127 | tzlookup_source_file = '%s/android/tzlookup.xml' % timezone_input_data_dir |
| 128 | tzlookup_dest_file = '%s/tzlookup.xml' % timezone_output_data_dir |
| 129 | shutil.copyfile(tzlookup_source_file, tzlookup_dest_file) |
| 130 | |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 131 | |
| 132 | |
| 133 | # Run with no arguments from any directory, with no special setup required. |
| 134 | # See http://www.iana.org/time-zones/ for more about the source of this data. |
| 135 | def main(): |
Neil Fuller | 56166d3 | 2017-06-12 12:57:10 +0100 | [diff] [blame^] | 136 | print 'Found source data file structure in %s ...' % timezone_input_data_dir |
| 137 | |
| 138 | iana_data_dir = '%s/iana' % timezone_input_data_dir |
| 139 | iana_tar_file = tzdatautil.GetIanaTarFile(iana_data_dir) |
| 140 | print 'Found IANA time zone data %s ...' % iana_tar_file |
| 141 | |
| 142 | print 'Found android output dir in %s ...' % timezone_output_data_dir |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 143 | print 'Found icu in %s ...' % updateicudata.icuDir() |
| 144 | |
Neil Fuller | 56166d3 | 2017-06-12 12:57:10 +0100 | [diff] [blame^] | 145 | BuildIcuData(iana_tar_file) |
| 146 | BuildTzdata(iana_tar_file) |
| 147 | BuildTzlookup() |
| 148 | print 'Look in %s and %s for new data files' % (timezone_output_data_dir, updateicudata.icuDir()) |
Neil Fuller | 86e72c5 | 2017-06-12 15:36:06 +0100 | [diff] [blame] | 149 | sys.exit(0) |
| 150 | |
| 151 | |
| 152 | if __name__ == '__main__': |
| 153 | main() |