blob: ba8ea0dff1e726ae497350b343640d2d674845d7 [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():
Elliott Hughese3063f42012-11-05 08:53:28 -080035 return open('%s/tzdata' % bionic_libc_zoneinfo_dir).read().split('\x00', 1)[0]
Elliott Hughes5d967e42012-07-20 16:52:39 -070036
37
Elliott Hughes5b1497a2012-10-19 14:47:37 -070038def WriteSetupFile():
Elliott Hughese3063f42012-11-05 08:53:28 -080039 """Writes the list of zones that ZoneCompactor should process."""
Elliott Hughes5b1497a2012-10-19 14:47:37 -070040 links = []
41 zones = []
42 for region in regions:
43 for line in open('extracted/%s' % region):
44 fields = line.split()
Elliott Hughese3063f42012-11-05 08:53:28 -080045 if fields:
46 if 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])
Elliott Hughes5b1497a2012-10-19 14:47:37 -070051 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 Hughese3063f42012-11-05 08:53:28 -080061def Retrieve(ftp, filename):
62 ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
63
64
65def UpgradeTo(ftp, data_filename):
66 """Downloads and repackages the given data from the given FTP server."""
67
68 new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1)
69 signature_filename = '%s.sign' % data_filename
Elliott Hughes5d967e42012-07-20 16:52:39 -070070
71 # Switch to a temporary directory.
72 tmp_dir = tempfile.mkdtemp('-tzdata')
73 os.chdir(tmp_dir)
74 print 'Created temporary directory "%s"...' % tmp_dir
75
Elliott Hughese3063f42012-11-05 08:53:28 -080076 print 'Downloading data and signature...'
77 Retrieve(ftp, data_filename)
78 Retrieve(ftp, signature_filename)
79
80 print 'Verifying signature...'
81 # If this fails for you, you probably need to import Paul Eggert's public key:
82 # gpg --recv-keys ED97E90E62AA7E34
83 subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34',
84 '--verify', signature_filename, data_filename])
Elliott Hughes5d967e42012-07-20 16:52:39 -070085
86 print 'Extracting...'
87 os.mkdir('extracted')
Elliott Hughese3063f42012-11-05 08:53:28 -080088 tar = tarfile.open(data_filename, 'r')
Elliott Hughes5d967e42012-07-20 16:52:39 -070089 tar.extractall('extracted')
90
91 print 'Calling zic(1)...'
92 os.mkdir('data')
93 for region in regions:
94 if region != 'backward':
95 subprocess.check_call(['zic', '-d', 'data', 'extracted/%s' % region])
96
Elliott Hughes5b1497a2012-10-19 14:47:37 -070097 WriteSetupFile()
Elliott Hughes5d967e42012-07-20 16:52:39 -070098
Elliott Hughes5b1497a2012-10-19 14:47:37 -070099 print 'Calling ZoneCompactor to update bionic to %s...' % new_version
Elliott Hugheseb061292012-10-19 12:05:24 -0700100 libcore_src_dir = '%s/../libcore/luni/src/main/java/' % bionic_dir
Elliott Hughes5d967e42012-07-20 16:52:39 -0700101 subprocess.check_call(['javac', '-d', '.',
102 '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir,
Elliott Hugheseb061292012-10-19 12:05:24 -0700103 '%s/libcore/util/ZoneInfo.java' % libcore_src_dir,
104 '%s/libcore/io/BufferIterator.java' % libcore_src_dir])
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700105 subprocess.check_call(['java', 'ZoneCompactor',
Elliott Hughes23935352012-10-22 14:47:58 -0700106 'setup', 'data', 'extracted/zone.tab',
107 bionic_libc_zoneinfo_dir, new_version])
Elliott Hughes5d967e42012-07-20 16:52:39 -0700108
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800109
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700110# Run with no arguments from any directory, with no special setup required.
Elliott Hughese3063f42012-11-05 08:53:28 -0800111# See http://www.iana.org/time-zones/ for more about the source of this data.
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700112def main():
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700113 print 'Looking for new tzdata...'
114 ftp = ftplib.FTP('ftp.iana.org')
115 ftp.login()
116 ftp.cwd('tz/releases')
117 tzdata_filenames = []
118 for filename in ftp.nlst():
Elliott Hughese3063f42012-11-05 08:53:28 -0800119 if filename.startswith('tzdata20') and filename.endswith('.tar.gz'):
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700120 tzdata_filenames.append(filename)
121 tzdata_filenames.sort()
Elliott Hughesbcb2eda2011-10-24 10:47:25 -0700122
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700123 # If you're several releases behind, we'll walk you through the upgrades
124 # one by one.
125 current_version = GetCurrentTzDataVersion()
126 current_filename = '%s.tar.gz' % current_version
127 for filename in tzdata_filenames:
128 if filename > current_filename:
129 print 'Found new tzdata: %s' % filename
130 UpgradeTo(ftp, filename)
131 sys.exit(0)
Elliott Hughesd40e63e2011-02-17 16:20:07 -0800132
Elliott Hughes5b1497a2012-10-19 14:47:37 -0700133 print 'You already have the latest tzdata (%s)!' % current_version
134 sys.exit(0)
135
136
137if __name__ == '__main__':
138 main()