[CRIU] [PATCH 3/7] crit: add crit

Ruslan Kuprieiev kupruser at gmail.com
Tue Oct 28 21:02:44 PDT 2014


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..438ef3b
--- /dev/null
+++ b/crit
@@ -0,0 +1,75 @@
+#!/usr/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
+
+	# 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)
+
+	# 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)
+
+def main():
+	#Handle cmdline options
+	opts = handle_cmdline_opts()
+
+	cmds = {
+		'convert' : convert_img
+	}
+
+	cmds[opts['command']](opts)
+
+if __name__ == '__main__':
+	main()
-- 
1.9.3



More information about the CRIU mailing list