| 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 | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 6 | import os | 
|  | 7 | import re | 
| Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 8 | import subprocess | 
|  | 9 | import sys | 
|  | 10 | import tarfile | 
|  | 11 | import tempfile | 
| Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 12 |  | 
| Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 13 | # Find the bionic directory, searching upward from this script. | 
|  | 14 | bionic_libc_tools_zoneinfo_dir = os.path.realpath(os.path.dirname(sys.argv[0])) | 
|  | 15 | bionic_libc_tools_dir = os.path.dirname(bionic_libc_tools_zoneinfo_dir) | 
|  | 16 | bionic_libc_dir = os.path.dirname(bionic_libc_tools_dir) | 
|  | 17 | bionic_dir = os.path.dirname(bionic_libc_dir) | 
|  | 18 | bionic_libc_zoneinfo_dir = '%s/libc/zoneinfo' % bionic_dir | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 19 |  | 
|  | 20 | if not os.path.isdir(bionic_libc_tools_zoneinfo_dir): | 
| Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 21 | print "Couldn't find bionic/libc/tools/zoneinfo!" | 
|  | 22 | sys.exit(1) | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 23 | if not os.path.isdir(bionic_libc_zoneinfo_dir): | 
|  | 24 | print "Couldn't find bionic/libc/zoneinfo!" | 
|  | 25 | sys.exit(1) | 
|  | 26 |  | 
| Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 27 | print 'Found bionic in %s...' % bionic_dir | 
| Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 28 |  | 
| Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 29 |  | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 30 | regions = ['africa', 'antarctica', 'asia', 'australasia', 'backward', | 
|  | 31 | 'etcetera', 'europe', 'northamerica', 'southamerica'] | 
| Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 32 |  | 
|  | 33 |  | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 34 | def GetCurrentTzDataVersion(): | 
| Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 35 | 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] | 36 |  | 
|  | 37 |  | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 38 | def WriteSetupFile(): | 
| Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 39 | """Writes the list of zones that ZoneCompactor should process.""" | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 40 | links = [] | 
|  | 41 | zones = [] | 
|  | 42 | for region in regions: | 
|  | 43 | for line in open('extracted/%s' % region): | 
|  | 44 | fields = line.split() | 
| Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 45 | if fields: | 
|  | 46 | if fields[0] == 'Link': | 
|  | 47 | links.append('%s %s %s\n' % (fields[0], fields[1], fields[2])) | 
|  | 48 | zones.append(fields[2]) | 
|  | 49 | elif fields[0] == 'Zone': | 
|  | 50 | zones.append(fields[1]) | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 51 | zones.sort() | 
|  | 52 |  | 
|  | 53 | setup = open('setup', 'w') | 
|  | 54 | for link in links: | 
|  | 55 | setup.write(link) | 
|  | 56 | for zone in zones: | 
|  | 57 | setup.write('%s\n' % zone) | 
|  | 58 | setup.close() | 
| Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 59 |  | 
|  | 60 |  | 
| Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 61 | def Retrieve(ftp, filename): | 
|  | 62 | ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write) | 
|  | 63 |  | 
|  | 64 |  | 
|  | 65 | def UpgradeTo(ftp, data_filename): | 
|  | 66 | """Downloads and repackages the given data from the given FTP server.""" | 
|  | 67 |  | 
|  | 68 | new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1) | 
|  | 69 | signature_filename = '%s.sign' % data_filename | 
| Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 70 |  | 
|  | 71 | # Switch to a temporary directory. | 
|  | 72 | tmp_dir = tempfile.mkdtemp('-tzdata') | 
|  | 73 | os.chdir(tmp_dir) | 
|  | 74 | print 'Created temporary directory "%s"...' % tmp_dir | 
|  | 75 |  | 
| Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 76 | print 'Downloading data and signature...' | 
|  | 77 | Retrieve(ftp, data_filename) | 
|  | 78 | Retrieve(ftp, signature_filename) | 
|  | 79 |  | 
|  | 80 | print 'Verifying signature...' | 
|  | 81 | # If this fails for you, you probably need to import Paul Eggert's public key: | 
|  | 82 | # gpg --recv-keys ED97E90E62AA7E34 | 
|  | 83 | subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', | 
|  | 84 | '--verify', signature_filename, data_filename]) | 
| Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 85 |  | 
|  | 86 | print 'Extracting...' | 
|  | 87 | os.mkdir('extracted') | 
| Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 88 | tar = tarfile.open(data_filename, 'r') | 
| Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 89 | tar.extractall('extracted') | 
|  | 90 |  | 
|  | 91 | print 'Calling zic(1)...' | 
|  | 92 | os.mkdir('data') | 
|  | 93 | for region in regions: | 
|  | 94 | if region != 'backward': | 
|  | 95 | subprocess.check_call(['zic', '-d', 'data', 'extracted/%s' % region]) | 
|  | 96 |  | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 97 | WriteSetupFile() | 
| Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 98 |  | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 99 | print 'Calling ZoneCompactor to update bionic to %s...' % new_version | 
| Elliott Hughes | eb06129 | 2012-10-19 12:05:24 -0700 | [diff] [blame] | 100 | libcore_src_dir = '%s/../libcore/luni/src/main/java/' % bionic_dir | 
| Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 101 | subprocess.check_call(['javac', '-d', '.', | 
|  | 102 | '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir, | 
| Elliott Hughes | eb06129 | 2012-10-19 12:05:24 -0700 | [diff] [blame] | 103 | '%s/libcore/util/ZoneInfo.java' % libcore_src_dir, | 
|  | 104 | '%s/libcore/io/BufferIterator.java' % libcore_src_dir]) | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 105 | subprocess.check_call(['java', 'ZoneCompactor', | 
| Elliott Hughes | 2393535 | 2012-10-22 14:47:58 -0700 | [diff] [blame] | 106 | 'setup', 'data', 'extracted/zone.tab', | 
|  | 107 | bionic_libc_zoneinfo_dir, new_version]) | 
| Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 108 |  | 
| Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 109 |  | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 110 | # Run with no arguments from any directory, with no special setup required. | 
| Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 111 | # 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] | 112 | def main(): | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 113 | print 'Looking for new tzdata...' | 
|  | 114 | ftp = ftplib.FTP('ftp.iana.org') | 
|  | 115 | ftp.login() | 
|  | 116 | ftp.cwd('tz/releases') | 
|  | 117 | tzdata_filenames = [] | 
|  | 118 | for filename in ftp.nlst(): | 
| Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 119 | if filename.startswith('tzdata20') and filename.endswith('.tar.gz'): | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 120 | tzdata_filenames.append(filename) | 
|  | 121 | tzdata_filenames.sort() | 
| Elliott Hughes | bcb2eda | 2011-10-24 10:47:25 -0700 | [diff] [blame] | 122 |  | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 123 | # If you're several releases behind, we'll walk you through the upgrades | 
|  | 124 | # one by one. | 
|  | 125 | current_version = GetCurrentTzDataVersion() | 
|  | 126 | current_filename = '%s.tar.gz' % current_version | 
|  | 127 | for filename in tzdata_filenames: | 
|  | 128 | if filename > current_filename: | 
|  | 129 | print 'Found new tzdata: %s' % filename | 
|  | 130 | UpgradeTo(ftp, filename) | 
|  | 131 | sys.exit(0) | 
| Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 132 |  | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 133 | print 'You already have the latest tzdata (%s)!' % current_version | 
|  | 134 | sys.exit(0) | 
|  | 135 |  | 
|  | 136 |  | 
|  | 137 | if __name__ == '__main__': | 
|  | 138 | main() |