• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env vpython3
2# Copyright 2020 The ChromiumOS Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Aggregate one or more protobuffer messages into an output message.
6
7Takes a list of input messages and produces a single output message of some
8aggregate type.  If the message and aggregate types are the same, then
9the following operation is performed, merging messages.  Note that any repeated
10fields are _concatenated_ not overwritten:
11
12    output = OutputType()
13    for input in inputs:
14      output.MergeFrom(input)
15
16If the types are different, then the inputs are aggregated into the
17values field of the output:
18
19    output = OutputType()
20    for input in inputs:
21        output.values.append(input)
22"""
23
24import argparse
25
26from checker import io_utils
27from common import proto_utils
28
29if __name__ == '__main__':
30  parser = argparse.ArgumentParser(
31      description=__doc__,
32      formatter_class=argparse.RawDescriptionHelpFormatter,
33  )
34
35  parser.add_argument(
36      'inputs',
37      type=str,
38      nargs='+',
39      help='message(s) to aggregate in jsonproto format',
40  )
41
42  parser.add_argument(
43      '-o',
44      '--output',
45      type=str,
46      required=True,
47      help='output file to write aggregated messages to',
48  )
49
50  # TODO: remove defaults once config_postsubmit change rolls out
51  parser.add_argument(
52      '-m',
53      '--message-type',
54      type=str,
55      default='chromiumos.config.payload.FlatConfigList',
56      help='input message type to aggregate',
57  )
58
59  parser.add_argument(
60      '-a',
61      '--aggregate-type',
62      type=str,
63      default='chromiumos.config.payload.FlatConfigList',
64      help='aggregage message type to output',
65  )
66
67  # load database of protobuffer name -> Type
68  protodb = proto_utils.create_symbol_db()
69
70  options = parser.parse_args()
71  output = protodb.GetSymbol(options.aggregate_type)()
72  for path in options.inputs:
73    if options.message_type == options.aggregate_type:
74      output.MergeFrom(
75          io_utils.read_json_proto(
76              protodb.GetSymbol(options.message_type)(), path))
77    else:
78      output.values.append(
79          io_utils.read_json_proto(
80              protodb.GetSymbol(options.message_type)(), path))
81
82  io_utils.write_message_json(
83      output, options.output, use_integers_for_enums=True)
84