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