[elbe-devel] [PATCH v2 1/5] Python3: change the urllib2 imports

Christian Teklenborg chris at linutronix.de
Fri Dec 13 12:45:15 CET 2019


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 |  8 ++++++--
 elbepack/elbexml.py          | 27 ++++++++++++++++-----------
 elbepack/pbuilder.py         |  8 +++++---
 elbepack/soapclient.py       |  9 ++++++---
 elbepack/xmlpreprocess.py    | 10 +++++++---
 6 files changed, 46 insertions(+), 24 deletions(-)

diff --git a/elbepack/commands/control.py b/elbepack/commands/control.py
index cb539b70..442df289 100644
--- a/elbepack/commands/control.py
+++ b/elbepack/commands/control.py
@@ -11,8 +11,12 @@ import socket
 import sys
 
 from optparse import (OptionParser, OptionGroup)
-from urllib2 import URLError
-from httplib import BadStatusLine
+try:
+    from urllib.error import URLError
+    from http.client import BadStatusLine
+except ImportError:
+    from urllib2 import URLError
+    from httplib import BadStatusLine
 
 from suds import WebFault
 
diff --git a/elbepack/commands/prjrepo.py b/elbepack/commands/prjrepo.py
index 51e2b845..25363e45 100644
--- a/elbepack/commands/prjrepo.py
+++ b/elbepack/commands/prjrepo.py
@@ -11,8 +11,12 @@ import socket
 import sys
 
 from optparse import (OptionParser, OptionGroup)
-from urllib2 import URLError
-from httplib import BadStatusLine
+try:
+    from urllib.error import URLError
+    from http.client import BadStatusLine
+except ImportError:
+    from urllib2 import URLError
+    from httplib import BadStatusline
 
 from suds import WebFault
 
diff --git a/elbepack/elbexml.py b/elbepack/elbexml.py
index e281bbd1..d75ad034 100644
--- a/elbepack/elbexml.py
+++ b/elbepack/elbexml.py
@@ -10,14 +10,19 @@
 
 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)
+    from urllib.error import URLError
+except ImportError:
+    from urllib2 import (urlopen, install_opener, build_opener, URLError,
+                         HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler)
 
 class ValidationError(Exception):
     def __init__(self, validation):
@@ -178,11 +183,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 +274,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..cbe07384 100644
--- a/elbepack/soapclient.py
+++ b/elbepack/soapclient.py
@@ -16,8 +16,12 @@ import os
 import fnmatch
 
 from datetime import datetime
-from urllib2 import URLError
-from httplib import BadStatusLine
+try:
+    from urllib.error import URLError
+    from http.client import BadStatusLine
+except ImportError:
+    from urllib2 import URLError
+    from httplib import BadStatusLine
 
 import deb822   # package for dealing with Debian related data
 
@@ -28,7 +32,6 @@ from elbepack.filesystem import Filesystem
 from elbepack.elbexml import ElbeXML, ValidationMode
 from elbepack.version import elbe_version, elbe_initvm_packagelist
 
-
 def set_suds_debug(debug):
     import logging
     if debug:
diff --git a/elbepack/xmlpreprocess.py b/elbepack/xmlpreprocess.py
index 598ced45..2bd6447e 100644
--- a/elbepack/xmlpreprocess.py
+++ b/elbepack/xmlpreprocess.py
@@ -8,7 +8,11 @@
 from __future__ import print_function
 
 import sys
-import urllib2
+try:
+    from urllib.request import urlopen
+    from urllib.error import HTTPError
+except ImportError:
+    from urllib2 import urlopen, HTTPError
 
 from tempfile import NamedTemporaryFile
 from optparse import OptionGroup
@@ -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:
+        except HTTPError:
             raise XMLPreprocessError("Invalid PGP Key URL in <key> tag: %s" % keyurl)
 
 def preprocess_iso_option(xml):
-- 
2.20.1




More information about the elbe-devel mailing list