blob: 37928064d7c700729c24a86d6899f9fc6446d282 [file] [log] [blame]
#!/usr/bin/python -B
# Copyright 2017 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Downloads the latest IANA timezone data."""
import ftplib
import os
import shutil
import subprocess
import sys
sys.path.append('%s/external/icu/tools' % os.environ.get('ANDROID_BUILD_TOP'))
import i18nutil
import tzdatautil
# Calculate the paths that are referred to by multiple functions.
android_build_top = i18nutil.GetAndroidRootOrDie()
iana_data_dir = os.path.realpath('%s/system/timezone/input_data/iana' % android_build_top)
def FtpRetrieveFile(ftp, filename):
ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
def FtpRetrieveFileAndCheckSignature(ftp, data_filename):
"""Downloads and repackages the given data from the given FTP server."""
print 'Downloading data (%s)...' % data_filename
FtpRetrieveFile(ftp, data_filename)
signature_filename = '%s.asc' % data_filename
print 'Downloading signature (%s)...' % signature_filename
FtpRetrieveFile(ftp, signature_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])
# Run with no arguments from any directory, with no special setup required.
# See http://www.iana.org/time-zones/ for more about the source of this data.
def main():
print 'Looking for new IANA tzdata...'
iana_tar_filenames = []
ftp = ftplib.FTP('ftp.iana.org')
ftp.login()
ftp.cwd('tz/releases')
for filename in ftp.nlst():
if "/" in filename:
print "FTP server returned bogus file name"
sys.exit(1)
if filename.startswith('tzdata20') and filename.endswith('.tar.gz'):
iana_tar_filenames.append(filename)
iana_tar_filenames.sort(reverse=True)
if len(iana_tar_filenames) == 0:
print 'No tzdata files found'
sys.exit(1)
latest_iana_tar_filename = iana_tar_filenames[0]
local_iana_tar_file = tzdatautil.GetIanaTarFile(iana_data_dir)
if local_iana_tar_file:
local_iana_tar_filename = os.path.basename(local_iana_tar_file)
if latest_iana_tar_filename <= local_iana_tar_filename:
print 'Available data %s is older or the same as current data %s' % (latest_iana_tar_filename, local_iana_tar_filename)
sys.exit(0)
print 'Found new tzdata: %s' % latest_iana_tar_filename
i18nutil.SwitchToNewTemporaryDirectory()
FtpRetrieveFileAndCheckSignature(ftp, latest_iana_tar_filename)
new_local_iana_tar_file = '%s/%s' % (iana_data_dir, latest_iana_tar_filename)
shutil.copyfile(latest_iana_tar_filename, new_local_iana_tar_file)
# Delete the existing local IANA tar file, if there is one.
if local_iana_tar_file:
os.remove(local_iana_tar_file)
print 'Look in %s for new IANA data files' % new_local_iana_tar_file
sys.exit(0)
if __name__ == '__main__':
main()