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 | |
| 6 | import os |
Andrew Lamb | 9328c40 | 2020-03-05 12:57:59 -0700 | [diff] [blame^] | 7 | import warnings |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 8 | |
Andrew Lamb | bc029d3 | 2020-02-24 12:42:50 -0700 | [diff] [blame] | 9 | from bindings.api import config_bundle_pb2 |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 10 | |
| 11 | |
Andrew Lamb | 9328c40 | 2020-03-05 12:57:59 -0700 | [diff] [blame^] | 12 | def read_config(path: str) -> config_bundle_pb2.ConfigBundle: |
| 13 | """Reads a binary proto from a file. |
| 14 | |
| 15 | Note that passing paths to repo roots is deprecated, and will raise a |
| 16 | warning. If a repo root is passed, it is assumed the config can be found at |
| 17 | 'generated/config.binaryproto'. |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 18 | |
| 19 | Args: |
Andrew Lamb | 9328c40 | 2020-03-05 12:57:59 -0700 | [diff] [blame^] | 20 | path: Path to the binary proto. See note above about deprecated repo |
| 21 | root behavior. |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 22 | """ |
Andrew Lamb | 9328c40 | 2020-03-05 12:57:59 -0700 | [diff] [blame^] | 23 | if os.path.isdir(path): |
| 24 | warnings.warn( |
| 25 | ('Passing a path to a repo root is deprecated, please pass a full path' |
| 26 | ' to a binary proto. Path: {}'.format(path)), FutureWarning) |
| 27 | path = os.path.join(path, 'generated', 'config.binaryproto') |
| 28 | |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 29 | project_config = config_bundle_pb2.ConfigBundle() |
Andrew Lamb | 9328c40 | 2020-03-05 12:57:59 -0700 | [diff] [blame^] | 30 | with open(os.path.join(path), 'rb') as f: |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 31 | project_config.ParseFromString(f.read()) |
| 32 | return project_config |