[CRIU] [PATCH 5/9] crit: add crit

Ruslan Kuprieiev kupruser at gmail.com
Wed Oct 8 09:40:50 PDT 2014


On 08.10.2014 19:06, Pavel Emelyanov wrote:
> On 10/08/2014 02:35 PM, Ruslan Kuprieiev wrote:
>> crit is a python script that helps user to manipulate criu
>> images. For now, it can only convert criu images to\from
>> human-readable format using pycriu.images module.
>>
>> Signed-off-by: Ruslan Kuprieiev <kupruser at gmail.com>
>> ---
>>   crit | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 75 insertions(+)
>>   create mode 100755 crit
>>
>> diff --git a/crit b/crit
>> new file mode 100755
>> index 0000000..2a106bd
>> --- /dev/null
>> +++ b/crit
>> @@ -0,0 +1,75 @@
>> +#!/bin/env python
>> +import argparse
>> +import sys
>> +
>> +import pycriu
>> +
>> +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')
>> +	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 convert_img(opts):
>> +	# Create an instance of criu_image() class to pass it
>> +	# data from input file.
>> +	img = pycriu.images.criu_image()
>> +
>> +	# 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()
>> +
>> +	# ParseFromString will try to detect type of an input
>> +	# and parse it into list of pb entries.
>> +	img.ParseFromString(in_str)
>> +
>> +	# For stdout --format nice is set by default.
>> +	if opts['format'] == 'nice':
>> +		nice = True
>> +	elif opts['format'] == None and opts['out'] == None:
>> +		nice = True
>> +	else:
>> +		nice = False
> nice is unused below
>
>> +
>> +	# img remembers data type it was parsed from, so
>> +	# SerializeToString will return data in type that
>> +	# is oposite to the original.
>> +	out_str = img.SerializeToString(nice=nice)
>> +

It is used here.

>> +	# If no output file is set -- stdout is used.
>> +	if opts['out']:
>> +		with open(opts['out'], 'w+') as f:
>> +			f.write(out_str)
>> +	else:
>> +		sys.stdout.write(out_str)
> out = opts.pop('out', sys.stdout)
> out.write(out_str)
>
> or smth like this :)

oh! Looks cool! But, maybe:
out = opts.pop('out', '/dev/stdout') ?
Because otherwise ugly "if" will be still needed.

>
>> +
>> +def main():
>> +	#Handle cmdline options
>> +	opts = handle_cmdline_opts()
>> +
>> +	cmds = {
>> +		'convert' : convert_img
>> +	}
>> +
>> +	cmds[opts['command']](opts)
>> +
>> +if __name__ == '__main__':
>> +	main()
>>



More information about the CRIU mailing list