File: textpb_to_binarypb.py

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (98 lines) | stat: -rwxr-xr-x 2,983 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
# Copyright 2020 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Lint as: python3
"""The tool converts a textpb into a binary proto using chromium protoc binary.

Make sure you have absl-py installed via 'python3 -m pip install absl-py'.

Usage example:
    python3 ./textpb_to_binarypb.py
    --chromium_path ~/chromium/src
    --output_file /tmp/binary.pb
    --source_file /tmp/original.textpb
"""

import base64
import glob
import os
import protoc_util
import subprocess
import sys
import urllib.parse

from absl import app
from absl import flags

DEFAULT_MESSAGE = 'feedwire.Response'

FLAGS = flags.FLAGS
flags.DEFINE_string('chromium_path', '', 'The path of your chromium depot.')
flags.DEFINE_string(
    'output_file',
    '',
    'The target output file path. If not set, writes to stdout.')
flags.DEFINE_string(
    'output_format',
    'bin',
    'When encoding text to binary, this may be set to base64 to encode output '
    + 'suitable for URLs.')
flags.DEFINE_string('source_file', '',
                    'The source proto file, in textpb format, path.')
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/v2'

def read_input():
  if FLAGS.source_file:
    with open(FLAGS.source_file, mode='r') as file:
      return file.read()
  return sys.stdin.buffer.read()

def text_to_binary():
  encoded = protoc_util.encode_proto(read_input(), FLAGS.message,
                                     FLAGS.chromium_path,
                                     COMPONENT_FEED_PROTO_PATH)

  if FLAGS.output_format == 'base64':
    encoded = urllib.parse.quote(
        base64.urlsafe_b64encode(encoded).decode('utf-8')).encode('utf-8')

  if FLAGS.output_file:
    with open(FLAGS.output_file, mode='wb') as file:
      file.write(encoded)
  else:
    sys.stdout.buffer.write(encoded)

def binary_to_text():
  encoded = protoc_util.decode_proto(read_input(), FLAGS.message,
                                     FLAGS.chromium_path,
                                     COMPONENT_FEED_PROTO_PATH)

  if FLAGS.output_file:
    with open(FLAGS.output_file, mode='w') as file:
      file.write(encoded)
  else:
    print(encoded)

def main(argv):
  if len(argv) > 1:
    raise app.UsageError('Too many arguments. Unknown: ' + ' '.join(argv[1:]))
  if not FLAGS.chromium_path:
    raise app.UsageError('chromium_path flag must be set.')
  if FLAGS.direction != 'forward' and FLAGS.direction != 'reverse':
    raise app.UsageError('direction must be forward or reverse')

  if FLAGS.direction == 'forward':
    text_to_binary()
  elif FLAGS.direction == 'reverse':
    binary_to_text()

if __name__ == '__main__':
  app.run(main)