blob: 1d698af58cd83ceecd46c1ea85e6411333a22a91 [file] [log] [blame]
Andrew Lambeceaec02020-02-11 14:16:41 -07001# 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 Lamb5d132052020-05-29 14:49:06 -06006import json
7import pathlib
Andrew Lambeceaec02020-02-11 14:16:41 -07008
Prathmesh Prabhu72f8a002020-04-10 09:57:53 -07009from chromiumos.config.payload import config_bundle_pb2
Andrew Lambeceaec02020-02-11 14:16:41 -070010
David Burger54f704e2020-04-30 13:33:47 -060011from google.protobuf import json_format
12
Andrew Lambeceaec02020-02-11 14:16:41 -070013
Andrew Lamb9328c402020-03-05 12:57:59 -070014def read_config(path: str) -> config_bundle_pb2.ConfigBundle:
David Burger54f704e2020-04-30 13:33:47 -060015 """Reads a json proto from a file.
Andrew Lamb9328c402020-03-05 12:57:59 -070016
Andrew Lambeceaec02020-02-11 14:16:41 -070017 Args:
David Burger54f704e2020-04-30 13:33:47 -060018 path: Path to the json proto. See note above about deprecated repo
Andrew Lamb9328c402020-03-05 12:57:59 -070019 root behavior.
Andrew Lambeceaec02020-02-11 14:16:41 -070020 """
David Burger54f704e2020-04-30 13:33:47 -060021 project_config = config_bundle_pb2.ConfigBundle()
22 with open(path, 'r') as f:
23 json_format.Parse(f.read(), project_config)
24 return project_config
Andrew Lamb5d132052020-05-29 14:49:06 -060025
26
27def read_model_sku_json(factory_dir: pathlib.Path) -> dict:
28 """Reads and parses the model_sku.json file.
29
30 Args:
31 factory_dir: Path to a project's factory dir.
32 """
33 with open(factory_dir.joinpath('generated', 'model_sku.json')) as f:
34 return json.load(f)