[elbe-devel] [PATCH 11/25] py3: fixup imports

Martin Kaistra martin.kaistra at linutronix.de
Fri Dec 22 15:59:43 CET 2017


Am 08.12.2017 um 19:08 schrieb Manuel Traut:
> some imports are renamed in py3, to be still py2 compatible fallback
> to old import names, if newones could not be imported.
> 
> urllib2 is not available in py3, but urllib3 is not available in py2.
> This tries to use urllib3 and if it is not available falls back to
> urllib2.
> Because some namespaces are different in both versions it is remapped
> and used via common variables.

This looks good, but I don't know if the lines dealing with 
python(3)-spyne should be in this patch but rather in "[PATCH 10/25] 
py3: print error if spyne is not available".
> 
> Signed-off-by: Manuel Traut <manut at linutronix.de>
> ---
>   elbepack/asyncworker.py                 | 14 ++++++++++++--
>   elbepack/commands/control.py            | 13 +++++++++++--
>   elbepack/commands/prjrepo.py            | 13 +++++++++++--
>   elbepack/daemons/soap/__init__.py       |  2 +-
>   elbepack/daemons/soap/authentication.py |  2 +-
>   elbepack/daemons/soap/datatypes.py      | 12 ++++++++++--
>   elbepack/daemons/soap/faults.py         | 12 +++++++++++-
>   elbepack/db.py                          |  6 +++++-
>   elbepack/elbexml.py                     | 31 +++++++++++++++++++++----------
>   elbepack/pbuilder.py                    | 11 +++++++----
>   elbepack/pkgutils.py                    |  2 +-
>   elbepack/rfs.py                         | 21 ++++++++++++++++-----
>   elbepack/soapclient.py                  | 10 ++++++++++
>   13 files changed, 117 insertions(+), 32 deletions(-)
> 
> diff --git a/elbepack/asyncworker.py b/elbepack/asyncworker.py
> index ac9eb273..1552a4ed 100644
> --- a/elbepack/asyncworker.py
> +++ b/elbepack/asyncworker.py
> @@ -19,10 +19,20 @@
>   # along with ELBE.  If not, see <http://www.gnu.org/licenses/>.
>   
>   from threading import Thread
> -from Queue import Queue
> +
> +try:
> +    from queue import Queue
> +except ImportError:
> +    from Queue import Queue
> +
> +try:
> +    from urllib.parse import quote
> +except ImportError:
> +    from urllib import quote
> +
> +
>   from os import path, getcwd, chdir
>   from contextlib import contextmanager
> -from urllib import quote
>   import traceback
>   
>   from elbepack.db import get_versioned_filename
> diff --git a/elbepack/commands/control.py b/elbepack/commands/control.py
> index c09ac3ab..fa69cb76 100755
> --- a/elbepack/commands/control.py
> +++ b/elbepack/commands/control.py
> @@ -25,8 +25,17 @@ import sys
>   
>   from optparse import (OptionParser, OptionGroup)
>   from suds import WebFault
> -from urllib2 import URLError
> -from httplib import BadStatusLine
> +
> +# different module names in python2 and 3
> +try:
> +    from urllib.error import URLError
> +except ImportError:
> +    from urllib2 import URLError
> +
> +try:
> +    from http.client import BadStatusLine
> +except ImportError:
> +    from httplib import BadStatusLine
>   
>   from elbepack.soapclient import ClientAction, ElbeSoapClient
>   from elbepack.version import elbe_version
> diff --git a/elbepack/commands/prjrepo.py b/elbepack/commands/prjrepo.py
> index 462fb3c4..4c979151 100755
> --- a/elbepack/commands/prjrepo.py
> +++ b/elbepack/commands/prjrepo.py
> @@ -20,13 +20,22 @@
>   
>   from __future__ import print_function
>   
> +#different module names in python 2 and 3
> +try:
> +    from urllib.error import URLError
> +except ImportError:
> +    from urllib2 import URLError
> +
> +try:
> +    from httplib2 import BadStatusLine
> +except ImportError:
> +    from httplib import BadStatusLine
> +
>   import socket
>   import sys
>   
>   from optparse import (OptionParser, OptionGroup)
>   from suds import WebFault
> -from urllib2 import URLError
> -from httplib import BadStatusLine
>   
>   from elbepack.soapclient import RepoAction, ElbeSoapClient
>   from elbepack.version import elbe_version
> diff --git a/elbepack/daemons/soap/__init__.py b/elbepack/daemons/soap/__init__.py
> index 560333fc..221d5142 100644
> --- a/elbepack/daemons/soap/__init__.py
> +++ b/elbepack/daemons/soap/__init__.py
> @@ -20,7 +20,7 @@ from __future__ import print_function
>   
>   import sys
>   
> -from esoap import ESoap
> +from .esoap import ESoap
>   
>   from beaker.middleware import SessionMiddleware
>   from cherrypy.process.plugins import SimplePlugin
> diff --git a/elbepack/daemons/soap/authentication.py b/elbepack/daemons/soap/authentication.py
> index 9cb14440..65c6cf89 100644
> --- a/elbepack/daemons/soap/authentication.py
> +++ b/elbepack/daemons/soap/authentication.py
> @@ -18,7 +18,7 @@
>   
>   
>   
> -from faults import SoapElbeNotLoggedIn, SoapElbeNotAuthorized
> +from .faults import SoapElbeNotLoggedIn, SoapElbeNotAuthorized
>   from functools import wraps
>   
>   def authenticated_uid(func):
> diff --git a/elbepack/daemons/soap/datatypes.py b/elbepack/daemons/soap/datatypes.py
> index 656ebbb7..bd03ed2e 100644
> --- a/elbepack/daemons/soap/datatypes.py
> +++ b/elbepack/daemons/soap/datatypes.py
> @@ -16,9 +16,17 @@
>   # You should have received a copy of the GNU General Public License
>   # along with ELBE.  If not, see <http://www.gnu.org/licenses/>.
>   
> +from __future__ import print_function
>   
> -from spyne.model.complex import ComplexModel
> -from spyne.model.primitive import Unicode, DateTime
> +import sys
> +
> +try:
> +    from spyne.model.complex import ComplexModel
> +    from spyne.model.primitive import Unicode, DateTime
> +except ImportError as e:
> +    print("failed to import spyne", file=sys.stderr)
> +    print("please install python(3)-spyne", file=sys.stderr)
> +    sys.exit(-20)
>   
>   class SoapProject (ComplexModel):
>       __namespace__ = 'soap'
> diff --git a/elbepack/daemons/soap/faults.py b/elbepack/daemons/soap/faults.py
> index ee80bfbe..54a1affe 100644
> --- a/elbepack/daemons/soap/faults.py
> +++ b/elbepack/daemons/soap/faults.py
> @@ -16,7 +16,17 @@
>   # You should have received a copy of the GNU General Public License
>   # along with ELBE.  If not, see <http://www.gnu.org/licenses/>.
>   
> -from spyne.model.fault import Fault
> +from __future__ import print_function
> +
> +import sys
> +
> +try:
> +    from spyne.model.fault import Fault
> +except ImportError as e:
> +    print("failed to import spyne", file=sys.stderr)
> +    print("please install python(3)-spyne", file=sys.stderr)
> +    sys.exit(-20)
> +
>   from traceback import format_exc
>   
>   from functools import wraps
> diff --git a/elbepack/db.py b/elbepack/db.py
> index baf4c091..5bd32211 100644
> --- a/elbepack/db.py
> +++ b/elbepack/db.py
> @@ -18,6 +18,11 @@
>   # You should have received a copy of the GNU General Public License
>   # along with ELBE.  If not, see <http://www.gnu.org/licenses/>.
>   
> +try:
> +    from urllib.parse import quote
> +except ImportError:
> +    from urllib import quote
> +
>   import os
>   import errno
>   import re
> @@ -25,7 +30,6 @@ import re
>   from datetime import datetime
>   from shutil import (rmtree, copyfile, copyfileobj)
>   from contextlib import contextmanager
> -from urllib import quote
>   from threading import Thread
>   
>   from passlib.hash import pbkdf2_sha512
> diff --git a/elbepack/elbexml.py b/elbepack/elbexml.py
> index e257856a..9e3c44fb 100644
> --- a/elbepack/elbexml.py
> +++ b/elbepack/elbexml.py
> @@ -25,7 +25,23 @@ from elbepack.version import elbe_version, is_devel
>   from base64 import standard_b64decode
>   from tempfile import NamedTemporaryFile
>   
> -import urllib2
> +try:
> +    import urllib.request
> +    from urllib.error import URLError
> +    passman = urllib.request.HTTPPasswordMgrWithDefaultRealm()
> +    authhandler = urllib.request.HTTPBasicAuthHandler(passman)
> +    opener = urllib.request.build_opener(authhandler)
> +    urllib.request.install_opener(opener)
> +    urlopen = urllib.request.urlopen
> +except ImportError:
> +    import urllib2
> +    from urllib2 import URLError
> +    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
> +    authhandler = urllib2.HTTPBasicAuthHandler(passman)
> +    opener = urllib2.build_opener(authhandler)
> +    urllib2.install_opener(opener)
> +    urlopen = urllib2.urlopen
> +
>   import os
>   import re
>   
> @@ -149,11 +165,11 @@ class ElbeXML(object):
>   
>       def validate_repo (self, r, url_validation):
>           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
> @@ -226,11 +242,6 @@ 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)
> -
>           for r in repos:
>               if '@' in r["url"]:
>                   t = r["url"].split('@')
> diff --git a/elbepack/pbuilder.py b/elbepack/pbuilder.py
> index 3ee603c3..1f65c98b 100644
> --- a/elbepack/pbuilder.py
> +++ b/elbepack/pbuilder.py
> @@ -1,8 +1,11 @@
> +try:
> +    import urllib.request
> +    urlopen = urllib.request.urlopen
> +except ImportError:
> +    import urllib2
> +    urlopen = urllib2.urlopen
>   
>   import os
> -import urllib2
> -
> -
>   
>   def pbuilder_ensure_chroot (builddir):
>       pass
> @@ -67,7 +70,7 @@ def pbuilder_write_apt_conf (builddir, xml):
>   
>   def mirror_script_add_key(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/pkgutils.py b/elbepack/pkgutils.py
> index d97baff7..32000f00 100644
> --- a/elbepack/pkgutils.py
> +++ b/elbepack/pkgutils.py
> @@ -90,7 +90,7 @@ def get_initrd_pkg( prj, defs ):
>   def get_url ( arch, suite, target_pkg, mirror, comp='main' ):
>       try:
>           pack_url = "%s/dists/%s/%s/binary-%s/Packages" % (mirror.replace("LOCALMACHINE", "localhost"), suite, comp, arch)
> -        packages = urllib2.urlopen(pack_url, None, 10)
> +        packages = urlopen(pack_url, None, 10)
>   
>           packages = packages.readlines()
>           packages = filter( lambda x: x.startswith( "Filename" ), packages )
> diff --git a/elbepack/rfs.py b/elbepack/rfs.py
> index 1a72dc81..fa6ac8dd 100644
> --- a/elbepack/rfs.py
> +++ b/elbepack/rfs.py
> @@ -16,9 +16,20 @@
>   # You should have received a copy of the GNU General Public License
>   # along with ELBE.  If not, see <http://www.gnu.org/licenses/>.
>   
> +from __future__ import print_function
> +
> +try:
> +    import urllib.parse
> +    import urllib.request
> +    urlopen = urllib.request.urlopen
> +    urlsplit = urllib.parse.urlsplit
> +except ImportError:
> +    import urlparse
> +    import urllib2
> +    urlopen = urllib2.urlopen
> +    urlsplit = urlparse.urlsplit
> +
>   import os
> -import urlparse
> -import urllib2
>   
>   from elbepack.efilesystem import BuildImgFs
>   from elbepack.templates import write_pack_template, get_preseed, preseed_to_text
> @@ -50,7 +61,7 @@ class BuildEnv ():
>               self.fresh_debootstrap = True
>               self.need_dumpdebootstrap = True
>           else:
> -            print 'work on existing rfs'
> +            print('work on existing rfs')
>               self.fresh_debootstrap = False
>               self.need_dumpdebootstrap = False
>   
> @@ -198,7 +209,7 @@ class BuildEnv ():
>                       l = url.text('key').strip()    # URL to key
>                       name = l.split('/')[-1]        # Filename of key
>   
> -                    myKey = urllib2.urlopen(l).read()
> +                    myKey = urlopen(l).read()
>                       self.log.do('echo "%s" > %s' % (myKey, self.rfs.fname("tmp/key.pub")))
>                       with self.rfs:
>                           self.log.chroot(self.rfs.path, 'apt-key add /tmp/key.pub' )
> @@ -240,7 +251,7 @@ class BuildEnv ():
>                   if not 'pin' in repo.et.attrib:
>                       continue
>   
> -                origin = urlparse.urlsplit(repo.et.text.strip()).hostname
> +                origin = urlsplit(repo.et.text.strip()).hostname
>                   pin = repo.et.attrib['pin']
>                   if 'package' in repo.et.attrib:
>                       package = repo.et.attrib['package']
> diff --git a/elbepack/soapclient.py b/elbepack/soapclient.py
> index ede9164e..0222eb38 100644
> --- a/elbepack/soapclient.py
> +++ b/elbepack/soapclient.py
> @@ -20,6 +20,16 @@
>   
>   from __future__ import print_function
>   
> +try:
> +    from urllib.error import URLError
> +except ImportError:
> +    from urllib2 import URLError
> +
> +try:
> +    from http.client import BadStatusLine
> +except ImportError:
> +    from httplib import BadStatusLine
> +
>   import binascii
>   import socket
>   import time
> 

-- 
Phone: +49 7556 25 999 29; Fax.: +49 7556 25 999 99

Firmensitz / Registered Office: D-88690 Uhldingen-Mühlhofen, Bahnhofstr. 3
Registergericht / Local District Court: Amtsgericht Freiburg i. Br.; HRB 
Nr. / Trade register no.: 700 806

Geschäftsführer / Managing Directors: Heinz Egger, Thomas Gleixner

Eine Bitte von uns: Sollten Sie diese E-Mail irrtümlich erhalten haben, 
benachrichtigen Sie uns in diesem Falle bitte sobald wie es Ihnen 
möglich ist, durch Antwort-Mail. Vielen Dank!



More information about the elbe-devel mailing list