[CRIU] [PATCH 1/2] crit: Get rid of --format option
Pavel Emelyanov
xemul at parallels.com
Thu Jan 29 07:38:51 PST 2015
On 01/29/2015 05:12 PM, Christopher Covington wrote:
> On 01/28/2015 09:15 AM, Pavel Emelyanov wrote:
>> I plan to mark some fields as IP address and print them respectively.
>> The --format hex is not nice switch for this and introducing one more
>> (--format hex ipadd) is too bad.
>>
>> So let's fix the cirt API to be simple and stupid. By default crit
>> generates canonical one-line JSON. With --nice option it splits the
>> output into lines, adds indentation and prints hex as hex and IP as
>> IP.
>
> Nit: I think most programs call this sort of formatting "pretty" rather than
> "nice".
Nice suggestion :) How could I forget about git.
> Chris
>
>> Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
>> ---
>> crit | 15 ++++-----------
>> pycriu/images/images.py | 24 ++++++++++++------------
>> pycriu/images/pb2dict.py | 16 ++++++----------
>> 3 files changed, 22 insertions(+), 33 deletions(-)
>>
>> diff --git a/crit b/crit
>> index 0c7f5e9..570a2eb 100755
>> --- a/crit
>> +++ b/crit
>> @@ -17,14 +17,8 @@ def handle_cmdline_opts():
>> 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)')
>> + parser.add_argument('--nice', help = 'Multiline with indents and some numerical fields in field-specific format',
>> + action = 'store_true')
>>
>> opts = vars(parser.parse_args())
>>
>> @@ -45,10 +39,9 @@ def outf(opts):
>>
>> def decode(opts):
>> indent = None
>> - img = pycriu.images.load(inf(opts), opts['format'])
>> + img = pycriu.images.load(inf(opts), opts['nice'])
>>
>> - # For stdout --format nice is set by default.
>> - if 'nice' in opts['format'] or ('raw' not in opts['format'] and opts['out'] == None):
>> + if opts['nice']:
>> indent = 4
>>
>> f = outf(opts)
>> diff --git a/pycriu/images/images.py b/pycriu/images/images.py
>> index 8c4b73e..f84faf8 100644
>> --- a/pycriu/images/images.py
>> +++ b/pycriu/images/images.py
>> @@ -59,7 +59,7 @@ class entry_handler:
>> self.payload = payload
>> self.extra_handler = extra_handler
>>
>> - def load(self, f, fmt = None):
>> + def load(self, f, nice = False):
>> """
>> Convert criu image entries from binary format to dict(json).
>> Takes a file-like object and returnes a list with entries in
>> @@ -77,7 +77,7 @@ class entry_handler:
>> break
>> size, = struct.unpack('i', buf)
>> pb.ParseFromString(f.read(size))
>> - entry = pb2dict.pb2dict(pb, fmt)
>> + entry = pb2dict.pb2dict(pb, nice)
>>
>> # Read extra
>> if self.extra_handler:
>> @@ -87,12 +87,12 @@ class entry_handler:
>>
>> return entries
>>
>> - def loads(self, s, fmt = None):
>> + def loads(self, s, nice = False):
>> """
>> Same as load(), but takes a string as an argument.
>> """
>> f = io.BytesIO(s)
>> - return self.load(f, fmt)
>> + return self.load(f, nice)
>>
>> def dump(self, entries, f):
>> """
>> @@ -131,7 +131,7 @@ class pagemap_handler:
>> that it has a header of pagemap_head type followed by entries
>> of pagemap_entry type.
>> """
>> - def load(self, f, fmt = None):
>> + def load(self, f, nice = False):
>> entries = []
>>
>> pb = pagemap_head()
>> @@ -141,15 +141,15 @@ class pagemap_handler:
>> break
>> size, = struct.unpack('i', buf)
>> pb.ParseFromString(f.read(size))
>> - entries.append(pb2dict.pb2dict(pb, fmt))
>> + entries.append(pb2dict.pb2dict(pb, nice))
>>
>> pb = pagemap_entry()
>>
>> return entries
>>
>> - def loads(self, s, fmt = None):
>> + def loads(self, s, nice = False):
>> f = io.BytesIO(s)
>> - return self.load(f, fmt)
>> + return self.load(f, nice)
>>
>> def dump(self, entries, f):
>> pb = pagemap_head()
>> @@ -258,7 +258,7 @@ handlers = {
>> 'IPCNS_MSG' : entry_handler(ipc_msg_entry)
>> }
>>
>> -def load(f, fmt = None):
>> +def load(f, nice = False):
>> """
>> Convert criu image from binary format to dict(json).
>> Takes a file-like object to read criu image from.
>> @@ -281,16 +281,16 @@ def load(f, fmt = None):
>> raise Exception("No handler found for image with such magic "+m)
>>
>> image['magic'] = m
>> - image['entries'] = handler.load(f, fmt)
>> + image['entries'] = handler.load(f, nice)
>>
>> return image
>>
>> -def loads(s, fmt = None):
>> +def loads(s, nice = False):
>> """
>> Same as load(), but takes a string.
>> """
>> f = io.BytesIO(s)
>> - return load(f, fmt)
>> + return load(f, nice)
>>
>> def dump(img, f):
>> """
>> diff --git a/pycriu/images/pb2dict.py b/pycriu/images/pb2dict.py
>> index 5205a71..b76390f 100644
>> --- a/pycriu/images/pb2dict.py
>> +++ b/pycriu/images/pb2dict.py
>> @@ -43,23 +43,19 @@ _basic_cast = {
>> def _marked_as_hex(field):
>> return field.GetOptions().Extensions[opts_pb2.criu].hex
>>
>> -def _pb2dict_cast(field, value, fmt = None, is_hex = False):
>> +def _pb2dict_cast(field, value, nice = False, is_hex = False):
>> if not is_hex:
>> is_hex = _marked_as_hex(field)
>> - if fmt:
>> - print_hex = 'hex' in fmt
>> - else:
>> - print_hex = False
>>
>> if field.type == FD.TYPE_MESSAGE:
>> - return pb2dict(value, fmt, is_hex)
>> + return pb2dict(value, nice, is_hex)
>> elif field.type == FD.TYPE_BYTES:
>> return value.encode('base64')
>> elif field.type == FD.TYPE_ENUM:
>> return field.enum_type.values_by_number.get(value, None).name
>> elif field.type in _basic_cast:
>> cast = _basic_cast[field.type]
>> - if (cast == int or cast == long) and is_hex and print_hex:
>> + if (cast == int or cast == long) and is_hex and nice:
>> # Fields that have (criu).hex = true option set
>> # should be stored in hex string format.
>> return "0x%x" % value
>> @@ -68,7 +64,7 @@ def _pb2dict_cast(field, value, fmt = None, is_hex = False):
>> else:
>> raise Exception("Field(%s) has unsupported type %d" % (field.name, field.type))
>>
>> -def pb2dict(pb, fmt = None, is_hex = False):
>> +def pb2dict(pb, nice = False, is_hex = False):
>> """
>> Convert protobuf msg to dictionary.
>> Takes a protobuf message and returns a dict.
>> @@ -78,9 +74,9 @@ def pb2dict(pb, fmt = None, is_hex = False):
>> if field.label == FD.LABEL_REPEATED:
>> d_val = []
>> for v in value:
>> - d_val.append(_pb2dict_cast(field, v, fmt, is_hex))
>> + d_val.append(_pb2dict_cast(field, v, nice, is_hex))
>> else:
>> - d_val = _pb2dict_cast(field, value, fmt, is_hex)
>> + d_val = _pb2dict_cast(field, value, nice, is_hex)
>>
>> d[field.name] = d_val
>> return d
>>
>
>
More information about the CRIU
mailing list