• 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"""Convert a ConfigBundle instance to a FlatConfig instance.
6
7ConfigBundle is a fully normalized format, where eg: Partners are referred to by
8id. By denormalizing into a flattened format, we can more easily query projects."""
9
10import argparse
11import logging
12
13from common import config_bundle_utils
14from checker import io_utils
15
16
17def flatten(infile, outfile):
18  """Flatten a ConfigBundle .jsonproto file.
19
20    Take a ConfigBundle in .jsonproto format, read it, flatten, and write the
21    output FlatConfig to the given output file.
22
23    Args:
24      infile  (str): input file containing ConfigBundle in .jsonproto format
25      outfile (str): filename to write flattened config to
26  pars"""
27
28  config = io_utils.read_config(infile)
29  flatlist = config_bundle_utils.flatten_config(config)
30  io_utils.write_message_json(flatlist, outfile)
31  print(str(len(flatlist.values)))
32
33
34if __name__ == "__main__":
35  parser = argparse.ArgumentParser(description=__doc__)
36  parser.add_argument(
37      '-i',
38      '--input',
39      type=str,
40      required=True,
41      help="""ConfigBundle to flatten in jsonpb format.""")
42  parser.add_argument(
43      '-o',
44      '--output',
45      type=str,
46      required=True,
47      help='output file to write FlatConfigList jsonproto to')
48  parser.add_argument("-l", "--log", type=str, help='set logging level')
49  parser.add_argument(
50      "-v", "--verbose", help="increase output verbosity", action="store_true")
51
52  args = parser.parse_args()
53  # pylint: disable=invalid-name
54  loglevel = logging.INFO if args.verbose else logging.WARNING
55  if args.log:
56    loglevel = {
57        "critical": logging.CRITICAL,
58        "error": logging.ERROR,
59        "warning": logging.WARNING,
60        "info": logging.INFO,
61        "debug": logging.DEBUG,
62    }.get(args.log.lower())
63
64    if not loglevel:
65      logging.error("invalid value for -l/--log '%s'", args.log)
66
67  logging.basicConfig(level=loglevel)
68  flatten(args.input, args.output)
69