blob: 2b30c156ae58856899e091848075c061930ceb03 [file] [log] [blame]
Elliott Hughes5d967e42012-07-20 16:52:39 -07001#!/usr/bin/python
Elliott Hughes5b1497a2012-10-19 14:47:37 -07002
3"""Updates the tzdata file."""
Elliott Hughesd40e63e2011-02-17 16:20:07 -08004
Elliott Hughes5d967e42012-07-20 16:52:39 -07005import ftplib
Elliott Hughesf8dff7d2013-04-22 11:11:43 -07006import httplib
Elliott Hughes5d967e42012-07-20 16:52:39 -07007import os
8import re
Elliott Hughes5d967e42012-07-20 16:52:39 -07009import subprocess
10import sys
11import tarfile
12import tempfile
Elliott Hughesd40e63e2011-02-17 16:20:07 -080013
Elliott Hughes5d967e42012-07-20 16:52:39 -070014# Find the bionic directory, searching upward from this script.
15bionic_libc_tools_zoneinfo_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
16bionic_libc_tools_dir = os.path.dirname(bionic_libc_tools_zoneinfo_dir)
17bionic_libc_dir = os.path.dirname(bionic_libc_tools_dir)
18bionic_dir = os.path.dirname(bionic_libc_dir)
19bionic_libc_zoneinfo_dir = '%s/libc/zoneinfo' % bionic_dir
Elliott Hughes5b1497a2012-10-19 14:47:37 -070020
21if not os.path.isdir(bionic_libc_tools_zoneinfo_dir):
Elliott Hughes5d967e42012-07-20 16:52:39 -070022 print "Couldn't find bionic/libc/tools/zoneinfo!"
23 sys.exit(1)
Elliott Hughes5b1497a2012-10-19 14:47:37 -070024if not os.path.isdir(bionic_libc_zoneinfo_dir):
25 print "Couldn't find bionic/libc/zoneinfo!"
26 sys.exit(1)
27
Elliott Hughes5d967e42012-07-20 16:52:39 -070028print 'Found bionic in %s...' % bionic_dir
Elliott Hughesd40e63e2011-02-17 16:20:07 -080029
Elliott Hughes5d967e42012-07-20 16:52:39 -070030
Elliott Hughes5b1497a2012-10-19 14:47:37 -070031regions = ['africa', 'antarctica', 'asia', 'australasia', 'backward',
32 'etcetera', 'europe', 'northamerica', 'southamerica']
Elliott Hughes5d967e42012-07-20 16:52:39 -070033
34
Elliott Hughes5b1497a2012-10-19 14:47:37 -070035def GetCurrentTzDataVersion():
Elliott Hughese3063f42012-11-05 08:53:28 -080036 return open('%s/tzdata' % bionic_libc_zoneinfo_dir).read().split('\x00', 1)[0]
Elliott Hughes5d967e42012-07-20 16:52:39 -070037
38
Elliott Hughes5b1497a2012-10-19 14:47:37 -070039def WriteSetupFile():
Elliott Hughese3063f42012-11-05 08:53:28 -080040 """Writes the list of zones that ZoneCompactor should process."""
Elliott Hughes5b1497a2012-10-19 14:47:37 -070041 links = []
42 zones = []
43 for region in regions:
44 for line in open('extracted/%s' % region):
45 fields = line.split()
Elliott Hughese3063f42012-11-05 08:53:28 -080046 if fields:
47 if fields[0] == 'Link':
48 links.append('%s %s %s\n' % (fields[0], fields[1], fields[2]))
49 zones.append(fields[2])
50 elif fields[0] == 'Zone':
51 zones.append(fields[1])
Elliott Hughes5b1497a2012-10-19 14:47:37 -070052 zones.sort()
53
54 setup = open('setup', 'w')
55 for link in links:
56 setup.write(link)
57 for zone in zones:
58 setup.write('%s\n' % zone)
59 setup.close()
Elliott Hughes5d967e42012-07-20 16:52:39 -070060
61
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070062def SwitchToNewTemporaryDirectory():
Elliott Hughes5d967e42012-07-20 16:52:39 -070063 tmp_dir = tempfile.mkdtemp('-tzdata')
64 os.chdir(tmp_dir)
65 print 'Created temporary directory "%s"...' % tmp_dir
66
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070067
68def FtpRetrieve(ftp, filename):
69 ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
70
71
72def FtpUpgrade(ftp, data_filename):
73 """Downloads and repackages the given data from the given FTP server."""
74 SwitchToNewTemporaryDirectory()
75
Elliott Hughes5d2ef872012-11-26 13:44:49 -080076 print 'Downloading data...'
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070077 FtpRetrieve(ftp, data_filename)
Elliott Hughes5d2ef872012-11-26 13:44:49 -080078
79 print 'Downloading signature...'
80 signature_filename = '%s.asc' % data_filename
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070081 FtpRetrieve(ftp, signature_filename)
Elliott Hughese3063f42012-11-05 08:53:28 -080082
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070083 ExtractAndCompile(data_filename)
84
85
Elliott Hughes676e66d2013-04-22 11:41:57 -070086def HttpRetrieve(http, path, output_filename):
87 http.request("GET", path)
88 f = open(output_filename, 'wb')
89 f.write(http.getresponse().read())
90 f.close()
91
92
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070093def HttpUpgrade(http, data_filename):
94 """Downloads and repackages the given data from the given HTTP server."""
95 SwitchToNewTemporaryDirectory()
96
Elliott Hughes676e66d2013-04-22 11:41:57 -070097 path = "/time-zones/repository/releases/%s" % data_filename
98
Elliott Hughesf8dff7d2013-04-22 11:11:43 -070099 print 'Downloading data...'
Elliott Hughes676e66d2013-04-22 11:41:57 -0700100 HttpRetrieve(http, path, data_filename)
101
102 print 'Downloading signature...'
103 signature_filename = '%s.asc' % data_filename
104 HttpRetrieve(http, "%s.asc" % path, signature_filename)
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700105
106 ExtractAndCompile(data_filename)
107
108
109def ExtractAndCompile(data_filename):
110 new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1)
111
Elliott Hughes676e66d2013-04-22 11:41:57 -0700112 signature_filename = '%s.asc' % data_filename
113 print 'Verifying signature...'
114 # If this fails for you, you probably need to import Paul Eggert's public key:
115 # gpg --recv-keys ED97E90E62AA7E34
116 subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify',
117 signature_filename, data_filename])
118
Elliott Hughes5d967e42012-07-20 16:52:39 -0700119 print 'Extracting...'
120 os.mkdir('extracted')
Elliott Hughese3063f42012-11-05 08:53:28 -0800121 tar = tarfile.open(data_filename, 'r')
Elliott Hughes5d967e42012-07-20 16:52:39 -0700122 tar.extractall('extracted')
123
124 print 'Calling zic(1)...'
125 os.mkdir('data')
126 for region in regions:
127 if region != 'backward':
128 subprocess.check_call(['zic', '-d', 'data', 'extracted/%s' % region])
129
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700130 WriteSetupFile()
Elliott Hughes5d967e42012-07-20 16:52:39 -0700131
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700132 print 'Calling ZoneCompactor to update bionic to %s...' % new_version
Elliott Hugheseb061292012-10-19 12:05:24 -0700133 libcore_src_dir = '%s/../libcore/luni/src/main/java/' % bionic_dir
Elliott Hughes5d967e42012-07-20 16:52:39 -0700134 subprocess.check_call(['javac', '-d', '.',
135 '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir,
Elliott Hugheseb061292012-10-19 12:05:24 -0700136 '%s/libcore/util/ZoneInfo.java' % libcore_src_dir,
137 '%s/libcore/io/BufferIterator.java' % libcore_src_dir])
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700138 subprocess.check_call(['java', 'ZoneCompactor',
Elliott Hughes23935352012-10-22 14:47:58 -0700139 'setup', 'data', 'extracted/zone.tab',
140 bionic_libc_zoneinfo_dir, new_version])
Elliott Hughes5d967e42012-07-20 16:52:39 -0700141
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800142
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700143# Run with no arguments from any directory, with no special setup required.
Elliott Hughese3063f42012-11-05 08:53:28 -0800144# See http://www.iana.org/time-zones/ for more about the source of this data.
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700145def main():
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700146 print 'Looking for new tzdata...'
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700147
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700148 tzdata_filenames = []
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700149
150 # The FTP server lets you download intermediate releases, and also lets you
151 # download the signatures for verification, so it's your best choice. It's
152 # also less reliable than the HTTP server, so we support that too as a backup.
153 use_ftp = True
154
155 if use_ftp:
156 ftp = ftplib.FTP('ftp.iana.org')
157 ftp.login()
158 ftp.cwd('tz/releases')
159 for filename in ftp.nlst():
160 if filename.startswith('tzdata20') and filename.endswith('.tar.gz'):
161 tzdata_filenames.append(filename)
162 tzdata_filenames.sort()
163 else:
164 http = httplib.HTTPConnection('www.iana.org')
165 http.request("GET", "/time-zones")
166 index_lines = http.getresponse().read().split('\n')
167 for line in index_lines:
168 m = re.compile('.*href="/time-zones/repository/releases/(tzdata20\d\d\c\.tar\.gz)".*').match(line)
169 if m:
170 tzdata_filenames.append(m.group(1))
Elliott Hughesbcb2eda2011-10-24 10:47:25 -0700171
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700172 # If you're several releases behind, we'll walk you through the upgrades
173 # one by one.
174 current_version = GetCurrentTzDataVersion()
175 current_filename = '%s.tar.gz' % current_version
176 for filename in tzdata_filenames:
177 if filename > current_filename:
178 print 'Found new tzdata: %s' % filename
Elliott Hughesf8dff7d2013-04-22 11:11:43 -0700179 if use_ftp:
180 FtpUpgrade(ftp, filename)
181 else:
182 HttpUpgrade(http, filename)
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700183 sys.exit(0)
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800184
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700185 print 'You already have the latest tzdata (%s)!' % current_version
186 sys.exit(0)
187
188
189if __name__ == '__main__':
190 main()