Home | History | Annotate | Download | only in zoneinfo
      1 #!/usr/bin/python
      2 
      3 """Updates the tzdata file."""
      4 
      5 import ftplib
      6 import httplib
      7 import os
      8 import re
      9 import subprocess
     10 import sys
     11 import tarfile
     12 import tempfile
     13 
     14 # Find the bionic directory, searching upward from this script.
     15 bionic_libc_tools_zoneinfo_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
     16 bionic_libc_tools_dir = os.path.dirname(bionic_libc_tools_zoneinfo_dir)
     17 bionic_libc_dir = os.path.dirname(bionic_libc_tools_dir)
     18 bionic_dir = os.path.dirname(bionic_libc_dir)
     19 bionic_libc_zoneinfo_dir = '%s/libc/zoneinfo' % bionic_dir
     20 
     21 if not os.path.isdir(bionic_libc_tools_zoneinfo_dir):
     22   print "Couldn't find bionic/libc/tools/zoneinfo!"
     23   sys.exit(1)
     24 if not os.path.isdir(bionic_libc_zoneinfo_dir):
     25   print "Couldn't find bionic/libc/zoneinfo!"
     26   sys.exit(1)
     27 
     28 print 'Found bionic in %s...' % bionic_dir
     29 
     30 
     31 regions = ['africa', 'antarctica', 'asia', 'australasia', 'backward',
     32            'etcetera', 'europe', 'northamerica', 'southamerica']
     33 
     34 
     35 def GetCurrentTzDataVersion():
     36   return open('%s/tzdata' % bionic_libc_zoneinfo_dir).read().split('\x00', 1)[0]
     37 
     38 
     39 def WriteSetupFile():
     40   """Writes the list of zones that ZoneCompactor should process."""
     41   links = []
     42   zones = []
     43   for region in regions:
     44     for line in open('extracted/%s' % region):
     45       fields = line.split()
     46       if fields:
     47         if fields[0] == 'Link':
     48           links.append('%s %s %s\n' % (fields[0], fields[1], fields[2]))
     49           zones.append(fields[2])
     50         elif fields[0] == 'Zone':
     51           zones.append(fields[1])
     52   zones.sort()
     53 
     54   setup = open('setup', 'w')
     55   for link in links:
     56     setup.write(link)
     57   for zone in zones:
     58     setup.write('%s\n' % zone)
     59   setup.close()
     60 
     61 
     62 def SwitchToNewTemporaryDirectory():
     63   tmp_dir = tempfile.mkdtemp('-tzdata')
     64   os.chdir(tmp_dir)
     65   print 'Created temporary directory "%s"...' % tmp_dir
     66 
     67 
     68 def FtpRetrieve(ftp, filename):
     69   ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
     70 
     71 
     72 def FtpUpgrade(ftp, data_filename):
     73   """Downloads and repackages the given data from the given FTP server."""
     74   SwitchToNewTemporaryDirectory()
     75 
     76   print 'Downloading data...'
     77   FtpRetrieve(ftp, data_filename)
     78 
     79   print 'Downloading signature...'
     80   signature_filename = '%s.asc' % data_filename
     81   FtpRetrieve(ftp, signature_filename)
     82 
     83   ExtractAndCompile(data_filename)
     84 
     85 
     86 def HttpRetrieve(http, path, output_filename):
     87   http.request("GET", path)
     88   f = open(output_filename, 'wb')
     89   f.write(http.getresponse().read())
     90   f.close()
     91 
     92 
     93 def HttpUpgrade(http, data_filename):
     94   """Downloads and repackages the given data from the given HTTP server."""
     95   SwitchToNewTemporaryDirectory()
     96 
     97   path = "/time-zones/repository/releases/%s" % data_filename
     98 
     99   print 'Downloading data...'
    100   HttpRetrieve(http, path, data_filename)
    101 
    102   print 'Downloading signature...'
    103   signature_filename = '%s.asc' % data_filename
    104   HttpRetrieve(http, "%s.asc" % path, signature_filename)
    105 
    106   ExtractAndCompile(data_filename)
    107 
    108 
    109 def ExtractAndCompile(data_filename):
    110   new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1)
    111 
    112   signature_filename = '%s.asc' % data_filename
    113   print 'Verifying signature...'
    114   # If this fails for you, you probably need to import Paul Eggert's public key:
    115   # gpg --recv-keys ED97E90E62AA7E34
    116   subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify',
    117                          signature_filename, data_filename])
    118 
    119   print 'Extracting...'
    120   os.mkdir('extracted')
    121   tar = tarfile.open(data_filename, 'r')
    122   tar.extractall('extracted')
    123 
    124   print 'Calling zic(1)...'
    125   os.mkdir('data')
    126   for region in regions:
    127     if region != 'backward':
    128       subprocess.check_call(['zic', '-d', 'data', 'extracted/%s' % region])
    129 
    130   WriteSetupFile()
    131 
    132   print 'Calling ZoneCompactor to update bionic to %s...' % new_version
    133   libcore_src_dir = '%s/../libcore/luni/src/main/java/' % bionic_dir
    134   subprocess.check_call(['javac', '-d', '.',
    135                          '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir,
    136                          '%s/libcore/util/ZoneInfo.java' % libcore_src_dir,
    137                          '%s/libcore/io/BufferIterator.java' % libcore_src_dir])
    138   subprocess.check_call(['java', 'ZoneCompactor',
    139                          'setup', 'data', 'extracted/zone.tab',
    140                          bionic_libc_zoneinfo_dir, new_version])
    141 
    142 
    143 # Run with no arguments from any directory, with no special setup required.
    144 # See http://www.iana.org/time-zones/ for more about the source of this data.
    145 def main():
    146   print 'Looking for new tzdata...'
    147 
    148   tzdata_filenames = []
    149 
    150   # The FTP server lets you download intermediate releases, and also lets you
    151   # download the signatures for verification, so it's your best choice.
    152   use_ftp = True
    153 
    154   if use_ftp:
    155     ftp = ftplib.FTP('ftp.iana.org')
    156     ftp.login()
    157     ftp.cwd('tz/releases')
    158     for filename in ftp.nlst():
    159       if filename.startswith('tzdata20') and filename.endswith('.tar.gz'):
    160         tzdata_filenames.append(filename)
    161     tzdata_filenames.sort()
    162   else:
    163     http = httplib.HTTPConnection('www.iana.org')
    164     http.request("GET", "/time-zones")
    165     index_lines = http.getresponse().read().split('\n')
    166     for line in index_lines:
    167       m = re.compile('.*href="/time-zones/repository/releases/(tzdata20\d\d\c\.tar\.gz)".*').match(line)
    168       if m:
    169         tzdata_filenames.append(m.group(1))
    170 
    171   # If you're several releases behind, we'll walk you through the upgrades
    172   # one by one.
    173   current_version = GetCurrentTzDataVersion()
    174   current_filename = '%s.tar.gz' % current_version
    175   for filename in tzdata_filenames:
    176     if filename > current_filename:
    177       print 'Found new tzdata: %s' % filename
    178       if use_ftp:
    179         FtpUpgrade(ftp, filename)
    180       else:
    181         HttpUpgrade(http, filename)
    182       sys.exit(0)
    183 
    184   print 'You already have the latest tzdata (%s)!' % current_version
    185   sys.exit(0)
    186 
    187 
    188 if __name__ == '__main__':
    189   main()
    190