Author: fw Date: 2012-08-06 19:32:58 +0000 (Mon, 06 Aug 2012) New Revision: 19903 Modified: lib/python/debian_support.py Log: lib/python/debian_support.py: replace urllib with urllib2 and set timeout Modified: lib/python/debian_support.py ==================================================================--- lib/python/debian_support.py 2012-08-06 18:50:38 UTC (rev 19902) +++ lib/python/debian_support.py 2012-08-06 19:32:58 UTC (rev 19903) @@ -17,12 +17,15 @@ """This module implements facilities to deal with Debian-specific metadata.""" -import types +import gzip import json import os.path import re import sys import tempfile +import types +import urllib2 +from cStringIO import StringIO try: from hashlib import sha1 @@ -33,6 +36,9 @@ import apt_pkg apt_pkg.init() +# Timeout for downloads. +TIMEOUT = 30 + class ParseError(Exception): """An exception which is used to signal a parse failure. @@ -282,23 +288,9 @@ Returns the lines in the file.""" - # The implementation is rather crude, but it seems that the gzip - # module needs a real file for input. - - import gzip - import tempfile - import urllib - - (handle, fname) = tempfile.mkstemp() - try: - os.close(handle) - (filename, headers) = urllib.urlretrieve(remote, fname) - gfile = gzip.GzipFile(filename) - lines = gfile.readlines() - gfile.close() - finally: - os.unlink(fname) - return lines + data = urllib2.urlopen(remote, timeout=TIMEOUT) + with gzip.GzipFile(fileobj=StringIO(data.read())) as gfile: + return gfile.readlines() def downloadFile(remote, local): """Copies a gzipped remote file to the local system. @@ -330,17 +322,14 @@ patches_to_apply = [] patch_hashes = {} - import urllib index_name = remote + ''.diff/Index'' re_whitespace=re.compile(''\s+'') try: - index_url = urllib.urlopen(index_name) + index_url = urllib2.urlopen(index_name, timeout=TIMEOUT) index_fields = list(PackageFile(index_name, index_url)) except ParseError: - # FIXME: urllib does not raise a proper exception, so we parse - # the error message. if verbose: print "updateFile: could not interpret patch index file" return downloadFile(remote, local)