Alex Klein | e191ed6 | 2020-02-27 15:59:55 -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 | |
| 5 | """Protobuf message utilities. |
| 6 | |
| 7 | The Serializer classes are adapters to standardize the reading and writing of |
| 8 | different protobuf message serialization formats to and from a message. |
| 9 | |
| 10 | The base MessageHandler class encapsulates the functionality of reading |
| 11 | a file containing serialized data into a protobuf message instance, and |
| 12 | writing serialized data from a message instance out to a file. |
| 13 | """ |
| 14 | |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 15 | import logging |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 16 | import os |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 17 | from typing import Optional, TYPE_CHECKING |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 18 | |
Mike Frysinger | 2c02406 | 2021-05-22 15:43:22 -0400 | [diff] [blame] | 19 | from chromite.third_party.google.protobuf import json_format |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 20 | |
| 21 | from chromite.lib import osutils |
| 22 | |
Mike Frysinger | a9b30c7 | 2020-04-18 03:36:03 -0400 | [diff] [blame] | 23 | |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 24 | if TYPE_CHECKING: |
| 25 | from chromite.third_party import google |
| 26 | |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 27 | FORMAT_BINARY = 1 |
| 28 | FORMAT_JSON = 2 |
| 29 | VALID_FORMATS = (FORMAT_BINARY, FORMAT_JSON) |
| 30 | |
| 31 | |
| 32 | class Error(Exception): |
| 33 | """Base error class for the module.""" |
| 34 | |
| 35 | |
| 36 | class InvalidHandlerError(Error): |
| 37 | """Raised when a message handler has no input/output argument when needed.""" |
| 38 | |
| 39 | |
| 40 | class InvalidInputFileError(Error): |
| 41 | """Raised when the input file cannot be read.""" |
| 42 | |
| 43 | |
| 44 | class InvalidInputFormatError(Error): |
| 45 | """Raised when the passed input protobuf can't be parsed.""" |
| 46 | |
| 47 | |
| 48 | class InvalidOutputFileError(Error): |
| 49 | """Raised when the output file cannot be written.""" |
| 50 | |
| 51 | |
| 52 | class UnknownHandlerError(Error): |
| 53 | """Raised when a valid type has not been implemented yet. |
| 54 | |
| 55 | This should only ever be raised when under active development. |
| 56 | See: get_message_handler. |
| 57 | """ |
| 58 | |
| 59 | |
| 60 | def get_message_handler(path, msg_format): |
| 61 | """Get a message handler to handle the given message format.""" |
| 62 | assert msg_format in VALID_FORMATS |
| 63 | |
| 64 | if msg_format == FORMAT_BINARY: |
| 65 | return MessageHandler( |
| 66 | path=path, |
| 67 | serializer=BinarySerializer(), |
| 68 | binary=True, |
| 69 | input_arg='--input-binary', |
| 70 | output_arg='--output-binary', |
| 71 | config_arg='--config-binary') |
| 72 | elif msg_format == FORMAT_JSON: |
| 73 | return MessageHandler( |
| 74 | path=path, |
| 75 | serializer=JsonSerializer(), |
| 76 | binary=False, |
| 77 | input_arg='--input-json', |
| 78 | output_arg='--output-json', |
| 79 | config_arg='--config-json') |
| 80 | else: |
| 81 | # Unexpected. Your new format type needs a case in this function if |
| 82 | # you got this error. |
| 83 | raise UnknownHandlerError('Unknown format type.') |
| 84 | |
| 85 | |
| 86 | class Serializer(object): |
| 87 | """Base (and null) serializer class.""" |
| 88 | |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 89 | def deserialize(self, data: str, message: 'google.protobuf.Message'): |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 90 | """Deserialize the data into the given message. |
| 91 | |
| 92 | Args: |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 93 | data: The message data to deserialize. |
| 94 | message: The message to load the data into. |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 95 | """ |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 96 | |
| 97 | # pylint: disable=unused-argument |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 98 | def serialize(self, message: 'google.protobuf.Message') -> str: |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 99 | """Serialize the message data. |
| 100 | |
| 101 | Args: |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 102 | message: The message to be serialized. |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 103 | |
| 104 | Returns: |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 105 | The message's serialized data. |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 106 | """ |
| 107 | return '' |
| 108 | |
| 109 | |
| 110 | class BinarySerializer(Serializer): |
| 111 | """Protobuf binary serializer class.""" |
| 112 | |
| 113 | def deserialize(self, data, message): |
| 114 | """Deserialize the data into the given message. |
| 115 | |
| 116 | See: Serializer.deserialize |
| 117 | """ |
| 118 | message.ParseFromString(data) |
| 119 | |
| 120 | def serialize(self, message): |
| 121 | """Serialize the message data. |
| 122 | |
| 123 | See: Serializer.serialize |
| 124 | """ |
| 125 | return message.SerializeToString() |
| 126 | |
| 127 | |
| 128 | class JsonSerializer(Serializer): |
| 129 | """Protobuf json serializer class.""" |
| 130 | |
| 131 | def deserialize(self, data, message): |
| 132 | """Deserialize the data into the given message. |
| 133 | |
| 134 | See: Serializer.deserialize |
| 135 | """ |
| 136 | try: |
| 137 | json_format.Parse(data, message, ignore_unknown_fields=True) |
| 138 | except json_format.ParseError as e: |
| 139 | raise InvalidInputFormatError('Unable to parse the input json: %s' % e) |
| 140 | |
| 141 | def serialize(self, message): |
| 142 | """Serialize the message data. |
| 143 | |
| 144 | See: Serializer.serialize |
| 145 | """ |
| 146 | return json_format.MessageToJson( |
Alex Klein | 9aba222 | 2020-04-13 13:43:08 -0600 | [diff] [blame] | 147 | message, sort_keys=True, use_integers_for_enums=True) or '{}' |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 148 | |
| 149 | |
| 150 | class MessageHandler(object): |
| 151 | """Class to handle message (de)serialization to and from files. |
| 152 | |
| 153 | The class is fairly tightly coupled to the build api, but we currently have |
| 154 | no other projected use cases for this, so it's handy. In particular, if we |
| 155 | scrap the "maintain the same input/output/config serialization when reexecing |
| 156 | inside the chroot" convention, this implementation is much less useful and |
| 157 | can be fairly trivially generalized. |
| 158 | |
| 159 | The instance's path is the primary path the message handler was built for. |
| 160 | For the Build API, this means one of the input/output/config arguments. In |
| 161 | practice, it's largely a convenience/shortcut so we don't have to either |
| 162 | track which input files are what types (which we know from the argument used |
| 163 | to pass them in), or create another containing data class for the |
| 164 | functionality provided by the handler and serializer classes and the build |
| 165 | api data. |
| 166 | |
| 167 | Examples: |
| 168 | message_handler = MessageHandler(path, ...) |
| 169 | message = ... |
| 170 | # Parse path into message. |
| 171 | message_handler.read_into(message) |
| 172 | # Write message to a different file. |
| 173 | message_handler.write_into(message, path=other_path) |
| 174 | """ |
| 175 | |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 176 | def __init__(self, path: str, serializer: Serializer, binary: bool, |
| 177 | input_arg: str, output_arg: str, config_arg: str): |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 178 | """MessageHandler init. |
| 179 | |
| 180 | Args: |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 181 | path: The path to the main file associated with this handler. |
| 182 | serializer: The serializer to be used for the messages. |
| 183 | binary: Whether the serialized content is binary. |
| 184 | input_arg: The --input-x argument used for this type. Used for |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 185 | reexecution inside the chroot. |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 186 | output_arg: The --output-x argument used for this type. Used for |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 187 | reexecution inside the chroot. |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 188 | config_arg: The --config-x argument used for this type. Used for |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 189 | reexecution inside the chroot. |
| 190 | """ |
| 191 | self.path = path |
| 192 | self.serializer = serializer |
| 193 | self.read_mode = 'rb' if binary else 'r' |
| 194 | self.write_mode = 'wb' if binary else 'w' |
| 195 | self.input_arg = input_arg |
| 196 | self.output_arg = output_arg |
| 197 | self.config_arg = config_arg |
| 198 | |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 199 | def read_into(self, message: 'google.protobuf.Message', |
| 200 | path: Optional[str] = None): |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 201 | """Read a file containing serialized data into a message. |
| 202 | |
| 203 | Args: |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 204 | message: The message to populate. |
| 205 | path: A path to read. Uses the instance's path when not given. |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 206 | |
| 207 | Raises: |
| 208 | InvalidInputFileError: When a path has not been given, does not exist, |
| 209 | or cannot be read. |
| 210 | """ |
Alex Klein | 7b44c5b | 2020-06-30 10:50:35 -0600 | [diff] [blame] | 211 | target_path = path or self.path |
| 212 | if not target_path: |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 213 | raise InvalidInputFileError('No input file has been specified.') |
Alex Klein | 7b44c5b | 2020-06-30 10:50:35 -0600 | [diff] [blame] | 214 | if not os.path.exists(target_path): |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 215 | raise InvalidInputFileError('The input file does not exist.') |
| 216 | |
| 217 | try: |
Alex Klein | 7b44c5b | 2020-06-30 10:50:35 -0600 | [diff] [blame] | 218 | content = osutils.ReadFile(target_path, mode=self.read_mode) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 219 | except IOError as e: |
| 220 | raise InvalidInputFileError('Unable to read input file: %s' % e) |
| 221 | |
Alex Klein | 7b44c5b | 2020-06-30 10:50:35 -0600 | [diff] [blame] | 222 | if content: |
| 223 | self.serializer.deserialize(content, message) |
| 224 | else: |
| 225 | logging.warning('No content found in %s to deserialize.', target_path) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 226 | |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 227 | def write_from(self, message: 'google.protobuf.Message', |
| 228 | path: Optional[str] = None): |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 229 | """Write serialized data from the message to a file. |
| 230 | |
| 231 | Args: |
Kevin Shelton | d0275c8 | 2022-08-05 01:57:51 +0000 | [diff] [blame^] | 232 | message: The message to serialize and persist. |
| 233 | path: An optional override of the instance's path. |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 234 | |
| 235 | Raises: |
| 236 | InvalidOutputFileError: When no path given, or it cannot be written to. |
| 237 | """ |
| 238 | if not path and not self.path: |
| 239 | raise InvalidOutputFileError('No output file has been specified.') |
| 240 | |
| 241 | try: |
| 242 | osutils.WriteFile( |
| 243 | path or self.path, |
| 244 | self.serializer.serialize(message), |
| 245 | mode=self.write_mode) |
| 246 | except IOError as e: |
| 247 | raise InvalidOutputFileError('Cannot write output file: %s' % e) |