| 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', | 
|  | 92 | 'setup', 'data', bionic_libc_zoneinfo_dir, new_version]) | 
| Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 93 |  | 
| Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 94 |  | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 95 | # Run with no arguments from any directory, with no special setup required. | 
|  | 96 | def main(): | 
|  | 97 | # URL from "Sources for Time Zone and Daylight Saving Time Data" | 
|  | 98 | # http://www.twinsun.com/tz/tz-link.htm | 
| Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 99 |  | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 100 | print 'Looking for new tzdata...' | 
|  | 101 | ftp = ftplib.FTP('ftp.iana.org') | 
|  | 102 | ftp.login() | 
|  | 103 | ftp.cwd('tz/releases') | 
|  | 104 | tzdata_filenames = [] | 
|  | 105 | for filename in ftp.nlst(): | 
|  | 106 | if filename.startswith('tzdata20'): | 
|  | 107 | tzdata_filenames.append(filename) | 
|  | 108 | tzdata_filenames.sort() | 
| Elliott Hughes | bcb2eda | 2011-10-24 10:47:25 -0700 | [diff] [blame] | 109 |  | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 110 | # If you're several releases behind, we'll walk you through the upgrades | 
|  | 111 | # one by one. | 
|  | 112 | current_version = GetCurrentTzDataVersion() | 
|  | 113 | current_filename = '%s.tar.gz' % current_version | 
|  | 114 | for filename in tzdata_filenames: | 
|  | 115 | if filename > current_filename: | 
|  | 116 | print 'Found new tzdata: %s' % filename | 
|  | 117 | UpgradeTo(ftp, filename) | 
|  | 118 | sys.exit(0) | 
| Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 119 |  | 
| Elliott Hughes | 5b1497a | 2012-10-19 14:47:37 -0700 | [diff] [blame] | 120 | print 'You already have the latest tzdata (%s)!' % current_version | 
|  | 121 | sys.exit(0) | 
|  | 122 |  | 
|  | 123 |  | 
|  | 124 | if __name__ == '__main__': | 
|  | 125 | main() |