[Piuparts-commits] [piuparts] 02/04: open_packages_url: switch to urllib2 and add error handling
Holger Levsen
holger at alioth.debian.org
Thu Oct 31 12:04:47 UTC 2013
This is an automated email from the git hooks/post-receive script.
holger pushed a commit to branch develop
in repository piuparts.
commit ede6a028b769ceae9ba6e305208e9eb584608e2a
Author: Andreas Beckmann <anbe at debian.org>
Date: Wed Oct 30 19:37:32 2013 +0100
open_packages_url: switch to urllib2 and add error handling
Signed-off-by: Andreas Beckmann <anbe at debian.org>
---
piupartslib/__init__.py | 35 ++++++++++++++++++++++++-----------
1 file changed, 24 insertions(+), 11 deletions(-)
diff --git a/piupartslib/__init__.py b/piupartslib/__init__.py
index e38254a..8130c06 100644
--- a/piupartslib/__init__.py
+++ b/piupartslib/__init__.py
@@ -18,7 +18,7 @@
import bz2
-import urllib
+import urllib2
import cStringIO
@@ -30,16 +30,29 @@ import packagesdb
def open_packages_url(url):
"""Open a Packages.bz2 file pointed to by a URL"""
assert url.endswith(".bz2")
- socket = urllib.urlopen(url)
- decompressor = bz2.BZ2Decompressor()
- bzfile = cStringIO.StringIO()
- while True:
- data = socket.read(1024)
- if not data:
+ url = url[:-len(".bz2")]
+ socket = None
+ for ext in ['.bz2']:
+ try:
+ socket = urllib2.urlopen(url + ext)
+ except urllib2.HTTPError as httperror:
+ pass
+ else:
break
- bzfile.write(decompressor.decompress(data))
- socket.close()
- bzfile.seek(0)
- return bzfile
+ if socket is None:
+ raise httperror
+ if ext == '.bz2':
+ decompressed = cStringIO.StringIO()
+ decompressor = bz2.BZ2Decompressor()
+ while True:
+ data = socket.read(1024)
+ if not data:
+ socket.close()
+ break
+ decompressed.write(decompressor.decompress(data))
+ decompressed.seek(0)
+ else:
+ raise ext
+ return decompressed
# vi:set et ts=4 sw=4 :
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/piuparts/piuparts.git
More information about the Piuparts-commits
mailing list