criu/crit
Ruslan Kuprieiev 7a8ddc0f4a crit: split convert command into decode and encode
Signed-off-by: Ruslan Kuprieiev <kupruser@gmail.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
2015-01-14 20:47:46 +04:00

81 lines
1.8 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)
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('-f',
'--format',
choices = ['raw', 'nice'],
help = 'well-formated output (by default: raw for files and nice for stdout)')
opts = vars(parser.parse_args())
return opts
def decode(opts):
indent = None
# If no input file is set -- stdin is used.
if opts['in']:
with open(opts['in'], 'r') as f:
img = pycriu.images.load(f)
else:
img = pycriu.images.load(sys.stdin)
# For stdout --format nice is set by default.
if opts['format'] == 'nice' or (opts['format'] == None and opts['out'] == None):
indent = 4
# If no output file is set -- stdout is used.
if opts['out']:
with open(opts['out'], 'w+') as f:
json.dump(img, f, indent=indent)
else:
json.dump(img, sys.stdout, indent=indent)
sys.stdout.write("\n")
def encode(opts):
# If no input file is set -- stdin is used.
if opts['in']:
with open(opts['in'], 'r') as f:
img = json.load(f)
else:
img = json.load(sys.stdin)
# If no output file is set -- stdout is used.
if opts['out']:
with open(opts['out'], 'w+') as f:
pycriu.images.dump(img, f)
else:
pycriu.images.dump(img, sys.stdout)
def main():
#Handle cmdline options
opts = handle_cmdline_opts()
cmds = {
'decode' : decode,
'encode' : encode
}
cmds[opts['command']](opts)
if __name__ == '__main__':
main()