Home | History | Annotate | Download | only in timezone
      1 #!/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 timezone data."""
     18 
     19 import ftplib
     20 import os
     21 import shutil
     22 import subprocess
     23 import sys
     24 
     25 sys.path.append('%s/external/icu/tools' % os.environ.get('ANDROID_BUILD_TOP'))
     26 import i18nutil
     27 import tzdatautil
     28 
     29 # Calculate the paths that are referred to by multiple functions.
     30 android_build_top = i18nutil.GetAndroidRootOrDie()
     31 iana_data_dir = os.path.realpath('%s/system/timezone/input_data/iana' % android_build_top)
     32 
     33 def FtpRetrieveFile(ftp, filename):
     34   ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
     35 
     36 
     37 def FtpRetrieveFileAndCheckSignature(ftp, data_filename):
     38   """Downloads and repackages the given data from the given FTP server."""
     39   print 'Downloading data (%s)...' % data_filename
     40   FtpRetrieveFile(ftp, data_filename)
     41 
     42   signature_filename = '%s.asc' % data_filename
     43   print 'Downloading signature (%s)...' % signature_filename
     44   FtpRetrieveFile(ftp, signature_filename)
     45 
     46   print 'Verifying signature...'
     47   # If this fails for you, you probably need to import Paul Eggert's public key:
     48   # gpg --recv-keys ED97E90E62AA7E34
     49   subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify',
     50                          signature_filename, data_filename])
     51 
     52 
     53 # Run with no arguments from any directory, with no special setup required.
     54 # See http://www.iana.org/time-zones/ for more about the source of this data.
     55 def main():
     56   print 'Looking for new IANA tzdata...'
     57 
     58   iana_tar_filenames = []
     59 
     60   ftp = ftplib.FTP('ftp.iana.org')
     61   ftp.login()
     62   ftp.cwd('tz/releases')
     63   for filename in ftp.nlst():
     64     if "/" in filename:
     65       print "FTP server returned bogus file name"
     66       sys.exit(1)
     67 
     68     if filename.startswith('tzdata20') and filename.endswith('.tar.gz'):
     69       iana_tar_filenames.append(filename)
     70   iana_tar_filenames.sort(reverse=True)
     71 
     72   if len(iana_tar_filenames) == 0:
     73     print 'No tzdata files found'
     74     sys.exit(1)
     75 
     76   latest_iana_tar_filename = iana_tar_filenames[0]
     77 
     78   local_iana_tar_file = tzdatautil.GetIanaTarFile(iana_data_dir)
     79 
     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 'Available data %s is older or the same as current data %s' % (latest_iana_tar_filename, local_iana_tar_filename)
     84       sys.exit(0)
     85 
     86   print 'Found new tzdata: %s' % latest_iana_tar_filename
     87   i18nutil.SwitchToNewTemporaryDirectory()
     88   FtpRetrieveFileAndCheckSignature(ftp, latest_iana_tar_filename)
     89 
     90   new_local_iana_tar_file = '%s/%s' % (iana_data_dir, latest_iana_tar_filename)
     91   shutil.copyfile(latest_iana_tar_filename, new_local_iana_tar_file)
     92 
     93   # Delete the existing local IANA tar file, if there is one.
     94   if local_iana_tar_file:
     95     os.remove(local_iana_tar_file)
     96 
     97   print 'Look in %s for new IANA data files' % new_local_iana_tar_file
     98   sys.exit(0)
     99 
    100 
    101 if __name__ == '__main__':
    102   main()
    103