diff --git a/crit/crit b/crit/crit index 08e84575d..6adccc9f2 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 886394fd5..2cea94781 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 8e1749961..96b3e9526 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 e4e46b52b..85c7c07f8 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 a95f6120b..cb328ec4c 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 379943b97..ea87e4e7b 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 b706fd3b3..c17b76f0e 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 fe3dcf175..d99f3e13c 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 = [ @@ -150,8 +153,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 ), @@ -159,8 +162,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) @@ -170,7 +173,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 @@ -181,7 +184,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: @@ -215,7 +218,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: @@ -267,13 +270,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 319e99812..14a155a43 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':