From nobody Thu May 15 09:55:14 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1512467648830454.24644412272573; Tue, 5 Dec 2017 01:54:08 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A6B47C02738C; Tue, 5 Dec 2017 09:54:07 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 808BA61984; Tue, 5 Dec 2017 09:54:07 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 477323D386; Tue, 5 Dec 2017 09:54:07 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id vB59ruXC007380 for ; Tue, 5 Dec 2017 04:53:56 -0500 Received: by smtp.corp.redhat.com (Postfix) id BA2175DA68; Tue, 5 Dec 2017 09:53:56 +0000 (UTC) Received: from mx1.redhat.com (ext-mx04.extmail.prod.ext.phx2.redhat.com [10.5.110.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B31D05DA67 for ; Tue, 5 Dec 2017 09:53:53 +0000 (UTC) Received: from smtp.nue.novell.com (smtp.nue.novell.com [195.135.221.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B24888046E for ; Tue, 5 Dec 2017 09:53:51 +0000 (UTC) Received: from emea4-mta.ukb.novell.com ([10.120.13.87]) by smtp.nue.novell.com with ESMTP (TLS encrypted); Tue, 05 Dec 2017 10:53:50 +0100 Received: from laptop.tf.local (nwb-a10-snat.microfocus.com [10.120.13.202]) by emea4-mta.ukb.novell.com with ESMTP (TLS encrypted); Tue, 05 Dec 2017 09:53:27 +0000 From: =?UTF-8?q?C=C3=A9dric=20Bosdonnat?= To: libvir-list@redhat.com Date: Tue, 5 Dec 2017 10:53:21 +0100 Message-Id: <20171205095322.24258-6-cbosdonnat@suse.com> In-Reply-To: <20171205095322.24258-1-cbosdonnat@suse.com> References: <20171205095322.24258-1-cbosdonnat@suse.com> X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 207 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 05 Dec 2017 09:53:52 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 05 Dec 2017 09:53:52 +0000 (UTC) for IP:'195.135.221.5' DOMAIN:'smtp.nue.novell.com' HELO:'smtp.nue.novell.com' FROM:'cbosdonnat@suse.com' RCPT:'' X-RedHat-Spam-Score: -2.301 (RCVD_IN_DNSWL_MED, SPF_PASS) 195.135.221.5 smtp.nue.novell.com 195.135.221.5 smtp.nue.novell.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.28 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Cc: =?UTF-8?q?C=C3=A9dric=20Bosdonnat?= Subject: [libvirt] [sandbox 5/6] Convert to python3 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 05 Dec 2017 09:54:08 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" 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=3D"/usr/share/locale", - unicode=3DFalse, codeset =3D 'utf-8') except IOError: import __builtin__ @@ -235,7 +233,7 @@ class Container: path =3D "%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 =3D=3D errno.ENOENT: raise =20 @@ -253,7 +251,7 @@ class Container: try: path =3D "%s%s" % (self.dest, d) os.makedirs(path) - except OSError, e: + except OSError as e: if not e.errno =3D=3D errno.EEXIST: raise =20 @@ -263,7 +261,7 @@ class Container: path =3D "%s%s" % (self.dest, f) fd=3Dopen(path, "w") fd.close() - except OSError, e: + except OSError as e: if not e.errno =3D=3D errno.EEXIST: raise =20 @@ -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 =20 @@ -418,6 +416,18 @@ class GenericContainer(Container): def is_template_unit(unit): return '@' in unit =20 +# 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=3D'utf-8') + return obj + class SystemdContainer(Container): IGNORE_DIRS =3D [ "/var/run/", "/etc/logrotate.d/", "/etc/pam.d= " ] DEFAULT_DIRS =3D [ "/etc", "/var" ] @@ -581,8 +591,8 @@ WantedBy=3Dmulti-user.target def get_rpm_for_unit(self, unitfile): mi =3D self.ts.dbMatch(rpm.RPMTAG_BASENAMES, unitfile) try: - h =3D mi.next(); - except exceptions.StopIteration: + h =3D get_next(mi); + except StopIteration: return None return h['name'] =20 @@ -590,8 +600,8 @@ WantedBy=3Dmulti-user.target def extract_rpm(self, rpm_name): mi =3D self.ts.dbMatch('name', rpm_name) try: - h =3D mi.next(); - except exceptions.StopIteration: + h =3D get_next(mi); + except StopIteration: raise ValueError([_("Cannot find package named %s") % rpm_name= ]) =20 for fentry in h.fiFromHeader(): @@ -602,16 +612,16 @@ WantedBy=3Dmulti-user.target if os.path.isfile(fname): self.add_file(fname) =20 - srcrpm =3D h[rpm.RPMTAG_SOURCERPM] + srcrpm =3D string(h[rpm.RPMTAG_SOURCERPM]) srcrpmbits =3D self.split_filename(srcrpm) =20 - if srcrpmbits[0] =3D=3D h[rpm.RPMTAG_NAME]: + if srcrpmbits[0] =3D=3D string(h[rpm.RPMTAG_NAME]): return =20 mi =3D self.ts.dbMatch(rpm.RPMTAG_NAME, srcrpmbits[0]) try: - h =3D mi.next(); - except exceptions.StopIteration: + h =3D get_next(mi); + except StopIteration: raise ValueError([_("Cannot find base package %s") % srcrpmbit= s[0]]) =20 for fentry in h.fiFromHeader(): @@ -771,7 +781,7 @@ PrivateNetwork=3Dfalse fd.write("[Unit]\n") fd.write("Description=3DSandbox multi-user target\n") fd.close() - except OSError, e: + except OSError as e: if not e.errno =3D=3D errno.EEXIST: raise =20 @@ -789,7 +799,7 @@ PrivateNetwork=3Dfalse jpath =3D "/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() =20 @@ -825,10 +835,10 @@ PrivateNetwork=3Dfalse =20 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 =20 @@ -923,10 +933,10 @@ def connect(args): execute(args) return =20 - 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__ =3D=3D '__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.stre= rror)) 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=3D"/usr/share/locale", - unicode=3DFalse, codeset =3D 'utf-8') except IOError: import __builtin__ @@ -149,7 +147,7 @@ def list_cached(args): tmpls.extend(template.Template.get_all(source, "%s/%s" % (args.templat= e_dir, source))) for tmpl in tmpls: - print tmpl + print (tmpl) =20 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/imag= e/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 # =20 -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") =20 def process_res(self, res): - self.token =3D res.info().getheader('X-Docker-Token') + self.token =3D res.info().get('X-Docker-Token') =20 def process_err(self, err): return False @@ -194,10 +195,10 @@ class DockerAuthBearer(DockerAuth): if params !=3D "": url =3D url + "?" + params =20 - req =3D urllib2.Request(url=3Durl) + req =3D urllib.request.Request(url=3Durl) req.add_header("Accept", "application/json") =20 - res =3D urllib2.urlopen(req) + res =3D urllib.request.urlopen(req) data =3D json.loads(res.read()) self.token =3D data["token"] return True @@ -207,7 +208,7 @@ class DockerRegistry(): =20 def __init__(self, uri_base): =20 - self.uri_base =3D list(urlparse.urlparse(uri_base)) + self.uri_base =3D list(urllib.parse.urlparse(uri_base)) self.auth_handler =3D DockerAuthNop() =20 def set_auth_handler(self, auth_handler): @@ -216,8 +217,8 @@ class DockerRegistry(): def supports_v2(self): try: (data, res) =3D self.get_json("/v2/") - ver =3D res.info().getheader("Docker-Distribution-Api-Version") - except urllib2.HTTPError as e: + ver =3D res.info().get("Docker-Distribution-Api-Version") + except urllib.error.HTTPError as e: ver =3D e.headers.get("Docker-Distribution-Api-Version", None) =20 if ver is None: @@ -243,17 +244,17 @@ class DockerRegistry(): else: server =3D "%s:%s" % (hostname, port) =20 - url =3D urlparse.urlunparse((protocol, server, "", None, None, Non= e)) + url =3D urllib.parse.urlunparse((protocol, server, "", None, None,= None)) =20 return cls(url) =20 def get_url(self, path, headers=3DNone): url_bits =3D copy.copy(self.uri_base) url_bits[2] =3D path - url =3D urlparse.urlunparse(url_bits) + url =3D urllib.parse.urlunparse(url_bits) debug("Fetching %s..." % url) =20 - req =3D urllib2.Request(url=3Durl) + req =3D urllib.request.Request(url=3Durl) =20 if headers is not None: for h in headers.keys(): @@ -262,16 +263,16 @@ class DockerRegistry(): self.auth_handler.prepare_req(req) =20 try: - res =3D urllib2.urlopen(req) + res =3D 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 =3D=3D 401: retry =3D self.auth_handler.process_err(e) if retry: debug("Re-Fetching %s..." % url) self.auth_handler.prepare_req(req) - res =3D urllib2.urlopen(req) + res =3D urllib.request.urlopen(req) self.auth_handler.process_res(res) return res else: @@ -284,7 +285,7 @@ class DockerRegistry(): try: res =3D self.get_url(path) =20 - datalen =3D res.info().getheader("Content-Length") + datalen =3D res.info().get("Content-Length") if datalen is not None: datalen =3D int(datalen) =20 @@ -296,7 +297,7 @@ class DockerRegistry(): patternIndex =3D 0 donelen =3D 0 =20 - with open(dest, "w") as f: + with open(dest, "wb") as f: while 1: buf =3D 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 =20 @@ -333,7 +334,7 @@ class DockerRegistry(): data =3D json.loads(res.read()) debug("OK\n") return (data, res) - except Exception, e: + except Exception as e: debug("FAIL %s\n" % str(e)) raise =20 @@ -426,10 +427,10 @@ class DockerSource(base.Source): (data, res) =3D registry.get_json("/v1/repositories/%s/%s/imag= es" % ( image.repo, image.name, )) - except urllib2.HTTPError, e: + except urllib.error.HTTPError as e: raise ValueError(["Image '%s' does not exist" % template]) =20 - registryendpoint =3D res.info().getheader('X-Docker-Endpoints') + registryendpoint =3D res.info().get('X-Docker-Endpoints') =20 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/temp= late.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. # =20 -import urlparse +import urllib.parse import importlib import re =20 @@ -71,7 +71,7 @@ class Template(object): classimpl =3D getattr(mod, classname) return classimpl() except Exception as e: - print e + print (e) raise Exception("Invalid source: '%s'" % source) =20 def get_source_impl(self): @@ -101,12 +101,12 @@ class Template(object): netloc =3D None =20 query =3D "&".join([key + "=3D" + self.params[key] for key in self= .params.keys()]) - ret =3D urlparse.urlunparse((scheme, netloc, self.path, None, quer= y, None)) + ret =3D urllib.parse.urlunparse((scheme, netloc, self.path, None, = query, None)) return ret =20 @classmethod def from_uri(klass, uri): - o =3D urlparse.urlparse(uri) + o =3D urllib.parse.urlparse(uri) =20 idx =3D o.scheme.find("+") if idx =3D=3D -1: --=20 2.15.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list