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"""Converts ConfigBundle json pb into json pb with int enums.""" 6 7import argparse 8import sys 9 10from google.protobuf import json_format 11 12from chromiumos.config.payload import config_bundle_pb2 13from chromiumos.config.prototype import prototype_config_bundle_pb2 14 15 16def Main(input_config, output_config): # pylint: disable=invalid-name 17 """Converts ConfigBundle json pb into json pb with enums as ints. 18 19 Args: 20 input_config: path to input file to convert. 21 output_config: path to write converted output file to. 22 """ 23 config = config_bundle_pb2.ConfigBundle() 24 try: 25 with open(input_config, 'r', encoding='utf-8') as f: 26 json_format.Parse(f.read(), config) 27 except json_format.ParseError: 28 config = prototype_config_bundle_pb2.PrototypeConfigBundle() 29 with open(input_config, 'r', encoding='utf-8') as f: 30 json_format.Parse(f.read(), config) 31 32 json_output = json_format.MessageToJson( 33 config, sort_keys=True, use_integers_for_enums=True) 34 with open(output_config, 'w', encoding='utf-8') as f: 35 print(json_output, file=f) 36 37 38def main(): 39 """Main program which parses args and runs Main.""" 40 parser = argparse.ArgumentParser(description=__doc__) 41 parser.add_argument( 42 '-i', 43 '--input_config', 44 type=str, 45 required=True, 46 help='Source ConfigBundle JSON PB file.') 47 parser.add_argument( 48 '-o', 49 '--output_config', 50 type=str, 51 required=True, 52 help='Output JSON PB file.') 53 args = parser.parse_args() 54 Main(args.input_config, args.output_config) 55 56 57if __name__ == '__main__': 58 sys.exit(main()) 59