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 | |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 3 | """Updates the timezone data held in bionic and ICU.""" |
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 |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 6 | import glob |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 7 | import httplib |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 8 | import os |
| 9 | import re |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 10 | import shutil |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 11 | import subprocess |
| 12 | import sys |
| 13 | import tarfile |
| 14 | import tempfile |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 15 | |
Elliott Hughes | 2c2463b | 2014-11-11 14:10:51 -0800 | [diff] [blame] | 16 | regions = ['africa', 'antarctica', 'asia', 'australasia', |
| 17 | 'etcetera', 'europe', 'northamerica', 'southamerica', |
| 18 | # These two deliberately come last so they override what came |
| 19 | # before (and each other). |
| 20 | 'backward', 'backzone' ] |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 21 | |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 22 | def CheckDirExists(dir, dirname): |
| 23 | if not os.path.isdir(dir): |
| 24 | print "Couldn't find %s (%s)!" % (dirname, dir) |
| 25 | sys.exit(1) |
| 26 | |
| 27 | bionic_libc_tools_zoneinfo_dir = os.path.realpath(os.path.dirname(sys.argv[0])) |
| 28 | |
| 29 | # Find the bionic directory, searching upward from this script. |
| 30 | bionic_dir = os.path.realpath('%s/../../..' % bionic_libc_tools_zoneinfo_dir) |
| 31 | bionic_libc_zoneinfo_dir = '%s/libc/zoneinfo' % bionic_dir |
| 32 | CheckDirExists(bionic_libc_zoneinfo_dir, 'bionic/libc/zoneinfo') |
| 33 | CheckDirExists(bionic_libc_tools_zoneinfo_dir, 'bionic/libc/tools/zoneinfo') |
| 34 | print 'Found bionic in %s ...' % bionic_dir |
| 35 | |
| 36 | # Find the icu4c directory. |
Elliott Hughes | 30ab939 | 2014-07-09 15:42:59 -0700 | [diff] [blame] | 37 | icu_dir = os.path.realpath('%s/../external/icu/icu4c/source' % bionic_dir) |
| 38 | CheckDirExists(icu_dir, 'external/icu/icu4c/source') |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 39 | print 'Found icu in %s ...' % icu_dir |
| 40 | |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 41 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 42 | def GetCurrentTzDataVersion(): |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 43 | 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] | 44 | |
| 45 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 46 | def WriteSetupFile(): |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 47 | """Writes the list of zones that ZoneCompactor should process.""" |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 48 | links = [] |
| 49 | zones = [] |
| 50 | for region in regions: |
| 51 | for line in open('extracted/%s' % region): |
| 52 | fields = line.split() |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 53 | if fields: |
| 54 | if fields[0] == 'Link': |
Elliott Hughes | 2c2463b | 2014-11-11 14:10:51 -0800 | [diff] [blame] | 55 | links.append('%s %s %s' % (fields[0], fields[1], fields[2])) |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 56 | zones.append(fields[2]) |
| 57 | elif fields[0] == 'Zone': |
| 58 | zones.append(fields[1]) |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 59 | zones.sort() |
| 60 | |
| 61 | setup = open('setup', 'w') |
Elliott Hughes | 2c2463b | 2014-11-11 14:10:51 -0800 | [diff] [blame] | 62 | for link in sorted(set(links)): |
| 63 | setup.write('%s\n' % link) |
| 64 | for zone in sorted(set(zones)): |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 65 | setup.write('%s\n' % zone) |
| 66 | setup.close() |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 67 | |
| 68 | |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 69 | def SwitchToNewTemporaryDirectory(): |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 70 | tmp_dir = tempfile.mkdtemp('-tzdata') |
| 71 | os.chdir(tmp_dir) |
| 72 | print 'Created temporary directory "%s"...' % tmp_dir |
| 73 | |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 74 | |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 75 | def FtpRetrieveFile(ftp, filename): |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 76 | ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write) |
| 77 | |
| 78 | |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 79 | def FtpRetrieveFileAndSignature(ftp, data_filename): |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 80 | """Downloads and repackages the given data from the given FTP server.""" |
Elliott Hughes | 5d2ef87 | 2012-11-26 13:44:49 -0800 | [diff] [blame] | 81 | print 'Downloading data...' |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 82 | FtpRetrieveFile(ftp, data_filename) |
Elliott Hughes | 5d2ef87 | 2012-11-26 13:44:49 -0800 | [diff] [blame] | 83 | |
| 84 | print 'Downloading signature...' |
| 85 | signature_filename = '%s.asc' % data_filename |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 86 | FtpRetrieveFile(ftp, signature_filename) |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 87 | |
| 88 | |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 89 | def HttpRetrieveFile(http, path, output_filename): |
Elliott Hughes | 676e66d | 2013-04-22 11:41:57 -0700 | [diff] [blame] | 90 | http.request("GET", path) |
| 91 | f = open(output_filename, 'wb') |
| 92 | f.write(http.getresponse().read()) |
| 93 | f.close() |
| 94 | |
| 95 | |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 96 | def HttpRetrieveFileAndSignature(http, data_filename): |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 97 | """Downloads and repackages the given data from the given HTTP server.""" |
Elliott Hughes | 676e66d | 2013-04-22 11:41:57 -0700 | [diff] [blame] | 98 | path = "/time-zones/repository/releases/%s" % data_filename |
| 99 | |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 100 | print 'Downloading data...' |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 101 | HttpRetrieveFile(http, path, data_filename) |
Elliott Hughes | 676e66d | 2013-04-22 11:41:57 -0700 | [diff] [blame] | 102 | |
| 103 | print 'Downloading signature...' |
| 104 | signature_filename = '%s.asc' % data_filename |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 105 | HttpRetrievefile(http, "%s.asc" % path, signature_filename) |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 106 | |
| 107 | |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 108 | def BuildIcuToolsAndData(data_filename): |
| 109 | # Keep track of the original cwd so we can go back to it at the end. |
| 110 | original_working_dir = os.getcwd() |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 111 | |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 112 | # Create a directory to run 'make' from. |
| 113 | icu_working_dir = '%s/icu' % original_working_dir |
| 114 | os.mkdir(icu_working_dir) |
| 115 | os.chdir(icu_working_dir) |
| 116 | |
| 117 | # Build the ICU tools. |
| 118 | print 'Configuring ICU tools...' |
| 119 | subprocess.check_call(['%s/runConfigureICU' % icu_dir, 'Linux']) |
| 120 | print 'Making ICU tools...' |
Elliott Hughes | 13bab43 | 2014-08-06 15:23:11 -0700 | [diff] [blame] | 121 | subprocess.check_call(['make', '-j32']) |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 122 | |
| 123 | # Run the ICU tools. |
| 124 | os.chdir('tools/tzcode') |
| 125 | shutil.copyfile('%s/%s' % (original_working_dir, data_filename), data_filename) |
| 126 | print 'Making ICU data...' |
| 127 | subprocess.check_call(['make']) |
| 128 | |
Elliott Hughes | f8896c6 | 2014-09-30 17:30:01 -0700 | [diff] [blame] | 129 | # Copy the source file to its ultimate destination. |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 130 | icu_txt_data_dir = '%s/data/misc' % icu_dir |
| 131 | print 'Copying zoneinfo64.txt to %s ...' % icu_txt_data_dir |
| 132 | shutil.copy('zoneinfo64.txt', icu_txt_data_dir) |
| 133 | |
Elliott Hughes | f8896c6 | 2014-09-30 17:30:01 -0700 | [diff] [blame] | 134 | # Regenerate the .dat file. |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 135 | os.chdir(icu_working_dir) |
Elliott Hughes | f8896c6 | 2014-09-30 17:30:01 -0700 | [diff] [blame] | 136 | subprocess.check_call(['make', '-j32']) |
| 137 | |
| 138 | # Copy the .dat file to its ultimate destination. |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 139 | icu_dat_data_dir = '%s/stubdata' % icu_dir |
Neil Fuller | 43f3715 | 2014-05-21 16:59:09 +0100 | [diff] [blame] | 140 | datfiles = glob.glob('data/out/tmp/icudt??l.dat') |
| 141 | if len(datfiles) != 1: |
| 142 | print 'ERROR: Unexpectedly found %d .dat files (%s). Halting.' % (len(datfiles), datfiles) |
| 143 | sys.exit(1) |
Neil Fuller | 43f3715 | 2014-05-21 16:59:09 +0100 | [diff] [blame] | 144 | datfile = datfiles[0] |
| 145 | print 'Copying %s to %s ...' % (datfile, icu_dat_data_dir) |
| 146 | shutil.copy(datfile, icu_dat_data_dir) |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 147 | |
| 148 | # Switch back to the original working cwd. |
| 149 | os.chdir(original_working_dir) |
| 150 | |
| 151 | |
| 152 | def CheckSignature(data_filename): |
Elliott Hughes | 676e66d | 2013-04-22 11:41:57 -0700 | [diff] [blame] | 153 | signature_filename = '%s.asc' % data_filename |
| 154 | print 'Verifying signature...' |
| 155 | # If this fails for you, you probably need to import Paul Eggert's public key: |
| 156 | # gpg --recv-keys ED97E90E62AA7E34 |
| 157 | subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify', |
| 158 | signature_filename, data_filename]) |
| 159 | |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 160 | |
| 161 | def BuildBionicToolsAndData(data_filename): |
| 162 | new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1) |
| 163 | |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 164 | print 'Extracting...' |
| 165 | os.mkdir('extracted') |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 166 | tar = tarfile.open(data_filename, 'r') |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 167 | tar.extractall('extracted') |
| 168 | |
| 169 | print 'Calling zic(1)...' |
| 170 | os.mkdir('data') |
Elliott Hughes | 2c2463b | 2014-11-11 14:10:51 -0800 | [diff] [blame] | 171 | zic_inputs = [ 'extracted/%s' % x for x in regions ] |
| 172 | zic_cmd = ['zic', '-d', 'data' ] |
| 173 | zic_cmd.extend(zic_inputs) |
| 174 | subprocess.check_call(zic_cmd) |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 175 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 176 | WriteSetupFile() |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 177 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 178 | print 'Calling ZoneCompactor to update bionic to %s...' % new_version |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 179 | subprocess.check_call(['javac', '-d', '.', |
Elliott Hughes | 13bab43 | 2014-08-06 15:23:11 -0700 | [diff] [blame] | 180 | '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir]) |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 181 | subprocess.check_call(['java', 'ZoneCompactor', |
Elliott Hughes | 2393535 | 2012-10-22 14:47:58 -0700 | [diff] [blame] | 182 | 'setup', 'data', 'extracted/zone.tab', |
| 183 | bionic_libc_zoneinfo_dir, new_version]) |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 184 | |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 185 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 186 | # Run with no arguments from any directory, with no special setup required. |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 187 | # 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] | 188 | def main(): |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 189 | print 'Looking for new tzdata...' |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 190 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 191 | tzdata_filenames = [] |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 192 | |
| 193 | # The FTP server lets you download intermediate releases, and also lets you |
Elliott Hughes | 21da42e | 2013-04-22 13:44:50 -0700 | [diff] [blame] | 194 | # download the signatures for verification, so it's your best choice. |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 195 | use_ftp = True |
| 196 | |
| 197 | if use_ftp: |
| 198 | ftp = ftplib.FTP('ftp.iana.org') |
| 199 | ftp.login() |
| 200 | ftp.cwd('tz/releases') |
| 201 | for filename in ftp.nlst(): |
| 202 | if filename.startswith('tzdata20') and filename.endswith('.tar.gz'): |
| 203 | tzdata_filenames.append(filename) |
| 204 | tzdata_filenames.sort() |
| 205 | else: |
| 206 | http = httplib.HTTPConnection('www.iana.org') |
| 207 | http.request("GET", "/time-zones") |
| 208 | index_lines = http.getresponse().read().split('\n') |
| 209 | for line in index_lines: |
| 210 | m = re.compile('.*href="/time-zones/repository/releases/(tzdata20\d\d\c\.tar\.gz)".*').match(line) |
| 211 | if m: |
| 212 | tzdata_filenames.append(m.group(1)) |
Elliott Hughes | bcb2eda | 2011-10-24 10:47:25 -0700 | [diff] [blame] | 213 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 214 | # If you're several releases behind, we'll walk you through the upgrades |
| 215 | # one by one. |
| 216 | current_version = GetCurrentTzDataVersion() |
| 217 | current_filename = '%s.tar.gz' % current_version |
| 218 | for filename in tzdata_filenames: |
| 219 | if filename > current_filename: |
| 220 | print 'Found new tzdata: %s' % filename |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 221 | SwitchToNewTemporaryDirectory() |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 222 | if use_ftp: |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 223 | FtpRetrieveFileAndSignature(ftp, filename) |
Elliott Hughes | f8dff7d | 2013-04-22 11:11:43 -0700 | [diff] [blame] | 224 | else: |
Neil Fuller | 246c688 | 2014-05-16 18:04:48 +0100 | [diff] [blame] | 225 | HttpRetrieveFileAndSignature(http, filename) |
| 226 | |
| 227 | CheckSignature(filename) |
| 228 | BuildIcuToolsAndData(filename) |
| 229 | BuildBionicToolsAndData(filename) |
| 230 | print 'Look in %s and %s for new data files' % (bionic_dir, icu_dir) |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 231 | sys.exit(0) |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 232 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 233 | print 'You already have the latest tzdata (%s)!' % current_version |
| 234 | sys.exit(0) |
| 235 | |
| 236 | |
| 237 | if __name__ == '__main__': |
| 238 | main() |