Commit 452b03f6 authored by Ian Wells's avatar Ian Wells Committed by Commit Bot

Add a script for converting a raw binary feed response to textproto

Bug: 1044139
Change-Id: I0eac7262fec61f477a31a61da5b277b685a32909
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2088409
Commit-Queue: Ian Wells <iwells@chromium.org>
Reviewed-by: default avatarDan H <harringtond@chromium.org>
Cr-Commit-Position: refs/heads/master@{#753316}
parent 255d2f85
#!/bin/bash
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Converts a Feed HTTP response from binary to text using proto definitions from
# Chromium.
#
# Usage: feed_response_to_textproto.sh <in.binarypb> <out.textproto>
IN_FILE=$1
OUT_FILE=$2
TMP_FILE=/tmp/trimmedfeedresponse.binarypb
CHROMIUM_SRC=$(realpath $(dirname $(readlink -f $0))/../../../../..)
FEEDPROTO="$CHROMIUM_SRC/components/feed/core/proto"
# Responses start with a 4-byte length value that must be removed.
tail -c +4 $IN_FILE > $TMP_FILE
python3 $CHROMIUM_SRC/components/feed/core/v2/tools/textpb_to_binarypb.py \
--chromium_path=$CHROMIUM_SRC \
--output_file=$OUT_FILE \
--source_file=$TMP_FILE \
--direction=reverse
......@@ -34,9 +34,31 @@ flags.DEFINE_string('source_file', '',
flags.DEFINE_string('message',
DEFAULT_MESSAGE,
'The message to look for in source_file.')
flags.DEFINE_string('direction', 'forward',
'Set --direction=reverse to convert binary to text.')
COMPONENT_FEED_PROTO_PATH = 'components/feed/core/proto'
def text_to_binary():
with open(FLAGS.source_file, mode='r') as file:
value_text_proto = file.read()
encoded = protoc_util.encode_proto(value_text_proto, FLAGS.message,
FLAGS.chromium_path,
COMPONENT_FEED_PROTO_PATH)
with open(FLAGS.output_file, mode='wb') as file:
file.write(encoded)
def binary_to_text():
with open(FLAGS.source_file, mode='rb') as file:
value_text_proto = file.read()
encoded = protoc_util.decode_proto(value_text_proto, FLAGS.message,
FLAGS.chromium_path,
COMPONENT_FEED_PROTO_PATH)
with open(FLAGS.output_file, mode='w') as file:
file.write(encoded)
def main(argv):
if len(argv) > 1:
......@@ -47,16 +69,13 @@ def main(argv):
raise app.UsageError('source_file flag must be set.')
if not FLAGS.output_file:
raise app.UsageError('output_file flag must be set.')
if FLAGS.direction != 'forward' and FLAGS.direction != 'reverse':
raise app.UsageError('direction must be forward or reverse')
with open(FLAGS.source_file) as file:
value_text_proto = file.read()
encoded = protoc_util.encode_proto(value_text_proto, FLAGS.message,
FLAGS.chromium_path,
COMPONENT_FEED_PROTO_PATH)
with open(FLAGS.output_file, 'wb') as file:
file.write(encoded)
if FLAGS.direction == 'forward':
text_to_binary()
elif FLAGS.direction == 'reverse':
binary_to_text()
if __name__ == '__main__':
app.run(main)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment