criu/crit
Pavel Emelyanov 7b4a264e28 crit: Small inf/outf evaluation cleanup
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
2015-01-15 19:51:18 +03:00

71 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)
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 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))
# For stdout --format nice is set by default.
if opts['format'] == 'nice' or (opts['format'] == None and opts['out'] == None):
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()