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