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