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) |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 69 | |
| 70 | # Switch to a temporary directory. |
| 71 | tmp_dir = tempfile.mkdtemp('-tzdata') |
| 72 | os.chdir(tmp_dir) |
| 73 | print 'Created temporary directory "%s"...' % tmp_dir |
| 74 | |
Elliott Hughes | 5d2ef87 | 2012-11-26 13:44:49 -0800 | [diff] [blame] | 75 | print 'Downloading data...' |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 76 | Retrieve(ftp, data_filename) |
Elliott Hughes | 5d2ef87 | 2012-11-26 13:44:49 -0800 | [diff] [blame] | 77 | |
| 78 | print 'Downloading signature...' |
| 79 | signature_filename = '%s.asc' % data_filename |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 80 | Retrieve(ftp, signature_filename) |
| 81 | |
| 82 | print 'Verifying signature...' |
| 83 | # If this fails for you, you probably need to import Paul Eggert's public key: |
| 84 | # gpg --recv-keys ED97E90E62AA7E34 |
Elliott Hughes | 5d2ef87 | 2012-11-26 13:44:49 -0800 | [diff] [blame] | 85 | subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify', |
| 86 | signature_filename, data_filename]) |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 87 | |
| 88 | print 'Extracting...' |
| 89 | os.mkdir('extracted') |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 90 | tar = tarfile.open(data_filename, 'r') |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 91 | tar.extractall('extracted') |
| 92 | |
| 93 | print 'Calling zic(1)...' |
| 94 | os.mkdir('data') |
| 95 | for region in regions: |
| 96 | if region != 'backward': |
| 97 | subprocess.check_call(['zic', '-d', 'data', 'extracted/%s' % region]) |
| 98 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 99 | WriteSetupFile() |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 100 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 101 | print 'Calling ZoneCompactor to update bionic to %s...' % new_version |
Elliott Hughes | eb06129 | 2012-10-19 12:05:24 -0700 | [diff] [blame] | 102 | libcore_src_dir = '%s/../libcore/luni/src/main/java/' % bionic_dir |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 103 | subprocess.check_call(['javac', '-d', '.', |
| 104 | '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir, |
Elliott Hughes | eb06129 | 2012-10-19 12:05:24 -0700 | [diff] [blame] | 105 | '%s/libcore/util/ZoneInfo.java' % libcore_src_dir, |
| 106 | '%s/libcore/io/BufferIterator.java' % libcore_src_dir]) |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 107 | subprocess.check_call(['java', 'ZoneCompactor', |
Elliott Hughes | 2393535 | 2012-10-22 14:47:58 -0700 | [diff] [blame] | 108 | 'setup', 'data', 'extracted/zone.tab', |
| 109 | bionic_libc_zoneinfo_dir, new_version]) |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 110 | |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 111 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 112 | # Run with no arguments from any directory, with no special setup required. |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 113 | # 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] | 114 | def main(): |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 115 | print 'Looking for new tzdata...' |
| 116 | ftp = ftplib.FTP('ftp.iana.org') |
| 117 | ftp.login() |
| 118 | ftp.cwd('tz/releases') |
| 119 | tzdata_filenames = [] |
| 120 | for filename in ftp.nlst(): |
Elliott Hughes | e3063f4 | 2012-11-05 08:53:28 -0800 | [diff] [blame] | 121 | if filename.startswith('tzdata20') and filename.endswith('.tar.gz'): |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 122 | tzdata_filenames.append(filename) |
| 123 | tzdata_filenames.sort() |
Elliott Hughes | bcb2eda | 2011-10-24 10:47:25 -0700 | [diff] [blame] | 124 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 125 | # If you're several releases behind, we'll walk you through the upgrades |
| 126 | # one by one. |
| 127 | current_version = GetCurrentTzDataVersion() |
| 128 | current_filename = '%s.tar.gz' % current_version |
| 129 | for filename in tzdata_filenames: |
| 130 | if filename > current_filename: |
| 131 | print 'Found new tzdata: %s' % filename |
| 132 | UpgradeTo(ftp, filename) |
| 133 | sys.exit(0) |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 134 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 135 | print 'You already have the latest tzdata (%s)!' % current_version |
| 136 | sys.exit(0) |
| 137 | |
| 138 | |
| 139 | if __name__ == '__main__': |
| 140 | main() |