Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 1 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | """IO-related helper functions.""" |
| 5 | |
Andrew Lamb | 5d13205 | 2020-05-29 14:49:06 -0600 | [diff] [blame] | 6 | import json |
| 7 | import pathlib |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 8 | |
Sean McAllister | 576d80c | 2020-05-29 13:00:19 -0600 | [diff] [blame] | 9 | from typing import Any, Dict |
| 10 | |
David Burger | 54f704e | 2020-04-30 13:33:47 -0600 | [diff] [blame] | 11 | from google.protobuf import json_format |
Sean McAllister | 576d80c | 2020-05-29 13:00:19 -0600 | [diff] [blame] | 12 | from google.protobuf.message import Message |
| 13 | |
Andrew Lamb | 0ed1b90 | 2020-06-08 17:03:47 -0600 | [diff] [blame^] | 14 | from chromiumos.config.payload import config_bundle_pb2 |
| 15 | |
| 16 | |
Sean McAllister | 576d80c | 2020-05-29 13:00:19 -0600 | [diff] [blame] | 17 | |
| 18 | def write_message_json(message: Message, path: pathlib.Path, \ |
| 19 | default_fields=False): |
| 20 | """Take a Message and write it to a file as json. |
| 21 | |
| 22 | Args: |
| 23 | message: protobuf message to write to file |
| 24 | path: output file write json to |
| 25 | default_fields: If true, include default values for fields |
| 26 | """ |
| 27 | # ow this is a long parameter |
| 28 | opts = {'including_default_value_fields': default_fields, 'sort_keys': True} |
| 29 | |
| 30 | with open(path, 'w') as outfile: |
| 31 | outfile.write(json_format.MessageToJson(message, **opts)) |
David Burger | 54f704e | 2020-04-30 13:33:47 -0600 | [diff] [blame] | 32 | |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 33 | |
Andrew Lamb | 9328c40 | 2020-03-05 12:57:59 -0700 | [diff] [blame] | 34 | def read_config(path: str) -> config_bundle_pb2.ConfigBundle: |
Sean McAllister | 576d80c | 2020-05-29 13:00:19 -0600 | [diff] [blame] | 35 | """Reads a ConfigBundle mesage from a jsonpb file. |
Andrew Lamb | 9328c40 | 2020-03-05 12:57:59 -0700 | [diff] [blame] | 36 | |
Sean McAllister | 576d80c | 2020-05-29 13:00:19 -0600 | [diff] [blame] | 37 | Args: |
| 38 | path: Path to the json proto. See note above about deprecated repo |
| 39 | root behavior. |
| 40 | |
| 41 | Returns: |
| 42 | ConfigBundle parsed from file. |
| 43 | """ |
David Burger | 54f704e | 2020-04-30 13:33:47 -0600 | [diff] [blame] | 44 | project_config = config_bundle_pb2.ConfigBundle() |
| 45 | with open(path, 'r') as f: |
| 46 | json_format.Parse(f.read(), project_config) |
| 47 | return project_config |
Andrew Lamb | 5d13205 | 2020-05-29 14:49:06 -0600 | [diff] [blame] | 48 | |
| 49 | |
Sean McAllister | 576d80c | 2020-05-29 13:00:19 -0600 | [diff] [blame] | 50 | def read_model_sku_json(factory_dir: pathlib.Path) -> Dict[str, Any]: |
Andrew Lamb | 5d13205 | 2020-05-29 14:49:06 -0600 | [diff] [blame] | 51 | """Reads and parses the model_sku.json file. |
| 52 | |
| 53 | Args: |
Sean McAllister | 576d80c | 2020-05-29 13:00:19 -0600 | [diff] [blame] | 54 | factory_dir: Path to a project's factory dir. |
| 55 | |
| 56 | Returns: |
| 57 | Parsed model_sku.json as a dict |
Andrew Lamb | 5d13205 | 2020-05-29 14:49:06 -0600 | [diff] [blame] | 58 | """ |
| 59 | with open(factory_dir.joinpath('generated', 'model_sku.json')) as f: |
| 60 | return json.load(f) |