[CRIU] [PATCH v3 1/5] crit: make crit python2/python3 compatible

Adrian Reber adrian at lisas.de
Wed May 16 09:20:22 MSK 2018


From: Adrian Reber <areber at redhat.com>

Signed-off-by: Adrian Reber <areber at redhat.com>
---
 crit/crit                 | 33 +++++++++++++++++----------------
 criu/Makefile.packages    | 15 ++++++++++++++-
 lib/py/__init__.py        |  6 +++---
 lib/py/criu.py            |  2 +-
 lib/py/images/Makefile    |  2 +-
 lib/py/images/__init__.py |  8 +++++---
 lib/py/images/images.py   |  8 ++++----
 lib/py/images/pb2dict.py  | 35 +++++++++++++++++++----------------
 scripts/magic-gen.py      |  2 +-
 9 files changed, 65 insertions(+), 46 deletions(-)

diff --git a/crit/crit b/crit/crit
index 08e8457..6adccc9 100755
--- a/crit/crit
+++ b/crit/crit
@@ -1,4 +1,5 @@
 #!/usr/bin/env python2
+from __future__ import print_function
 import argparse
 import sys
 import json
@@ -8,7 +9,7 @@ import pycriu
 
 def inf(opts):
 	if opts['in']:
-		return open(opts['in'], 'r')
+		return open(opts['in'], 'rb')
 	else:
 		return sys.stdin
 
@@ -27,9 +28,9 @@ def decode(opts):
 	try:
 		img = pycriu.images.load(inf(opts), opts['pretty'], opts['nopl'])
 	except pycriu.images.MagicException as exc:
-		print >>sys.stderr, "Unknown magic %#x.\n"\
+		print("Unknown magic %#x.\n"\
 				"Maybe you are feeding me an image with "\
-				"raw data(i.e. pages.img)?" % exc.magic
+				"raw data(i.e. pages.img)?" % exc.magic, file=sys.stderr)
 		sys.exit(1)
 
 	if opts['pretty']:
@@ -47,7 +48,7 @@ def encode(opts):
 def info(opts):
 	infs = pycriu.images.info(inf(opts))
 	json.dump(infs, sys.stdout, indent = 4)
-	print
+	print()
 
 def get_task_id(p, val):
 	return p[val] if val in p else p['ns_' + val][0]
@@ -64,8 +65,8 @@ class ps_item:
 		self.kids = []
 
 def show_ps(p, opts, depth = 0):
-	print "%7d%7d%7d   %s%s" % (p.pid, get_task_id(p.p, 'pgid'), get_task_id(p.p, 'sid'),
-			' ' * (4 * depth), p.core['tc']['comm'])
+	print("%7d%7d%7d   %s%s" % (p.pid, get_task_id(p.p, 'pgid'), get_task_id(p.p, 'sid'),
+			' ' * (4 * depth), p.core['tc']['comm']))
 	for kid in p.kids:
 		show_ps(kid, opts, depth + 1)
 
@@ -88,7 +89,7 @@ def explore_ps(opts):
 		pp = pss[p.ppid]
 		pp.kids.append(p)
 
-	print "%7s%7s%7s   %s" % ('PID', 'PGID', 'SID', 'COMM')
+	print("%7s%7s%7s   %s" % ('PID', 'PGID', 'SID', 'COMM'))
 	show_ps(psr, opts)
 
 files_img = None
@@ -169,13 +170,13 @@ def explore_fds(opts):
 		fdt = idi['entries'][0]['files_id']
 		fdi = pycriu.images.load(dinf(opts, 'fdinfo-%d.img' % fdt))
 
-		print "%d" % pid
+		print("%d" % pid)
 		for fd in fdi['entries']:
-			print "\t%7d: %s" % (fd['fd'], get_file_str(opts, fd))
+			print("\t%7d: %s" % (fd['fd'], get_file_str(opts, fd)))
 
 		fdi = pycriu.images.load(dinf(opts, 'fs-%d.img' % pid))['entries'][0]
-		print "\t%7s: %s" % ('cwd', get_file_str(opts, {'type': 'REG', 'id': fdi['cwd_id']}))
-		print "\t%7s: %s" % ('root', get_file_str(opts, {'type': 'REG', 'id': fdi['root_id']}))
+		print("\t%7s: %s" % ('cwd', get_file_str(opts, {'type': 'REG', 'id': fdi['cwd_id']})))
+		print("\t%7s: %s" % ('root', get_file_str(opts, {'type': 'REG', 'id': fdi['root_id']})))
 
 
 class vma_id:
@@ -199,8 +200,8 @@ def explore_mems(opts):
 		pid = get_task_id(p, 'pid')
 		mmi = pycriu.images.load(dinf(opts, 'mm-%d.img' % pid))['entries'][0]
 
-		print "%d" % pid
-		print "\t%-36s    %s" % ('exe', get_file_str(opts, {'type': 'REG', 'id': mmi['exe_file_id']}))
+		print("%d" % pid)
+		print("\t%-36s    %s" % ('exe', get_file_str(opts, {'type': 'REG', 'id': mmi['exe_file_id']})))
 
 		for vma in mmi['vmas']:
 			st = vma['status']
@@ -235,7 +236,7 @@ def explore_mems(opts):
 			prot += vma['prot'] & 0x4 and 'x' or '-'
 
 			astr = '%08lx-%08lx' % (vma['start'], vma['end'])
-			print "\t%-36s%s%s" % (astr, prot, fn)
+			print("\t%-36s%s%s" % (astr, prot, fn))
 
 
 def explore_rss(opts):
@@ -245,7 +246,7 @@ def explore_rss(opts):
 		vmas = pycriu.images.load(dinf(opts, 'mm-%d.img' % pid))['entries'][0]['vmas']
 		pms = pycriu.images.load(dinf(opts, 'pagemap-%d.img' % pid))['entries']
 
-		print "%d" % pid
+		print("%d" % pid)
 		vmi = 0
 		pvmi = -1
 		for pm in pms[1:]:
@@ -269,7 +270,7 @@ def explore_rss(opts):
 
 			vmi -= 1
 
-			print '%-24s%s' % (pstr, vstr)
+			print('%-24s%s' % (pstr, vstr))
 
 
 
diff --git a/criu/Makefile.packages b/criu/Makefile.packages
index 886394f..2cea947 100644
--- a/criu/Makefile.packages
+++ b/criu/Makefile.packages
@@ -6,6 +6,7 @@ REQ-RPM-PKG-NAMES	+= protobuf-devel
 REQ-RPM-PKG-NAMES	+= protobuf-python
 REQ-RPM-PKG-NAMES	+= libnl3-devel
 REQ-RPM-PKG-NAMES	+= libcap-devel
+REQ-RPM-PKG-NAMES	+= $(PYTHON)-future
 
 REQ-RPM-PKG-TEST-NAMES  += libaio-devel
 
@@ -17,7 +18,19 @@ REQ-DEB-PKG-NAMES	+= python-protobuf
 REQ-DEB-PKG-NAMES	+= libnl-3-dev
 REQ-DEB-PKG-NAMES	+= libcap-dev
 
-REQ-DEB-PKG-TEST-NAMES  += libaio-dev
+REQ-DEB-PKG-TEST-NAMES	+= python-yaml
+REQ-DEB-PKG-TEST-NAMES	+= libaio-dev
+
+ifeq ($(PYTHON),python3)
+REQ-DEB-PKG-NAMES	+= $(PYTHON)-future
+REQ-DEB-PKG-TEST-NAMES	+= libaio-dev
+
+REQ-RPM-PKG-TEST-NAMES	+= $(PYTHON)-PyYAML
+else
+REQ-DEB-PKG-NAMES	+= python-future
+
+REQ-RPM-PKG-TEST-NAMES	+= $(PYTHON)-pyyaml
+endif
 
 export LIBS		+= -lrt -lpthread -lprotobuf-c -ldl -lnl-3 -lsoccr -Lsoccr/ -lnet
 
