Commit 7a8ddc0f authored by Ruslan Kuprieiev's avatar Ruslan Kuprieiev Committed by Pavel Emelyanov

crit: split convert command into decode and encode

Signed-off-by: 's avatarRuslan Kuprieiev <kupruser@gmail.com>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 598a83cf
...@@ -9,8 +9,8 @@ def handle_cmdline_opts(): ...@@ -9,8 +9,8 @@ def handle_cmdline_opts():
desc = 'CRiu Image Tool' desc = 'CRiu Image Tool'
parser = argparse.ArgumentParser(description=desc) parser = argparse.ArgumentParser(description=desc)
parser.add_argument('command', parser.add_argument('command',
choices = ['convert'], choices = ['decode', 'encode'],
help = 'use \"covert\" to convert CRIU image to/from human-readable format') help = 'decode/encode - convert criu image from/to binary type to/from json')
parser.add_argument('-i', parser.add_argument('-i',
'--in', '--in',
help = 'input file (stdin by default)') help = 'input file (stdin by default)')
...@@ -26,50 +26,53 @@ def handle_cmdline_opts(): ...@@ -26,50 +26,53 @@ def handle_cmdline_opts():
return opts return opts
def convert_img(opts):
orig_type = None def decode(opts):
indent = None indent = None
# If no input file is set -- stdin is used. # If no input file is set -- stdin is used.
if opts['in']: if opts['in']:
with open(opts['in'], 'r') as f: with open(opts['in'], 'r') as f:
in_str = f.read() img = pycriu.images.load(f)
else:
in_str = sys.stdin.read()
# Detect what is the type we are dealing with.
if in_str[len(in_str) - len(in_str.lstrip())] == '{':
img = json.loads(in_str)
orig_type = 'json'
else: else:
img = pycriu.images.loads(in_str) img = pycriu.images.load(sys.stdin)
orig_type = 'bin'
# For stdout --format nice is set by default. # For stdout --format nice is set by default.
if opts['format'] == 'nice' or (opts['format'] == None and opts['out'] == None): if opts['format'] == 'nice' or (opts['format'] == None and opts['out'] == None):
indent = 4 indent = 4
# Just convert image to the opposite format # If no output file is set -- stdout is used.
if orig_type == 'json': if opts['out']:
out_str = pycriu.images.dumps(img) with open(opts['out'], 'w+') as f:
json.dump(img, f, indent=indent)
else: else:
out_str = json.dumps(img, indent=indent) 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 no output file is set -- stdout is used.
if opts['out']: if opts['out']:
with open(opts['out'], 'w+') as f: with open(opts['out'], 'w+') as f:
f.write(out_str) pycriu.images.dump(img, f)
else: else:
if orig_type == 'bin': pycriu.images.dump(img, sys.stdout)
out_str += "\n"
sys.stdout.write(out_str)
def main(): def main():
#Handle cmdline options #Handle cmdline options
opts = handle_cmdline_opts() opts = handle_cmdline_opts()
cmds = { cmds = {
'convert' : convert_img 'decode' : decode,
'encode' : encode
} }
cmds[opts['command']](opts) cmds[opts['command']](opts)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment