Rename the tzdata update tool, and add HTTP support.

The FTP server is currently unavailable, but the HTTP server is working
fine.

Change-Id: If7f2f396e89aca022a60af531f3604523b7bf95c
This commit is contained in:
Elliott Hughes 2013-04-22 11:11:43 -07:00
parent 8c181aa8fe
commit f8dff7d449

View File

@ -3,6 +3,7 @@
"""Updates the tzdata file.""" """Updates the tzdata file."""
import ftplib import ftplib
import httplib
import os import os
import re import re
import subprocess import subprocess
@ -58,26 +59,26 @@ def WriteSetupFile():
setup.close() setup.close()
def Retrieve(ftp, filename): def SwitchToNewTemporaryDirectory():
ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
def UpgradeTo(ftp, data_filename):
"""Downloads and repackages the given data from the given FTP server."""
new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1)
# Switch to a temporary directory.
tmp_dir = tempfile.mkdtemp('-tzdata') tmp_dir = tempfile.mkdtemp('-tzdata')
os.chdir(tmp_dir) os.chdir(tmp_dir)
print 'Created temporary directory "%s"...' % tmp_dir print 'Created temporary directory "%s"...' % tmp_dir
def FtpRetrieve(ftp, filename):
ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
def FtpUpgrade(ftp, data_filename):
"""Downloads and repackages the given data from the given FTP server."""
SwitchToNewTemporaryDirectory()
print 'Downloading data...' print 'Downloading data...'
Retrieve(ftp, data_filename) FtpRetrieve(ftp, data_filename)
print 'Downloading signature...' print 'Downloading signature...'
signature_filename = '%s.asc' % data_filename signature_filename = '%s.asc' % data_filename
Retrieve(ftp, signature_filename) FtpRetrieve(ftp, signature_filename)
print 'Verifying signature...' print 'Verifying signature...'
# If this fails for you, you probably need to import Paul Eggert's public key: # If this fails for you, you probably need to import Paul Eggert's public key:
@ -85,6 +86,25 @@ def UpgradeTo(ftp, data_filename):
subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify', subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify',
signature_filename, data_filename]) signature_filename, data_filename])
ExtractAndCompile(data_filename)
def HttpUpgrade(http, data_filename):
"""Downloads and repackages the given data from the given HTTP server."""
SwitchToNewTemporaryDirectory()
print 'Downloading data...'
http.request("GET", "/time-zones/repository/releases/%s" % data_filename)
f = open(data_filename, 'wb')
f.write(http.getresponse().read())
f.close()
ExtractAndCompile(data_filename)
def ExtractAndCompile(data_filename):
new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1)
print 'Extracting...' print 'Extracting...'
os.mkdir('extracted') os.mkdir('extracted')
tar = tarfile.open(data_filename, 'r') tar = tarfile.open(data_filename, 'r')
@ -113,14 +133,30 @@ def UpgradeTo(ftp, data_filename):
# See http://www.iana.org/time-zones/ for more about the source of this data. # See http://www.iana.org/time-zones/ for more about the source of this data.
def main(): def main():
print 'Looking for new tzdata...' print 'Looking for new tzdata...'
ftp = ftplib.FTP('ftp.iana.org')
ftp.login()
ftp.cwd('tz/releases')
tzdata_filenames = [] tzdata_filenames = []
for filename in ftp.nlst():
if filename.startswith('tzdata20') and filename.endswith('.tar.gz'): # The FTP server lets you download intermediate releases, and also lets you
tzdata_filenames.append(filename) # download the signatures for verification, so it's your best choice. It's
tzdata_filenames.sort() # also less reliable than the HTTP server, so we support that too as a backup.
use_ftp = True
if use_ftp:
ftp = ftplib.FTP('ftp.iana.org')
ftp.login()
ftp.cwd('tz/releases')
for filename in ftp.nlst():
if filename.startswith('tzdata20') and filename.endswith('.tar.gz'):
tzdata_filenames.append(filename)
tzdata_filenames.sort()
else:
http = httplib.HTTPConnection('www.iana.org')
http.request("GET", "/time-zones")
index_lines = http.getresponse().read().split('\n')
for line in index_lines:
m = re.compile('.*href="/time-zones/repository/releases/(tzdata20\d\d\c\.tar\.gz)".*').match(line)
if m:
tzdata_filenames.append(m.group(1))
# If you're several releases behind, we'll walk you through the upgrades # If you're several releases behind, we'll walk you through the upgrades
# one by one. # one by one.
@ -129,7 +165,10 @@ def main():
for filename in tzdata_filenames: for filename in tzdata_filenames:
if filename > current_filename: if filename > current_filename:
print 'Found new tzdata: %s' % filename print 'Found new tzdata: %s' % filename
UpgradeTo(ftp, filename) if use_ftp:
FtpUpgrade(ftp, filename)
else:
HttpUpgrade(http, filename)
sys.exit(0) sys.exit(0)
print 'You already have the latest tzdata (%s)!' % current_version print 'You already have the latest tzdata (%s)!' % current_version