blob: 68a5ff51c874d82d6a7559ad13b1e2ac545b5ddf [file] [log] [blame]
Elliott Hughes5d967e42012-07-20 16:52:39 -07001#!/usr/bin/python
Elliott Hughes5b1497a2012-10-19 14:47:37 -07002
Neil Fuller246c6882014-05-16 18:04:48 +01003"""Updates the timezone data held in bionic and ICU."""
Elliott Hughesd40e63e2011-02-17 16:20:07 -08004
Elliott Hughes5d967e42012-07-20 16:52:39 -07005import ftplib
Neil Fuller246c6882014-05-16 18:04:48 +01006import glob
Elliott Hughesf8dff7d2013-04-22 11:11:43 -07007import httplib
Elliott Hughes5d967e42012-07-20 16:52:39 -07008import os
9import re
Neil Fuller246c6882014-05-16 18:04:48 +010010import shutil
Elliott Hughes5d967e42012-07-20 16:52:39 -070011import subprocess
12import sys
13import tarfile
14import tempfile
Elliott Hughesd40e63e2011-02-17 16:20:07 -080015
Elliott Hughes2c2463b2014-11-11 14:10:51 -080016regions = ['africa', 'antarctica', 'asia', 'australasia',
17 'etcetera', 'europe', 'northamerica', 'southamerica',
18 # These two deliberately come last so they override what came
19 # before (and each other).
20 'backward', 'backzone' ]
Elliott Hughes5d967e42012-07-20 16:52:39 -070021
Neil Fuller246c6882014-05-16 18:04:48 +010022def CheckDirExists(dir, dirname):
23 if not os.path.isdir(dir):
24 print "Couldn't find %s (%s)!" % (dirname, dir)
25 sys.exit(1)
26
27bionic_libc_tools_zoneinfo_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
28
29# Find the bionic directory, searching upward from this script.
30bionic_dir = os.path.realpath('%s/../../..' % bionic_libc_tools_zoneinfo_dir)
31bionic_libc_zoneinfo_dir = '%s/libc/zoneinfo' % bionic_dir
32CheckDirExists(bionic_libc_zoneinfo_dir, 'bionic/libc/zoneinfo')
33CheckDirExists(bionic_libc_tools_zoneinfo_dir, 'bionic/libc/tools/zoneinfo')
34print 'Found bionic in %s ...' % bionic_dir
35
Neil Fuller4d3abcb2015-04-09 09:22:25 +010036# Find the icu directory.
37icu_dir = os.path.realpath('%s/../external/icu' % bionic_dir)
38icu4c_dir = os.path.realpath('%s/icu4c/source' % icu_dir)
39icu4j_dir = os.path.realpath('%s/icu4j' % icu_dir)
40CheckDirExists(icu4c_dir, 'external/icu/icu4c/source')
41CheckDirExists(icu4j_dir, 'external/icu/icu4j')
Neil Fuller246c6882014-05-16 18:04:48 +010042print 'Found icu in %s ...' % icu_dir
43
Elliott Hughes5d967e42012-07-20 16:52:39 -070044
Elliott Hughes5b1497a2012-10-19 14:47:37 -070045def GetCurrentTzDataVersion():
Elliott Hughese3063f42012-11-05 08:53:28 -080046 return open('%s/tzdata' % bionic_libc_zoneinfo_dir).read().split('\x00', 1)[0]
Elliott Hughes5d967e42012-07-20 16:52:39 -070047
48
Elliott Hughes5b1497a2012-10-19 14:47:37 -070049def WriteSetupFile():
Elliott Hughese3063f42012-11-05 08:53:28 -080050 """Writes the list of zones that ZoneCompactor should process."""
Elliott Hughes5b1497a2012-10-19 14:47:37 -070051 links = []
52 zones = []
53 for region in regions:
54 for line in open('extracted/%s' % region):
55 fields = line.split()
Elliott Hughese3063f42012-11-05 08:53:28 -080056 if fields:
57 if fields[0] == 'Link':
Elliott Hughes2c2463b2014-11-11 14:10:51 -080058 links.append('%s %s %s' % (fields[0], fields[1], fields[2]))
Elliott Hughese3063f42012-11-05 08:53:28 -080059 zones.append(fields[2])
60 elif fields[0] == 'Zone':
61 zones.append(fields[1])
Elliott Hughes5b1497a2012-10-19 14:47:37 -070062 zones.sort()
63
64 setup = open('setup', 'w')
Elliott Hughes2c2463b2014-11-11 14:10:51 -080065 for link in sorted(set(links)):
66 setup.write('%s\n' % link)
67 for zone in sorted(set(zones)):
Elliott Hughes5b1497a2012-10-19 14:47:37 -070068 setup.write('%s\n' % zone)
69 setup.close()
Elliott Hughes5d967e42012-07-20 16:52:39 -070070
71
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070072def SwitchToNewTemporaryDirectory():
Elliott Hughes5d967e42012-07-20 16:52:39 -070073 tmp_dir = tempfile.mkdtemp('-tzdata')
74 os.chdir(tmp_dir)
75 print 'Created temporary directory "%s"...' % tmp_dir
76
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070077
Neil Fuller246c6882014-05-16 18:04:48 +010078def FtpRetrieveFile(ftp, filename):
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070079 ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
80
81
Neil Fuller246c6882014-05-16 18:04:48 +010082def FtpRetrieveFileAndSignature(ftp, data_filename):
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070083 """Downloads and repackages the given data from the given FTP server."""
Elliott Hughes5d2ef872012-11-26 13:44:49 -080084 print 'Downloading data...'
Neil Fuller246c6882014-05-16 18:04:48 +010085 FtpRetrieveFile(ftp, data_filename)
Elliott Hughes5d2ef872012-11-26 13:44:49 -080086
87 print 'Downloading signature...'
88 signature_filename = '%s.asc' % data_filename
Neil Fuller246c6882014-05-16 18:04:48 +010089 FtpRetrieveFile(ftp, signature_filename)
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070090
91
Neil Fuller246c6882014-05-16 18:04:48 +010092def HttpRetrieveFile(http, path, output_filename):
Elliott Hughes676e66d2013-04-22 11:41:57 -070093 http.request("GET", path)
94 f = open(output_filename, 'wb')
95 f.write(http.getresponse().read())
96 f.close()
97
98
Neil Fuller246c6882014-05-16 18:04:48 +010099def HttpRetrieveFileAndSignature(http, data_filename):
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700100 """Downloads and repackages the given data from the given HTTP server."""
Elliott Hughes676e66d2013-04-22 11:41:57 -0700101 path = "/time-zones/repository/releases/%s" % data_filename
102
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700103 print 'Downloading data...'
Neil Fuller246c6882014-05-16 18:04:48 +0100104 HttpRetrieveFile(http, path, data_filename)
Elliott Hughes676e66d2013-04-22 11:41:57 -0700105
106 print 'Downloading signature...'
107 signature_filename = '%s.asc' % data_filename
Neil Fuller246c6882014-05-16 18:04:48 +0100108 HttpRetrievefile(http, "%s.asc" % path, signature_filename)
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700109
110
Neil Fuller246c6882014-05-16 18:04:48 +0100111def BuildIcuToolsAndData(data_filename):
112 # Keep track of the original cwd so we can go back to it at the end.
113 original_working_dir = os.getcwd()
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700114
Neil Fuller246c6882014-05-16 18:04:48 +0100115 # Create a directory to run 'make' from.
116 icu_working_dir = '%s/icu' % original_working_dir
117 os.mkdir(icu_working_dir)
118 os.chdir(icu_working_dir)
119
120 # Build the ICU tools.
121 print 'Configuring ICU tools...'
Neil Fuller4d3abcb2015-04-09 09:22:25 +0100122 subprocess.check_call(['%s/runConfigureICU' % icu4c_dir, 'Linux'])
Neil Fuller246c6882014-05-16 18:04:48 +0100123
124 # Run the ICU tools.
125 os.chdir('tools/tzcode')
Neil Fuller0662c3e2015-02-02 16:50:05 +0000126
127 # The tz2icu tool only picks up icuregions and icuzones in they are in the CWD
128 for icu_data_file in [ 'icuregions', 'icuzones']:
Neil Fuller4d3abcb2015-04-09 09:22:25 +0100129 icu_data_file_source = '%s/tools/tzcode/%s' % (icu4c_dir, icu_data_file)
Neil Fuller0662c3e2015-02-02 16:50:05 +0000130 icu_data_file_symlink = './%s' % icu_data_file
131 os.symlink(icu_data_file_source, icu_data_file_symlink)
132
Neil Fuller246c6882014-05-16 18:04:48 +0100133 shutil.copyfile('%s/%s' % (original_working_dir, data_filename), data_filename)
134 print 'Making ICU data...'
Neil Fuller0662c3e2015-02-02 16:50:05 +0000135 # The Makefile assumes the existence of the bin directory.
136 os.mkdir('%s/bin' % icu_working_dir)
Neil Fuller246c6882014-05-16 18:04:48 +0100137 subprocess.check_call(['make'])
138
Elliott Hughesf8896c62014-09-30 17:30:01 -0700139 # Copy the source file to its ultimate destination.
Neil Fuller4d3abcb2015-04-09 09:22:25 +0100140 icu_txt_data_dir = '%s/data/misc' % icu4c_dir
Neil Fuller246c6882014-05-16 18:04:48 +0100141 print 'Copying zoneinfo64.txt to %s ...' % icu_txt_data_dir
142 shutil.copy('zoneinfo64.txt', icu_txt_data_dir)
143
Elliott Hughesf8896c62014-09-30 17:30:01 -0700144 # Regenerate the .dat file.
Neil Fuller246c6882014-05-16 18:04:48 +0100145 os.chdir(icu_working_dir)
Fredrik Roubertbdd84522015-02-04 17:17:34 +0100146 subprocess.check_call(['make', 'INCLUDE_UNI_CORE_DATA=1', '-j32'])
Elliott Hughesf8896c62014-09-30 17:30:01 -0700147
148 # Copy the .dat file to its ultimate destination.
Neil Fuller4d3abcb2015-04-09 09:22:25 +0100149 icu_dat_data_dir = '%s/stubdata' % icu4c_dir
Neil Fuller43f37152014-05-21 16:59:09 +0100150 datfiles = glob.glob('data/out/tmp/icudt??l.dat')
151 if len(datfiles) != 1:
152 print 'ERROR: Unexpectedly found %d .dat files (%s). Halting.' % (len(datfiles), datfiles)
153 sys.exit(1)
Neil Fuller43f37152014-05-21 16:59:09 +0100154 datfile = datfiles[0]
155 print 'Copying %s to %s ...' % (datfile, icu_dat_data_dir)
156 shutil.copy(datfile, icu_dat_data_dir)
Neil Fuller246c6882014-05-16 18:04:48 +0100157
Neil Fuller4d3abcb2015-04-09 09:22:25 +0100158 # Generate the ICU4J .jar files
159 os.chdir('%s/data' % icu_working_dir)
160 subprocess.check_call(['make', 'icu4j-data'])
161
162 # Copy the ICU4J .jar files to their ultimate destination.
163 icu_jar_data_dir = '%s/main/shared/data' % icu4j_dir
164 jarfiles = glob.glob('out/icu4j/*.jar')
165 if len(jarfiles) != 2:
166 print 'ERROR: Unexpectedly found %d .jar files (%s). Halting.' % (len(jarfiles), jarfiles)
167 sys.exit(1)
168 for jarfile in jarfiles:
169 print 'Copying %s to %s ...' % (jarfile, icu_jar_data_dir)
170 shutil.copy(jarfile, icu_jar_data_dir)
171
Neil Fuller246c6882014-05-16 18:04:48 +0100172 # Switch back to the original working cwd.
173 os.chdir(original_working_dir)
174
175
176def CheckSignature(data_filename):
Elliott Hughes676e66d2013-04-22 11:41:57 -0700177 signature_filename = '%s.asc' % data_filename
178 print 'Verifying signature...'
179 # If this fails for you, you probably need to import Paul Eggert's public key:
180 # gpg --recv-keys ED97E90E62AA7E34
181 subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify',
182 signature_filename, data_filename])
183
Neil Fuller246c6882014-05-16 18:04:48 +0100184
185def BuildBionicToolsAndData(data_filename):
186 new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1)
187
Elliott Hughes5d967e42012-07-20 16:52:39 -0700188 print 'Extracting...'
189 os.mkdir('extracted')
Elliott Hughese3063f42012-11-05 08:53:28 -0800190 tar = tarfile.open(data_filename, 'r')
Elliott Hughes5d967e42012-07-20 16:52:39 -0700191 tar.extractall('extracted')
192
193 print 'Calling zic(1)...'
194 os.mkdir('data')
Elliott Hughes2c2463b2014-11-11 14:10:51 -0800195 zic_inputs = [ 'extracted/%s' % x for x in regions ]
196 zic_cmd = ['zic', '-d', 'data' ]
197 zic_cmd.extend(zic_inputs)
198 subprocess.check_call(zic_cmd)
Elliott Hughes5d967e42012-07-20 16:52:39 -0700199
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700200 WriteSetupFile()
Elliott Hughes5d967e42012-07-20 16:52:39 -0700201
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700202 print 'Calling ZoneCompactor to update bionic to %s...' % new_version
Elliott Hughes5d967e42012-07-20 16:52:39 -0700203 subprocess.check_call(['javac', '-d', '.',
Elliott Hughes13bab432014-08-06 15:23:11 -0700204 '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir])
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700205 subprocess.check_call(['java', 'ZoneCompactor',
Elliott Hughes23935352012-10-22 14:47:58 -0700206 'setup', 'data', 'extracted/zone.tab',
207 bionic_libc_zoneinfo_dir, new_version])
Elliott Hughes5d967e42012-07-20 16:52:39 -0700208
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800209
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700210# Run with no arguments from any directory, with no special setup required.
Elliott Hughese3063f42012-11-05 08:53:28 -0800211# See http://www.iana.org/time-zones/ for more about the source of this data.
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700212def main():
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700213 print 'Looking for new tzdata...'
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700214
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700215 tzdata_filenames = []
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700216
217 # The FTP server lets you download intermediate releases, and also lets you
Elliott Hughes21da42e2013-04-22 13:44:50 -0700218 # download the signatures for verification, so it's your best choice.
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700219 use_ftp = True
220
221 if use_ftp:
222 ftp = ftplib.FTP('ftp.iana.org')
223 ftp.login()
224 ftp.cwd('tz/releases')
225 for filename in ftp.nlst():
226 if filename.startswith('tzdata20') and filename.endswith('.tar.gz'):
227 tzdata_filenames.append(filename)
228 tzdata_filenames.sort()
229 else:
230 http = httplib.HTTPConnection('www.iana.org')
231 http.request("GET", "/time-zones")
232 index_lines = http.getresponse().read().split('\n')
233 for line in index_lines:
234 m = re.compile('.*href="/time-zones/repository/releases/(tzdata20\d\d\c\.tar\.gz)".*').match(line)
235 if m:
236 tzdata_filenames.append(m.group(1))
Elliott Hughesbcb2eda2011-10-24 10:47:25 -0700237
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700238 # If you're several releases behind, we'll walk you through the upgrades
239 # one by one.
240 current_version = GetCurrentTzDataVersion()
241 current_filename = '%s.tar.gz' % current_version
242 for filename in tzdata_filenames:
243 if filename > current_filename:
244 print 'Found new tzdata: %s' % filename
Neil Fuller246c6882014-05-16 18:04:48 +0100245 SwitchToNewTemporaryDirectory()
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700246 if use_ftp:
Neil Fuller246c6882014-05-16 18:04:48 +0100247 FtpRetrieveFileAndSignature(ftp, filename)
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700248 else:
Neil Fuller246c6882014-05-16 18:04:48 +0100249 HttpRetrieveFileAndSignature(http, filename)
250
251 CheckSignature(filename)
252 BuildIcuToolsAndData(filename)
253 BuildBionicToolsAndData(filename)
254 print 'Look in %s and %s for new data files' % (bionic_dir, icu_dir)
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700255 sys.exit(0)
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800256
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700257 print 'You already have the latest tzdata (%s)!' % current_version
258 sys.exit(0)
259
260
261if __name__ == '__main__':
262 main()