diff options
author | Florian Weimer <fw@deneb.enyo.de> | 2012-08-06 19:32:58 +0000 |
---|---|---|
committer | Florian Weimer <fw@deneb.enyo.de> | 2012-08-06 19:32:58 +0000 |
commit | d2af65cd27972b830130eeb2afb0c907234bbd14 (patch) | |
tree | f9887b5022040071b907dc679f043b039a55f45c /lib | |
parent | e4daf18453b88ca0a14b24e40875f103d3f26c7f (diff) |
lib/python/debian_support.py: replace urllib with urllib2 and set timeout
git-svn-id: svn+ssh://svn.debian.org/svn/secure-testing@19903 e39458fd-73e7-0310-bf30-c45bca0a0e42
Diffstat (limited to 'lib')
-rw-r--r-- | lib/python/debian_support.py | 33 |
1 files changed, 11 insertions, 22 deletions
diff --git a/lib/python/debian_support.py b/lib/python/debian_support.py index 41dcb4f397..7979a6bdca 100644 --- a/lib/python/debian_support.py +++ b/lib/python/debian_support.py @@ -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 @@ except ImportError: 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 @@ def downloadGunzipLines(remote): 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 @@ def updateFile(remote, local, verbose=None): 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) |