2014-12-31 14:06:49 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import argparse
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
|
|
|
|
import pycriu
|
|
|
|
|
2015-01-15 19:51:18 +03:00
|
|
|
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
|
|
|
|
|
2015-01-14 16:32:38 +02:00
|
|
|
|
|
|
|
def decode(opts):
|
2014-12-31 14:06:49 +02:00
|
|
|
indent = None
|
2015-05-29 16:01:00 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
img = pycriu.images.load(inf(opts), opts['pretty'])
|
|
|
|
except pycriu.images.MagicException as exc:
|
|
|
|
print >>sys.stderr, "Unknown magic %#x.\n"\
|
|
|
|
"Maybe you are feeding me an image with "\
|
|
|
|
"raw data(i.e. pages.img)?" % exc.magic
|
|
|
|
sys.exit(1)
|
2014-12-31 14:06:49 +02:00
|
|
|
|
2015-01-28 17:15:00 +03:00
|
|
|
if opts['pretty']:
|
2014-12-31 14:06:49 +02:00
|
|
|
indent = 4
|
|
|
|
|
2015-01-15 19:51:18 +03:00
|
|
|
f = outf(opts)
|
|
|
|
json.dump(img, f, indent=indent)
|
|
|
|
if f == sys.stdout:
|
|
|
|
f.write("\n")
|
2015-01-14 16:32:38 +02:00
|
|
|
|
|
|
|
def encode(opts):
|
2015-01-15 19:51:18 +03:00
|
|
|
img = json.load(inf(opts))
|
|
|
|
pycriu.images.dump(img, outf(opts))
|
2014-12-31 14:06:49 +02:00
|
|
|
|
|
|
|
def main():
|
2015-02-09 14:08:22 +03:00
|
|
|
desc = 'CRiu Image Tool'
|
|
|
|
parser = argparse.ArgumentParser(description=desc,
|
|
|
|
formatter_class=argparse.RawTextHelpFormatter)
|
2014-12-31 14:06:49 +02:00
|
|
|
|
2015-02-09 14:08:22 +03:00
|
|
|
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)
|
|
|
|
|
2015-07-23 02:51:00 +03:00
|
|
|
# Show
|
|
|
|
show_parser = subparsers.add_parser('show',
|
|
|
|
help = "convert criu image from binary to human-readable json")
|
|
|
|
show_parser.add_argument("in")
|
|
|
|
show_parser.set_defaults(func=decode, pretty=True, out=None)
|
|
|
|
|
2015-02-09 14:08:22 +03:00
|
|
|
opts = vars(parser.parse_args())
|
2014-12-31 14:06:49 +02:00
|
|
|
|
2015-02-09 14:08:22 +03:00
|
|
|
opts["func"](opts)
|
2014-12-31 14:06:49 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|