Adjust checker code to read json first with binary fallback.
Adjust the checker code used by CQ checkers to attempt to read the
config input as json first with a temporary fallback to binary. When the
binary is removed the fallback parsing can be removed.
BUG=chromium:1073530
TEST=./run_py_unittests.sh
Change-Id: I5976ff83291a5f59461dcac54f97fc936ff7aca9
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/config/+/2174612
Reviewed-by: Andrew Lamb <andrewlamb@chromium.org>
Commit-Queue: David Burger <dburger@chromium.org>
Tested-by: David Burger <dburger@chromium.org>
diff --git a/payload_utils/checker/io_utils.py b/payload_utils/checker/io_utils.py
index 4fb4981..12252a3 100644
--- a/payload_utils/checker/io_utils.py
+++ b/payload_utils/checker/io_utils.py
@@ -7,14 +7,32 @@
from chromiumos.config.payload import config_bundle_pb2
+from google.protobuf import json_format
+
def read_config(path: str) -> config_bundle_pb2.ConfigBundle:
- """Reads a binary proto from a file.
+ """Reads a json proto from a file.
Args:
- path: Path to the binary proto. See note above about deprecated repo
+ path: Path to the json proto. See note above about deprecated repo
root behavior.
"""
+ try:
+ return _read_json_config(path)
+ except:
+ # TODO(crbug.com/1073530): remove binary pb fallback when transition to
+ # json pb is complete.
+ return _read_binary_config(path)
+
+
+def _read_json_config(path: str) -> config_bundle_pb2.ConfigBundle:
+ project_config = config_bundle_pb2.ConfigBundle()
+ with open(path, 'r') as f:
+ json_format.Parse(f.read(), project_config)
+ return project_config
+
+
+def _read_binary_config(path: str) -> config_bundle_pb2.ConfigBundle:
project_config = config_bundle_pb2.ConfigBundle()
with open(os.path.join(path), 'rb') as f:
project_config.ParseFromString(f.read())