criu/crit
Ruslan Kuprieiev 87da3e3437 crit: add --format hex option
Pavel reported that decimal values for some fields are hard to read,
because people used to see hex values in there. Unfortunately, json
doesn't support hex representation of integers, so we can only store
them as hex strings. Not all field need to be represented as hex
strings, so this set introduces a custom field option called "criu"
to use in our proto files. One should use [(criu).hex = true] to mark
which field should be represented as a hex string. pb2dict module
from pycriu package will look into field options and if he finds that
criu.hex is set to True, it will convert such field to/from hex string.
Though, such behaviour is optional and user can request it by specifying
 --format hex when calling crit decode("crit encode" in its turn, detects
such fields automatically and doesn't require any special cmdline options
to be set).

We need our proto files to compile with both protoc and
protoc-c compilers, which requires creating google/protobuf
directory with a symlink to /usr/include/google/protobuf/
descriptor.proto to make protoc-c and generated c files happy.

Reported-by: Pavel Emelyanov <xemul@parallels.com>
Signed-off-by: Ruslan Kuprieiev <kupruser@gmail.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
2015-01-19 18:13:55 +04:00

75 lines
1.7 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, formatter_class=argparse.RawTextHelpFormatter)
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', 'hex'],
nargs = '+',
default = [],
help = 'raw - all in one line\n'\
'nice - add indents and newlines to look nice(default for stdout)\n'\
'hex - print int fields as hex strings where suitable(could be combined with others)')
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), opts['format'])
# For stdout --format nice is set by default.
if 'nice' in opts['format'] or ('raw' not in opts['format'] 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()