1#!/usr/bin/python3 -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 FindRemoteTar(ftp, file_prefix, release_version):
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  if release_version is not None:
66    if file_prefix.endswith('20'):
67      file_prefix = file_prefix[0:-2]
68    expected_filename = file_prefix + release_version + '.tar.gz'
69    if expected_filename not in iana_tar_filenames:
70      print(f'Expected {expected_filename} not found')
71      sys.exit(1)
72    return expected_filename
73
74
75  iana_tar_filenames.sort(reverse=True)
76
77  if len(iana_tar_filenames) == 0:
78    print('No files found')
79    sys.exit(1)
80
81  return iana_tar_filenames[0]
82
83
84def DownloadAndReplaceLocalFiles(file_prefixes, ftp, local_dir,
85                                 release_version):
86  output_files = []
87
88  for file_prefix in file_prefixes:
89    remote_iana_tar_filename = FindRemoteTar(ftp, file_prefix, release_version)
90    local_iana_tar_file = tzdatautil.GetIanaTarFile(local_dir, file_prefix)
91    if local_iana_tar_file:
92      local_iana_tar_filename = os.path.basename(local_iana_tar_file)
93      if remote_iana_tar_filename <= local_iana_tar_filename:
94        print('Latest remote file for %s is called %s and is older or the same as'
95              ' current local file %s'
96              % (local_dir, remote_iana_tar_filename, local_iana_tar_filename))
97        continue
98
99    print('Found new %s* file for %s: %s' % (file_prefix, local_dir, remote_iana_tar_filename))
100    i18nutil.SwitchToNewTemporaryDirectory()
101
102    print('Downloading file %s...' % remote_iana_tar_filename)
103    FtpRetrieveFile(ftp, remote_iana_tar_filename)
104
105    signature_filename = '%s.asc' % remote_iana_tar_filename
106    print('Downloading signature %s...' % signature_filename)
107    FtpRetrieveFile(ftp, signature_filename)
108
109    CheckSignature(remote_iana_tar_filename, signature_filename)
110
111    new_local_iana_tar_file = '%s/%s' % (local_dir, remote_iana_tar_filename)
112    shutil.copyfile(remote_iana_tar_filename, new_local_iana_tar_file)
113    new_local_signature_file = '%s/%s' % (local_dir, signature_filename)
114    shutil.copyfile(signature_filename, new_local_signature_file)
115
116    output_files.append(new_local_iana_tar_file)
117    output_files.append(new_local_signature_file)
118
119    # Delete the existing local IANA tar file, if there is one.
120    if local_iana_tar_file:
121      os.remove(local_iana_tar_file)
122
123    local_signature_file = '%s.asc' % local_iana_tar_file
124    if os.path.exists(local_signature_file):
125      os.remove(local_signature_file)
126
127  return output_files
128
129# Run from any directory after having run source/envsetup.sh / lunch
130# See http://www.iana.org/time-zones/ for more about the source of this data.
131def main():
132  parser = argparse.ArgumentParser(description=('Update IANA files from upstream'))
133  parser.add_argument('--tools', help='Download tools files', action='store_true')
134  parser.add_argument('--data', help='Download data files', action='store_true')
135  parser.add_argument('-r', '--release', default=None,
136                      dest='release_version',
137                      help='Pick a specific release revision instead of '
138                      'latest. For example --release 2023a ')
139  args = parser.parse_args()
140  if not args.tools and not args.data:
141    parser.error("Nothing to do")
142    sys.exit(1)
143
144  print('Looking for new IANA files...')
145
146  ftp = ftplib.FTP('ftp.iana.org')
147  ftp.login()
148  ftp.cwd('tz/releases')
149
150  output_files = []
151  if args.tools:
152    # The tools and data files are kept separate to make the updates independent.
153    # This means we duplicate the tzdata20xx file (once in the tools dir, once
154    # in the data dir) but the contents of the data tar appear to be needed for
155    # the zic build.
156    new_files = DownloadAndReplaceLocalFiles(['tzdata20', 'tzcode20'], ftp,
157                                             iana_tools_dir,
158                                             args.release_version)
159    output_files += new_files
160  if args.data:
161    new_files = DownloadAndReplaceLocalFiles(['tzdata20'], ftp, iana_data_dir,
162                                             args.release_version)
163    output_files += new_files
164
165  if len(output_files) == 0:
166    print('No files updated')
167  else:
168    print('New files:')
169    for output_file in output_files:
170      print('  %s' % output_file)
171
172
173if __name__ == '__main__':
174  main()
175