mirror of
https://github.com/checkpoint-restore/criu.git
synced 2026-01-23 02:14:37 +00:00
This will allow us to easily extend commands that crit supports, avoiding "--help" confusion. Signed-off-by: Ruslan Kuprieiev <kupruser@gmail.com> Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
74 lines
1.8 KiB
Python
Executable file
74 lines
1.8 KiB
Python
Executable file
#!/usr/bin/env python
|
|
import argparse
|
|
import sys
|
|
import json
|
|
|
|
import pycriu
|
|
|
|
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():
|
|
desc = 'CRiu Image Tool'
|
|
parser = argparse.ArgumentParser(description=desc,
|
|
formatter_class=argparse.RawTextHelpFormatter)
|
|
|
|
subparsers = parser.add_subparsers(help='Use crit CMD --help for command-specific help')
|
|
|
|
# Decode
|
|
decode_parser = subparsers.add_parser('decode',
|
|
help = 'convert criu image from binary type json')
|
|
decode_parser.add_argument('--pretty',
|
|
help = 'Multiline with indents and some numerical fields in field-specific format',
|
|
action = 'store_true')
|
|
decode_parser.add_argument('-i',
|
|
'--in',
|
|
help = 'criu image in binary format to be decoded (stdin by default)')
|
|
decode_parser.add_argument('-o',
|
|
'--out',
|
|
help = 'where to put criu image in json format (stdout by default)')
|
|
decode_parser.set_defaults(func=decode)
|
|
|
|
# Encode
|
|
encode_parser = subparsers.add_parser('encode',
|
|
help = 'convert criu image from json type to binary')
|
|
encode_parser.add_argument('-i',
|
|
'--in',
|
|
help = 'criu image in json format to be encoded (stdin by default)')
|
|
encode_parser.add_argument('-o',
|
|
'--out',
|
|
help = 'where to put criu image in binary format (stdout by default)')
|
|
encode_parser.set_defaults(func=encode)
|
|
|
|
opts = vars(parser.parse_args())
|
|
|
|
opts["func"](opts)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|