From 19dbf6bea119b3a202b1430fafd1046134153d0f Mon Sep 17 00:00:00 2001 From: Michael Prokop Date: Tue, 2 Jul 2019 12:29:18 +0200 Subject: [PATCH] TT#61761 Fix urllib.request handling Fixes: | No attribute 'code' on urllib.request._HTTPResponse [attribute-error] as reported by pytype. The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error, and therefore will fail with: | AttributeError: module 'urllib' has no attribute 'request' This used to work only because other components loaded within repoapi did the `import urllib.request` for us. Also the errors were never reported as such, as e.g. 404 errors are throwing an exception (instead of provided as return code as expected by our code so far), which we didn't catch so far. And the arguments provided to logger/ logging were incorrect (number vs string), so fix this and also slightly improve the logging messages while at it. JFTR, `import urllib.request` is py3k-only, which is fine for us though, since repoapi is supporting only py3k nowadays. We should also think about migrating from urllib towards requests everywhere. Change-Id: Ibc965daa94216aaad80758dba0f50a74d8658887 --- repoapi/utils.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/repoapi/utils.py b/repoapi/utils.py index 3384646..1afaec0 100644 --- a/repoapi/utils.py +++ b/repoapi/utils.py @@ -19,7 +19,8 @@ import logging import os import shutil import subprocess -import urllib +import urllib.request +from urllib.error import HTTPError from django.conf import settings logger = logging.getLogger(__name__) @@ -51,14 +52,18 @@ def dlfile(url, path): def openurl(url): req = urllib.request.Request(url) - logger.debug("url:[%s]", url) - response = urllib.request.urlopen(req) - if response.code > 199 and response.code < 300: - logger.debug("OK[%d] url: %s", url, response.code) - return True - else: - logger.error("Error[%d] retrieving %s", url, response.code) - return False + logger.debug("Trying to retrieve url: [%s]", url) + try: + response = urllib.request.urlopen(req) + if 199 < response.getcode() < 300: + logger.debug("OK[%d] URL[%s]", response.getcode(), url) + return True + except urllib.error.HTTPError as e: + logger.error('Error[%d] retrieving URL[%s]', e.getcode(), url) + except Exception: + logger.error("Fatal error retrieving URL[%s]", url) + + return False def jenkins_remove_ppa(repo):