blob: 330f1662d275e3f91e7cedb1a0577865b51131c4 [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'])
120 print 'Making ICU tools...'
Elliott Hughes90cb5ff2014-08-06 15:23:11 -0700121 subprocess.check_call(['make', '-j32'])
Neil Fuller246c6882014-05-16 18:04:48 +0100122
123 # Run the ICU tools.
124 os.chdir('tools/tzcode')
125 shutil.copyfile('%s/%s' % (original_working_dir, data_filename), data_filename)
126 print 'Making ICU data...'
127 subprocess.check_call(['make'])
128
Elliott Hughesb5f5b0e2014-09-30 17:30:01 -0700129 # Copy the source file to its ultimate destination.
Neil Fuller246c6882014-05-16 18:04:48 +0100130 icu_txt_data_dir = '%s/data/misc' % icu_dir
131 print 'Copying zoneinfo64.txt to %s ...' % icu_txt_data_dir
132 shutil.copy('zoneinfo64.txt', icu_txt_data_dir)
133
Elliott Hughesb5f5b0e2014-09-30 17:30:01 -0700134 # Regenerate the .dat file.
Neil Fuller246c6882014-05-16 18:04:48 +0100135 os.chdir(icu_working_dir)
Elliott Hughesb5f5b0e2014-09-30 17:30:01 -0700136 subprocess.check_call(['make', '-j32'])
137
138 # Copy the .dat file to its ultimate destination.
Neil Fuller246c6882014-05-16 18:04:48 +0100139 icu_dat_data_dir = '%s/stubdata' % icu_dir
Neil Fuller43f37152014-05-21 16:59:09 +0100140 datfiles = glob.glob('data/out/tmp/icudt??l.dat')
141 if len(datfiles) != 1:
142 print 'ERROR: Unexpectedly found %d .dat files (%s). Halting.' % (len(datfiles), datfiles)
143 sys.exit(1)
Neil Fuller43f37152014-05-21 16:59:09 +0100144 datfile = datfiles[0]
145 print 'Copying %s to %s ...' % (datfile, icu_dat_data_dir)
146 shutil.copy(datfile, icu_dat_data_dir)
Neil Fuller246c6882014-05-16 18:04:48 +0100147
148 # Switch back to the original working cwd.
149 os.chdir(original_working_dir)
150
151
152def CheckSignature(data_filename):
Elliott Hughes676e66d2013-04-22 11:41:57 -0700153 signature_filename = '%s.asc' % data_filename
154 print 'Verifying signature...'
155 # If this fails for you, you probably need to import Paul Eggert's public key:
156 # gpg --recv-keys ED97E90E62AA7E34
157 subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify',
158 signature_filename, data_filename])
159
Neil Fuller246c6882014-05-16 18:04:48 +0100160
161def BuildBionicToolsAndData(data_filename):
162 new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1)
163
Elliott Hughes5d967e42012-07-20 16:52:39 -0700164 print 'Extracting...'
165 os.mkdir('extracted')
Elliott Hughese3063f42012-11-05 08:53:28 -0800166 tar = tarfile.open(data_filename, 'r')
Elliott Hughes5d967e42012-07-20 16:52:39 -0700167 tar.extractall('extracted')
168
169 print 'Calling zic(1)...'
170 os.mkdir('data')
Elliott Hughes371dcc12014-11-11 14:10:51 -0800171 zic_inputs = [ 'extracted/%s' % x for x in regions ]
172 zic_cmd = ['zic', '-d', 'data' ]
173 zic_cmd.extend(zic_inputs)
174 subprocess.check_call(zic_cmd)
Elliott Hughes5d967e42012-07-20 16:52:39 -0700175
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700176 WriteSetupFile()
Elliott Hughes5d967e42012-07-20 16:52:39 -0700177
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700178 print 'Calling ZoneCompactor to update bionic to %s...' % new_version
Elliott Hughes5d967e42012-07-20 16:52:39 -0700179 subprocess.check_call(['javac', '-d', '.',
Elliott Hughes90cb5ff2014-08-06 15:23:11 -0700180 '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir])
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700181 subprocess.check_call(['java', 'ZoneCompactor',
Elliott Hughes23935352012-10-22 14:47:58 -0700182 'setup', 'data', 'extracted/zone.tab',
183 bionic_libc_zoneinfo_dir, new_version])
Elliott Hughes5d967e42012-07-20 16:52:39 -0700184
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800185
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700186# Run with no arguments from any directory, with no special setup required.
Elliott Hughese3063f42012-11-05 08:53:28 -0800187# See http://www.iana.org/time-zones/ for more about the source of this data.
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700188def main():
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700189 print 'Looking for new tzdata...'
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700190
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700191 tzdata_filenames = []
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700192
193 # The FTP server lets you download intermediate releases, and also lets you
Elliott Hughes21da42e2013-04-22 13:44:50 -0700194 # download the signatures for verification, so it's your best choice.
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700195 use_ftp = True
196
197 if use_ftp:
198 ftp = ftplib.FTP('ftp.iana.org')
199 ftp.login()
200 ftp.cwd('tz/releases')
201 for filename in ftp.nlst():
202 if filename.startswith('tzdata20') and filename.endswith('.tar.gz'):
203 tzdata_filenames.append(filename)
204 tzdata_filenames.sort()
205 else:
206 http = httplib.HTTPConnection('www.iana.org')
207 http.request("GET", "/time-zones")
208 index_lines = http.getresponse().read().split('\n')
209 for line in index_lines:
210 m = re.compile('.*href="/time-zones/repository/releases/(tzdata20\d\d\c\.tar\.gz)".*').match(line)
211 if m:
212 tzdata_filenames.append(m.group(1))
Elliott Hughesbcb2eda2011-10-24 10:47:25 -0700213
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700214 # If you're several releases behind, we'll walk you through the upgrades
215 # one by one.
216 current_version = GetCurrentTzDataVersion()
217 current_filename = '%s.tar.gz' % current_version
218 for filename in tzdata_filenames:
219 if filename > current_filename:
220 print 'Found new tzdata: %s' % filename
Neil Fuller246c6882014-05-16 18:04:48 +0100221 SwitchToNewTemporaryDirectory()
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700222 if use_ftp:
Neil Fuller246c6882014-05-16 18:04:48 +0100223 FtpRetrieveFileAndSignature(ftp, filename)
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700224 else:
Neil Fuller246c6882014-05-16 18:04:48 +0100225 HttpRetrieveFileAndSignature(http, filename)
226
227 CheckSignature(filename)
228 BuildIcuToolsAndData(filename)
229 BuildBionicToolsAndData(filename)
230 print 'Look in %s and %s for new data files' % (bionic_dir, icu_dir)
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700231 sys.exit(0)
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800232
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700233 print 'You already have the latest tzdata (%s)!' % current_version
234 sys.exit(0)
235
236
237if __name__ == '__main__':
238 main()