1*2fd832c6SAndroid Build Coastguard Worker#!/usr/bin/python3 -B 2*2fd832c6SAndroid Build Coastguard Worker 3*2fd832c6SAndroid Build Coastguard Worker# Copyright 2017 The Android Open Source Project 4*2fd832c6SAndroid Build Coastguard Worker# 5*2fd832c6SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*2fd832c6SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*2fd832c6SAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*2fd832c6SAndroid Build Coastguard Worker# 9*2fd832c6SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*2fd832c6SAndroid Build Coastguard Worker# 11*2fd832c6SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*2fd832c6SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*2fd832c6SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*2fd832c6SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*2fd832c6SAndroid Build Coastguard Worker# limitations under the License. 16*2fd832c6SAndroid Build Coastguard Worker 17*2fd832c6SAndroid Build Coastguard Worker"""Downloads the latest IANA time zone files.""" 18*2fd832c6SAndroid Build Coastguard Worker 19*2fd832c6SAndroid Build Coastguard Workerimport argparse 20*2fd832c6SAndroid Build Coastguard Workerimport ftplib 21*2fd832c6SAndroid Build Coastguard Workerimport os 22*2fd832c6SAndroid Build Coastguard Workerimport shutil 23*2fd832c6SAndroid Build Coastguard Workerimport subprocess 24*2fd832c6SAndroid Build Coastguard Workerimport sys 25*2fd832c6SAndroid Build Coastguard Worker 26*2fd832c6SAndroid Build Coastguard Workersys.path.append('%s/external/icu/tools' % os.environ.get('ANDROID_BUILD_TOP')) 27*2fd832c6SAndroid Build Coastguard Workerimport i18nutil 28*2fd832c6SAndroid Build Coastguard Workerimport tzdatautil 29*2fd832c6SAndroid Build Coastguard Worker 30*2fd832c6SAndroid Build Coastguard Worker# Calculate the paths that are referred to by multiple functions. 31*2fd832c6SAndroid Build Coastguard Workerandroid_build_top = i18nutil.GetAndroidRootOrDie() 32*2fd832c6SAndroid Build Coastguard Workeriana_data_dir = os.path.realpath('%s/system/timezone/input_data/iana' % android_build_top) 33*2fd832c6SAndroid Build Coastguard Workeriana_tools_dir = os.path.realpath('%s/system/timezone/input_tools/iana' % android_build_top) 34*2fd832c6SAndroid Build Coastguard Worker 35*2fd832c6SAndroid Build Coastguard Workerdef FtpRetrieveFile(ftp, filename): 36*2fd832c6SAndroid Build Coastguard Worker ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write) 37*2fd832c6SAndroid Build Coastguard Worker 38*2fd832c6SAndroid Build Coastguard Worker 39*2fd832c6SAndroid Build Coastguard Workerdef CheckSignature(data_filename, signature_filename): 40*2fd832c6SAndroid Build Coastguard Worker """Checks the signature of a file.""" 41*2fd832c6SAndroid Build Coastguard Worker print('Verifying signature of %s using %s...' % (data_filename, signature_filename)) 42*2fd832c6SAndroid Build Coastguard Worker try: 43*2fd832c6SAndroid Build Coastguard Worker subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify', 44*2fd832c6SAndroid Build Coastguard Worker signature_filename, data_filename]) 45*2fd832c6SAndroid Build Coastguard Worker except subprocess.CalledProcessError as err: 46*2fd832c6SAndroid Build Coastguard Worker print('Unable to verify signature') 47*2fd832c6SAndroid Build Coastguard Worker print('\n\n******') 48*2fd832c6SAndroid Build Coastguard Worker print('If this fails for you, you probably need to import Paul Eggert''s public key:') 49*2fd832c6SAndroid Build Coastguard Worker print(' gpg --receive-keys ED97E90E62AA7E34') 50*2fd832c6SAndroid Build Coastguard Worker print('******\n\n') 51*2fd832c6SAndroid Build Coastguard Worker raise 52*2fd832c6SAndroid Build Coastguard Worker 53*2fd832c6SAndroid Build Coastguard Worker 54*2fd832c6SAndroid Build Coastguard Workerdef FindRemoteTar(ftp, file_prefix, release_version): 55*2fd832c6SAndroid Build Coastguard Worker iana_tar_filenames = [] 56*2fd832c6SAndroid Build Coastguard Worker 57*2fd832c6SAndroid Build Coastguard Worker for filename in ftp.nlst(): 58*2fd832c6SAndroid Build Coastguard Worker if "/" in filename: 59*2fd832c6SAndroid Build Coastguard Worker print("FTP server returned bogus file name") 60*2fd832c6SAndroid Build Coastguard Worker sys.exit(1) 61*2fd832c6SAndroid Build Coastguard Worker 62*2fd832c6SAndroid Build Coastguard Worker if filename.startswith(file_prefix) and filename.endswith('.tar.gz'): 63*2fd832c6SAndroid Build Coastguard Worker iana_tar_filenames.append(filename) 64*2fd832c6SAndroid Build Coastguard Worker 65*2fd832c6SAndroid Build Coastguard Worker if release_version is not None: 66*2fd832c6SAndroid Build Coastguard Worker if file_prefix.endswith('20'): 67*2fd832c6SAndroid Build Coastguard Worker file_prefix = file_prefix[0:-2] 68*2fd832c6SAndroid Build Coastguard Worker expected_filename = file_prefix + release_version + '.tar.gz' 69*2fd832c6SAndroid Build Coastguard Worker if expected_filename not in iana_tar_filenames: 70*2fd832c6SAndroid Build Coastguard Worker print(f'Expected {expected_filename} not found') 71*2fd832c6SAndroid Build Coastguard Worker sys.exit(1) 72*2fd832c6SAndroid Build Coastguard Worker return expected_filename 73*2fd832c6SAndroid Build Coastguard Worker 74*2fd832c6SAndroid Build Coastguard Worker 75*2fd832c6SAndroid Build Coastguard Worker iana_tar_filenames.sort(reverse=True) 76*2fd832c6SAndroid Build Coastguard Worker 77*2fd832c6SAndroid Build Coastguard Worker if len(iana_tar_filenames) == 0: 78*2fd832c6SAndroid Build Coastguard Worker print('No files found') 79*2fd832c6SAndroid Build Coastguard Worker sys.exit(1) 80*2fd832c6SAndroid Build Coastguard Worker 81*2fd832c6SAndroid Build Coastguard Worker return iana_tar_filenames[0] 82*2fd832c6SAndroid Build Coastguard Worker 83*2fd832c6SAndroid Build Coastguard Worker 84*2fd832c6SAndroid Build Coastguard Workerdef DownloadAndReplaceLocalFiles(file_prefixes, ftp, local_dir, 85*2fd832c6SAndroid Build Coastguard Worker release_version): 86*2fd832c6SAndroid Build Coastguard Worker output_files = [] 87*2fd832c6SAndroid Build Coastguard Worker 88*2fd832c6SAndroid Build Coastguard Worker for file_prefix in file_prefixes: 89*2fd832c6SAndroid Build Coastguard Worker remote_iana_tar_filename = FindRemoteTar(ftp, file_prefix, release_version) 90*2fd832c6SAndroid Build Coastguard Worker local_iana_tar_file = tzdatautil.GetIanaTarFile(local_dir, file_prefix) 91*2fd832c6SAndroid Build Coastguard Worker if local_iana_tar_file: 92*2fd832c6SAndroid Build Coastguard Worker local_iana_tar_filename = os.path.basename(local_iana_tar_file) 93*2fd832c6SAndroid Build Coastguard Worker if remote_iana_tar_filename <= local_iana_tar_filename: 94*2fd832c6SAndroid Build Coastguard Worker print('Latest remote file for %s is called %s and is older or the same as' 95*2fd832c6SAndroid Build Coastguard Worker ' current local file %s' 96*2fd832c6SAndroid Build Coastguard Worker % (local_dir, remote_iana_tar_filename, local_iana_tar_filename)) 97*2fd832c6SAndroid Build Coastguard Worker continue 98*2fd832c6SAndroid Build Coastguard Worker 99*2fd832c6SAndroid Build Coastguard Worker print('Found new %s* file for %s: %s' % (file_prefix, local_dir, remote_iana_tar_filename)) 100*2fd832c6SAndroid Build Coastguard Worker i18nutil.SwitchToNewTemporaryDirectory() 101*2fd832c6SAndroid Build Coastguard Worker 102*2fd832c6SAndroid Build Coastguard Worker print('Downloading file %s...' % remote_iana_tar_filename) 103*2fd832c6SAndroid Build Coastguard Worker FtpRetrieveFile(ftp, remote_iana_tar_filename) 104*2fd832c6SAndroid Build Coastguard Worker 105*2fd832c6SAndroid Build Coastguard Worker signature_filename = '%s.asc' % remote_iana_tar_filename 106*2fd832c6SAndroid Build Coastguard Worker print('Downloading signature %s...' % signature_filename) 107*2fd832c6SAndroid Build Coastguard Worker FtpRetrieveFile(ftp, signature_filename) 108*2fd832c6SAndroid Build Coastguard Worker 109*2fd832c6SAndroid Build Coastguard Worker CheckSignature(remote_iana_tar_filename, signature_filename) 110*2fd832c6SAndroid Build Coastguard Worker 111*2fd832c6SAndroid Build Coastguard Worker new_local_iana_tar_file = '%s/%s' % (local_dir, remote_iana_tar_filename) 112*2fd832c6SAndroid Build Coastguard Worker shutil.copyfile(remote_iana_tar_filename, new_local_iana_tar_file) 113*2fd832c6SAndroid Build Coastguard Worker new_local_signature_file = '%s/%s' % (local_dir, signature_filename) 114*2fd832c6SAndroid Build Coastguard Worker shutil.copyfile(signature_filename, new_local_signature_file) 115*2fd832c6SAndroid Build Coastguard Worker 116*2fd832c6SAndroid Build Coastguard Worker output_files.append(new_local_iana_tar_file) 117*2fd832c6SAndroid Build Coastguard Worker output_files.append(new_local_signature_file) 118*2fd832c6SAndroid Build Coastguard Worker 119*2fd832c6SAndroid Build Coastguard Worker # Delete the existing local IANA tar file, if there is one. 120*2fd832c6SAndroid Build Coastguard Worker if local_iana_tar_file: 121*2fd832c6SAndroid Build Coastguard Worker os.remove(local_iana_tar_file) 122*2fd832c6SAndroid Build Coastguard Worker 123*2fd832c6SAndroid Build Coastguard Worker local_signature_file = '%s.asc' % local_iana_tar_file 124*2fd832c6SAndroid Build Coastguard Worker if os.path.exists(local_signature_file): 125*2fd832c6SAndroid Build Coastguard Worker os.remove(local_signature_file) 126*2fd832c6SAndroid Build Coastguard Worker 127*2fd832c6SAndroid Build Coastguard Worker return output_files 128*2fd832c6SAndroid Build Coastguard Worker 129*2fd832c6SAndroid Build Coastguard Worker# Run from any directory after having run source/envsetup.sh / lunch 130*2fd832c6SAndroid Build Coastguard Worker# See http://www.iana.org/time-zones/ for more about the source of this data. 131*2fd832c6SAndroid Build Coastguard Workerdef main(): 132*2fd832c6SAndroid Build Coastguard Worker parser = argparse.ArgumentParser(description=('Update IANA files from upstream')) 133*2fd832c6SAndroid Build Coastguard Worker parser.add_argument('--tools', help='Download tools files', action='store_true') 134*2fd832c6SAndroid Build Coastguard Worker parser.add_argument('--data', help='Download data files', action='store_true') 135*2fd832c6SAndroid Build Coastguard Worker parser.add_argument('-r', '--release', default=None, 136*2fd832c6SAndroid Build Coastguard Worker dest='release_version', 137*2fd832c6SAndroid Build Coastguard Worker help='Pick a specific release revision instead of ' 138*2fd832c6SAndroid Build Coastguard Worker 'latest. For example --release 2023a ') 139*2fd832c6SAndroid Build Coastguard Worker args = parser.parse_args() 140*2fd832c6SAndroid Build Coastguard Worker if not args.tools and not args.data: 141*2fd832c6SAndroid Build Coastguard Worker parser.error("Nothing to do") 142*2fd832c6SAndroid Build Coastguard Worker sys.exit(1) 143*2fd832c6SAndroid Build Coastguard Worker 144*2fd832c6SAndroid Build Coastguard Worker print('Looking for new IANA files...') 145*2fd832c6SAndroid Build Coastguard Worker 146*2fd832c6SAndroid Build Coastguard Worker ftp = ftplib.FTP('ftp.iana.org') 147*2fd832c6SAndroid Build Coastguard Worker ftp.login() 148*2fd832c6SAndroid Build Coastguard Worker ftp.cwd('tz/releases') 149*2fd832c6SAndroid Build Coastguard Worker 150*2fd832c6SAndroid Build Coastguard Worker output_files = [] 151*2fd832c6SAndroid Build Coastguard Worker if args.tools: 152*2fd832c6SAndroid Build Coastguard Worker # The tools and data files are kept separate to make the updates independent. 153*2fd832c6SAndroid Build Coastguard Worker # This means we duplicate the tzdata20xx file (once in the tools dir, once 154*2fd832c6SAndroid Build Coastguard Worker # in the data dir) but the contents of the data tar appear to be needed for 155*2fd832c6SAndroid Build Coastguard Worker # the zic build. 156*2fd832c6SAndroid Build Coastguard Worker new_files = DownloadAndReplaceLocalFiles(['tzdata20', 'tzcode20'], ftp, 157*2fd832c6SAndroid Build Coastguard Worker iana_tools_dir, 158*2fd832c6SAndroid Build Coastguard Worker args.release_version) 159*2fd832c6SAndroid Build Coastguard Worker output_files += new_files 160*2fd832c6SAndroid Build Coastguard Worker if args.data: 161*2fd832c6SAndroid Build Coastguard Worker new_files = DownloadAndReplaceLocalFiles(['tzdata20'], ftp, iana_data_dir, 162*2fd832c6SAndroid Build Coastguard Worker args.release_version) 163*2fd832c6SAndroid Build Coastguard Worker output_files += new_files 164*2fd832c6SAndroid Build Coastguard Worker 165*2fd832c6SAndroid Build Coastguard Worker if len(output_files) == 0: 166*2fd832c6SAndroid Build Coastguard Worker print('No files updated') 167*2fd832c6SAndroid Build Coastguard Worker else: 168*2fd832c6SAndroid Build Coastguard Worker print('New files:') 169*2fd832c6SAndroid Build Coastguard Worker for output_file in output_files: 170*2fd832c6SAndroid Build Coastguard Worker print(' %s' % output_file) 171*2fd832c6SAndroid Build Coastguard Worker 172*2fd832c6SAndroid Build Coastguard Worker 173*2fd832c6SAndroid Build Coastguard Workerif __name__ == '__main__': 174*2fd832c6SAndroid Build Coastguard Worker main() 175