Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 2 | |
| 3 | """Updates the tzdata file.""" |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 4 | |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 5 | import ftplib |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 6 | import httplib |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 7 | import os |
| 8 | import re |
Elliott Hughes | 21da42e | 2013-04-22 13:44:50 -0700 | [diff] [blame^] | 9 | import socket |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 10 | import subprocess |
| 11 | import sys |
| 12 | import tarfile |
| 13 | import tempfile |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 14 | |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 15 | # Find the bionic directory, searching upward from this script. |
| 16 | bionic_libc_tools_zoneinfo_dir = os.path.realpath(os.path.dirname(sys.argv[0])) |
| 17 | bionic_libc_tools_dir = os.path.dirname(bionic_libc_tools_zoneinfo_dir) |
| 18 | bionic_libc_dir = os.path.dirname(bionic_libc_tools_dir) |
| 19 | bionic_dir = os.path.dirname(bionic_libc_dir) |
| 20 | bionic_libc_zoneinfo_dir = '%s/libc/zoneinfo' % bionic_dir |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 21 | |
| 22 | if not os.path.isdir(bionic_libc_tools_zoneinfo_dir): |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 23 | print "Couldn't find bionic/libc/tools/zoneinfo!" |
| 24 | sys.exit(1) |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 25 | if not os.path.isdir(bionic_libc_zoneinfo_dir): |
| 26 | print "Couldn't find bionic/libc/zoneinfo!" |
| 27 | sys.exit(1) |
| 28 | |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 29 | print 'Found bionic in %s...' % bionic_dir |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 30 | |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 32 | regions = ['africa', 'antarctica', 'asia', 'australasia', 'backward', |
| 33 | 'etcetera', 'europe', 'northamerica', 'southamerica'] |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 34 | |
| 35 | |
Elliott Hughes | 21da42e | 2013-04-22 13:44:50 -0700 | [diff] [blame^] | 36 | def DisableIpv6(): |
| 37 | """Replaces socket.getaddrinfo with a version that only requests IPv4 addresses.""" |
| 38 | __real_getaddrinfo = socket.getaddrinfo |
| 39 | def __ipv4_getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0): |
| 40 | return __real_getaddrinfo(host, port, socket.AF_INET, socktype, proto, flags) |
| 41 | socket.getaddrinfo = __ipv4_getaddrinfo |
| 42 | |
| 43 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 44 | def GetCurrentTzDataVersion(): |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 45 | return open('%s/tzdata' % bionic_libc_zoneinfo_dir).read().split('\x00', 1)[0] |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 46 | |
| 47 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 48 | def WriteSetupFile(): |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 49 | """Writes the list of zones that ZoneCompactor should process.""" |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 50 | links = [] |
| 51 | zones = [] |
| 52 | for region in regions: |
| 53 | for line in open('extracted/%s' % region): |
| 54 | fields = line.split() |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 55 | if fields: |
| 56 | if fields[0] == 'Link': |
| 57 | links.append('%s %s %s\n' % (fields[0], fields[1], fields[2])) |
| 58 | zones.append(fields[2]) |
| 59 | elif fields[0] == 'Zone': |
| 60 | zones.append(fields[1]) |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 61 | zones.sort() |
| 62 | |
| 63 | setup = open('setup', 'w') |
| 64 | for link in links: |
| 65 | setup.write(link) |
| 66 | for zone in zones: |
| 67 | setup.write('%s\n' % zone) |
| 68 | setup.close() |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 69 | |
| 70 | |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 71 | def SwitchToNewTemporaryDirectory(): |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 72 | tmp_dir = tempfile.mkdtemp('-tzdata') |
| 73 | os.chdir(tmp_dir) |
| 74 | print 'Created temporary directory "%s"...' % tmp_dir |
| 75 | |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 76 | |
| 77 | def FtpRetrieve(ftp, filename): |
| 78 | ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write) |
| 79 | |
| 80 | |
| 81 | def FtpUpgrade(ftp, data_filename): |
| 82 | """Downloads and repackages the given data from the given FTP server.""" |
| 83 | SwitchToNewTemporaryDirectory() |
| 84 | |
Elliott Hughes | 5d2ef87 | 2012-11-26 13:44:49 -0800 | [diff] [blame] | 85 | print 'Downloading data...' |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 86 | FtpRetrieve(ftp, data_filename) |
Elliott Hughes | 5d2ef87 | 2012-11-26 13:44:49 -0800 | [diff] [blame] | 87 | |
| 88 | print 'Downloading signature...' |
| 89 | signature_filename = '%s.asc' % data_filename |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 90 | FtpRetrieve(ftp, signature_filename) |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 91 | |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 92 | ExtractAndCompile(data_filename) |
| 93 | |
| 94 | |
Elliott Hughes | 676e66d | 2013-04-22 11:41:57 -0700 | [diff] [blame] | 95 | def HttpRetrieve(http, path, output_filename): |
| 96 | http.request("GET", path) |
| 97 | f = open(output_filename, 'wb') |
| 98 | f.write(http.getresponse().read()) |
| 99 | f.close() |
| 100 | |
| 101 | |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 102 | def HttpUpgrade(http, data_filename): |
| 103 | """Downloads and repackages the given data from the given HTTP server.""" |
| 104 | SwitchToNewTemporaryDirectory() |
| 105 | |
Elliott Hughes | 676e66d | 2013-04-22 11:41:57 -0700 | [diff] [blame] | 106 | path = "/time-zones/repository/releases/%s" % data_filename |
| 107 | |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 108 | print 'Downloading data...' |
Elliott Hughes | 676e66d | 2013-04-22 11:41:57 -0700 | [diff] [blame] | 109 | HttpRetrieve(http, path, data_filename) |
| 110 | |
| 111 | print 'Downloading signature...' |
| 112 | signature_filename = '%s.asc' % data_filename |
| 113 | HttpRetrieve(http, "%s.asc" % path, signature_filename) |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 114 | |
| 115 | ExtractAndCompile(data_filename) |
| 116 | |
| 117 | |
| 118 | def ExtractAndCompile(data_filename): |
| 119 | new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1) |
| 120 | |
Elliott Hughes | 676e66d | 2013-04-22 11:41:57 -0700 | [diff] [blame] | 121 | signature_filename = '%s.asc' % data_filename |
| 122 | print 'Verifying signature...' |
| 123 | # If this fails for you, you probably need to import Paul Eggert's public key: |
| 124 | # gpg --recv-keys ED97E90E62AA7E34 |
| 125 | subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify', |
| 126 | signature_filename, data_filename]) |
| 127 | |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 128 | print 'Extracting...' |
| 129 | os.mkdir('extracted') |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 130 | tar = tarfile.open(data_filename, 'r') |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 131 | tar.extractall('extracted') |
| 132 | |
| 133 | print 'Calling zic(1)...' |
| 134 | os.mkdir('data') |
| 135 | for region in regions: |
| 136 | if region != 'backward': |
| 137 | subprocess.check_call(['zic', '-d', 'data', 'extracted/%s' % region]) |
| 138 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 139 | WriteSetupFile() |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 140 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 141 | print 'Calling ZoneCompactor to update bionic to %s...' % new_version |
Elliott Hughes | eb06129 | 2012-10-19 12:05:24 -0700 | [diff] [blame] | 142 | libcore_src_dir = '%s/../libcore/luni/src/main/java/' % bionic_dir |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 143 | subprocess.check_call(['javac', '-d', '.', |
| 144 | '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir, |
Elliott Hughes | eb06129 | 2012-10-19 12:05:24 -0700 | [diff] [blame] | 145 | '%s/libcore/util/ZoneInfo.java' % libcore_src_dir, |
| 146 | '%s/libcore/io/BufferIterator.java' % libcore_src_dir]) |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 147 | subprocess.check_call(['java', 'ZoneCompactor', |
Elliott Hughes | 2393535 | 2012-10-22 14:47:58 -0700 | [diff] [blame] | 148 | 'setup', 'data', 'extracted/zone.tab', |
| 149 | bionic_libc_zoneinfo_dir, new_version]) |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 150 | |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 151 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 152 | # Run with no arguments from any directory, with no special setup required. |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 153 | # See http://www.iana.org/time-zones/ for more about the source of this data. |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 154 | def main(): |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 155 | print 'Looking for new tzdata...' |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 156 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 157 | tzdata_filenames = [] |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 158 | |
| 159 | # The FTP server lets you download intermediate releases, and also lets you |
Elliott Hughes | 21da42e | 2013-04-22 13:44:50 -0700 | [diff] [blame^] | 160 | # download the signatures for verification, so it's your best choice. |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 161 | use_ftp = True |
Elliott Hughes | 21da42e | 2013-04-22 13:44:50 -0700 | [diff] [blame^] | 162 | DisableIpv6() # I've been unable to talk to the FTP server over IPv6 (2013-04). |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 163 | |
| 164 | if use_ftp: |
| 165 | ftp = ftplib.FTP('ftp.iana.org') |
| 166 | ftp.login() |
| 167 | ftp.cwd('tz/releases') |
| 168 | for filename in ftp.nlst(): |
| 169 | if filename.startswith('tzdata20') and filename.endswith('.tar.gz'): |
| 170 | tzdata_filenames.append(filename) |
| 171 | tzdata_filenames.sort() |
| 172 | else: |
| 173 | http = httplib.HTTPConnection('www.iana.org') |
| 174 | http.request("GET", "/time-zones") |
| 175 | index_lines = http.getresponse().read().split('\n') |
| 176 | for line in index_lines: |
| 177 | m = re.compile('.*href="/time-zones/repository/releases/(tzdata20\d\d\c\.tar\.gz)".*').match(line) |
| 178 | if m: |
| 179 | tzdata_filenames.append(m.group(1)) |
Elliott Hughes | bcb2eda | 2011-10-24 10:47:25 -0700 | [diff] [blame] | 180 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 181 | # If you're several releases behind, we'll walk you through the upgrades |
| 182 | # one by one. |
| 183 | current_version = GetCurrentTzDataVersion() |
| 184 | current_filename = '%s.tar.gz' % current_version |
| 185 | for filename in tzdata_filenames: |
| 186 | if filename > current_filename: |
| 187 | print 'Found new tzdata: %s' % filename |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 188 | if use_ftp: |
| 189 | FtpUpgrade(ftp, filename) |
| 190 | else: |
| 191 | HttpUpgrade(http, filename) |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 192 | sys.exit(0) |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 193 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 194 | print 'You already have the latest tzdata (%s)!' % current_version |
| 195 | sys.exit(0) |
| 196 | |
| 197 | |
| 198 | if __name__ == '__main__': |
| 199 | main() |