Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/checkpoint-restore/criu.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/crit
diff options
context:
space:
mode:
authorRuslan Kuprieiev <kupruser@gmail.com>2015-01-14 17:32:38 +0300
committerPavel Emelyanov <xemul@parallels.com>2015-01-14 19:47:46 +0300
commit7a8ddc0f4a9012afd56b7a1268488f644b036ae1 (patch)
treed1057aef8531b6ed95b6fe2390bb4b7b00a11723 /crit
parent598a83cfd36e7512054a7364bed0ee5f2db9ed1e (diff)
crit: split convert command into decode and encode
Signed-off-by: Ruslan Kuprieiev <kupruser@gmail.com> Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
Diffstat (limited to 'crit')
-rwxr-xr-xcrit49
1 files changed, 26 insertions, 23 deletions
diff --git a/crit b/crit
index b87a62da9..667efa857 100755
--- a/crit
+++ b/crit
@@ -9,8 +9,8 @@ def handle_cmdline_opts():
desc = 'CRiu Image Tool'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('command',
- choices = ['convert'],
- help = 'use \"covert\" to convert CRIU image to/from human-readable format')
+ 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)')
@@ -26,50 +26,53 @@ def handle_cmdline_opts():
return opts
-def convert_img(opts):
- orig_type = None
+
+def decode(opts):
indent = None
# If no input file is set -- stdin is used.
if opts['in']:
with open(opts['in'], 'r') as f:
- in_str = f.read()
- 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'
+ img = pycriu.images.load(f)
else:
- img = pycriu.images.loads(in_str)
- orig_type = 'bin'
+ 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
- # Just convert image to the opposite format
- if orig_type == 'json':
- out_str = pycriu.images.dumps(img)
+ # 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:
- 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 opts['out']:
with open(opts['out'], 'w+') as f:
- f.write(out_str)
+ pycriu.images.dump(img, f)
else:
- if orig_type == 'bin':
- out_str += "\n"
- sys.stdout.write(out_str)
+ pycriu.images.dump(img, sys.stdout)
+
def main():
#Handle cmdline options
opts = handle_cmdline_opts()
cmds = {
- 'convert' : convert_img
+ 'decode' : decode,
+ 'encode' : encode
}
cmds[opts['command']](opts)