2012-07-21 01:52:39 +02:00
|
|
|
#!/usr/bin/python
|
2012-10-19 23:47:37 +02:00
|
|
|
|
|
|
|
"""Updates the tzdata file."""
|
2011-02-18 01:20:07 +01:00
|
|
|
|
2012-07-21 01:52:39 +02:00
|
|
|
import ftplib
|
2013-04-22 20:11:43 +02:00
|
|
|
import httplib
|
2012-07-21 01:52:39 +02:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tarfile
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
# Find the bionic directory, searching upward from this script.
|
|
|
|
bionic_libc_tools_zoneinfo_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
|
|
|
|
bionic_libc_tools_dir = os.path.dirname(bionic_libc_tools_zoneinfo_dir)
|
|
|
|
bionic_libc_dir = os.path.dirname(bionic_libc_tools_dir)
|
|
|
|
bionic_dir = os.path.dirname(bionic_libc_dir)
|
|
|
|
bionic_libc_zoneinfo_dir = '%s/libc/zoneinfo' % bionic_dir
|
2012-10-19 23:47:37 +02:00
|
|
|
|
|
|
|
if not os.path.isdir(bionic_libc_tools_zoneinfo_dir):
|
2012-07-21 01:52:39 +02:00
|
|
|
print "Couldn't find bionic/libc/tools/zoneinfo!"
|
|
|
|
sys.exit(1)
|
2012-10-19 23:47:37 +02:00
|
|
|
if not os.path.isdir(bionic_libc_zoneinfo_dir):
|
|
|
|
print "Couldn't find bionic/libc/zoneinfo!"
|
|
|
|
sys.exit(1)
|
|
|
|
|
2012-07-21 01:52:39 +02:00
|
|
|
print 'Found bionic in %s...' % bionic_dir
|
|
|
|
|
|
|
|
|
2012-10-19 23:47:37 +02:00
|
|
|
regions = ['africa', 'antarctica', 'asia', 'australasia', 'backward',
|
|
|
|
'etcetera', 'europe', 'northamerica', 'southamerica']
|
|
|
|
|
2012-07-21 01:52:39 +02:00
|
|
|
|
2012-10-19 23:47:37 +02:00
|
|
|
def GetCurrentTzDataVersion():
|
2012-11-05 17:53:28 +01:00
|
|
|
return open('%s/tzdata' % bionic_libc_zoneinfo_dir).read().split('\x00', 1)[0]
|
2012-07-21 01:52:39 +02:00
|
|
|
|
|
|
|
|
2012-10-19 23:47:37 +02:00
|
|
|
def WriteSetupFile():
|
2012-11-05 17:53:28 +01:00
|
|
|
"""Writes the list of zones that ZoneCompactor should process."""
|
2012-10-19 23:47:37 +02:00
|
|
|
links = []
|
|
|
|
zones = []
|
|
|
|
for region in regions:
|
|
|
|
for line in open('extracted/%s' % region):
|
|
|
|
fields = line.split()
|
2012-11-05 17:53:28 +01:00
|
|
|
if fields:
|
|
|
|
if fields[0] == 'Link':
|
|
|
|
links.append('%s %s %s\n' % (fields[0], fields[1], fields[2]))
|
|
|
|
zones.append(fields[2])
|
|
|
|
elif fields[0] == 'Zone':
|
|
|
|
zones.append(fields[1])
|
2012-10-19 23:47:37 +02:00
|
|
|
zones.sort()
|
2012-07-21 01:52:39 +02:00
|
|
|
|
2012-10-19 23:47:37 +02:00
|
|
|
setup = open('setup', 'w')
|
|
|
|
for link in links:
|
|
|
|
setup.write(link)
|
|
|
|
for zone in zones:
|
|
|
|
setup.write('%s\n' % zone)
|
|
|
|
setup.close()
|
2012-07-21 01:52:39 +02:00
|
|
|
|
|
|
|
|
2013-04-22 20:11:43 +02:00
|
|
|
def SwitchToNewTemporaryDirectory():
|
|
|
|
tmp_dir = tempfile.mkdtemp('-tzdata')
|
|
|
|
os.chdir(tmp_dir)
|
|
|
|
print 'Created temporary directory "%s"...' % tmp_dir
|
2012-11-05 17:53:28 +01:00
|
|
|
|
|
|
|
|
2013-04-22 20:11:43 +02:00
|
|
|
def FtpRetrieve(ftp, filename):
|
|
|
|
ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
|
2012-11-05 17:53:28 +01:00
|
|
|
|
2012-07-21 01:52:39 +02:00
|
|
|
|
2013-04-22 20:11:43 +02:00
|
|
|
def FtpUpgrade(ftp, data_filename):
|
|
|
|
"""Downloads and repackages the given data from the given FTP server."""
|
|
|
|
SwitchToNewTemporaryDirectory()
|
2012-07-21 01:52:39 +02:00
|
|
|
|
2012-11-26 22:44:49 +01:00
|
|
|
print 'Downloading data...'
|
2013-04-22 20:11:43 +02:00
|
|
|
FtpRetrieve(ftp, data_filename)
|
2012-11-26 22:44:49 +01:00
|
|
|
|
|
|
|
print 'Downloading signature...'
|
|
|
|
signature_filename = '%s.asc' % data_filename
|
2013-04-22 20:11:43 +02:00
|
|
|
FtpRetrieve(ftp, signature_filename)
|
2012-11-05 17:53:28 +01:00
|
|
|
|
2013-04-22 20:11:43 +02:00
|
|
|
ExtractAndCompile(data_filename)
|
|
|
|
|
|
|
|
|
2013-04-22 20:41:57 +02:00
|
|
|
def HttpRetrieve(http, path, output_filename):
|
|
|
|
http.request("GET", path)
|
|
|
|
f = open(output_filename, 'wb')
|
|
|
|
f.write(http.getresponse().read())
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
2013-04-22 20:11:43 +02:00
|
|
|
def HttpUpgrade(http, data_filename):
|
|
|
|
"""Downloads and repackages the given data from the given HTTP server."""
|
|
|
|
SwitchToNewTemporaryDirectory()
|
|
|
|
|
2013-04-22 20:41:57 +02:00
|
|
|
path = "/time-zones/repository/releases/%s" % data_filename
|
|
|
|
|
2013-04-22 20:11:43 +02:00
|
|
|
print 'Downloading data...'
|
2013-04-22 20:41:57 +02:00
|
|
|
HttpRetrieve(http, path, data_filename)
|
|
|
|
|
|
|
|
print 'Downloading signature...'
|
|
|
|
signature_filename = '%s.asc' % data_filename
|
|
|
|
HttpRetrieve(http, "%s.asc" % path, signature_filename)
|
2013-04-22 20:11:43 +02:00
|
|
|
|
|
|
|
ExtractAndCompile(data_filename)
|
|
|
|
|
|
|
|
|
|
|
|
def ExtractAndCompile(data_filename):
|
|
|
|
new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1)
|
|
|
|
|
2013-04-22 20:41:57 +02:00
|
|
|
signature_filename = '%s.asc' % data_filename
|
|
|
|
print 'Verifying signature...'
|
|
|
|
# If this fails for you, you probably need to import Paul Eggert's public key:
|
|
|
|
# gpg --recv-keys ED97E90E62AA7E34
|
|
|
|
subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify',
|
|
|
|
signature_filename, data_filename])
|
|
|
|
|
2012-07-21 01:52:39 +02:00
|
|
|
print 'Extracting...'
|
|
|
|
os.mkdir('extracted')
|
2012-11-05 17:53:28 +01:00
|
|
|
tar = tarfile.open(data_filename, 'r')
|
2012-07-21 01:52:39 +02:00
|
|
|
tar.extractall('extracted')
|
|
|
|
|
|
|
|
print 'Calling zic(1)...'
|
|
|
|
os.mkdir('data')
|
|
|
|
for region in regions:
|
|
|
|
if region != 'backward':
|
|
|
|
subprocess.check_call(['zic', '-d', 'data', 'extracted/%s' % region])
|
|
|
|
|
2012-10-19 23:47:37 +02:00
|
|
|
WriteSetupFile()
|
2012-07-21 01:52:39 +02:00
|
|
|
|
2012-10-19 23:47:37 +02:00
|
|
|
print 'Calling ZoneCompactor to update bionic to %s...' % new_version
|
2012-10-19 21:05:24 +02:00
|
|
|
libcore_src_dir = '%s/../libcore/luni/src/main/java/' % bionic_dir
|
2012-07-21 01:52:39 +02:00
|
|
|
subprocess.check_call(['javac', '-d', '.',
|
|
|
|
'%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir,
|
2012-10-19 21:05:24 +02:00
|
|
|
'%s/libcore/util/ZoneInfo.java' % libcore_src_dir,
|
|
|
|
'%s/libcore/io/BufferIterator.java' % libcore_src_dir])
|
2012-10-19 23:47:37 +02:00
|
|
|
subprocess.check_call(['java', 'ZoneCompactor',
|
2012-10-22 23:47:58 +02:00
|
|
|
'setup', 'data', 'extracted/zone.tab',
|
|
|
|
bionic_libc_zoneinfo_dir, new_version])
|
2012-10-19 23:47:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Run with no arguments from any directory, with no special setup required.
|
2012-11-05 17:53:28 +01:00
|
|
|
# See http://www.iana.org/time-zones/ for more about the source of this data.
|
2012-10-19 23:47:37 +02:00
|
|
|
def main():
|
|
|
|
print 'Looking for new tzdata...'
|
2013-04-22 20:11:43 +02:00
|
|
|
|
2012-10-19 23:47:37 +02:00
|
|
|
tzdata_filenames = []
|
2013-04-22 20:11:43 +02:00
|
|
|
|
|
|
|
# The FTP server lets you download intermediate releases, and also lets you
|
2013-04-22 22:44:50 +02:00
|
|
|
# download the signatures for verification, so it's your best choice.
|
2013-04-22 20:11:43 +02:00
|
|
|
use_ftp = True
|
|
|
|
|
|
|
|
if use_ftp:
|
|
|
|
ftp = ftplib.FTP('ftp.iana.org')
|
|
|
|
ftp.login()
|
|
|
|
ftp.cwd('tz/releases')
|
|
|
|
for filename in ftp.nlst():
|
|
|
|
if filename.startswith('tzdata20') and filename.endswith('.tar.gz'):
|
|
|
|
tzdata_filenames.append(filename)
|
|
|
|
tzdata_filenames.sort()
|
|
|
|
else:
|
|
|
|
http = httplib.HTTPConnection('www.iana.org')
|
|
|
|
http.request("GET", "/time-zones")
|
|
|
|
index_lines = http.getresponse().read().split('\n')
|
|
|
|
for line in index_lines:
|
|
|
|
m = re.compile('.*href="/time-zones/repository/releases/(tzdata20\d\d\c\.tar\.gz)".*').match(line)
|
|
|
|
if m:
|
|
|
|
tzdata_filenames.append(m.group(1))
|
2012-10-19 23:47:37 +02:00
|
|
|
|
|
|
|
# If you're several releases behind, we'll walk you through the upgrades
|
|
|
|
# one by one.
|
|
|
|
current_version = GetCurrentTzDataVersion()
|
|
|
|
current_filename = '%s.tar.gz' % current_version
|
|
|
|
for filename in tzdata_filenames:
|
|
|
|
if filename > current_filename:
|
|
|
|
print 'Found new tzdata: %s' % filename
|
2013-04-22 20:11:43 +02:00
|
|
|
if use_ftp:
|
|
|
|
FtpUpgrade(ftp, filename)
|
|
|
|
else:
|
|
|
|
HttpUpgrade(http, filename)
|
2012-10-19 23:47:37 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
print 'You already have the latest tzdata (%s)!' % current_version
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|