[elbe-devel] [PATCH 1/5] Python3: change the urllib2 imports
Torben Hohn
torben.hohn at linutronix.de
Fri Dec 6 17:37:50 CET 2019
On Fri, Dec 06, 2019 at 02:08:45PM +0100, Christian Teklenborg wrote:
> Add the necessary urllib imports for Python3 compatibility.
> Also change the module for BadStatusLine to make it Python3 compatible.
>
> Signed-off-by: Christian Teklenborg <chris at linutronix.de>
> ---
> elbepack/commands/control.py | 8 ++++++--
> elbepack/commands/prjrepo.py | 9 +++++++--
> elbepack/elbexml.py | 24 +++++++++++++-----------
> elbepack/pbuilder.py | 8 +++++---
> elbepack/soapclient.py | 10 +++++++---
> elbepack/xmlpreprocess.py | 10 +++++++---
> 6 files changed, 45 insertions(+), 24 deletions(-)
>
> diff --git a/elbepack/commands/control.py b/elbepack/commands/control.py
> index cb539b70..4df4c4ef 100644
> --- a/elbepack/commands/control.py
> +++ b/elbepack/commands/control.py
> @@ -11,8 +11,6 @@ import socket
> import sys
>
> from optparse import (OptionParser, OptionGroup)
> -from urllib2 import URLError
> -from httplib import BadStatusLine
please dont move the block.
leave it where it was.
i am also getting pycodestyle error:
elbepack/commands/control.py|24| [W0611(unused-import), ] Unused HTTPException imported from http.client
>
> from suds import WebFault
>
> @@ -21,6 +19,12 @@ from elbepack.version import elbe_version
> from elbepack.config import cfg
> from elbepack.elbexml import ValidationMode
>
> +try:
> + from urllib.error import URLError
> + from http.client import HTTPException, BadStatusLine
> +except ImportError:
> + from urllib2 import URLError
> + from httplib import HTTPException, BadStatusLine
and here it yields
elbepack/commands/control.py|29 col 1| E302 expected 2 blank lines, found 1
>
> def run_command(argv):
>
> diff --git a/elbepack/commands/prjrepo.py b/elbepack/commands/prjrepo.py
> index 51e2b845..5877b64f 100644
> --- a/elbepack/commands/prjrepo.py
> +++ b/elbepack/commands/prjrepo.py
> @@ -11,8 +11,7 @@ import socket
> import sys
>
> from optparse import (OptionParser, OptionGroup)
> -from urllib2 import URLError
> -from httplib import BadStatusLine
> +
dont move it. and dont add 2 nelines here.
>
> from suds import WebFault
>
> @@ -20,6 +19,12 @@ from elbepack.soapclient import RepoAction, ElbeSoapClient
> from elbepack.version import elbe_version
> from elbepack.config import cfg
>
> +try:
> + from urllib.error import URLError
> + from http.client import BadStatusLine
> +except ImportError:
> + from urllib2 import URLError
> + from httplib import BadStatusline
lbepack/commands/prjrepo.py|29 col 1| E302 expected 2 blank lines, found 1
>
> def run_command(argv):
>
> diff --git a/elbepack/elbexml.py b/elbepack/elbexml.py
> index e281bbd1..5994e87b 100644
> --- a/elbepack/elbexml.py
> +++ b/elbepack/elbexml.py
> @@ -10,14 +10,16 @@
>
> import os
> import re
> -import urllib2
> -
> from elbepack.treeutils import etree
> from elbepack.validate import validate_xml
> from elbepack.xmldefaults import ElbeDefaults
>
> from elbepack.version import elbe_version, is_devel
> -
> +try:
> + from urllib.request import urlopen, install_opener, build_opener, HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler
line too long.
> + from urllib.error import URLError
> +except ImportError:
> + from urllib2 import urlopen, install_opener, build_opener, URLError
the fallback does not include HTTPPasswordMgrWithDefaultRealm,
HTTPBasicAuthHandler
>
> class ValidationError(Exception):
> def __init__(self, validation):
> @@ -178,11 +180,11 @@ class ElbeXML(object):
>
> def validate_repo(self, r):
> try:
> - fp = urllib2.urlopen(r["url"] + "InRelease", None, 10)
> - except urllib2.URLError:
> + fp = urlopen(r["url"] + "InRelease", None, 10)
> + except URLError:
> try:
> - fp = urllib2.urlopen(r["url"] + "Release", None, 10)
> - except urllib2.URLError:
> + fp = urlopen(r["url"] + "Release", None, 10)
> + except URLError:
> return False
>
> ret = False
> @@ -269,10 +271,10 @@ class ElbeXML(object):
> os.environ["https_proxy"] = ""
> os.environ["no_proxy"] = ""
>
> - passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
> - authhandler = urllib2.HTTPBasicAuthHandler(passman)
> - opener = urllib2.build_opener(authhandler)
> - urllib2.install_opener(opener)
> + passman = HTTPPasswordMgrWithDefaultRealm()
> + authhandler = HTTPBasicAuthHandler(passman)
> + opener = build_opener(authhandler)
> + install_opener(opener)
>
> for r in repos:
> if '@' in r["url"]:
> diff --git a/elbepack/pbuilder.py b/elbepack/pbuilder.py
> index 32da14ce..43b7b423 100644
> --- a/elbepack/pbuilder.py
> +++ b/elbepack/pbuilder.py
> @@ -7,8 +7,10 @@
> # SPDX-License-Identifier: GPL-3.0-or-later
>
> import os
> -import urllib2
> -
> +try:
> + from urllib.request import urlopen
> +except ImportError:
> + from urllib2 import urlopen
>
> def pbuilder_write_config(builddir, xml):
> distname = xml.prj.text('suite')
> @@ -95,7 +97,7 @@ def mirror_script_add_key_text(mirror, key_text):
>
> def mirror_script_add_key_url(mirror, key_url):
> key_url = key_url.replace("LOCALMACHINE", "10.0.2.2")
> - key_conn = urllib2.urlopen(key_url, None, 10)
> + key_conn = urlopen(key_url, None, 10)
> key_text = key_conn.read()
> key_conn.close()
>
> diff --git a/elbepack/soapclient.py b/elbepack/soapclient.py
> index fa562762..51cd474f 100644
> --- a/elbepack/soapclient.py
> +++ b/elbepack/soapclient.py
> @@ -16,8 +16,7 @@ import os
> import fnmatch
>
> from datetime import datetime
> -from urllib2 import URLError
> -from httplib import BadStatusLine
> +
dont move it. and dont add 2 nelines here.
>
> import deb822 # package for dealing with Debian related data
>
> @@ -27,7 +26,12 @@ from suds import WebFault
> from elbepack.filesystem import Filesystem
> from elbepack.elbexml import ElbeXML, ValidationMode
> from elbepack.version import elbe_version, elbe_initvm_packagelist
> -
one empty line. not 2 and not 0
> +try:
> + from urllib.error import URLError
> + from http.client import BadStatusLine
> +except ImportError:
> + from urllib2 import URLError
> + from httplib import BadStatusLine
>
> def set_suds_debug(debug):
> import logging
> diff --git a/elbepack/xmlpreprocess.py b/elbepack/xmlpreprocess.py
> index 598ced45..e310048e 100644
> --- a/elbepack/xmlpreprocess.py
> +++ b/elbepack/xmlpreprocess.py
> @@ -8,7 +8,6 @@
> from __future__ import print_function
>
> import sys
> -import urllib2
dont move it.
>
> from tempfile import NamedTemporaryFile
> from optparse import OptionGroup
> @@ -22,6 +21,11 @@ from elbepack.directories import elbe_exe
> from elbepack.shellhelper import command_out_stderr, CommandError
> from elbepack.isooptions import iso_option_valid
> from elbepack.validate import error_log_to_strings
> +try:
> + from urllib.request import urlopen
> + from urllib.error import HTTPError
> +except ImportError:
> + from urllib2 import urlopen, HTTPError
>
> # list of sections that are allowed to exists multiple times before
> # preprocess and that childrens are merge into one section during preprocess
> @@ -39,10 +43,10 @@ def preprocess_pgp_key(xml):
> print("[WARN] <key>%s</key> is deprecated. You should use raw-key instead." % key.text)
> try:
> keyurl = key.text.strip().replace('LOCALMACHINE', 'localhost')
> - myKey = urllib2.urlopen(keyurl).read()
> + myKey = urlopen(keyurl).read()
> key.tag = "raw-key"
> key.text = "\n%s\n" % myKey
> - except urllib2.HTTPError as E:
elbepack/xmlpreprocess.py|49| [W0612(unused-variable), preprocess_pgp_key] Unused variable 'E'
> + except HTTPError as E:
> raise XMLPreprocessError("Invalid PGP Key URL in <key> tag: %s" % keyurl)
>
> def preprocess_iso_option(xml):
> --
> 2.20.1
>
>
> _______________________________________________
> elbe-devel mailing list
> elbe-devel at linutronix.de
> https://lists.linutronix.de/mailman/listinfo/elbe-devel
--
Torben Hohn
Linutronix GmbH | Bahnhofstrasse 3 | D-88690 Uhldingen-Mühlhofen
Phone: +49 7556 25 999 18; Fax.: +49 7556 25 999 99
Hinweise zum Datenschutz finden Sie hier (Informations on data privacy
can be found here): https://linutronix.de/kontakt/Datenschutz.php
Linutronix GmbH | Firmensitz (Registered Office): Uhldingen-Mühlhofen |
Registergericht (Registration Court): Amtsgericht Freiburg i.Br., HRB700
806 | Geschäftsführer (Managing Directors): Heinz Egger, Thomas Gleixner
More information about the elbe-devel
mailing list