Commit 98cb5c62 authored by Adrian Reber's avatar Adrian Reber Committed by Pavel Emelyanov

crit: make crit python2/python3 compatible

Signed-off-by: 's avatarAdrian Reber <areber@redhat.com>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent e61309a7
#!/usr/bin/env python2 #!/usr/bin/env python2
from __future__ import print_function
import argparse import argparse
import sys import sys
import json import json
...@@ -8,7 +9,7 @@ import pycriu ...@@ -8,7 +9,7 @@ import pycriu
def inf(opts): def inf(opts):
if opts['in']: if opts['in']:
return open(opts['in'], 'r') return open(opts['in'], 'rb')
else: else:
return sys.stdin return sys.stdin
...@@ -27,9 +28,9 @@ def decode(opts): ...@@ -27,9 +28,9 @@ def decode(opts):
try: try:
img = pycriu.images.load(inf(opts), opts['pretty'], opts['nopl']) img = pycriu.images.load(inf(opts), opts['pretty'], opts['nopl'])
except pycriu.images.MagicException as exc: 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 "\ "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) sys.exit(1)
if opts['pretty']: if opts['pretty']:
...@@ -47,7 +48,7 @@ def encode(opts): ...@@ -47,7 +48,7 @@ def encode(opts):
def info(opts): def info(opts):
infs = pycriu.images.info(inf(opts)) infs = pycriu.images.info(inf(opts))
json.dump(infs, sys.stdout, indent = 4) json.dump(infs, sys.stdout, indent = 4)
print print()
def get_task_id(p, val): def get_task_id(p, val):
return p[val] if val in p else p['ns_' + val][0] return p[val] if val in p else p['ns_' + val][0]
...@@ -64,8 +65,8 @@ class ps_item: ...@@ -64,8 +65,8 @@ class ps_item:
self.kids = [] self.kids = []
def show_ps(p, opts, depth = 0): 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'), 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']) ' ' * (4 * depth), p.core['tc']['comm']))
for kid in p.kids: for kid in p.kids:
show_ps(kid, opts, depth + 1) show_ps(kid, opts, depth + 1)
...@@ -88,7 +89,7 @@ def explore_ps(opts): ...@@ -88,7 +89,7 @@ def explore_ps(opts):
pp = pss[p.ppid] pp = pss[p.ppid]
pp.kids.append(p) 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) show_ps(psr, opts)
files_img = None files_img = None
...@@ -169,13 +170,13 @@ def explore_fds(opts): ...@@ -169,13 +170,13 @@ def explore_fds(opts):
fdt = idi['entries'][0]['files_id'] fdt = idi['entries'][0]['files_id']
fdi = pycriu.images.load(dinf(opts, 'fdinfo-%d.img' % fdt)) fdi = pycriu.images.load(dinf(opts, 'fdinfo-%d.img' % fdt))
print "%d" % pid print("%d" % pid)
for fd in fdi['entries']: 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] 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" % ('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" % ('root', get_file_str(opts, {'type': 'REG', 'id': fdi['root_id']})))
class vma_id: class vma_id:
...@@ -199,8 +200,8 @@ def explore_mems(opts): ...@@ -199,8 +200,8 @@ def explore_mems(opts):
pid = get_task_id(p, 'pid') pid = get_task_id(p, 'pid')
mmi = pycriu.images.load(dinf(opts, 'mm-%d.img' % pid))['entries'][0] mmi = pycriu.images.load(dinf(opts, 'mm-%d.img' % pid))['entries'][0]
print "%d" % pid print("%d" % pid)
print "\t%-36s %s" % ('exe', get_file_str(opts, {'type': 'REG', 'id': mmi['exe_file_id']})) print("\t%-36s %s" % ('exe', get_file_str(opts, {'type': 'REG', 'id': mmi['exe_file_id']})))
for vma in mmi['vmas']: for vma in mmi['vmas']:
st = vma['status'] st = vma['status']
...@@ -235,7 +236,7 @@ def explore_mems(opts): ...@@ -235,7 +236,7 @@ def explore_mems(opts):
prot += vma['prot'] & 0x4 and 'x' or '-' prot += vma['prot'] & 0x4 and 'x' or '-'
astr = '%08lx-%08lx' % (vma['start'], vma['end']) 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): def explore_rss(opts):
...@@ -245,7 +246,7 @@ 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'] vmas = pycriu.images.load(dinf(opts, 'mm-%d.img' % pid))['entries'][0]['vmas']
pms = pycriu.images.load(dinf(opts, 'pagemap-%d.img' % pid))['entries'] pms = pycriu.images.load(dinf(opts, 'pagemap-%d.img' % pid))['entries']
print "%d" % pid print("%d" % pid)
vmi = 0 vmi = 0
pvmi = -1 pvmi = -1
for pm in pms[1:]: for pm in pms[1:]:
...@@ -269,7 +270,7 @@ def explore_rss(opts): ...@@ -269,7 +270,7 @@ def explore_rss(opts):
vmi -= 1 vmi -= 1
print '%-24s%s' % (pstr, vstr) print('%-24s%s' % (pstr, vstr))
......
...@@ -6,6 +6,7 @@ REQ-RPM-PKG-NAMES += protobuf-devel ...@@ -6,6 +6,7 @@ REQ-RPM-PKG-NAMES += protobuf-devel
REQ-RPM-PKG-NAMES += protobuf-python REQ-RPM-PKG-NAMES += protobuf-python
REQ-RPM-PKG-NAMES += libnl3-devel REQ-RPM-PKG-NAMES += libnl3-devel
REQ-RPM-PKG-NAMES += libcap-devel REQ-RPM-PKG-NAMES += libcap-devel
REQ-RPM-PKG-NAMES += $(PYTHON)-future
REQ-RPM-PKG-TEST-NAMES += libaio-devel REQ-RPM-PKG-TEST-NAMES += libaio-devel
...@@ -17,7 +18,19 @@ REQ-DEB-PKG-NAMES += python-protobuf ...@@ -17,7 +18,19 @@ REQ-DEB-PKG-NAMES += python-protobuf
REQ-DEB-PKG-NAMES += libnl-3-dev REQ-DEB-PKG-NAMES += libnl-3-dev
REQ-DEB-PKG-NAMES += libcap-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 export LIBS += -lrt -lpthread -lprotobuf-c -ldl -lnl-3 -lsoccr -Lsoccr/ -lnet
......
import rpc_pb2 as rpc from . import rpc_pb2 as rpc
import images from . import images
from criu import * from .criu import *
...@@ -9,7 +9,7 @@ import signal ...@@ -9,7 +9,7 @@ import signal
import sys import sys
import struct import struct
import rpc_pb2 as rpc import pycriu.rpc_pb2 as rpc
class _criu_comm: class _criu_comm:
""" """
......
...@@ -17,7 +17,7 @@ magic.py: scripts/magic-gen.py criu/include/magic.h ...@@ -17,7 +17,7 @@ magic.py: scripts/magic-gen.py criu/include/magic.h
pb.py: images pb.py: images
$(Q) echo "# Autogenerated. Do not edit!" > $(obj)/$@ $(Q) echo "# Autogenerated. Do not edit!" > $(obj)/$@
$(Q) for m in $(proto-py-modules); do \ $(Q) for m in $(proto-py-modules); do \
echo "from $$m import *" >> $(obj)/$@ ;\ echo "from .$$m import *" >> $(obj)/$@ ;\
done done
.PHONY: pb.py .PHONY: pb.py
......
from magic import * import sys, os
from images import * sys.path.append(os.path.dirname(os.path.realpath(__file__)))
from pb import * from .magic import *
from .images import *
from .pb import *
...@@ -43,11 +43,11 @@ import struct ...@@ -43,11 +43,11 @@ import struct
import os import os
import sys import sys
import json import json
import pb2dict from . import pb2dict
import array import array
import magic from . import magic
from pb import * from .pb import *
# #
# Predefined hardcoded constants # Predefined hardcoded constants
...@@ -91,7 +91,7 @@ class entry_handler: ...@@ -91,7 +91,7 @@ class entry_handler:
# Read payload # Read payload
pb = self.payload() pb = self.payload()
buf = f.read(4) buf = f.read(4)
if buf == '': if buf == b'':
break break
size, = struct.unpack('i', buf) size, = struct.unpack('i', buf)
pb.ParseFromString(f.read(size)) pb.ParseFromString(f.read(size))
......
from google.protobuf.descriptor import FieldDescriptor as FD from google.protobuf.descriptor import FieldDescriptor as FD
import opts_pb2 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 socket
import collections import collections
import os import os
...@@ -39,7 +42,7 @@ _basic_cast = { ...@@ -39,7 +42,7 @@ _basic_cast = {
FD.TYPE_BOOL : bool, FD.TYPE_BOOL : bool,
FD.TYPE_STRING : unicode FD.TYPE_STRING : str
} }
def _marked_as_hex(field): def _marked_as_hex(field):
...@@ -98,11 +101,11 @@ mmap_status_map = [ ...@@ -98,11 +101,11 @@ mmap_status_map = [
]; ];
rfile_flags_map = [ rfile_flags_map = [
('O_WRONLY', 01), ('O_WRONLY', 0o1),
('O_RDWR', 02), ('O_RDWR', 0o2),
('O_APPEND', 02000), ('O_APPEND', 0o2000),
('O_DIRECT', 040000), ('O_DIRECT', 0o40000),
('O_LARGEFILE', 0100000), ('O_LARGEFILE', 0o100000),
]; ];
pmap_flags_map = [ pmap_flags_map = [
...@@ -150,8 +153,8 @@ sk_maps = { ...@@ -150,8 +153,8 @@ sk_maps = {
136: 'UDPLITE' }, 136: 'UDPLITE' },
} }
gen_rmaps = { k: {v2:k2 for k2,v2 in v.items()} for k,v in gen_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 v.items()} for k,v in sk_maps.items() } sk_rmaps = { k: {v2:k2 for k2,v2 in list(v.items())} for k,v in list(sk_maps.items()) }
dict_maps = { dict_maps = {
'gen' : ( gen_maps, gen_rmaps ), 'gen' : ( gen_maps, gen_rmaps ),
...@@ -159,8 +162,8 @@ dict_maps = { ...@@ -159,8 +162,8 @@ dict_maps = {
} }
def map_flags(value, flags_map): def map_flags(value, flags_map):
bs = map(lambda x: x[0], filter(lambda x: value & x[1], flags_map)) bs = [x[0] for x in [x for x in flags_map if value & x[1]]]
value &= ~sum(map(lambda x: x[1], flags_map)) value &= ~sum([x[1] for x in flags_map])
if value: if value:
bs.append("0x%x" % value) bs.append("0x%x" % value)
return " | ".join(bs) return " | ".join(bs)
...@@ -170,7 +173,7 @@ def unmap_flags(value, flags_map): ...@@ -170,7 +173,7 @@ def unmap_flags(value, flags_map):
return 0 return 0
bd = dict(flags_map) 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 kern_minorbits = 20 # This is how kernel encodes dev_t in new format
...@@ -181,7 +184,7 @@ def decode_dev(field, value): ...@@ -181,7 +184,7 @@ def decode_dev(field, value):
return "%d:%d" % (value >> kern_minorbits, value & ((1 << kern_minorbits) - 1)) return "%d:%d" % (value >> kern_minorbits, value & ((1 << kern_minorbits) - 1))
def encode_dev(field, value): 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): if _marked_as_odev(field):
return os.makedev(dev[0], dev[1]) return os.makedev(dev[0], dev[1])
else: else:
...@@ -215,7 +218,7 @@ def get_bytes_dec(field): ...@@ -215,7 +218,7 @@ def get_bytes_dec(field):
return decode_base64 return decode_base64
def is_string(value): 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): def _pb2dict_cast(field, value, pretty = False, is_hex = False):
if not is_hex: if not is_hex:
...@@ -267,13 +270,13 @@ def pb2dict(pb, pretty = False, is_hex = False): ...@@ -267,13 +270,13 @@ def pb2dict(pb, pretty = False, is_hex = False):
if pretty and _marked_as_ip(field): if pretty and _marked_as_ip(field):
if len(value) == 1: if len(value) == 1:
v = socket.ntohl(value[0]) v = socket.ntohl(value[0])
addr = ipaddr.IPv4Address(v) addr = IPv4Address(v)
else: else:
v = 0 + (socket.ntohl(value[0]) << (32 * 3)) + \ v = 0 + (socket.ntohl(value[0]) << (32 * 3)) + \
(socket.ntohl(value[1]) << (32 * 2)) + \ (socket.ntohl(value[1]) << (32 * 2)) + \
(socket.ntohl(value[2]) << (32 * 1)) + \ (socket.ntohl(value[2]) << (32 * 1)) + \
(socket.ntohl(value[3])) (socket.ntohl(value[3]))
addr = ipaddr.IPv6Address(v) addr = IPv6Address(v)
d_val.append(addr.compressed) d_val.append(addr.compressed)
else: else:
......
...@@ -44,7 +44,7 @@ def main(argv): ...@@ -44,7 +44,7 @@ def main(argv):
out.write('#Autogenerated. Do not edit!\n') out.write('#Autogenerated. Do not edit!\n')
out.write('by_name = {}\n') out.write('by_name = {}\n')
out.write('by_val = {}\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 # We don't need RAW or V1 magic, because
# they can't be used to identify images. # they can't be used to identify images.
if v == '0x0' or v == '1' or k == '0x0' or v == '1': if v == '0x0' or v == '1' or k == '0x0' or v == '1':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment