Keep py3.X compatibility for urllib

urllib/urllib2 is incompatible for python 3
Use modules from six.moves.urllib instead of urllib
and urllib2

Change-Id: Ic9d8ce4e700d19053c67cf667e1d802bd1477108
Partial-Bug:#1280105
This commit is contained in:
Swapnil Kulkarni (coolsvap) 2015-12-24 15:15:08 +05:30
parent 1c3a82d818
commit 7bd4ebdc38
2 changed files with 11 additions and 12 deletions

View File

@ -24,7 +24,9 @@ import logging
import os import os
import shutil import shutil
import sys import sys
import urllib2
from six.moves.urllib import error as urlerr
from six.moves.urllib import request as urlreq
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import glob2 import glob2
@ -52,12 +54,12 @@ def download_listing(dest):
'Typos?action=raw') 'Typos?action=raw')
logger.debug("Downloading latest RETF listing from %s into %s.", logger.debug("Downloading latest RETF listing from %s into %s.",
url, dest) url, dest)
response = urllib2.urlopen(url) response = urlreq.urlopen(url)
data = response.read() data = response.read()
logger.info("Downloading latest RETF listing from %s succeeded.", url) logger.info("Downloading latest RETF listing from %s succeeded.", url)
except urllib2.HTTPError as ex: except urlerr.HTTPError as ex:
raise DownloadRetfListingFailed(six.text_type(ex)) raise DownloadRetfListingFailed(six.text_type(ex))
except urllib2.URLError as ex: except urlerr.URLError as ex:
raise DownloadRetfListingFailed(six.text_type(ex)) raise DownloadRetfListingFailed(six.text_type(ex))
try: try:

View File

@ -36,15 +36,12 @@ import operator
import os import os
import re import re
import shutil import shutil
import six
import subprocess import subprocess
import sys import sys
import time import time
if six.PY3: from six.moves.urllib import error as urlerr
from urllib import request as urllib2 from six.moves.urllib import request as urlreq
else:
import urllib2
from lxml import etree from lxml import etree
from oslo_config import cfg from oslo_config import cfg
@ -307,8 +304,8 @@ def verify_valid_links(doc):
continue continue
try: try:
urllib2.urlopen(url) urlreq.urlopen(url)
except urllib2.HTTPError as e: except urlerr.HTTPError as e:
# Ignore some error codes: # Ignore some error codes:
# 403 (Forbidden) since it often means that the user-agent # 403 (Forbidden) since it often means that the user-agent
# is wrong. # is wrong.
@ -317,7 +314,7 @@ def verify_valid_links(doc):
e_line = node.sourceline e_line = node.sourceline
msg.append("URL %s not reachable at line %d, error %s" % ( msg.append("URL %s not reachable at line %d, error %s" % (
url, e_line, e)) url, e_line, e))
except urllib2.URLError as e: except urlerr.URLError as e:
e_line = node.sourceline e_line = node.sourceline
msg.append("URL %s invalid at line %d, error %s" % ( msg.append("URL %s invalid at line %d, error %s" % (
url, e_line, e)) url, e_line, e))