blob: e800e8f8bd8000f657cb6b3b2cc63b37ef6ce899 [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 Hughes90cb5ff2014-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
126 # Copy the output files to their ultimate destination.
127 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
131 os.chdir(icu_working_dir)
132 icu_dat_data_dir = '%s/stubdata' % icu_dir
Neil Fuller43f37152014-05-21 16:59:09 +0100133 datfiles = glob.glob('data/out/tmp/icudt??l.dat')
134 if len(datfiles) != 1:
135 print 'ERROR: Unexpectedly found %d .dat files (%s). Halting.' % (len(datfiles), datfiles)
136 sys.exit(1)
137
138 datfile = datfiles[0]
139 print 'Copying %s to %s ...' % (datfile, icu_dat_data_dir)
140 shutil.copy(datfile, icu_dat_data_dir)
Neil Fuller246c6882014-05-16 18:04:48 +0100141
142 # Switch back to the original working cwd.
143 os.chdir(original_working_dir)
144
145
146def CheckSignature(data_filename):
Elliott Hughes676e66d2013-04-22 11:41:57 -0700147 signature_filename = '%s.asc' % data_filename
148 print 'Verifying signature...'
149 # If this fails for you, you probably need to import Paul Eggert's public key:
150 # gpg --recv-keys ED97E90E62AA7E34
151 subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify',
152 signature_filename, data_filename])
153
Neil Fuller246c6882014-05-16 18:04:48 +0100154
155def BuildBionicToolsAndData(data_filename):
156 new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1)
157
Elliott Hughes5d967e42012-07-20 16:52:39 -0700158 print 'Extracting...'
159 os.mkdir('extracted')
Elliott Hughese3063f42012-11-05 08:53:28 -0800160 tar = tarfile.open(data_filename, 'r')
Elliott Hughes5d967e42012-07-20 16:52:39 -0700161 tar.extractall('extracted')
162
163 print 'Calling zic(1)...'
164 os.mkdir('data')
165 for region in regions:
166 if region != 'backward':
167 subprocess.check_call(['zic', '-d', 'data', 'extracted/%s' % region])
168
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700169 WriteSetupFile()
Elliott Hughes5d967e42012-07-20 16:52:39 -0700170
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700171 print 'Calling ZoneCompactor to update bionic to %s...' % new_version
Elliott Hughes5d967e42012-07-20 16:52:39 -0700172 subprocess.check_call(['javac', '-d', '.',
Elliott Hughes90cb5ff2014-08-06 15:23:11 -0700173 '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir])
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700174 subprocess.check_call(['java', 'ZoneCompactor',
Elliott Hughes23935352012-10-22 14:47:58 -0700175 'setup', 'data', 'extracted/zone.tab',
176 bionic_libc_zoneinfo_dir, new_version])
Elliott Hughes5d967e42012-07-20 16:52:39 -0700177
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800178
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700179# Run with no arguments from any directory, with no special setup required.
Elliott Hughese3063f42012-11-05 08:53:28 -0800180# See http://www.iana.org/time-zones/ for more about the source of this data.
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700181def main():
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700182 print 'Looking for new tzdata...'
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700183
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700184 tzdata_filenames = []
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700185
186 # The FTP server lets you download intermediate releases, and also lets you
Elliott Hughes21da42e2013-04-22 13:44:50 -0700187 # download the signatures for verification, so it's your best choice.
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700188 use_ftp = True
189
190 if use_ftp:
191 ftp = ftplib.FTP('ftp.iana.org')
192 ftp.login()
193 ftp.cwd('tz/releases')
194 for filename in ftp.nlst():
195 if filename.startswith('tzdata20') and filename.endswith('.tar.gz'):
196 tzdata_filenames.append(filename)
197 tzdata_filenames.sort()
198 else:
199 http = httplib.HTTPConnection('www.iana.org')
200 http.request("GET", "/time-zones")
201 index_lines = http.getresponse().read().split('\n')
202 for line in index_lines:
203 m = re.compile('.*href="/time-zones/repository/releases/(tzdata20\d\d\c\.tar\.gz)".*').match(line)
204 if m:
205 tzdata_filenames.append(m.group(1))
Elliott Hughesbcb2eda2011-10-24 10:47:25 -0700206
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700207 # If you're several releases behind, we'll walk you through the upgrades
208 # one by one.
209 current_version = GetCurrentTzDataVersion()
210 current_filename = '%s.tar.gz' % current_version
211 for filename in tzdata_filenames:
212 if filename > current_filename:
213 print 'Found new tzdata: %s' % filename
Neil Fuller246c6882014-05-16 18:04:48 +0100214 SwitchToNewTemporaryDirectory()
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700215 if use_ftp:
Neil Fuller246c6882014-05-16 18:04:48 +0100216 FtpRetrieveFileAndSignature(ftp, filename)
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700217 else:
Neil Fuller246c6882014-05-16 18:04:48 +0100218 HttpRetrieveFileAndSignature(http, filename)
219
220 CheckSignature(filename)
221 BuildIcuToolsAndData(filename)
222 BuildBionicToolsAndData(filename)
223 print 'Look in %s and %s for new data files' % (bionic_dir, icu_dir)
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700224 sys.exit(0)
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800225
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700226 print 'You already have the latest tzdata (%s)!' % current_version
227 sys.exit(0)
228
229
230if __name__ == '__main__':
231 main()