From patchwork Wed Dec 18 15:05:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 11300991 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F00406C1 for ; Wed, 18 Dec 2019 15:06:59 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C01F2218AC for ; Wed, 18 Dec 2019 15:06:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=citrix.com header.i=@citrix.com header.b="HAuk4/Ej" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C01F2218AC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=citrix.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1ihatN-00079p-Oo; Wed, 18 Dec 2019 15:05:37 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1ihatM-00079k-UU for xen-devel@lists.xenproject.org; Wed, 18 Dec 2019 15:05:36 +0000 X-Inumbo-ID: d31744d0-21a7-11ea-88e7-bc764e2007e4 Received: from esa4.hc3370-68.iphmx.com (unknown [216.71.155.144]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id d31744d0-21a7-11ea-88e7-bc764e2007e4; Wed, 18 Dec 2019 15:05:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1576681527; h=from:to:cc:subject:date:message-id:mime-version; bh=5aT7trezLy5JRg+JZ+LxxxyojXrsqG5YzzzHDl9g6WI=; b=HAuk4/EjhS4Wp+tr+rVYDwugf6nYmnjd5PqnJHqlb9fSPCXFJpG8YnOs 4bTa3EkdLFqIK18Te+MsidnlSjLAJXxBwA3pnVdX998uOMxiWsIip9GaR MmyZbqKtxrHVFC86ydfkHKq4Mcay3O1qbgGHAZGCn9FPwi5NbhB7IyV49 g=; Authentication-Results: esa4.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=andrew.cooper3@citrix.com; spf=Pass smtp.mailfrom=Andrew.Cooper3@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: None (esa4.hc3370-68.iphmx.com: no sender authenticity information available from domain of andrew.cooper3@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="andrew.cooper3@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa4.hc3370-68.iphmx.com: domain of Andrew.Cooper3@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="Andrew.Cooper3@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ip4:168.245.78.127 ~all" Received-SPF: None (esa4.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: rtZ/od3WehVj9CPuL+fMoMhTfHnQ4aI2kd2G3cyN+eml6PLiMNNlEtt5fjEVJiQgIyvPfNN/rw K3UfTHC3Br9WR+piYPHJ8XKKjDkTCU/zBvte/4dPxbeUeoizA97FhydsoyULobsRtq2L01oHVb ENmP5zH9a4wWtCpYi/Oy68mk/alSJJRgr/A9Ze41YYXKjZEu852tmS9JwiHOf6b3hB1Fm/ngyS ysd4wEhioB2d4SSAK+HV89sPWBJiEb0sJP+Fe8I0J+1bsisHCczOzRLQHSiDUcvlvbhzieESLv VbM= X-SBRS: 2.7 X-MesageID: 10464680 X-Ironport-Server: esa4.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.69,329,1571716800"; d="scan'208";a="10464680" From: Andrew Cooper To: Xen-devel Date: Wed, 18 Dec 2019 15:05:22 +0000 Message-ID: <20191218150522.8697-1-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 Subject: [Xen-devel] [PATCH] tools/python: Python 3 compatibility X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , Wei Liu , Ian Jackson Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" convert-legacy-stream is only used for incomming migration from pre Xen 4.7, and verify-stream-v2 appears to only be used by me during migration development - it is little surprise that they missed the main converstion effort in Xen 4.13. Fix it all up. Move open_file_or_fd() into a new util.py to avoid duplication, making it a more generic wrapper around open() or fdopen(). Signed-off-by: Andrew Cooper --- CC: Ian Jackson CC: Wei Liu This needs backporting to 4.13 ASAP --- tools/python/scripts/convert-legacy-stream | 49 +++++++----------------------- tools/python/scripts/verify-stream-v2 | 43 +++++--------------------- tools/python/xen/migration/libxc.py | 2 +- tools/python/xen/migration/libxl.py | 2 +- tools/python/xen/migration/verify.py | 4 +-- tools/python/xen/util.py | 23 ++++++++++++++ 6 files changed, 46 insertions(+), 77 deletions(-) create mode 100644 tools/python/xen/util.py diff --git a/tools/python/scripts/convert-legacy-stream b/tools/python/scripts/convert-legacy-stream index 5f80f13654..b0d81aa92e 100755 --- a/tools/python/scripts/convert-legacy-stream +++ b/tools/python/scripts/convert-legacy-stream @@ -5,6 +5,8 @@ Convert a legacy migration stream to a v2 stream. """ +from __future__ import print_function + import sys import os, os.path import syslog @@ -12,6 +14,7 @@ import traceback from struct import calcsize, unpack, pack +from xen.util import open_file_or_fd as open_file_or_fd from xen.migration import legacy, public, libxc, libxl, xl __version__ = 1 @@ -39,16 +42,16 @@ def info(msg): for line in msg.split("\n"): syslog.syslog(syslog.LOG_INFO, line) else: - print msg + print(msg) def err(msg): """Error message, routed to appropriate destination""" if log_to_syslog: for line in msg.split("\n"): syslog.syslog(syslog.LOG_ERR, line) - print >> sys.stderr, msg + print(msg, file = sys.stderr) -class StreamError(StandardError): +class StreamError(Exception): """Error with the incoming migration stream""" pass @@ -70,7 +73,7 @@ class VM(object): # libxl self.libxl = fmt == "libxl" - self.emu_xenstore = "" # NUL terminated key&val pairs from "toolstack" records + self.emu_xenstore = b"" # NUL terminated key&val pairs from "toolstack" records def write_libxc_ihdr(): stream_write(pack(libxc.IHDR_FORMAT, @@ -336,7 +339,7 @@ def read_libxl_toolstack(vm, data): if twidth == 64: name = name[:-4] - if name[-1] != '\x00': + if name[-1] != b'\x00': raise StreamError("physmap name not NUL terminated") root = "physmap/%x" % (phys,) @@ -347,7 +350,7 @@ def read_libxl_toolstack(vm, data): for key, val in zip(kv[0::2], kv[1::2]): info(" '%s' = '%s'" % (key, val)) - vm.emu_xenstore += '\x00'.join(kv) + '\x00' + vm.emu_xenstore += b'\x00'.join(kv) + b'\x00' def read_chunks(vm): @@ -534,7 +537,7 @@ def read_qemu(vm): sig, = unpack("21s", rawsig) info("Qemu signature: %s" % (sig, )) - if sig == "DeviceModelRecord0002": + if sig == b"DeviceModelRecord0002": rawsz = rdexact(4) sz, = unpack("I", rawsz) qdata = rdexact(sz) @@ -617,36 +620,6 @@ def read_legacy_stream(vm): return 2 return 0 -def open_file_or_fd(val, mode): - """ - If 'val' looks like a decimal integer, open it as an fd. If not, try to - open it as a regular file. - """ - - fd = -1 - try: - # Does it look like an integer? - try: - fd = int(val, 10) - except ValueError: - pass - - # Try to open it... - if fd != -1: - return os.fdopen(fd, mode, 0) - else: - return open(val, mode, 0) - - except StandardError, e: - if fd != -1: - err("Unable to open fd %d: %s: %s" % - (fd, e.__class__.__name__, e)) - else: - err("Unable to open file '%s': %s: %s" % - (val, e.__class__.__name__, e)) - - raise SystemExit(1) - def main(): from optparse import OptionParser @@ -723,7 +696,7 @@ def main(): if __name__ == "__main__": try: sys.exit(main()) - except SystemExit, e: + except SystemExit as e: sys.exit(e.code) except KeyboardInterrupt: sys.exit(1) diff --git a/tools/python/scripts/verify-stream-v2 b/tools/python/scripts/verify-stream-v2 index 3daf25791e..8355c2d206 100755 --- a/tools/python/scripts/verify-stream-v2 +++ b/tools/python/scripts/verify-stream-v2 @@ -3,12 +3,15 @@ """ Verify a v2 format migration stream """ +from __future__ import print_function + import sys import struct import os, os.path import syslog import traceback +from xen.util import open_file_or_fd as open_file_or_fd from xen.migration.verify import StreamError, RecordError from xen.migration.libxc import VerifyLibxc from xen.migration.libxl import VerifyLibxl @@ -25,7 +28,7 @@ def info(msg): for line in msg.split("\n"): syslog.syslog(syslog.LOG_INFO, line) else: - print msg + print(msg) def err(msg): """Error message, routed to appropriate destination""" @@ -33,7 +36,7 @@ def err(msg): if log_to_syslog: for line in msg.split("\n"): syslog.syslog(syslog.LOG_ERR, line) - print >> sys.stderr, msg + print(msg, file = sys.stderr) def stream_read(_ = None): """Read from input""" @@ -56,7 +59,7 @@ def skip_xl_header(): """Skip over an xl header in the stream""" hdr = rdexact(32) - if hdr != "Xen saved domain, xl format\n \0 \r": + if hdr != b"Xen saved domain, xl format\n \0 \r": raise StreamError("No xl header") _, mflags, _, optlen = unpack_exact("=IIII") @@ -86,7 +89,7 @@ def read_stream(fmt): err(traceback.format_exc()) return 1 - except StandardError: + except Exception: err("Script Error:") err(traceback.format_exc()) err("Please fix me") @@ -94,36 +97,6 @@ def read_stream(fmt): return 0 -def open_file_or_fd(val, mode, buffering): - """ - If 'val' looks like a decimal integer, open it as an fd. If not, try to - open it as a regular file. - """ - - fd = -1 - try: - # Does it look like an integer? - try: - fd = int(val, 10) - except ValueError: - pass - - # Try to open it... - if fd != -1: - return os.fdopen(fd, mode, buffering) - else: - return open(val, mode, buffering) - - except StandardError, e: - if fd != -1: - err("Unable to open fd %d: %s: %s" % - (fd, e.__class__.__name__, e)) - else: - err("Unable to open file '%s': %s: %s" % - (val, e.__class__.__name__, e)) - - raise SystemExit(2) - def main(): """ main """ from optparse import OptionParser @@ -168,7 +141,7 @@ def main(): if __name__ == "__main__": try: sys.exit(main()) - except SystemExit, e: + except SystemExit as e: sys.exit(e.code) except KeyboardInterrupt: sys.exit(2) diff --git a/tools/python/xen/migration/libxc.py b/tools/python/xen/migration/libxc.py index f24448a9ef..cbffd1975e 100644 --- a/tools/python/xen/migration/libxc.py +++ b/tools/python/xen/migration/libxc.py @@ -223,7 +223,7 @@ def verify_record(self): self.squashed_pagedata_records += 1 padding = content[length:] - if padding != "\x00" * len(padding): + if padding != b"\x00" * len(padding): raise StreamError("Padding containing non0 bytes found") if rtype not in record_verifiers: diff --git a/tools/python/xen/migration/libxl.py b/tools/python/xen/migration/libxl.py index d5f54dc489..79f4024e72 100644 --- a/tools/python/xen/migration/libxl.py +++ b/tools/python/xen/migration/libxl.py @@ -128,7 +128,7 @@ def verify_record(self): content = self.rdexact(contentsz) padding = content[length:] - if padding != "\x00" * len(padding): + if padding != b"\x00" * len(padding): raise StreamError("Padding containing non0 bytes found") if rtype not in record_verifiers: diff --git a/tools/python/xen/migration/verify.py b/tools/python/xen/migration/verify.py index 7a42dbfc58..1e38f4a3c0 100644 --- a/tools/python/xen/migration/verify.py +++ b/tools/python/xen/migration/verify.py @@ -7,11 +7,11 @@ from struct import calcsize, unpack -class StreamError(StandardError): +class StreamError(Exception): """Error with the stream""" pass -class RecordError(StandardError): +class RecordError(Exception): """Error with a record in the stream""" pass diff --git a/tools/python/xen/util.py b/tools/python/xen/util.py new file mode 100644 index 0000000000..a11358eefa --- /dev/null +++ b/tools/python/xen/util.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import os + +def open_file_or_fd(val, *argl, **kwargs): + """ + If 'val' looks like a decimal integer, open it as an fd. If not, try to + open it as a regular file. + """ + + fd = -1 + try: + # Does it look like an integer? + fd = int(val, 10) + except ValueError: + pass + + # Try to open it... + if fd != -1: + return os.fdopen(fd, *argl, **kwargs) + else: + return open(val, *argl, **kwargs)