blob: e9ff59bf68fdb215238d34259a948dbb11df956b [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',
92 'setup', 'data', bionic_libc_zoneinfo_dir, new_version])
Elliott Hughes5d967e42012-07-20 16:52:39 -070093
Elliott Hughesd40e63e2011-02-17 16:20:07 -080094
Elliott Hughes5b1497a2012-10-19 14:47:37 -070095# Run with no arguments from any directory, with no special setup required.
96def main():
97 # URL from "Sources for Time Zone and Daylight Saving Time Data"
98 # http://www.twinsun.com/tz/tz-link.htm
Elliott Hughesd40e63e2011-02-17 16:20:07 -080099
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700100 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 Hughesbcb2eda2011-10-24 10:47:25 -0700109
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700110 # 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 Hughesd40e63e2011-02-17 16:20:07 -0800119
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700120 print 'You already have the latest tzdata (%s)!' % current_version
121 sys.exit(0)
122
123
124if __name__ == '__main__':
125 main()