Python2 is going to die soon, convert to python3.
---
bin/virt-sandbox-service | 68 ++++++++++++++++++---------------
libvirt-sandbox/image/cli.py | 18 ++++-----
libvirt-sandbox/image/sources/docker.py | 41 ++++++++++----------
libvirt-sandbox/image/template.py | 8 ++--
4 files changed, 71 insertions(+), 64 deletions(-)
diff --git a/bin/virt-sandbox-service b/bin/virt-sandbox-service
index c34c6f3..e78defb 100755
--- a/bin/virt-sandbox-service
+++ b/bin/virt-sandbox-service
@@ -30,7 +30,6 @@ from gi.repository import GLib
import gi
import re
import os, sys, shutil, errno, stat
-import exceptions
import rpm
from subprocess import Popen, PIPE, STDOUT
import gettext
@@ -49,7 +48,6 @@ gettext.textdomain("libvirt-sandbox")
try:
gettext.install("libvirt-sandbox",
localedir="/usr/share/locale",
- unicode=False,
codeset = 'utf-8')
except IOError:
import __builtin__
@@ -235,7 +233,7 @@ class Container:
path = "%s%s" % (self.dest, f)
os.chown(path, s.st_uid, s.st_gid)
os.chmod(path, s.st_mode)
- except OSError, e:
+ except OSError as e:
if not e.errno == errno.ENOENT:
raise
@@ -253,7 +251,7 @@ class Container:
try:
path = "%s%s" % (self.dest, d)
os.makedirs(path)
- except OSError, e:
+ except OSError as e:
if not e.errno == errno.EEXIST:
raise
@@ -263,7 +261,7 @@ class Container:
path = "%s%s" % (self.dest, f)
fd=open(path, "w")
fd.close()
- except OSError, e:
+ except OSError as e:
if not e.errno == errno.EEXIST:
raise
@@ -404,10 +402,10 @@ class GenericContainer(Container):
def create(self):
try:
self.create_generic()
- except Exception, e:
+ except Exception as e:
try:
self.delete()
- except Exception, e2:
+ except Exception as e2:
pass
raise e
@@ -418,6 +416,18 @@ class GenericContainer(Container):
def is_template_unit(unit):
return '@' in unit
+# Python 2 / 3 compability helpers
+def get_next(obj):
+ if hasattr(obj, 'next'):
+ return obj.next()
+ else:
+ return next(obj)
+
+def string(obj):
+ if isinstance(obj, bytes):
+ return str(obj, encoding='utf-8')
+ return obj
+
class SystemdContainer(Container):
IGNORE_DIRS = [ "/var/run/", "/etc/logrotate.d/", "/etc/pam.d" ]
DEFAULT_DIRS = [ "/etc", "/var" ]
@@ -581,8 +591,8 @@ WantedBy=multi-user.target
def get_rpm_for_unit(self, unitfile):
mi = self.ts.dbMatch(rpm.RPMTAG_BASENAMES, unitfile)
try:
- h = mi.next();
- except exceptions.StopIteration:
+ h = get_next(mi);
+ except StopIteration:
return None
return h['name']
@@ -590,8 +600,8 @@ WantedBy=multi-user.target
def extract_rpm(self, rpm_name):
mi = self.ts.dbMatch('name', rpm_name)
try:
- h = mi.next();
- except exceptions.StopIteration:
+ h = get_next(mi);
+ except StopIteration:
raise ValueError([_("Cannot find package named %s") % rpm_name])
for fentry in h.fiFromHeader():
@@ -602,16 +612,16 @@ WantedBy=multi-user.target
if os.path.isfile(fname):
self.add_file(fname)
- srcrpm = h[rpm.RPMTAG_SOURCERPM]
+ srcrpm = string(h[rpm.RPMTAG_SOURCERPM])
srcrpmbits = self.split_filename(srcrpm)
- if srcrpmbits[0] == h[rpm.RPMTAG_NAME]:
+ if srcrpmbits[0] == string(h[rpm.RPMTAG_NAME]):
return
mi = self.ts.dbMatch(rpm.RPMTAG_NAME, srcrpmbits[0])
try:
- h = mi.next();
- except exceptions.StopIteration:
+ h = get_next(mi);
+ except StopIteration:
raise ValueError([_("Cannot find base package %s") % srcrpmbits[0]])
for fentry in h.fiFromHeader():
@@ -771,7 +781,7 @@ PrivateNetwork=false
fd.write("[Unit]\n")
fd.write("Description=Sandbox multi-user target\n")
fd.close()
- except OSError, e:
+ except OSError as e:
if not e.errno == errno.EEXIST:
raise
@@ -789,7 +799,7 @@ PrivateNetwork=false
jpath = "/var/log/journal/" + uuid
if os.path.lexists(jpath):
os.remove(jpath)
- except Exception, e:
+ except Exception as e:
sys.stderr.write("%s: %s\n" % (sys.argv[0], e))
sys.stderr.flush()
@@ -825,10 +835,10 @@ PrivateNetwork=false
try:
self.create_systemd()
- except Exception, e:
+ except Exception as e:
try:
self.delete()
- except Exception, e2:
+ except Exception as e2:
sys.stderr.write("Cleanup failed: %s\n" % str(e2))
raise
@@ -923,10 +933,10 @@ def connect(args):
execute(args)
return
- print """\
+ print ("""\
Connected to %s.
Type 'Ctrl + ]' to detach from the console.
-""" % ( args.name )
+""" % ( args.name ))
os.execl("/usr/libexec/virt-sandbox-service-util",
"virt-sandbox-service-util",
"-c", args.uri,
@@ -1014,7 +1024,7 @@ def clone(args):
newcontainer.set_security(args.security)
newcontainer.set_security_label()
newcontainer.save_config()
- except Exception, e:
+ except Exception as e:
if newcontainer is not None:
newcontainer.delete()
raise
@@ -1296,23 +1306,21 @@ if __name__ == '__main__':
sys.exit(1)
args.func(args)
sys.exit(0)
- except KeyboardInterrupt, e:
+ except KeyboardInterrupt as e:
sys.exit(0)
- except ValueError, e:
- for line in e:
- for l in line:
- sys.stderr.write("%s: %s\n" % (sys.argv[0], l))
+ except ValueError as e:
+ sys.stderr.write("%s: %s\n" % (sys.argv[0], e))
sys.stderr.flush()
sys.exit(1)
- except IOError, e:
+ except IOError as e:
sys.stderr.write("%s: %s: %s\n" % (sys.argv[0], e.filename, e.strerror))
sys.stderr.flush()
sys.exit(1)
- except OSError, e:
+ except OSError as e:
sys.stderr.write("%s: %s\n" % (sys.argv[0], e))
sys.stderr.flush()
sys.exit(1)
- except GLib.GError, e:
+ except GLib.GError as e:
sys.stderr.write("%s: %s\n" % (sys.argv[0], e))
sys.stderr.flush()
sys.exit(1)
diff --git a/libvirt-sandbox/image/cli.py b/libvirt-sandbox/image/cli.py
index d5e624c..fa3cace 100644
--- a/libvirt-sandbox/image/cli.py
+++ b/libvirt-sandbox/image/cli.py
@@ -30,7 +30,6 @@ import os.path
import re
import shutil
import sys
-import urllib2
import subprocess
import random
import string
@@ -52,7 +51,6 @@ gettext.textdomain("libvirt-sandbox")
try:
gettext.install("libvirt-sandbox",
localedir="/usr/share/locale",
- unicode=False,
codeset = 'utf-8')
except IOError:
import __builtin__
@@ -149,7 +147,7 @@ def list_cached(args):
tmpls.extend(template.Template.get_all(source,
"%s/%s" % (args.template_dir, source)))
for tmpl in tmpls:
- print tmpl
+ print (tmpl)
def requires_template(parser):
parser.add_argument("template",
@@ -265,20 +263,20 @@ def main():
try:
args.func(args)
sys.exit(0)
- except KeyboardInterrupt, e:
+ except KeyboardInterrupt as e:
sys.exit(0)
- except ValueError, e:
+ except ValueError as e:
sys.stderr.write("%s: %s\n" % (sys.argv[0], e))
sys.stderr.flush()
sys.exit(1)
- except IOError, e:
- sys.stderr.write("%s: %s: %s\n" % (sys.argv[0], e.filename, e.reason))
+ except IOError as e:
+ sys.stderr.write("%s: %s\n" % (sys.argv[0], e.filename))
sys.stderr.flush()
sys.exit(1)
- except OSError, e:
+ except OSError as e:
sys.stderr.write("%s: %s\n" % (sys.argv[0], e))
sys.stderr.flush()
sys.exit(1)
- except Exception, e:
- print e.message
+ except Exception as e:
+ print (e)
sys.exit(1)
diff --git a/libvirt-sandbox/image/sources/docker.py b/libvirt-sandbox/image/sources/docker.py
index 6ca086c..e979054 100755
--- a/libvirt-sandbox/image/sources/docker.py
+++ b/libvirt-sandbox/image/sources/docker.py
@@ -21,14 +21,15 @@
# Author: Eren Yagdiran <erenyagdiran@gmail.com>
#
-import urllib2
import sys
import json
import traceback
import os
import subprocess
import shutil
-import urlparse
+import urllib.error
+import urllib.parse
+import urllib.request
import hashlib
from abc import ABCMeta, abstractmethod
import copy
@@ -133,7 +134,7 @@ class DockerAuthBasic(DockerAuth):
req.add_header("X-Docker-Token", "true")
def process_res(self, res):
- self.token = res.info().getheader('X-Docker-Token')
+ self.token = res.info().get('X-Docker-Token')
def process_err(self, err):
return False
@@ -194,10 +195,10 @@ class DockerAuthBearer(DockerAuth):
if params != "":
url = url + "?" + params
- req = urllib2.Request(url=url)
+ req = urllib.request.Request(url=url)
req.add_header("Accept", "application/json")
- res = urllib2.urlopen(req)
+ res = urllib.request.urlopen(req)
data = json.loads(res.read())
self.token = data["token"]
return True
@@ -207,7 +208,7 @@ class DockerRegistry():
def __init__(self, uri_base):
- self.uri_base = list(urlparse.urlparse(uri_base))
+ self.uri_base = list(urllib.parse.urlparse(uri_base))
self.auth_handler = DockerAuthNop()
def set_auth_handler(self, auth_handler):
@@ -216,8 +217,8 @@ class DockerRegistry():
def supports_v2(self):
try:
(data, res) = self.get_json("/v2/")
- ver = res.info().getheader("Docker-Distribution-Api-Version")
- except urllib2.HTTPError as e:
+ ver = res.info().get("Docker-Distribution-Api-Version")
+ except urllib.error.HTTPError as e:
ver = e.headers.get("Docker-Distribution-Api-Version", None)
if ver is None:
@@ -243,17 +244,17 @@ class DockerRegistry():
else:
server = "%s:%s" % (hostname, port)
- url = urlparse.urlunparse((protocol, server, "", None, None, None))
+ url = urllib.parse.urlunparse((protocol, server, "", None, None, None))
return cls(url)
def get_url(self, path, headers=None):
url_bits = copy.copy(self.uri_base)
url_bits[2] = path
- url = urlparse.urlunparse(url_bits)
+ url = urllib.parse.urlunparse(url_bits)
debug("Fetching %s..." % url)
- req = urllib2.Request(url=url)
+ req = urllib.request.Request(url=url)
if headers is not None:
for h in headers.keys():
@@ -262,16 +263,16 @@ class DockerRegistry():
self.auth_handler.prepare_req(req)
try:
- res = urllib2.urlopen(req)
+ res = urllib.request.urlopen(req)
self.auth_handler.process_res(res)
return res
- except urllib2.HTTPError as e:
+ except urllib.error.HTTPError as e:
if e.code == 401:
retry = self.auth_handler.process_err(e)
if retry:
debug("Re-Fetching %s..." % url)
self.auth_handler.prepare_req(req)
- res = urllib2.urlopen(req)
+ res = urllib.request.urlopen(req)
self.auth_handler.process_res(res)
return res
else:
@@ -284,7 +285,7 @@ class DockerRegistry():
try:
res = self.get_url(path)
- datalen = res.info().getheader("Content-Length")
+ datalen = res.info().get("Content-Length")
if datalen is not None:
datalen = int(datalen)
@@ -296,7 +297,7 @@ class DockerRegistry():
patternIndex = 0
donelen = 0
- with open(dest, "w") as f:
+ with open(dest, "wb") as f:
while 1:
buf = res.read(1024*64)
if not buf:
@@ -321,7 +322,7 @@ class DockerRegistry():
raise IOError("Checksum '%s' for data does not match '%s'" % (csumstr, checksum))
debug("OK\n")
return res
- except Exception, e:
+ except Exception as e:
debug("FAIL %s\n" % str(e))
raise
@@ -333,7 +334,7 @@ class DockerRegistry():
data = json.loads(res.read())
debug("OK\n")
return (data, res)
- except Exception, e:
+ except Exception as e:
debug("FAIL %s\n" % str(e))
raise
@@ -426,10 +427,10 @@ class DockerSource(base.Source):
(data, res) = registry.get_json("/v1/repositories/%s/%s/images" % (
image.repo, image.name,
))
- except urllib2.HTTPError, e:
+ except urllib.error.HTTPError as e:
raise ValueError(["Image '%s' does not exist" % template])
- registryendpoint = res.info().getheader('X-Docker-Endpoints')
+ registryendpoint = res.info().get('X-Docker-Endpoints')
if basicauth.token is not None:
registry.set_auth_handler(DockerAuthToken(basicauth.token))
diff --git a/libvirt-sandbox/image/template.py b/libvirt-sandbox/image/template.py
index 79dc33d..ab2ea29 100644
--- a/libvirt-sandbox/image/template.py
+++ b/libvirt-sandbox/image/template.py
@@ -19,7 +19,7 @@
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
-import urlparse
+import urllib.parse
import importlib
import re
@@ -71,7 +71,7 @@ class Template(object):
classimpl = getattr(mod, classname)
return classimpl()
except Exception as e:
- print e
+ print (e)
raise Exception("Invalid source: '%s'" % source)
def get_source_impl(self):
@@ -101,12 +101,12 @@ class Template(object):
netloc = None
query = "&".join([key + "=" + self.params[key] for key in self.params.keys()])
- ret = urlparse.urlunparse((scheme, netloc, self.path, None, query, None))
+ ret = urllib.parse.urlunparse((scheme, netloc, self.path, None, query, None))
return ret
@classmethod
def from_uri(klass, uri):
- o = urlparse.urlparse(uri)
+ o = urllib.parse.urlparse(uri)
idx = o.scheme.find("+")
if idx == -1:
--
2.15.1
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
On Tue, Dec 05, 2017 at 10:53:21AM +0100, Cédric Bosdonnat wrote: > Python2 is going to die soon, convert to python3. I'm unclear whether this change drops py2 support, or whether it makes it work with py2+3 in parallel. The commit message suggests py3 only, but then this: > @@ -418,6 +416,18 @@ class GenericContainer(Container): > def is_template_unit(unit): > return '@' in unit > > +# Python 2 / 3 compability helpers > +def get_next(obj): > + if hasattr(obj, 'next'): > + return obj.next() > + else: > + return next(obj) > + > +def string(obj): > + if isinstance(obj, bytes): > + return str(obj, encoding='utf-8') > + return obj suggests py2+3 in parallel I don't mind being py3 only. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :| -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
On Tue, 2017-12-05 at 16:57 +0000, Daniel P. Berrange wrote: > On Tue, Dec 05, 2017 at 10:53:21AM +0100, Cédric Bosdonnat wrote: > > Python2 is going to die soon, convert to python3. > > I'm unclear whether this change drops py2 support, or whether it makes it > work with py2+3 in parallel. > > The commit message suggests py3 only, but then this: > > > @@ -418,6 +416,18 @@ class GenericContainer(Container): > > def is_template_unit(unit): > > return '@' in unit > > > > +# Python 2 / 3 compability helpers > > +def get_next(obj): > > + if hasattr(obj, 'next'): > > + return obj.next() > > + else: > > + return next(obj) > > + > > +def string(obj): > > + if isinstance(obj, bytes): > > + return str(obj, encoding='utf-8') > > + return obj > > > suggests py2+3 in parallel > > I don't mind being py3 only. > Oops, that one is a left over. I'll remove it and drop python2. And at least the commit message will be in sync with the code -- Cedric -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
On Wed, Dec 06, 2017 at 01:50:56PM +0100, Cedric Bosdonnat wrote: > On Tue, 2017-12-05 at 16:57 +0000, Daniel P. Berrange wrote: > > On Tue, Dec 05, 2017 at 10:53:21AM +0100, Cédric Bosdonnat wrote: > > > Python2 is going to die soon, convert to python3. > > > > I'm unclear whether this change drops py2 support, or whether it makes it > > work with py2+3 in parallel. > > > > The commit message suggests py3 only, but then this: > > > > > @@ -418,6 +416,18 @@ class GenericContainer(Container): > > > def is_template_unit(unit): > > > return '@' in unit > > > > > > +# Python 2 / 3 compability helpers > > > +def get_next(obj): > > > + if hasattr(obj, 'next'): > > > + return obj.next() > > > + else: > > > + return next(obj) > > > + > > > +def string(obj): > > > + if isinstance(obj, bytes): > > > + return str(obj, encoding='utf-8') > > > + return obj > > > > > > suggests py2+3 in parallel > > > > I don't mind being py3 only. > > > Oops, that one is a left over. I'll remove it and drop python2. > And at least the commit message will be in sync with the code Ok, ack with that dropped Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :| -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
© 2016 - 2025 Red Hat, Inc.