Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 2 | # Run with no arguments from any directory, with no special setup required. |
| 3 | |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 4 | import ftplib |
| 5 | import hashlib |
| 6 | import os |
| 7 | import re |
| 8 | import shutil |
| 9 | import string |
| 10 | import subprocess |
| 11 | import sys |
| 12 | import tarfile |
| 13 | import tempfile |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 14 | |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 15 | # Find the bionic directory, searching upward from this script. |
| 16 | bionic_libc_tools_zoneinfo_dir = os.path.realpath(os.path.dirname(sys.argv[0])) |
| 17 | bionic_libc_tools_dir = os.path.dirname(bionic_libc_tools_zoneinfo_dir) |
| 18 | bionic_libc_dir = os.path.dirname(bionic_libc_tools_dir) |
| 19 | bionic_dir = os.path.dirname(bionic_libc_dir) |
| 20 | bionic_libc_zoneinfo_dir = '%s/libc/zoneinfo' % bionic_dir |
| 21 | if not os.path.isdir(bionic_libc_tools_zoneinfo_dir) or not os.path.isdir(bionic_libc_zoneinfo_dir): |
| 22 | print "Couldn't find bionic/libc/tools/zoneinfo!" |
| 23 | sys.exit(1) |
| 24 | print 'Found bionic in %s...' % bionic_dir |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 25 | |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 26 | |
Elliott Hughes | 541c225 | 2012-07-26 16:19:46 -0700 | [diff] [blame] | 27 | regions = ['africa', 'antarctica', 'asia', 'australasia', 'backward', 'etcetera', 'europe', 'northamerica', 'southamerica'] |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def current_tzdata_version(): |
| 31 | return open('%s/zoneinfo.version' % bionic_libc_zoneinfo_dir).readline().rstrip('\n') |
| 32 | |
| 33 | |
| 34 | def md5_file(filename): |
| 35 | md5 = hashlib.md5() |
| 36 | f = open(filename, 'rb') |
| 37 | while True: |
| 38 | data = f.read(8192) |
| 39 | if not data: |
| 40 | break |
| 41 | md5.update(data) |
| 42 | return md5.hexdigest() |
| 43 | |
| 44 | |
| 45 | def upgrade_to(ftp, filename): |
| 46 | version = re.search('tzdata(.+)\.tar\.gz', filename).group(1) |
| 47 | |
| 48 | # Switch to a temporary directory. |
| 49 | tmp_dir = tempfile.mkdtemp('-tzdata') |
| 50 | os.chdir(tmp_dir) |
| 51 | print 'Created temporary directory "%s"...' % tmp_dir |
| 52 | |
| 53 | print 'Downloading %s...' % filename |
| 54 | ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write) |
| 55 | print 'MD5: %s' % md5_file(filename) |
| 56 | |
| 57 | print 'Extracting...' |
| 58 | os.mkdir('extracted') |
| 59 | tar = tarfile.open(filename, 'r') |
| 60 | tar.extractall('extracted') |
| 61 | |
| 62 | print 'Calling zic(1)...' |
| 63 | os.mkdir('data') |
| 64 | for region in regions: |
| 65 | if region != 'backward': |
| 66 | subprocess.check_call(['zic', '-d', 'data', 'extracted/%s' % region]) |
| 67 | |
| 68 | # Collect the data ZoneCompactor needs. |
| 69 | links = [] |
| 70 | zones = [] |
| 71 | for region in regions: |
| 72 | for line in open('extracted/%s' % region).readlines(): |
| 73 | fields = string.split(line) |
| 74 | if len(fields) == 0: |
| 75 | continue |
| 76 | elif fields[0] == 'Link': |
| 77 | links.append('%s %s %s\n' % (fields[0], fields[1], fields[2])) |
| 78 | zones.append(fields[2]) |
| 79 | elif fields[0] == 'Zone': |
| 80 | zones.append(fields[1]) |
| 81 | zones.sort() |
| 82 | |
| 83 | # Write it into the "setup" file. |
| 84 | setup = open('setup', 'w') |
| 85 | for link in links: |
| 86 | setup.write(link) |
| 87 | for zone in zones: |
| 88 | setup.write('%s\n' % zone) |
| 89 | setup.close() |
| 90 | |
| 91 | print 'Calling ZoneCompactor...' |
Elliott Hughes | eb06129 | 2012-10-19 12:05:24 -0700 | [diff] [blame^] | 92 | libcore_src_dir = '%s/../libcore/luni/src/main/java/' % bionic_dir |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 93 | subprocess.check_call(['javac', '-d', '.', |
| 94 | '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir, |
Elliott Hughes | eb06129 | 2012-10-19 12:05:24 -0700 | [diff] [blame^] | 95 | '%s/libcore/util/ZoneInfo.java' % libcore_src_dir, |
| 96 | '%s/libcore/io/BufferIterator.java' % libcore_src_dir]) |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 97 | subprocess.check_call(['java', 'ZoneCompactor', 'setup', 'data']) |
| 98 | |
| 99 | print 'Updating bionic from %s to %s...' % (current_tzdata_version(), version) |
| 100 | # Move the .dat and .idx files... |
| 101 | os.remove('%s/zoneinfo.dat' % bionic_libc_zoneinfo_dir) |
| 102 | shutil.move('zoneinfo.dat', bionic_libc_zoneinfo_dir) |
| 103 | os.remove('%s/zoneinfo.idx' % bionic_libc_zoneinfo_dir) |
| 104 | shutil.move('zoneinfo.idx', bionic_libc_zoneinfo_dir) |
| 105 | # Write the .version file... |
| 106 | zoneinfo_version = open('%s/zoneinfo.version' % bionic_libc_zoneinfo_dir, 'wb+') |
| 107 | zoneinfo_version.write('%s\n' % version) |
| 108 | zoneinfo_version.close() |
| 109 | |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 110 | |
| 111 | # URL from "Sources for Time Zone and Daylight Saving Time Data" |
| 112 | # http://www.twinsun.com/tz/tz-link.htm |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 113 | |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 114 | print 'Looking for new tzdata...' |
| 115 | ftp = ftplib.FTP('ftp.iana.org') |
| 116 | ftp.login() |
| 117 | ftp.cwd('tz/releases') |
| 118 | tzdata_filenames = [] |
| 119 | for filename in ftp.nlst(): |
| 120 | if filename.startswith('tzdata20'): |
| 121 | tzdata_filenames.append(filename) |
| 122 | tzdata_filenames.sort() |
Elliott Hughes | bcb2eda | 2011-10-24 10:47:25 -0700 | [diff] [blame] | 123 | |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 124 | # If you're several releases behind, we'll walk you through the upgrades one by one. |
| 125 | current_version = current_tzdata_version() |
| 126 | current_filename = 'tzdata%s.tar.gz' % current_version |
| 127 | for filename in tzdata_filenames: |
| 128 | if filename > current_filename: |
| 129 | upgrade_to(ftp, filename) |
| 130 | sys.exit(0) |
Elliott Hughes | d40e63e | 2011-02-17 16:20:07 -0800 | [diff] [blame] | 131 | |
Elliott Hughes | 5d967e4 | 2012-07-20 16:52:39 -0700 | [diff] [blame] | 132 | print 'You already have the latest tzdata (%s)!' % current_version |
| 133 | sys.exit(0) |