diff --git a/lib/py/__init__.py b/lib/py/__init__.py
index 8e17499..96b3e95 100644
--- a/lib/py/__init__.py
+++ b/lib/py/__init__.py
@@ -1,3 +1,3 @@
-import rpc_pb2 as rpc
-import images
-from criu import *
+from . import rpc_pb2 as rpc
+from . import images
+from .criu import *
diff --git a/lib/py/criu.py b/lib/py/criu.py
index e4e46b5..85c7c07 100644
--- a/lib/py/criu.py
+++ b/lib/py/criu.py
@@ -9,7 +9,7 @@ import signal
 import sys
 import struct
 
-import rpc_pb2 as rpc
+import pycriu.rpc_pb2 as rpc
 
 class _criu_comm:
 	"""
diff --git a/lib/py/images/Makefile b/lib/py/images/Makefile
index a95f612..cb328ec 100644
--- a/lib/py/images/Makefile
+++ b/lib/py/images/Makefile
@@ -17,7 +17,7 @@ magic.py: scripts/magic-gen.py criu/include/magic.h
 pb.py: images
 	$(Q) echo "# Autogenerated. Do not edit!" > $(obj)/$@
 	$(Q) for m in $(proto-py-modules); do \
-		echo "from $$m import *" >> $(obj)/$@ ;\
+		echo "from .$$m import *" >> $(obj)/$@ ;\
 	done
 .PHONY: pb.py
 
diff --git a/lib/py/images/__init__.py b/lib/py/images/__init__.py
index 379943b..ea87e4e 100644
--- a/lib/py/images/__init__.py
+++ b/lib/py/images/__init__.py
@@ -1,3 +1,5 @@
-from magic import *
-from images import *
-from pb import *
+import sys, os
+sys.path.append(os.path.dirname(os.path.realpath(__file__)))
+from .magic import *
+from .images import *
+from .pb import *
diff --git a/lib/py/images/images.py b/lib/py/images/images.py
index 60f2bdc..3ad8550 100644
--- a/lib/py/images/images.py
+++ b/lib/py/images/images.py
@@ -43,11 +43,11 @@ import struct
 import os
 import sys
 import json
-import pb2dict
+from . import pb2dict
 import array
 
-import magic
-from pb import *
+from . import magic
+from .pb import *
 
 #
 # Predefined hardcoded constants
@@ -91,7 +91,7 @@ class entry_handler:
 			# Read payload
 			pb = self.payload()
 			buf = f.read(4)
-			if buf == '':
+			if buf == b'':
 				break
 			size, = struct.unpack('i', buf)
 			pb.ParseFromString(f.read(size))
diff --git a/lib/py/images/pb2dict.py b/lib/py/images/pb2dict.py
index 5f098f8..441dacf 100644
--- a/lib/py/images/pb2dict.py
+++ b/lib/py/images/pb2dict.py
@@ -1,6 +1,9 @@
 from google.protobuf.descriptor import FieldDescriptor as FD
 import opts_pb2
-import ipaddr
+from builtins import str
+from past.builtins import long
+from ipaddress import IPv4Address
+from ipaddress import IPv6Address
 import socket
 import collections
 import os
@@ -39,7 +42,7 @@ _basic_cast = {
 
 	FD.TYPE_BOOL		: bool,
 
-	FD.TYPE_STRING		: unicode
+	FD.TYPE_STRING		: str
 }
 
 def _marked_as_hex(field):
@@ -98,11 +101,11 @@ mmap_status_map = [
 ];
 
 rfile_flags_map = [
-	('O_WRONLY',	01),
-	('O_RDWR',	02),
-	('O_APPEND',	02000),
-	('O_DIRECT',	040000),
-	('O_LARGEFILE',	0100000),
+	('O_WRONLY',	0o1),
+	('O_RDWR',	0o2),
+	('O_APPEND',	0o2000),
+	('O_DIRECT',	0o40000),
+	('O_LARGEFILE',	0o100000),
 ];
 
 pmap_flags_map = [
@@ -161,8 +164,8 @@ sk_maps = {
 			136: 'UDPLITE' },
 }
 
-gen_rmaps = { k: {v2:k2 for k2,v2 in v.items()} for k,v in gen_maps.items() }
-sk_rmaps = { k: {v2:k2 for k2,v2 in v.items()} for k,v in sk_maps.items() }
+gen_rmaps = { k: {v2:k2 for k2,v2 in list(v.items())} for k,v in list(gen_maps.items()) }
+sk_rmaps = { k: {v2:k2 for k2,v2 in list(v.items())} for k,v in list(sk_maps.items()) }
 
 dict_maps = {
 	'gen' : ( gen_maps, gen_rmaps ),
@@ -170,8 +173,8 @@ dict_maps = {
 }
 
 def map_flags(value, flags_map):
-	bs = map(lambda x: x[0], filter(lambda x: value & x[1], flags_map))
-	value &= ~sum(map(lambda x: x[1], flags_map))
+	bs = [x[0] for x in [x for x in flags_map if value & x[1]]]
+	value &= ~sum([x[1] for x in flags_map])
 	if value:
 		bs.append("0x%x" % value)
 	return " | ".join(bs)
@@ -181,7 +184,7 @@ def unmap_flags(value, flags_map):
 		return 0
 
 	bd = dict(flags_map)
-	return sum(map(lambda x: int(str(bd.get(x, x)), 0), map(lambda x: x.strip(), value.split('|'))))
+	return sum([int(str(bd.get(x, x)), 0) for x in [x.strip() for x in value.split('|')]])
 
 kern_minorbits = 20 # This is how kernel encodes dev_t in new format
 
@@ -192,7 +195,7 @@ def decode_dev(field, value):
 		return "%d:%d" % (value >> kern_minorbits, value & ((1 << kern_minorbits) - 1))
 
 def encode_dev(field, value):
-	dev = map(lambda x: int(x), value.split(':'))
+	dev = [int(x) for x in value.split(':')]
 	if _marked_as_odev(field):
 		return os.makedev(dev[0], dev[1])
 	else:
@@ -226,7 +229,7 @@ def get_bytes_dec(field):
 		return decode_base64
 
 def is_string(value):
-	return isinstance(value, unicode) or isinstance(value, str)
+	return isinstance(value, str)
 
 def _pb2dict_cast(field, value, pretty = False, is_hex = False):
 	if not is_hex:
@@ -278,13 +281,13 @@ def pb2dict(pb, pretty = False, is_hex = False):
 			if pretty and _marked_as_ip(field):
 				if len(value) == 1:
 					v = socket.ntohl(value[0])
-					addr = ipaddr.IPv4Address(v)
+					addr = IPv4Address(v)
 				else:
 					v = 0 +	(socket.ntohl(value[0]) << (32 * 3)) + \
 						(socket.ntohl(value[1]) << (32 * 2)) + \
 						(socket.ntohl(value[2]) << (32 * 1)) + \
 						(socket.ntohl(value[3]))
-					addr = ipaddr.IPv6Address(v)
+					addr = IPv6Address(v)
 
 				d_val.append(addr.compressed)
 			else:
diff --git a/scripts/magic-gen.py b/scripts/magic-gen.py
index 319e998..14a155a 100755
--- a/scripts/magic-gen.py
+++ b/scripts/magic-gen.py
@@ -44,7 +44,7 @@ def main(argv):
 	out.write('#Autogenerated. Do not edit!\n')
 	out.write('by_name = {}\n')
 	out.write('by_val = {}\n')
-	for k,v in magic.items():
+	for k,v in list(magic.items()):
 		# We don't need RAW or V1 magic, because
 		# they can't be used to identify images.
 		if v == '0x0' or v == '1' or k == '0x0' or v == '1':
-- 
1.8.3.1



More information about the CRIU mailing list