blob: 4847356dfa090c3575a9ba6a5a89d862ad5c6e20 [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 Hughes371dcc12014-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
36# Find the icu4c directory.
Elliott Hughes30ab9392014-07-09 15:42:59 -070037icu_dir = os.path.realpath('%s/../external/icu/icu4c/source' % bionic_dir)
38CheckDirExists(icu_dir, 'external/icu/icu4c/source')
Neil Fuller246c6882014-05-16 18:04:48 +010039print 'Found icu in %s ...' % icu_dir
40
Elliott Hughes5d967e42012-07-20 16:52:39 -070041
Elliott Hughes5b1497a2012-10-19 14:47:37 -070042def GetCurrentTzDataVersion():
Elliott Hughese3063f42012-11-05 08:53:28 -080043 return open('%s/tzdata' % bionic_libc_zoneinfo_dir).read().split('\x00', 1)[0]
Elliott Hughes5d967e42012-07-20 16:52:39 -070044
45
Elliott Hughes5b1497a2012-10-19 14:47:37 -070046def WriteSetupFile():
Elliott Hughese3063f42012-11-05 08:53:28 -080047 """Writes the list of zones that ZoneCompactor should process."""
Elliott Hughes5b1497a2012-10-19 14:47:37 -070048 links = []
49 zones = []
50 for region in regions:
51 for line in open('extracted/%s' % region):
52 fields = line.split()
Elliott Hughese3063f42012-11-05 08:53:28 -080053 if fields:
54 if fields[0] == 'Link':
Elliott Hughes371dcc12014-11-11 14:10:51 -080055 links.append('%s %s %s' % (fields[0], fields[1], fields[2]))
Elliott Hughese3063f42012-11-05 08:53:28 -080056 zones.append(fields[2])
57 elif fields[0] == 'Zone':
58 zones.append(fields[1])
Elliott Hughes5b1497a2012-10-19 14:47:37 -070059 zones.sort()
60
61 setup = open('setup', 'w')
Elliott Hughes371dcc12014-11-11 14:10:51 -080062 for link in sorted(set(links)):
63 setup.write('%s\n' % link)
64 for zone in sorted(set(zones)):
Elliott Hughes5b1497a2012-10-19 14:47:37 -070065 setup.write('%s\n' % zone)
66 setup.close()
Elliott Hughes5d967e42012-07-20 16:52:39 -070067
68
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070069def SwitchToNewTemporaryDirectory():
Elliott Hughes5d967e42012-07-20 16:52:39 -070070 tmp_dir = tempfile.mkdtemp('-tzdata')
71 os.chdir(tmp_dir)
72 print 'Created temporary directory "%s"...' % tmp_dir
73
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070074
Neil Fuller246c6882014-05-16 18:04:48 +010075def FtpRetrieveFile(ftp, filename):
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070076 ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
77
78
Neil Fuller246c6882014-05-16 18:04:48 +010079def FtpRetrieveFileAndSignature(ftp, data_filename):
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070080 """Downloads and repackages the given data from the given FTP server."""
Elliott Hughes5d2ef872012-11-26 13:44:49 -080081 print 'Downloading data...'
Neil Fuller246c6882014-05-16 18:04:48 +010082 FtpRetrieveFile(ftp, data_filename)
Elliott Hughes5d2ef872012-11-26 13:44:49 -080083
84 print 'Downloading signature...'
85 signature_filename = '%s.asc' % data_filename
Neil Fuller246c6882014-05-16 18:04:48 +010086 FtpRetrieveFile(ftp, signature_filename)
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070087
88
Neil Fuller246c6882014-05-16 18:04:48 +010089def HttpRetrieveFile(http, path, output_filename):
Elliott Hughes676e66d2013-04-22 11:41:57 -070090 http.request("GET", path)
91 f = open(output_filename, 'wb')
92 f.write(http.getresponse().read())
93 f.close()
94
95
Neil Fuller246c6882014-05-16 18:04:48 +010096def HttpRetrieveFileAndSignature(http, data_filename):
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070097 """Downloads and repackages the given data from the given HTTP server."""
Elliott Hughes676e66d2013-04-22 11:41:57 -070098 path = "/time-zones/repository/releases/%s" % data_filename
99
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700100 print 'Downloading data...'
Neil Fuller246c6882014-05-16 18:04:48 +0100101 HttpRetrieveFile(http, path, data_filename)
Elliott Hughes676e66d2013-04-22 11:41:57 -0700102
103 print 'Downloading signature...'
104 signature_filename = '%s.asc' % data_filename
Neil Fuller246c6882014-05-16 18:04:48 +0100105 HttpRetrievefile(http, "%s.asc" % path, signature_filename)
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700106
107
Neil Fuller246c6882014-05-16 18:04:48 +0100108def BuildIcuToolsAndData(data_filename):
109 # Keep track of the original cwd so we can go back to it at the end.
110 original_working_dir = os.getcwd()
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700111
Neil Fuller246c6882014-05-16 18:04:48 +0100112 # Create a directory to run 'make' from.
113 icu_working_dir = '%s/icu' % original_working_dir
114 os.mkdir(icu_working_dir)
115 os.chdir(icu_working_dir)
116
117 # Build the ICU tools.
118 print 'Configuring ICU tools...'
119 subprocess.check_call(['%s/runConfigureICU' % icu_dir, 'Linux'])
Neil Fuller246c6882014-05-16 18:04:48 +0100120
121 # Run the ICU tools.
122 os.chdir('tools/tzcode')
Neil Fuller4177bd82015-02-02 16:50:05 +0000123
124 # The tz2icu tool only picks up icuregions and icuzones in they are in the CWD
125 for icu_data_file in [ 'icuregions', 'icuzones']:
126 icu_data_file_source = '%s/tools/tzcode/%s' % (icu_dir, icu_data_file)
127 icu_data_file_symlink = './%s' % icu_data_file
128 os.symlink(icu_data_file_source, icu_data_file_symlink)
129
Neil Fuller246c6882014-05-16 18:04:48 +0100130 shutil.copyfile('%s/%s' % (original_working_dir, data_filename), data_filename)
131 print 'Making ICU data...'
Neil Fuller4177bd82015-02-02 16:50:05 +0000132 # The Makefile assumes the existence of the bin directory.
133 os.mkdir('%s/bin' % icu_working_dir)
Neil Fuller246c6882014-05-16 18:04:48 +0100134 subprocess.check_call(['make'])
135
Elliott Hughesb5f5b0e2014-09-30 17:30:01 -0700136 # Copy the source file to its ultimate destination.
Neil Fuller246c6882014-05-16 18:04:48 +0100137 icu_txt_data_dir = '%s/data/misc' % icu_dir
138 print 'Copying zoneinfo64.txt to %s ...' % icu_txt_data_dir
139 shutil.copy('zoneinfo64.txt', icu_txt_data_dir)
140
Elliott Hughesb5f5b0e2014-09-30 17:30:01 -0700141 # Regenerate the .dat file.
Neil Fuller246c6882014-05-16 18:04:48 +0100142 os.chdir(icu_working_dir)
Elliott Hughesb5f5b0e2014-09-30 17:30:01 -0700143 subprocess.check_call(['make', '-j32'])
144
145 # Copy the .dat file to its ultimate destination.
Neil Fuller246c6882014-05-16 18:04:48 +0100146 icu_dat_data_dir = '%s/stubdata' % icu_dir
Neil Fuller43f37152014-05-21 16:59:09 +0100147 datfiles = glob.glob('data/out/tmp/icudt??l.dat')
148 if len(datfiles) != 1:
149 print 'ERROR: Unexpectedly found %d .dat files (%s). Halting.' % (len(datfiles), datfiles)
150 sys.exit(1)
Neil Fuller43f37152014-05-21 16:59:09 +0100151 datfile = datfiles[0]
152 print 'Copying %s to %s ...' % (datfile, icu_dat_data_dir)
153 shutil.copy(datfile, icu_dat_data_dir)
Neil Fuller246c6882014-05-16 18:04:48 +0100154
155 # Switch back to the original working cwd.
156 os.chdir(original_working_dir)
157
158
159def CheckSignature(data_filename):
Elliott Hughes676e66d2013-04-22 11:41:57 -0700160 signature_filename = '%s.asc' % data_filename
161 print 'Verifying signature...'
162 # If this fails for you, you probably need to import Paul Eggert's public key:
163 # gpg --recv-keys ED97E90E62AA7E34
164 subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify',
165 signature_filename, data_filename])
166
Neil Fuller246c6882014-05-16 18:04:48 +0100167
168def BuildBionicToolsAndData(data_filename):
169 new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1)
170
Elliott Hughes5d967e42012-07-20 16:52:39 -0700171 print 'Extracting...'
172 os.mkdir('extracted')
Elliott Hughese3063f42012-11-05 08:53:28 -0800173 tar = tarfile.open(data_filename, 'r')
Elliott Hughes5d967e42012-07-20 16:52:39 -0700174 tar.extractall('extracted')
175
176 print 'Calling zic(1)...'
177 os.mkdir('data')
Elliott Hughes371dcc12014-11-11 14:10:51 -0800178 zic_inputs = [ 'extracted/%s' % x for x in regions ]
179 zic_cmd = ['zic', '-d', 'data' ]
180 zic_cmd.extend(zic_inputs)
181 subprocess.check_call(zic_cmd)
Elliott Hughes5d967e42012-07-20 16:52:39 -0700182
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700183 WriteSetupFile()
Elliott Hughes5d967e42012-07-20 16:52:39 -0700184
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700185 print 'Calling ZoneCompactor to update bionic to %s...' % new_version
Elliott Hughes5d967e42012-07-20 16:52:39 -0700186 subprocess.check_call(['javac', '-d', '.',
Elliott Hughes90cb5ff2014-08-06 15:23:11 -0700187 '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir])
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700188 subprocess.check_call(['java', 'ZoneCompactor',
Elliott Hughes23935352012-10-22 14:47:58 -0700189 'setup', 'data', 'extracted/zone.tab',
190 bionic_libc_zoneinfo_dir, new_version])
Elliott Hughes5d967e42012-07-20 16:52:39 -0700191
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800192
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700193# Run with no arguments from any directory, with no special setup required.
Elliott Hughese3063f42012-11-05 08:53:28 -0800194# See http://www.iana.org/time-zones/ for more about the source of this data.
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700195def main():
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700196 print 'Looking for new tzdata...'
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700197
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700198 tzdata_filenames = []
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700199
200 # The FTP server lets you download intermediate releases, and also lets you
Elliott Hughes21da42e2013-04-22 13:44:50 -0700201 # download the signatures for verification, so it's your best choice.
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700202 use_ftp = True
203
204 if use_ftp:
205 ftp = ftplib.FTP('ftp.iana.org')
206 ftp.login()
207 ftp.cwd('tz/releases')
208 for filename in ftp.nlst():
209 if filename.startswith('tzdata20') and filename.endswith('.tar.gz'):
210 tzdata_filenames.append(filename)
211 tzdata_filenames.sort()
212 else:
213 http = httplib.HTTPConnection('www.iana.org')
214 http.request("GET", "/time-zones")
215 index_lines = http.getresponse().read().split('\n')
216 for line in index_lines:
217 m = re.compile('.*href="/time-zones/repository/releases/(tzdata20\d\d\c\.tar\.gz)".*').match(line)
218 if m:
219 tzdata_filenames.append(m.group(1))
Elliott Hughesbcb2eda2011-10-24 10:47:25 -0700220
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700221 # If you're several releases behind, we'll walk you through the upgrades
222 # one by one.
223 current_version = GetCurrentTzDataVersion()
224 current_filename = '%s.tar.gz' % current_version
225 for filename in tzdata_filenames:
226 if filename > current_filename:
227 print 'Found new tzdata: %s' % filename
Neil Fuller246c6882014-05-16 18:04:48 +0100228 SwitchToNewTemporaryDirectory()
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700229 if use_ftp:
Neil Fuller246c6882014-05-16 18:04:48 +0100230 FtpRetrieveFileAndSignature(ftp, filename)
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700231 else:
Neil Fuller246c6882014-05-16 18:04:48 +0100232 HttpRetrieveFileAndSignature(http, filename)
233
234 CheckSignature(filename)
235 BuildIcuToolsAndData(filename)
236 BuildBionicToolsAndData(filename)
237 print 'Look in %s and %s for new data files' % (bionic_dir, icu_dir)
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700238 sys.exit(0)
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800239
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700240 print 'You already have the latest tzdata (%s)!' % current_version
241 sys.exit(0)
242
243
244if __name__ == '__main__':
245 main()