download-iana-data.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/python -B
  2. # Copyright 2017 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Downloads the latest IANA timezone data."""
  16. from __future__ import print_function
  17. import ftplib
  18. import os
  19. import shutil
  20. import subprocess
  21. import sys
  22. sys.path.append('%s/external/icu/tools' % os.environ.get('ANDROID_BUILD_TOP'))
  23. import i18nutil
  24. import tzdatautil
  25. # Calculate the paths that are referred to by multiple functions.
  26. android_build_top = i18nutil.GetAndroidRootOrDie()
  27. iana_data_dir = os.path.realpath('%s/system/timezone/input_data/iana' % android_build_top)
  28. def FtpRetrieveFile(ftp, filename):
  29. ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
  30. def CheckSignature(data_filename, signature_filename):
  31. """Checks the signature of a file."""
  32. print('Verifying signature...')
  33. try:
  34. subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify',
  35. signature_filename, data_filename])
  36. except subprocess.CalledProcessError as err:
  37. print('Unable to verify signature')
  38. print('\n\n******')
  39. print('If this fails for you, you probably need to import Paul Eggert''s public key:')
  40. print(' gpg --receive-keys ED97E90E62AA7E34')
  41. print('******\n\n')
  42. raise
  43. # Run with no arguments from any directory, with no special setup required.
  44. # See http://www.iana.org/time-zones/ for more about the source of this data.
  45. def main():
  46. print('Looking for new IANA tzdata...')
  47. tar_prefix = 'tzdata20'
  48. # Use the input tzdata version name if it exists
  49. if len(sys.argv) > 1:
  50. tar_prefix = 'tzdata' + sys.argv[1]
  51. iana_tar_filenames = []
  52. ftp = ftplib.FTP('ftp.iana.org')
  53. ftp.login()
  54. ftp.cwd('tz/releases')
  55. for filename in ftp.nlst():
  56. if "/" in filename:
  57. print("FTP server returned bogus file name")
  58. sys.exit(1)
  59. if filename.startswith(tar_prefix) and filename.endswith('.tar.gz'):
  60. iana_tar_filenames.append(filename)
  61. iana_tar_filenames.sort(reverse=True)
  62. if len(iana_tar_filenames) == 0:
  63. print('No tzdata files found')
  64. sys.exit(1)
  65. latest_iana_tar_filename = iana_tar_filenames[0]
  66. local_iana_tar_file = tzdatautil.GetIanaTarFile(iana_data_dir, 'data')
  67. if local_iana_tar_file:
  68. local_iana_tar_filename = os.path.basename(local_iana_tar_file)
  69. if latest_iana_tar_filename <= local_iana_tar_filename:
  70. print('Available data %s is older or the same as current data %s' % (latest_iana_tar_filename, local_iana_tar_filename))
  71. sys.exit(0)
  72. print('Found new tzdata: %s' % latest_iana_tar_filename)
  73. i18nutil.SwitchToNewTemporaryDirectory()
  74. print('Downloading data (%s)...' % latest_iana_tar_filename)
  75. FtpRetrieveFile(ftp, latest_iana_tar_filename)
  76. signature_filename = '%s.asc' % latest_iana_tar_filename
  77. print('Downloading signature (%s)...' % signature_filename)
  78. FtpRetrieveFile(ftp, signature_filename)
  79. CheckSignature(latest_iana_tar_filename, signature_filename)
  80. new_local_iana_tar_file = '%s/%s' % (iana_data_dir, latest_iana_tar_filename)
  81. shutil.copyfile(latest_iana_tar_filename, new_local_iana_tar_file)
  82. new_local_signature_file = '%s/%s' % (iana_data_dir, signature_filename)
  83. shutil.copyfile(signature_filename, new_local_signature_file)
  84. # Delete the existing local IANA tar file, if there is one.
  85. if local_iana_tar_file:
  86. os.remove(local_iana_tar_file)
  87. local_signature_file = '%s.asc' % local_iana_tar_file
  88. if os.path.exists(local_signature_file):
  89. os.remove(local_signature_file)
  90. print('Look in %s for new IANA data files' % new_local_iana_tar_file)
  91. sys.exit(0)
  92. if __name__ == '__main__':
  93. main()