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(): |
| 35 | return open('%s/tzdata' % bionic_libc_zoneinfo_dir).read().split('\0', 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(): |
| 39 | links = [] |
| 40 | zones = [] |
| 41 | for region in regions: |
| 42 | for line in open('extracted/%s' % region): |
| 43 | fields = line.split() |
| 44 | if len(fields) == 0: |
| 45 | continue |
| 46 | elif 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]) |
| 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 | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 61 | def UpgradeTo(ftp, filename): |
| 62 | new_version = re.search('(tzdata.+)\.tar\.gz', filename).group(1) |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 63 | |
| 64 | # Switch to a temporary directory. |
| 65 | tmp_dir = tempfile.mkdtemp('-tzdata') |
| 66 | os.chdir(tmp_dir) |
| 67 | print 'Created temporary directory "%s"...' % tmp_dir |
| 68 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 69 | print 'Downloading...' |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 70 | ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write) |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 71 | |
| 72 | print 'Extracting...' |
| 73 | os.mkdir('extracted') |
| 74 | tar = tarfile.open(filename, 'r') |
| 75 | tar.extractall('extracted') |
| 76 | |
| 77 | print 'Calling zic(1)...' |
| 78 | os.mkdir('data') |
| 79 | for region in regions: |
| 80 | if region != 'backward': |
| 81 | subprocess.check_call(['zic', '-d', 'data', 'extracted/%s' % region]) |
| 82 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 83 | WriteSetupFile() |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 84 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 85 | print 'Calling ZoneCompactor to update bionic to %s...' % new_version |
Elliott Hughes | eb06129 | 2012-10-19 12:05:24 -0700 | [diff] [blame] | 86 | libcore_src_dir = '%s/../libcore/luni/src/main/java/' % bionic_dir |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 87 | subprocess.check_call(['javac', '-d', '.', |
| 88 | '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir, |
Elliott Hughes | eb06129 | 2012-10-19 12:05:24 -0700 | [diff] [blame] | 89 | '%s/libcore/util/ZoneInfo.java' % libcore_src_dir, |
| 90 | '%s/libcore/io/BufferIterator.java' % libcore_src_dir]) |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 91 | subprocess.check_call(['java', 'ZoneCompactor', |
Elliott Hughes | 2393535 | 2012-10-22 14:47:58 -0700 | [diff] [blame] | 92 | 'setup', 'data', 'extracted/zone.tab', |
| 93 | bionic_libc_zoneinfo_dir, new_version]) |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 94 | |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 95 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 96 | # Run with no arguments from any directory, with no special setup required. |
| 97 | def main(): |
| 98 | # URL from "Sources for Time Zone and Daylight Saving Time Data" |
| 99 | # http://www.twinsun.com/tz/tz-link.htm |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 100 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 101 | print 'Looking for new tzdata...' |
| 102 | ftp = ftplib.FTP('ftp.iana.org') |
| 103 | ftp.login() |
| 104 | ftp.cwd('tz/releases') |
| 105 | tzdata_filenames = [] |
| 106 | for filename in ftp.nlst(): |
| 107 | if filename.startswith('tzdata20'): |
| 108 | tzdata_filenames.append(filename) |
| 109 | tzdata_filenames.sort() |
Elliott Hughes | bcb2eda | 2011-10-24 10:47:25 -0700 | [diff] [blame] | 110 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 111 | # If you're several releases behind, we'll walk you through the upgrades |
| 112 | # one by one. |
| 113 | current_version = GetCurrentTzDataVersion() |
| 114 | current_filename = '%s.tar.gz' % current_version |
| 115 | for filename in tzdata_filenames: |
| 116 | if filename > current_filename: |
| 117 | print 'Found new tzdata: %s' % filename |
| 118 | UpgradeTo(ftp, filename) |
| 119 | sys.exit(0) |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 120 | |
Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 121 | print 'You already have the latest tzdata (%s)!' % current_version |
| 122 | sys.exit(0) |
| 123 | |
| 124 | |
| 125 | if __name__ == '__main__': |
| 126 | main() |