Fix error reporting for fetch_key (#2662)

Since fetch_url already take care of the exception, the try/except
clause is no longer working, so replace it with proper status
checking, thus permitting to remove urlib2 from the import list.
This commit is contained in:
Michael Scherer 2016-04-20 00:03:14 +02:00 committed by Matt Clay
parent 404f07af8e
commit 69f2b3d727

View file

@ -62,7 +62,6 @@ EXAMPLES = '''
''' '''
import re import re
import os.path import os.path
import urllib2
import tempfile import tempfile
def is_pubkey(string): def is_pubkey(string):
@ -115,18 +114,18 @@ class RpmKey:
def fetch_key(self, url): def fetch_key(self, url):
"""Downloads a key from url, returns a valid path to a gpg key""" """Downloads a key from url, returns a valid path to a gpg key"""
try: rsp, info = fetch_url(self.module, url)
rsp, info = fetch_url(self.module, url) if info['status'] != 200:
key = rsp.read() self.module.fail_json(msg="failed to fetch key at %s , error was: %s" % (url, info['msg']))
if not is_pubkey(key):
self.module.fail_json(msg="Not a public key: %s" % url) key = rsp.read()
tmpfd, tmpname = tempfile.mkstemp() if not is_pubkey(key):
tmpfile = os.fdopen(tmpfd, "w+b") self.module.fail_json(msg="Not a public key: %s" % url)
tmpfile.write(key) tmpfd, tmpname = tempfile.mkstemp()
tmpfile.close() tmpfile = os.fdopen(tmpfd, "w+b")
return tmpname tmpfile.write(key)
except urllib2.URLError, e: tmpfile.close()
self.module.fail_json(msg=str(e)) return tmpname
def normalize_keyid(self, keyid): def normalize_keyid(self, keyid):
"""Ensure a keyid doesn't have a leading 0x, has leading or trailing whitespace, and make sure is lowercase""" """Ensure a keyid doesn't have a leading 0x, has leading or trailing whitespace, and make sure is lowercase"""