tools/python: Python 3 compatibility

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().

In libxc.py, drop all long() conversion.  Python 2 will DTRT with int => long
promotion, even on 32bit builds.

In convert-legacy-stream, don't pass empty strings to write_record().  Join on
the empty argl will do the right thing.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Acked-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
This commit is contained in:
Andrew Cooper committed 2019-12-20 11:19:56 +00:00
1 parent 25164571fc
commit 1430c5a8ca
8 files changed
+73 -109

No files matched your search

+8 -34
View File
@@ -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,35 +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 """
@@ -168,7 +142,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)
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.