mirror of
https://github.com/checkpoint-restore/criu.git
synced 2026-07-17 16:47:50 +00:00
I plan to mark some fields as IP address and print them respectively. The --format hex is not nice switch for this and introducing one more (--format hex ipadd) is too bad. So let's fix the cirt API to be simple and stupid. By default crit generates canonical one-line JSON. With --pretty option it splits the output into lines, adds indentation and prints hex as hex and IP as IP. Signed-off-by: Pavel Emelyanov <xemul@parallels.com> Acked-by: Ruslan Kuprieiev <kupruser@gmail.com>
68 lines
1.4 KiB
Python
Executable file
68 lines
1.4 KiB
Python
Executable file
#!/usr/bin/env python
|
|
import argparse
|
|
import sys
|
|
import json
|
|
|
|
import pycriu
|
|
|
|
def handle_cmdline_opts():
|
|
desc = 'CRiu Image Tool'
|
|
parser = argparse.ArgumentParser(description=desc, formatter_class=argparse.RawTextHelpFormatter)
|
|
parser.add_argument('command',
|
|
choices = ['decode', 'encode'],
|
|
help = 'decode/encode - convert criu image from/to binary type to/from json')
|
|
parser.add_argument('-i',
|
|
'--in',
|
|
help = 'input file (stdin by default)')
|
|
parser.add_argument('-o',
|
|
'--out',
|
|
help = 'output file (stdout by default)')
|
|
parser.add_argument('--pretty', help = 'Multiline with indents and some numerical fields in field-specific format',
|
|
action = 'store_true')
|
|
|
|
opts = vars(parser.parse_args())
|
|
|
|
return opts
|
|
|
|
def inf(opts):
|
|
if opts['in']:
|
|
return open(opts['in'], 'r')
|
|
else:
|
|
return sys.stdin
|
|
|
|
def outf(opts):
|
|
if opts['out']:
|
|
return open(opts['out'], 'w+')
|
|
else:
|
|
return sys.stdout
|
|
|
|
|
|
def decode(opts):
|
|
indent = None
|
|
img = pycriu.images.load(inf(opts), opts['pretty'])
|
|
|
|
if opts['pretty']:
|
|
indent = 4
|
|
|
|
f = outf(opts)
|
|
json.dump(img, f, indent=indent)
|
|
if f == sys.stdout:
|
|
f.write("\n")
|
|
|
|
def encode(opts):
|
|
img = json.load(inf(opts))
|
|
pycriu.images.dump(img, outf(opts))
|
|
|
|
def main():
|
|
#Handle cmdline options
|
|
opts = handle_cmdline_opts()
|
|
|
|
cmds = {
|
|
'decode' : decode,
|
|
'encode' : encode
|
|
}
|
|
|
|
cmds[opts['command']](opts)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|