blob: efdb4c8c418284c982df72146d94935f6cf3d3a1 [file] [log] [blame]
Neil Fuller86e72c52017-06-12 15:36:06 +01001#!/usr/bin/python -B
2
Neil Fuller56166d32017-06-12 12:57:10 +01003# 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 Fuller86e72c52017-06-12 15:36:06 +010018
19import ftplib
20import glob
21import httplib
22import os
23import re
24import shutil
25import subprocess
26import sys
27import tarfile
28import tempfile
29
30sys.path.append('../../external/icu/tools')
31import i18nutil
Neil Fuller56166d32017-06-12 12:57:10 +010032import tzdatautil
Neil Fuller86e72c52017-06-12 15:36:06 +010033import updateicudata
34
35regions = ['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.
42android_build_top = i18nutil.GetAndroidRootOrDie()
Neil Fuller56166d32017-06-12 12:57:10 +010043timezone_dir = os.path.realpath('%s/system/timezone' % android_build_top)
44i18nutil.CheckDirExists(timezone_dir, 'system/timezone')
45
46zone_compactor_dir = os.path.realpath('%s/system/timezone/zone_compactor' % android_build_top)
47i18nutil.CheckDirExists(timezone_dir, 'system/timezone/zone_zompactor')
48
49timezone_input_data_dir = os.path.realpath('%s/input_data' % timezone_dir)
50
51# TODO(nfuller): Move to {timezone_dir}/output_data. http://b/36882778
Neil Fuller35467e12017-06-12 16:55:44 +010052timezone_output_data_dir = '%s/output_data' % timezone_dir
53i18nutil.CheckDirExists(timezone_output_data_dir, 'output_data')
Neil Fuller86e72c52017-06-12 15:36:06 +010054
55tmp_dir = tempfile.mkdtemp('-tzdata')
56
57
Neil Fuller86e72c52017-06-12 15:36:06 +010058def 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 Fuller86e72c52017-06-12 15:36:06 +010083def 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 Fuller86e72c52017-06-12 15:36:06 +010091def 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
Neil Fuller35467e12017-06-12 16:55:44 +0100120
121 iana_output_data_dir = '%s/iana' % timezone_output_data_dir
Neil Fuller86e72c52017-06-12 15:36:06 +0100122 subprocess.check_call(['java', '-cp', class_files_dir, 'ZoneCompactor',
123 zone_compactor_setup_file, zic_output_dir, zone_tab_file,
Neil Fuller35467e12017-06-12 16:55:44 +0100124 iana_output_data_dir, new_version])
Neil Fuller56166d32017-06-12 12:57:10 +0100125
126
127def BuildTzlookup():
128 # We currently just copy a manually-maintained xml file.
129 tzlookup_source_file = '%s/android/tzlookup.xml' % timezone_input_data_dir
Neil Fuller35467e12017-06-12 16:55:44 +0100130 tzlookup_dest_file = '%s/android/tzlookup.xml' % timezone_output_data_dir
Neil Fuller56166d32017-06-12 12:57:10 +0100131 shutil.copyfile(tzlookup_source_file, tzlookup_dest_file)
132
Neil Fuller86e72c52017-06-12 15:36:06 +0100133
134
135# Run with no arguments from any directory, with no special setup required.
136# See http://www.iana.org/time-zones/ for more about the source of this data.
137def main():
Neil Fuller56166d32017-06-12 12:57:10 +0100138 print 'Found source data file structure in %s ...' % timezone_input_data_dir
139
140 iana_data_dir = '%s/iana' % timezone_input_data_dir
141 iana_tar_file = tzdatautil.GetIanaTarFile(iana_data_dir)
142 print 'Found IANA time zone data %s ...' % iana_tar_file
143
144 print 'Found android output dir in %s ...' % timezone_output_data_dir
Neil Fuller86e72c52017-06-12 15:36:06 +0100145 print 'Found icu in %s ...' % updateicudata.icuDir()
146
Neil Fuller56166d32017-06-12 12:57:10 +0100147 BuildIcuData(iana_tar_file)
148 BuildTzdata(iana_tar_file)
149 BuildTzlookup()
150 print 'Look in %s and %s for new data files' % (timezone_output_data_dir, updateicudata.icuDir())
Neil Fuller86e72c52017-06-12 15:36:06 +0100151 sys.exit(0)
152
153
154if __name__ == '__main__':
155 main()