blob: 27d023fdb9e0a908c8cc962b997258df634fa6dd [file] [log] [blame]
Elliott Hughes5d967e42012-07-20 16:52:39 -07001#!/usr/bin/python
Elliott Hughes5b1497a2012-10-19 14:47:37 -07002
3"""Updates the tzdata file."""
Elliott Hughesd40e63e2011-02-17 16:20:07 -08004
Elliott Hughes5d967e42012-07-20 16:52:39 -07005import ftplib
Elliott Hughes5d967e42012-07-20 16:52:39 -07006import os
7import re
Elliott Hughes5d967e42012-07-20 16:52:39 -07008import subprocess
9import sys
10import tarfile
11import tempfile
Elliott Hughesd40e63e2011-02-17 16:20:07 -080012
Elliott Hughes5d967e42012-07-20 16:52:39 -070013# Find the bionic directory, searching upward from this script.
14bionic_libc_tools_zoneinfo_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
15bionic_libc_tools_dir = os.path.dirname(bionic_libc_tools_zoneinfo_dir)
16bionic_libc_dir = os.path.dirname(bionic_libc_tools_dir)
17bionic_dir = os.path.dirname(bionic_libc_dir)
18bionic_libc_zoneinfo_dir = '%s/libc/zoneinfo' % bionic_dir
Elliott Hughes5b1497a2012-10-19 14:47:37 -070019
20if not os.path.isdir(bionic_libc_tools_zoneinfo_dir):
Elliott Hughes5d967e42012-07-20 16:52:39 -070021 print "Couldn't find bionic/libc/tools/zoneinfo!"
22 sys.exit(1)
Elliott Hughes5b1497a2012-10-19 14:47:37 -070023if not os.path.isdir(bionic_libc_zoneinfo_dir):
24 print "Couldn't find bionic/libc/zoneinfo!"
25 sys.exit(1)
26
Elliott Hughes5d967e42012-07-20 16:52:39 -070027print 'Found bionic in %s...' % bionic_dir
Elliott Hughesd40e63e2011-02-17 16:20:07 -080028
Elliott Hughes5d967e42012-07-20 16:52:39 -070029
Elliott Hughes5b1497a2012-10-19 14:47:37 -070030regions = ['africa', 'antarctica', 'asia', 'australasia', 'backward',
31 'etcetera', 'europe', 'northamerica', 'southamerica']
Elliott Hughes5d967e42012-07-20 16:52:39 -070032
33
Elliott Hughes5b1497a2012-10-19 14:47:37 -070034def GetCurrentTzDataVersion():
35 return open('%s/tzdata' % bionic_libc_zoneinfo_dir).read().split('\0', 1)[0]
Elliott Hughes5d967e42012-07-20 16:52:39 -070036
37
Elliott Hughes5b1497a2012-10-19 14:47:37 -070038def 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 Hughes5d967e42012-07-20 16:52:39 -070059
60
Elliott Hughes5b1497a2012-10-19 14:47:37 -070061def UpgradeTo(ftp, filename):
62 new_version = re.search('(tzdata.+)\.tar\.gz', filename).group(1)
Elliott Hughes5d967e42012-07-20 16:52:39 -070063
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 Hughes5b1497a2012-10-19 14:47:37 -070069 print 'Downloading...'
Elliott Hughes5d967e42012-07-20 16:52:39 -070070 ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
Elliott Hughes5d967e42012-07-20 16:52:39 -070071
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 Hughes5b1497a2012-10-19 14:47:37 -070083 WriteSetupFile()
Elliott Hughes5d967e42012-07-20 16:52:39 -070084
Elliott Hughes5b1497a2012-10-19 14:47:37 -070085 print 'Calling ZoneCompactor to update bionic to %s...' % new_version
Elliott Hugheseb061292012-10-19 12:05:24 -070086 libcore_src_dir = '%s/../libcore/luni/src/main/java/' % bionic_dir
Elliott Hughes5d967e42012-07-20 16:52:39 -070087 subprocess.check_call(['javac', '-d', '.',
88 '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir,
Elliott Hugheseb061292012-10-19 12:05:24 -070089 '%s/libcore/util/ZoneInfo.java' % libcore_src_dir,
90 '%s/libcore/io/BufferIterator.java' % libcore_src_dir])
Elliott Hughes5b1497a2012-10-19 14:47:37 -070091 subprocess.check_call(['java', 'ZoneCompactor',
Elliott Hughes23935352012-10-22 14:47:58 -070092 'setup', 'data', 'extracted/zone.tab',
93 bionic_libc_zoneinfo_dir, new_version])
Elliott Hughes5d967e42012-07-20 16:52:39 -070094
Elliott Hughesd40e63e2011-02-17 16:20:07 -080095
Elliott Hughes5b1497a2012-10-19 14:47:37 -070096# Run with no arguments from any directory, with no special setup required.
97def main():
98 # URL from "Sources for Time Zone and Daylight Saving Time Data"
99 # http://www.twinsun.com/tz/tz-link.htm
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800100
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700101 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 Hughesbcb2eda2011-10-24 10:47:25 -0700110
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700111 # 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 Hughesd40e63e2011-02-17 16:20:07 -0800120
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700121 print 'You already have the latest tzdata (%s)!' % current_version
122 sys.exit(0)
123
124
125if __name__ == '__main__':
126 main()