blob: f5681beb22dbd8c8f25e8347bcea8b6955164400 [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 Hughes5b1497a2012-10-19 14:47:37 -070016regions = ['africa', 'antarctica', 'asia', 'australasia', 'backward',
17 'etcetera', 'europe', 'northamerica', 'southamerica']
Elliott Hughes5d967e42012-07-20 16:52:39 -070018
Neil Fuller246c6882014-05-16 18:04:48 +010019def CheckDirExists(dir, dirname):
20 if not os.path.isdir(dir):
21 print "Couldn't find %s (%s)!" % (dirname, dir)
22 sys.exit(1)
23
24bionic_libc_tools_zoneinfo_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
25
26# Find the bionic directory, searching upward from this script.
27bionic_dir = os.path.realpath('%s/../../..' % bionic_libc_tools_zoneinfo_dir)
28bionic_libc_zoneinfo_dir = '%s/libc/zoneinfo' % bionic_dir
29CheckDirExists(bionic_libc_zoneinfo_dir, 'bionic/libc/zoneinfo')
30CheckDirExists(bionic_libc_tools_zoneinfo_dir, 'bionic/libc/tools/zoneinfo')
31print 'Found bionic in %s ...' % bionic_dir
32
33# Find the icu4c directory.
Elliott Hughes30ab9392014-07-09 15:42:59 -070034icu_dir = os.path.realpath('%s/../external/icu/icu4c/source' % bionic_dir)
35CheckDirExists(icu_dir, 'external/icu/icu4c/source')
Neil Fuller246c6882014-05-16 18:04:48 +010036print 'Found icu in %s ...' % icu_dir
37
Elliott Hughes5d967e42012-07-20 16:52:39 -070038
Elliott Hughes5b1497a2012-10-19 14:47:37 -070039def GetCurrentTzDataVersion():
Elliott Hughese3063f42012-11-05 08:53:28 -080040 return open('%s/tzdata' % bionic_libc_zoneinfo_dir).read().split('\x00', 1)[0]
Elliott Hughes5d967e42012-07-20 16:52:39 -070041
42
Elliott Hughes5b1497a2012-10-19 14:47:37 -070043def WriteSetupFile():
Elliott Hughese3063f42012-11-05 08:53:28 -080044 """Writes the list of zones that ZoneCompactor should process."""
Elliott Hughes5b1497a2012-10-19 14:47:37 -070045 links = []
46 zones = []
47 for region in regions:
48 for line in open('extracted/%s' % region):
49 fields = line.split()
Elliott Hughese3063f42012-11-05 08:53:28 -080050 if fields:
51 if fields[0] == 'Link':
52 links.append('%s %s %s\n' % (fields[0], fields[1], fields[2]))
53 zones.append(fields[2])
54 elif fields[0] == 'Zone':
55 zones.append(fields[1])
Elliott Hughes5b1497a2012-10-19 14:47:37 -070056 zones.sort()
57
58 setup = open('setup', 'w')
59 for link in links:
60 setup.write(link)
61 for zone in zones:
62 setup.write('%s\n' % zone)
63 setup.close()
Elliott Hughes5d967e42012-07-20 16:52:39 -070064
65
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070066def SwitchToNewTemporaryDirectory():
Elliott Hughes5d967e42012-07-20 16:52:39 -070067 tmp_dir = tempfile.mkdtemp('-tzdata')
68 os.chdir(tmp_dir)
69 print 'Created temporary directory "%s"...' % tmp_dir
70
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070071
Neil Fuller246c6882014-05-16 18:04:48 +010072def FtpRetrieveFile(ftp, filename):
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070073 ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
74
75
Neil Fuller246c6882014-05-16 18:04:48 +010076def FtpRetrieveFileAndSignature(ftp, data_filename):
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070077 """Downloads and repackages the given data from the given FTP server."""
Elliott Hughes5d2ef872012-11-26 13:44:49 -080078 print 'Downloading data...'
Neil Fuller246c6882014-05-16 18:04:48 +010079 FtpRetrieveFile(ftp, data_filename)
Elliott Hughes5d2ef872012-11-26 13:44:49 -080080
81 print 'Downloading signature...'
82 signature_filename = '%s.asc' % data_filename
Neil Fuller246c6882014-05-16 18:04:48 +010083 FtpRetrieveFile(ftp, signature_filename)
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070084
85
Neil Fuller246c6882014-05-16 18:04:48 +010086def HttpRetrieveFile(http, path, output_filename):
Elliott Hughes676e66d2013-04-22 11:41:57 -070087 http.request("GET", path)
88 f = open(output_filename, 'wb')
89 f.write(http.getresponse().read())
90 f.close()
91
92
Neil Fuller246c6882014-05-16 18:04:48 +010093def HttpRetrieveFileAndSignature(http, data_filename):
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070094 """Downloads and repackages the given data from the given HTTP server."""
Elliott Hughes676e66d2013-04-22 11:41:57 -070095 path = "/time-zones/repository/releases/%s" % data_filename
96
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070097 print 'Downloading data...'
Neil Fuller246c6882014-05-16 18:04:48 +010098 HttpRetrieveFile(http, path, data_filename)
Elliott Hughes676e66d2013-04-22 11:41:57 -070099
100 print 'Downloading signature...'
101 signature_filename = '%s.asc' % data_filename
Neil Fuller246c6882014-05-16 18:04:48 +0100102 HttpRetrievefile(http, "%s.asc" % path, signature_filename)
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700103
104
Neil Fuller246c6882014-05-16 18:04:48 +0100105def BuildIcuToolsAndData(data_filename):
106 # Keep track of the original cwd so we can go back to it at the end.
107 original_working_dir = os.getcwd()
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700108
Neil Fuller246c6882014-05-16 18:04:48 +0100109 # Create a directory to run 'make' from.
110 icu_working_dir = '%s/icu' % original_working_dir
111 os.mkdir(icu_working_dir)
112 os.chdir(icu_working_dir)
113
114 # Build the ICU tools.
115 print 'Configuring ICU tools...'
116 subprocess.check_call(['%s/runConfigureICU' % icu_dir, 'Linux'])
117 print 'Making ICU tools...'
Elliott Hughes13bab432014-08-06 15:23:11 -0700118 subprocess.check_call(['make', '-j32'])
Neil Fuller246c6882014-05-16 18:04:48 +0100119
120 # Run the ICU tools.
121 os.chdir('tools/tzcode')
122 shutil.copyfile('%s/%s' % (original_working_dir, data_filename), data_filename)
123 print 'Making ICU data...'
124 subprocess.check_call(['make'])
125
Elliott Hughesf8896c62014-09-30 17:30:01 -0700126 # Copy the source file to its ultimate destination.
Neil Fuller246c6882014-05-16 18:04:48 +0100127 icu_txt_data_dir = '%s/data/misc' % icu_dir
128 print 'Copying zoneinfo64.txt to %s ...' % icu_txt_data_dir
129 shutil.copy('zoneinfo64.txt', icu_txt_data_dir)
130
Elliott Hughesf8896c62014-09-30 17:30:01 -0700131 # Regenerate the .dat file.
Neil Fuller246c6882014-05-16 18:04:48 +0100132 os.chdir(icu_working_dir)
Elliott Hughesf8896c62014-09-30 17:30:01 -0700133 subprocess.check_call(['make', '-j32'])
134
135 # Copy the .dat file to its ultimate destination.
Neil Fuller246c6882014-05-16 18:04:48 +0100136 icu_dat_data_dir = '%s/stubdata' % icu_dir
Neil Fuller43f37152014-05-21 16:59:09 +0100137 datfiles = glob.glob('data/out/tmp/icudt??l.dat')
138 if len(datfiles) != 1:
139 print 'ERROR: Unexpectedly found %d .dat files (%s). Halting.' % (len(datfiles), datfiles)
140 sys.exit(1)
Neil Fuller43f37152014-05-21 16:59:09 +0100141 datfile = datfiles[0]
142 print 'Copying %s to %s ...' % (datfile, icu_dat_data_dir)
143 shutil.copy(datfile, icu_dat_data_dir)
Neil Fuller246c6882014-05-16 18:04:48 +0100144
145 # Switch back to the original working cwd.
146 os.chdir(original_working_dir)
147
148
149def CheckSignature(data_filename):
Elliott Hughes676e66d2013-04-22 11:41:57 -0700150 signature_filename = '%s.asc' % data_filename
151 print 'Verifying signature...'
152 # If this fails for you, you probably need to import Paul Eggert's public key:
153 # gpg --recv-keys ED97E90E62AA7E34
154 subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify',
155 signature_filename, data_filename])
156
Neil Fuller246c6882014-05-16 18:04:48 +0100157
158def BuildBionicToolsAndData(data_filename):
159 new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1)
160
Elliott Hughes5d967e42012-07-20 16:52:39 -0700161 print 'Extracting...'
162 os.mkdir('extracted')
Elliott Hughese3063f42012-11-05 08:53:28 -0800163 tar = tarfile.open(data_filename, 'r')
Elliott Hughes5d967e42012-07-20 16:52:39 -0700164 tar.extractall('extracted')
165
166 print 'Calling zic(1)...'
167 os.mkdir('data')
168 for region in regions:
169 if region != 'backward':
170 subprocess.check_call(['zic', '-d', 'data', 'extracted/%s' % region])
171
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700172 WriteSetupFile()
Elliott Hughes5d967e42012-07-20 16:52:39 -0700173
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700174 print 'Calling ZoneCompactor to update bionic to %s...' % new_version
Elliott Hughes5d967e42012-07-20 16:52:39 -0700175 subprocess.check_call(['javac', '-d', '.',
Elliott Hughes13bab432014-08-06 15:23:11 -0700176 '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir])
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700177 subprocess.check_call(['java', 'ZoneCompactor',
Elliott Hughes23935352012-10-22 14:47:58 -0700178 'setup', 'data', 'extracted/zone.tab',
179 bionic_libc_zoneinfo_dir, new_version])
Elliott Hughes5d967e42012-07-20 16:52:39 -0700180
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800181
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700182# Run with no arguments from any directory, with no special setup required.
Elliott Hughese3063f42012-11-05 08:53:28 -0800183# See http://www.iana.org/time-zones/ for more about the source of this data.
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700184def main():
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700185 print 'Looking for new tzdata...'
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700186
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700187 tzdata_filenames = []
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700188
189 # The FTP server lets you download intermediate releases, and also lets you
Elliott Hughes21da42e2013-04-22 13:44:50 -0700190 # download the signatures for verification, so it's your best choice.
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700191 use_ftp = True
192
193 if use_ftp:
194 ftp = ftplib.FTP('ftp.iana.org')
195 ftp.login()
196 ftp.cwd('tz/releases')
197 for filename in ftp.nlst():
198 if filename.startswith('tzdata20') and filename.endswith('.tar.gz'):
199 tzdata_filenames.append(filename)
200 tzdata_filenames.sort()
201 else:
202 http = httplib.HTTPConnection('www.iana.org')
203 http.request("GET", "/time-zones")
204 index_lines = http.getresponse().read().split('\n')
205 for line in index_lines:
206 m = re.compile('.*href="/time-zones/repository/releases/(tzdata20\d\d\c\.tar\.gz)".*').match(line)
207 if m:
208 tzdata_filenames.append(m.group(1))
Elliott Hughesbcb2eda2011-10-24 10:47:25 -0700209
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700210 # If you're several releases behind, we'll walk you through the upgrades
211 # one by one.
212 current_version = GetCurrentTzDataVersion()
213 current_filename = '%s.tar.gz' % current_version
214 for filename in tzdata_filenames:
215 if filename > current_filename:
216 print 'Found new tzdata: %s' % filename
Neil Fuller246c6882014-05-16 18:04:48 +0100217 SwitchToNewTemporaryDirectory()
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700218 if use_ftp:
Neil Fuller246c6882014-05-16 18:04:48 +0100219 FtpRetrieveFileAndSignature(ftp, filename)
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700220 else:
Neil Fuller246c6882014-05-16 18:04:48 +0100221 HttpRetrieveFileAndSignature(http, filename)
222
223 CheckSignature(filename)
224 BuildIcuToolsAndData(filename)
225 BuildBionicToolsAndData(filename)
226 print 'Look in %s and %s for new data files' % (bionic_dir, icu_dir)
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700227 sys.exit(0)
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800228
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700229 print 'You already have the latest tzdata (%s)!' % current_version
230 sys.exit(0)
231
232
233if __name__ == '__main__':
234 main()