2012-07-20 16:52:39 -07:00
|
|
|
#!/usr/bin/python
|
2012-10-19 14:47:37 -07:00
|
|
|
|
2014-05-16 18:04:48 +01:00
|
|
|
"""Updates the timezone data held in bionic and ICU."""
|
2011-02-17 16:20:07 -08:00
|
|
|
|
2012-07-20 16:52:39 -07:00
|
|
|
import ftplib
|
2014-05-16 18:04:48 +01:00
|
|
|
import glob
|
2013-04-22 11:11:43 -07:00
|
|
|
import httplib
|
2012-07-20 16:52:39 -07:00
|
|
|
import os
|
|
|
|
import re
|
2014-05-16 18:04:48 +01:00
|
|
|
import shutil
|
2012-07-20 16:52:39 -07:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tarfile
|
|
|
|
import tempfile
|
|
|
|
|
2014-11-11 14:10:51 -08:00
|
|
|
regions = ['africa', 'antarctica', 'asia', 'australasia',
|
|
|
|
'etcetera', 'europe', 'northamerica', 'southamerica',
|
|
|
|
# These two deliberately come last so they override what came
|
|
|
|
# before (and each other).
|
|
|
|
'backward', 'backzone' ]
|
2012-10-19 14:47:37 -07:00
|
|
|
|
2014-05-16 18:04:48 +01:00
|
|
|
def CheckDirExists(dir, dirname):
|
|
|
|
if not os.path.isdir(dir):
|
|
|
|
print "Couldn't find %s (%s)!" % (dirname, dir)
|
|
|
|
sys.exit(1)
|
2012-10-19 14:47:37 -07:00
|
|
|
|
2014-05-16 18:04:48 +01:00
|
|
|
bionic_libc_tools_zoneinfo_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
|
2012-07-20 16:52:39 -07:00
|
|
|
|
2014-05-16 18:04:48 +01:00
|
|
|
# Find the bionic directory, searching upward from this script.
|
|
|
|
bionic_dir = os.path.realpath('%s/../../..' % bionic_libc_tools_zoneinfo_dir)
|
|
|
|
bionic_libc_zoneinfo_dir = '%s/libc/zoneinfo' % bionic_dir
|
|
|
|
CheckDirExists(bionic_libc_zoneinfo_dir, 'bionic/libc/zoneinfo')
|
|
|
|
CheckDirExists(bionic_libc_tools_zoneinfo_dir, 'bionic/libc/tools/zoneinfo')
|
|
|
|
print 'Found bionic in %s ...' % bionic_dir
|
2012-07-20 16:52:39 -07:00
|
|
|
|
2015-04-09 09:22:25 +01:00
|
|
|
# Find the icu directory.
|
|
|
|
icu_dir = os.path.realpath('%s/../external/icu' % bionic_dir)
|
|
|
|
icu4c_dir = os.path.realpath('%s/icu4c/source' % icu_dir)
|
|
|
|
icu4j_dir = os.path.realpath('%s/icu4j' % icu_dir)
|
|
|
|
CheckDirExists(icu4c_dir, 'external/icu/icu4c/source')
|
|
|
|
CheckDirExists(icu4j_dir, 'external/icu/icu4j')
|
2014-05-16 18:04:48 +01:00
|
|
|
print 'Found icu in %s ...' % icu_dir
|
2012-10-19 14:47:37 -07:00
|
|
|
|
2012-07-20 16:52:39 -07:00
|
|
|
|
2012-10-19 14:47:37 -07:00
|
|
|
def GetCurrentTzDataVersion():
|
2012-11-05 08:53:28 -08:00
|
|
|
return open('%s/tzdata' % bionic_libc_zoneinfo_dir).read().split('\x00', 1)[0]
|
2012-07-20 16:52:39 -07:00
|
|
|
|
|
|
|
|
2012-10-19 14:47:37 -07:00
|
|
|
def WriteSetupFile():
|
2012-11-05 08:53:28 -08:00
|
|
|
"""Writes the list of zones that ZoneCompactor should process."""
|
2012-10-19 14:47:37 -07:00
|
|
|
links = []
|
|
|
|
zones = []
|
|
|
|
for region in regions:
|
|
|
|
for line in open('extracted/%s' % region):
|
|
|
|
fields = line.split()
|
2012-11-05 08:53:28 -08:00
|
|
|
if fields:
|
|
|
|
if fields[0] == 'Link':
|
2014-11-11 14:10:51 -08:00
|
|
|
links.append('%s %s %s' % (fields[0], fields[1], fields[2]))
|
2012-11-05 08:53:28 -08:00
|
|
|
zones.append(fields[2])
|
|
|
|
elif fields[0] == 'Zone':
|
|
|
|
zones.append(fields[1])
|
2012-10-19 14:47:37 -07:00
|
|
|
zones.sort()
|
2012-07-20 16:52:39 -07:00
|
|
|
|
2012-10-19 14:47:37 -07:00
|
|
|
setup = open('setup', 'w')
|
2014-11-11 14:10:51 -08:00
|
|
|
for link in sorted(set(links)):
|
|
|
|
setup.write('%s\n' % link)
|
|
|
|
for zone in sorted(set(zones)):
|
2012-10-19 14:47:37 -07:00
|
|
|
setup.write('%s\n' % zone)
|
|
|
|
setup.close()
|
2012-07-20 16:52:39 -07:00
|
|
|
|
|
|
|
|
2013-04-22 11:11:43 -07:00
|
|
|
def SwitchToNewTemporaryDirectory():
|
|
|
|
tmp_dir = tempfile.mkdtemp('-tzdata')
|
|
|
|
os.chdir(tmp_dir)
|
|
|
|
print 'Created temporary directory "%s"...' % tmp_dir
|
2012-11-05 08:53:28 -08:00
|
|
|
|
|
|
|
|
2014-05-16 18:04:48 +01:00
|
|
|
def FtpRetrieveFile(ftp, filename):
|
2013-04-22 11:11:43 -07:00
|
|
|
ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
|
2012-11-05 08:53:28 -08:00
|
|
|
|
2012-07-20 16:52:39 -07:00
|
|
|
|
2014-05-16 18:04:48 +01:00
|
|
|
def FtpRetrieveFileAndSignature(ftp, data_filename):
|
2013-04-22 11:11:43 -07:00
|
|
|
"""Downloads and repackages the given data from the given FTP server."""
|
2012-11-26 13:44:49 -08:00
|
|
|
print 'Downloading data...'
|
2014-05-16 18:04:48 +01:00
|
|
|
FtpRetrieveFile(ftp, data_filename)
|
2012-11-26 13:44:49 -08:00
|
|
|
|
|
|
|
print 'Downloading signature...'
|
|
|
|
signature_filename = '%s.asc' % data_filename
|
2014-05-16 18:04:48 +01:00
|
|
|
FtpRetrieveFile(ftp, signature_filename)
|
2013-04-22 11:11:43 -07:00
|
|
|
|
|
|
|
|
2014-05-16 18:04:48 +01:00
|
|
|
def HttpRetrieveFile(http, path, output_filename):
|
2013-04-22 11:41:57 -07:00
|
|
|
http.request("GET", path)
|
|
|
|
f = open(output_filename, 'wb')
|
|
|
|
f.write(http.getresponse().read())
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
2014-05-16 18:04:48 +01:00
|
|
|
def HttpRetrieveFileAndSignature(http, data_filename):
|
2013-04-22 11:11:43 -07:00
|
|
|
"""Downloads and repackages the given data from the given HTTP server."""
|
2013-04-22 11:41:57 -07:00
|
|
|
path = "/time-zones/repository/releases/%s" % data_filename
|
|
|
|
|
2013-04-22 11:11:43 -07:00
|
|
|
print 'Downloading data...'
|
2014-05-16 18:04:48 +01:00
|
|
|
HttpRetrieveFile(http, path, data_filename)
|
2013-04-22 11:41:57 -07:00
|
|
|
|
|
|
|
print 'Downloading signature...'
|
|
|
|
signature_filename = '%s.asc' % data_filename
|
2014-05-16 18:04:48 +01:00
|
|
|
HttpRetrievefile(http, "%s.asc" % path, signature_filename)
|
2013-04-22 11:11:43 -07:00
|
|
|
|
|
|
|
|
2014-05-16 18:04:48 +01:00
|
|
|
def BuildIcuToolsAndData(data_filename):
|
|
|
|
# Keep track of the original cwd so we can go back to it at the end.
|
|
|
|
original_working_dir = os.getcwd()
|
2013-04-22 11:11:43 -07:00
|
|
|
|
2014-05-16 18:04:48 +01:00
|
|
|
# Create a directory to run 'make' from.
|
|
|
|
icu_working_dir = '%s/icu' % original_working_dir
|
|
|
|
os.mkdir(icu_working_dir)
|
|
|
|
os.chdir(icu_working_dir)
|
|
|
|
|
|
|
|
# Build the ICU tools.
|
|
|
|
print 'Configuring ICU tools...'
|
2015-04-09 09:22:25 +01:00
|
|
|
subprocess.check_call(['%s/runConfigureICU' % icu4c_dir, 'Linux'])
|
2014-05-16 18:04:48 +01:00
|
|
|
|
|
|
|
# Run the ICU tools.
|
|
|
|
os.chdir('tools/tzcode')
|
2015-02-02 16:50:05 +00:00
|
|
|
|
|
|
|
# The tz2icu tool only picks up icuregions and icuzones in they are in the CWD
|
|
|
|
for icu_data_file in [ 'icuregions', 'icuzones']:
|
2015-04-09 09:22:25 +01:00
|
|
|
icu_data_file_source = '%s/tools/tzcode/%s' % (icu4c_dir, icu_data_file)
|
2015-02-02 16:50:05 +00:00
|
|
|
icu_data_file_symlink = './%s' % icu_data_file
|
|
|
|
os.symlink(icu_data_file_source, icu_data_file_symlink)
|
|
|
|
|
2014-05-16 18:04:48 +01:00
|
|
|
shutil.copyfile('%s/%s' % (original_working_dir, data_filename), data_filename)
|
|
|
|
print 'Making ICU data...'
|
2015-02-02 16:50:05 +00:00
|
|
|
# The Makefile assumes the existence of the bin directory.
|
|
|
|
os.mkdir('%s/bin' % icu_working_dir)
|
2014-05-16 18:04:48 +01:00
|
|
|
subprocess.check_call(['make'])
|
|
|
|
|
2014-09-30 17:30:01 -07:00
|
|
|
# Copy the source file to its ultimate destination.
|
2015-04-09 09:22:25 +01:00
|
|
|
icu_txt_data_dir = '%s/data/misc' % icu4c_dir
|
2014-05-16 18:04:48 +01:00
|
|
|
print 'Copying zoneinfo64.txt to %s ...' % icu_txt_data_dir
|
|
|
|
shutil.copy('zoneinfo64.txt', icu_txt_data_dir)
|
|
|
|
|
2014-09-30 17:30:01 -07:00
|
|
|
# Regenerate the .dat file.
|
2014-05-16 18:04:48 +01:00
|
|
|
os.chdir(icu_working_dir)
|
2015-02-04 17:17:34 +01:00
|
|
|
subprocess.check_call(['make', 'INCLUDE_UNI_CORE_DATA=1', '-j32'])
|
2014-09-30 17:30:01 -07:00
|
|
|
|
|
|
|
# Copy the .dat file to its ultimate destination.
|
2015-04-09 09:22:25 +01:00
|
|
|
icu_dat_data_dir = '%s/stubdata' % icu4c_dir
|
2014-05-21 16:59:09 +01:00
|
|
|
datfiles = glob.glob('data/out/tmp/icudt??l.dat')
|
|
|
|
if len(datfiles) != 1:
|
|
|
|
print 'ERROR: Unexpectedly found %d .dat files (%s). Halting.' % (len(datfiles), datfiles)
|
|
|
|
sys.exit(1)
|
|
|
|
datfile = datfiles[0]
|
|
|
|
print 'Copying %s to %s ...' % (datfile, icu_dat_data_dir)
|
|
|
|
shutil.copy(datfile, icu_dat_data_dir)
|
2013-04-22 11:11:43 -07:00
|
|
|
|
2015-04-09 09:22:25 +01:00
|
|
|
# Generate the ICU4J .jar files
|
|
|
|
os.chdir('%s/data' % icu_working_dir)
|
|
|
|
subprocess.check_call(['make', 'icu4j-data'])
|
|
|
|
|
|
|
|
# Copy the ICU4J .jar files to their ultimate destination.
|
|
|
|
icu_jar_data_dir = '%s/main/shared/data' % icu4j_dir
|
|
|
|
jarfiles = glob.glob('out/icu4j/*.jar')
|
|
|
|
if len(jarfiles) != 2:
|
|
|
|
print 'ERROR: Unexpectedly found %d .jar files (%s). Halting.' % (len(jarfiles), jarfiles)
|
|
|
|
sys.exit(1)
|
|
|
|
for jarfile in jarfiles:
|
|
|
|
print 'Copying %s to %s ...' % (jarfile, icu_jar_data_dir)
|
|
|
|
shutil.copy(jarfile, icu_jar_data_dir)
|
|
|
|
|
2014-05-16 18:04:48 +01:00
|
|
|
# Switch back to the original working cwd.
|
|
|
|
os.chdir(original_working_dir)
|
|
|
|
|
|
|
|
|
|
|
|
def CheckSignature(data_filename):
|
2013-04-22 11:41:57 -07: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])
|
|
|
|
|
2014-05-16 18:04:48 +01:00
|
|
|
|
|
|
|
def BuildBionicToolsAndData(data_filename):
|
|
|
|
new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1)
|
|
|
|
|
2012-07-20 16:52:39 -07:00
|
|
|
print 'Extracting...'
|
|
|
|
os.mkdir('extracted')
|
2012-11-05 08:53:28 -08:00
|
|
|
tar = tarfile.open(data_filename, 'r')
|
2012-07-20 16:52:39 -07:00
|
|
|
tar.extractall('extracted')
|
|
|
|
|
|
|
|
print 'Calling zic(1)...'
|
|
|
|
os.mkdir('data')
|
2014-11-11 14:10:51 -08:00
|
|
|
zic_inputs = [ 'extracted/%s' % x for x in regions ]
|
|
|
|
zic_cmd = ['zic', '-d', 'data' ]
|
|
|
|
zic_cmd.extend(zic_inputs)
|
|
|
|
subprocess.check_call(zic_cmd)
|
2012-07-20 16:52:39 -07:00
|
|
|
|
2012-10-19 14:47:37 -07:00
|
|
|
WriteSetupFile()
|
2012-07-20 16:52:39 -07:00
|
|
|
|
2012-10-19 14:47:37 -07:00
|
|
|
print 'Calling ZoneCompactor to update bionic to %s...' % new_version
|
2012-07-20 16:52:39 -07:00
|
|
|
subprocess.check_call(['javac', '-d', '.',
|
2014-08-06 15:23:11 -07:00
|
|
|
'%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir])
|
2012-10-19 14:47:37 -07:00
|
|
|
subprocess.check_call(['java', 'ZoneCompactor',
|
2012-10-22 14:47:58 -07:00
|
|
|
'setup', 'data', 'extracted/zone.tab',
|
|
|
|
bionic_libc_zoneinfo_dir, new_version])
|
2012-10-19 14:47:37 -07:00
|
|
|
|
|
|
|
|
|
|
|
# Run with no arguments from any directory, with no special setup required.
|
2012-11-05 08:53:28 -08:00
|
|
|
# See http://www.iana.org/time-zones/ for more about the source of this data.
|
2012-10-19 14:47:37 -07:00
|
|
|
def main():
|
|
|
|
print 'Looking for new tzdata...'
|
2013-04-22 11:11:43 -07:00
|
|
|
|
2012-10-19 14:47:37 -07:00
|
|
|
tzdata_filenames = []
|
2013-04-22 11:11:43 -07:00
|
|
|
|
|
|
|
# The FTP server lets you download intermediate releases, and also lets you
|
2013-04-22 13:44:50 -07:00
|
|
|
# download the signatures for verification, so it's your best choice.
|
2013-04-22 11:11:43 -07: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 14:47:37 -07: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
|
2014-05-16 18:04:48 +01:00
|
|
|
SwitchToNewTemporaryDirectory()
|
2013-04-22 11:11:43 -07:00
|
|
|
if use_ftp:
|
2014-05-16 18:04:48 +01:00
|
|
|
FtpRetrieveFileAndSignature(ftp, filename)
|
2013-04-22 11:11:43 -07:00
|
|
|
else:
|
2014-05-16 18:04:48 +01:00
|
|
|
HttpRetrieveFileAndSignature(http, filename)
|
|
|
|
|
|
|
|
CheckSignature(filename)
|
|
|
|
BuildIcuToolsAndData(filename)
|
|
|
|
BuildBionicToolsAndData(filename)
|
|
|
|
print 'Look in %s and %s for new data files' % (bionic_dir, icu_dir)
|
2012-10-19 14:47:37 -07:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
print 'You already have the latest tzdata (%s)!' % current_version
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|