Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Field handler classes. |
| 6 | |
| 7 | The field handlers are meant to parse information from or do some other generic |
| 8 | action for a specific field type for the build_api script. |
| 9 | """ |
| 10 | |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 11 | import contextlib |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 12 | import functools |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 13 | import logging |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 14 | import os |
| 15 | import shutil |
Mike Frysinger | 4044359 | 2022-05-05 13:03:40 -0400 | [diff] [blame] | 16 | from typing import Iterator, List, Optional, TYPE_CHECKING |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 17 | |
Mike Frysinger | 2c02406 | 2021-05-22 15:43:22 -0400 | [diff] [blame] | 18 | from chromite.third_party.google.protobuf import message as protobuf_message |
Mike Frysinger | 849d640 | 2019-10-17 00:14:16 -0400 | [diff] [blame] | 19 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 20 | from chromite.api.controller import controller_util |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 21 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 22 | from chromite.lib import osutils |
| 23 | |
| 24 | |
Mike Frysinger | 4044359 | 2022-05-05 13:03:40 -0400 | [diff] [blame] | 25 | if TYPE_CHECKING: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 26 | from chromite.lib import chroot_lib |
Mike Frysinger | 4044359 | 2022-05-05 13:03:40 -0400 | [diff] [blame] | 27 | |
| 28 | |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 29 | class Error(Exception): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 30 | """Base error class for the module.""" |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 31 | |
| 32 | |
| 33 | class InvalidResultPathError(Error): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 34 | """Result path is invalid.""" |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 35 | |
| 36 | |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 37 | class ChrootHandler(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | """Translate a Chroot message to chroot enter arguments and env.""" |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 39 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 40 | def __init__(self, clear_field): |
| 41 | self.clear_field = clear_field |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 42 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 43 | def handle(self, message, recurse=True) -> Optional["chroot_lib.Chroot"]: |
| 44 | """Parse a message for a chroot field.""" |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame^] | 45 | # Find the Chroot field. Search for the field by type to prevent it |
| 46 | # being tied to a naming convention. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | for descriptor in message.DESCRIPTOR.fields: |
| 48 | field = getattr(message, descriptor.name) |
| 49 | if isinstance(field, common_pb2.Chroot): |
| 50 | chroot = field |
| 51 | if self.clear_field: |
| 52 | message.ClearField(descriptor.name) |
| 53 | return self.parse_chroot(chroot) |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 54 | |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame^] | 55 | # Recurse down one level. This is handy for meta-endpoints that use |
| 56 | # another endpoint's request to produce data for or about the second |
| 57 | # endpoint. e.g. PackageService/NeedsChromeSource. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 58 | if recurse: |
| 59 | for descriptor in message.DESCRIPTOR.fields: |
| 60 | field = getattr(message, descriptor.name) |
| 61 | if isinstance(field, protobuf_message.Message): |
| 62 | chroot = self.handle(field, recurse=False) |
| 63 | if chroot: |
| 64 | return chroot |
Alex Klein | 6becabc | 2020-09-11 14:03:05 -0600 | [diff] [blame] | 65 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 66 | return None |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 67 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 68 | def parse_chroot( |
| 69 | self, chroot_message: common_pb2.Chroot |
| 70 | ) -> "chroot_lib.Chroot": |
| 71 | """Parse a Chroot message instance.""" |
| 72 | return controller_util.ParseChroot(chroot_message) |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 73 | |
| 74 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 75 | def handle_chroot( |
| 76 | message: protobuf_message.Message, clear_field: Optional[bool] = True |
| 77 | ) -> "chroot_lib.Chroot": |
| 78 | """Find and parse the chroot field, returning the Chroot instance.""" |
| 79 | handler = ChrootHandler(clear_field) |
| 80 | chroot = handler.handle(message) |
| 81 | if chroot: |
| 82 | return chroot |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | logging.warning("No chroot message found, falling back to defaults.") |
| 85 | return handler.parse_chroot(common_pb2.Chroot()) |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 86 | |
| 87 | |
Alex Klein | 9b7331e | 2019-12-30 14:37:21 -0700 | [diff] [blame] | 88 | def handle_goma(message, chroot_path): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 89 | """Find and parse the GomaConfig field, returning the Goma instance.""" |
| 90 | for descriptor in message.DESCRIPTOR.fields: |
| 91 | field = getattr(message, descriptor.name) |
| 92 | if isinstance(field, common_pb2.GomaConfig): |
| 93 | goma_config = field |
| 94 | return controller_util.ParseGomaConfig(goma_config, chroot_path) |
Alex Klein | 9b7331e | 2019-12-30 14:37:21 -0700 | [diff] [blame] | 95 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 96 | return None |
Alex Klein | 9b7331e | 2019-12-30 14:37:21 -0700 | [diff] [blame] | 97 | |
| 98 | |
Joanna Wang | 92cad81 | 2021-11-03 14:52:08 -0700 | [diff] [blame] | 99 | def handle_remoteexec(message: protobuf_message.Message): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 100 | """Find the RemoteexecConfig field, returning the Remoteexec instance.""" |
| 101 | for descriptor in message.DESCRIPTOR.fields: |
| 102 | field = getattr(message, descriptor.name) |
| 103 | if isinstance(field, common_pb2.RemoteexecConfig): |
| 104 | remoteexec_config = field |
| 105 | return controller_util.ParseRemoteexecConfig(remoteexec_config) |
Joanna Wang | 92cad81 | 2021-11-03 14:52:08 -0700 | [diff] [blame] | 106 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 107 | return None |
Joanna Wang | 92cad81 | 2021-11-03 14:52:08 -0700 | [diff] [blame] | 108 | |
| 109 | |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 110 | class PathHandler(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 111 | """Handles copying a file or directory into or out of the chroot.""" |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 112 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 113 | INSIDE = common_pb2.Path.INSIDE |
| 114 | OUTSIDE = common_pb2.Path.OUTSIDE |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 115 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 116 | def __init__( |
| 117 | self, |
| 118 | field: common_pb2.Path, |
| 119 | destination: str, |
| 120 | delete: bool, |
| 121 | prefix: Optional[str] = None, |
| 122 | reset: Optional[bool] = True, |
| 123 | ) -> None: |
| 124 | """Path handler initialization. |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 125 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 126 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 127 | field: The Path message. |
| 128 | destination: The destination base path. |
| 129 | delete: Whether the copied file(s) should be deleted on cleanup. |
| 130 | prefix: A path prefix to remove from the destination path when |
| 131 | moving files inside the chroot, or to add to the source paths |
| 132 | when moving files out of the chroot. |
| 133 | reset: Whether to reset the state on cleanup. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 134 | """ |
| 135 | assert isinstance(field, common_pb2.Path) |
| 136 | assert field.path |
| 137 | assert field.location |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 138 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 139 | self.field = field |
| 140 | self.destination = destination |
| 141 | self.prefix = "" if prefix is None else str(prefix) |
| 142 | self.delete = delete |
| 143 | self.tempdir = None |
| 144 | self.reset = reset |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 145 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 146 | # For resetting the state. |
| 147 | self._transferred = False |
| 148 | self._original_message = common_pb2.Path() |
| 149 | self._original_message.CopyFrom(self.field) |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 150 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 151 | def transfer(self, direction: int) -> None: |
| 152 | """Copy the file or directory to its destination. |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 153 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 154 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 155 | direction: The direction files are being copied (into or out of the |
| 156 | chroot). Specifying the direction allows avoiding performing |
| 157 | unnecessary copies. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 158 | """ |
| 159 | if self._transferred: |
| 160 | return |
Alex Klein | aa70541 | 2019-06-04 15:00:30 -0600 | [diff] [blame] | 161 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 162 | assert direction in [self.INSIDE, self.OUTSIDE] |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 163 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 164 | if self.field.location == direction: |
| 165 | # Already in the correct location, nothing to do. |
| 166 | return |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 167 | |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame^] | 168 | # Create a tempdir for the copied file if we're cleaning it up |
| 169 | # afterwords. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 170 | if self.delete: |
| 171 | self.tempdir = osutils.TempDir(base_dir=self.destination) |
| 172 | destination = self.tempdir.tempdir |
| 173 | else: |
| 174 | destination = self.destination |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 175 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 176 | source = self.field.path |
| 177 | if direction == self.OUTSIDE and self.prefix: |
| 178 | # When we're extracting files, we need /tmp/result to be |
| 179 | # /path/to/chroot/tmp/result. |
| 180 | source = os.path.join(self.prefix, source.lstrip(os.sep)) |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 181 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 182 | if os.path.isfile(source): |
| 183 | # File - use the old file name, just copy it into the destination. |
| 184 | dest_path = os.path.join(destination, os.path.basename(source)) |
| 185 | copy_fn = shutil.copy |
| 186 | else: |
| 187 | # Directory - just copy everything into the new location. |
| 188 | dest_path = destination |
| 189 | copy_fn = functools.partial( |
| 190 | osutils.CopyDirContents, allow_nonempty=True |
| 191 | ) |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 192 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 193 | logging.debug("Copying %s to %s", source, dest_path) |
| 194 | copy_fn(source, dest_path) |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 195 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 196 | # Clean up the destination path for returning, if applicable. |
| 197 | return_path = dest_path |
| 198 | if direction == self.INSIDE and return_path.startswith(self.prefix): |
| 199 | return_path = return_path[len(self.prefix) :] |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 200 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 201 | self.field.path = return_path |
| 202 | self.field.location = direction |
| 203 | self._transferred = True |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 204 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 205 | def cleanup(self): |
Alex Klein | b6d5202 | 2022-10-18 08:55:06 -0600 | [diff] [blame] | 206 | """Post-execution cleanup.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 207 | if self.tempdir: |
| 208 | self.tempdir.Cleanup() |
| 209 | self.tempdir = None |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 210 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 211 | if self.reset: |
| 212 | self.field.CopyFrom(self._original_message) |
Alex Klein | aa70541 | 2019-06-04 15:00:30 -0600 | [diff] [blame] | 213 | |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 214 | |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 215 | class SyncedDirHandler(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 216 | """Handler for syncing directories across the chroot boundary.""" |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 217 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 218 | def __init__(self, field, destination, prefix): |
| 219 | self.field = field |
| 220 | self.prefix = prefix |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 221 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 222 | self.source = self.field.dir |
| 223 | if not self.source.endswith(os.sep): |
| 224 | self.source += os.sep |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 225 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 226 | self.destination = destination |
| 227 | if not self.destination.endswith(os.sep): |
| 228 | self.destination += os.sep |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 229 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 230 | # For resetting the message later. |
| 231 | self._original_message = common_pb2.SyncedDir() |
| 232 | self._original_message.CopyFrom(self.field) |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 233 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 234 | def _sync(self, src, dest): |
| 235 | logging.info("Syncing %s to %s", src, dest) |
| 236 | # TODO: This would probably be more efficient with rsync. |
| 237 | osutils.EmptyDir(dest) |
| 238 | osutils.CopyDirContents(src, dest) |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 239 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 240 | def sync_in(self): |
| 241 | """Sync files from the source directory to the destination directory.""" |
| 242 | self._sync(self.source, self.destination) |
| 243 | self.field.dir = "/%s" % os.path.relpath(self.destination, self.prefix) |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 244 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 245 | def sync_out(self): |
| 246 | """Sync files from the destination directory to the source directory.""" |
| 247 | self._sync(self.destination, self.source) |
| 248 | self.field.CopyFrom(self._original_message) |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 249 | |
| 250 | |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 251 | @contextlib.contextmanager |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 252 | def copy_paths_in( |
| 253 | message: protobuf_message.Message, |
| 254 | destination: str, |
| 255 | delete: Optional[bool] = True, |
| 256 | prefix: Optional[str] = None, |
| 257 | ) -> Iterator[List[PathHandler]]: |
| 258 | """Context manager function to transfer and cleanup all Path messages. |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 259 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 260 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 261 | message: A message whose Path messages should be transferred. |
| 262 | destination: The base destination path. |
| 263 | delete: Whether the file(s) should be deleted. |
| 264 | prefix: A prefix path to remove from the final destination path in the |
| 265 | Path message (i.e. remove the chroot path). |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 266 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 267 | Yields: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 268 | list[PathHandler]: The path handlers. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 269 | """ |
| 270 | assert destination |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 271 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 272 | handlers = _extract_handlers( |
| 273 | message, destination, prefix, delete=delete, reset=True |
| 274 | ) |
Alex Klein | aa70541 | 2019-06-04 15:00:30 -0600 | [diff] [blame] | 275 | |
Alex Klein | aa70541 | 2019-06-04 15:00:30 -0600 | [diff] [blame] | 276 | for handler in handlers: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 277 | handler.transfer(PathHandler.INSIDE) |
| 278 | |
| 279 | try: |
| 280 | yield handlers |
| 281 | finally: |
| 282 | for handler in handlers: |
| 283 | handler.cleanup() |
Alex Klein | aa70541 | 2019-06-04 15:00:30 -0600 | [diff] [blame] | 284 | |
| 285 | |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 286 | @contextlib.contextmanager |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 287 | def sync_dirs( |
| 288 | message: protobuf_message.Message, destination: str, prefix: str |
| 289 | ) -> Iterator[SyncedDirHandler]: |
| 290 | """Context manager function to handle SyncedDir messages. |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 291 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 292 | The sync semantics are effectively: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 293 | rsync -r --del source/ destination/ |
| 294 | * The endpoint runs. * |
| 295 | rsync -r --del destination/ source/ |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 296 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 297 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 298 | message: A message whose SyncedPath messages should be synced. |
| 299 | destination: The destination path. |
| 300 | prefix: A prefix path to remove from the final destination path in the |
| 301 | Path message (i.e. remove the chroot path). |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 302 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 303 | Yields: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 304 | The handlers. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 305 | """ |
| 306 | assert destination |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 307 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 308 | handlers = _extract_handlers( |
| 309 | message, |
| 310 | destination, |
| 311 | prefix=prefix, |
| 312 | delete=False, |
| 313 | reset=True, |
| 314 | message_type=common_pb2.SyncedDir, |
| 315 | ) |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 316 | |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 317 | for handler in handlers: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 318 | handler.sync_in() |
| 319 | |
| 320 | try: |
| 321 | yield handlers |
| 322 | finally: |
| 323 | for handler in handlers: |
| 324 | handler.sync_out() |
Alex Klein | f0717a6 | 2019-12-06 09:45:00 -0700 | [diff] [blame] | 325 | |
| 326 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 327 | def extract_results( |
| 328 | request_message: protobuf_message.Message, |
| 329 | response_message: protobuf_message.Message, |
| 330 | chroot: "chroot_lib.Chroot", |
| 331 | ) -> None: |
| 332 | """Transfer all response Path messages to the request's ResultPath. |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 333 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 334 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 335 | request_message: The request message containing a ResultPath message. |
| 336 | response_message: The response message whose Path message(s) are to be |
| 337 | transferred. |
| 338 | chroot: The chroot the files are being copied out of. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 339 | """ |
| 340 | # Find the ResultPath. |
| 341 | for descriptor in request_message.DESCRIPTOR.fields: |
| 342 | field = getattr(request_message, descriptor.name) |
| 343 | if isinstance(field, common_pb2.ResultPath): |
| 344 | result_path_message = field |
| 345 | break |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 346 | else: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 347 | # No ResultPath to handle. |
| 348 | return |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 349 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 350 | destination = result_path_message.path.path |
| 351 | handlers = _extract_handlers( |
| 352 | response_message, destination, chroot.path, delete=False, reset=False |
| 353 | ) |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 354 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 355 | for handler in handlers: |
| 356 | handler.transfer(PathHandler.OUTSIDE) |
| 357 | handler.cleanup() |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 358 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 359 | |
| 360 | def _extract_handlers( |
| 361 | message, |
| 362 | destination, |
| 363 | prefix, |
| 364 | delete=False, |
| 365 | reset=False, |
| 366 | field_name=None, |
| 367 | message_type=None, |
| 368 | ): |
| 369 | """Recursive helper for handle_paths to extract Path messages.""" |
| 370 | message_type = message_type or common_pb2.Path |
| 371 | is_path_target = message_type is common_pb2.Path |
| 372 | is_synced_target = message_type is common_pb2.SyncedDir |
| 373 | |
| 374 | is_message = isinstance(message, protobuf_message.Message) |
| 375 | is_result_path = isinstance(message, common_pb2.ResultPath) |
| 376 | if not is_message or is_result_path: |
| 377 | # Base case: Nothing to handle. |
| 378 | # There's nothing we can do with scalar values. |
| 379 | # Skip ResultPath instances to avoid unnecessary file copying. |
| 380 | return [] |
| 381 | elif is_path_target and isinstance(message, common_pb2.Path): |
| 382 | # Base case: Create handler for this message. |
| 383 | if not message.path or not message.location: |
| 384 | logging.debug("Skipping %s; incomplete.", field_name or "message") |
| 385 | return [] |
| 386 | |
| 387 | handler = PathHandler( |
| 388 | message, destination, delete=delete, prefix=prefix, reset=reset |
| 389 | ) |
| 390 | return [handler] |
| 391 | elif is_synced_target and isinstance(message, common_pb2.SyncedDir): |
| 392 | if not message.dir: |
| 393 | logging.debug( |
| 394 | "Skipping %s; no directory given.", field_name or "message" |
| 395 | ) |
| 396 | return [] |
| 397 | |
| 398 | handler = SyncedDirHandler(message, destination, prefix) |
| 399 | return [handler] |
| 400 | |
| 401 | # Iterate through each field and recurse. |
| 402 | handlers = [] |
| 403 | for descriptor in message.DESCRIPTOR.fields: |
| 404 | field = getattr(message, descriptor.name) |
| 405 | if field_name: |
| 406 | new_field_name = "%s.%s" % (field_name, descriptor.name) |
| 407 | else: |
| 408 | new_field_name = descriptor.name |
| 409 | |
| 410 | if isinstance(field, protobuf_message.Message): |
| 411 | # Recurse for nested Paths. |
| 412 | handlers.extend( |
| 413 | _extract_handlers( |
| 414 | field, |
| 415 | destination, |
| 416 | prefix, |
| 417 | delete, |
| 418 | reset, |
| 419 | field_name=new_field_name, |
| 420 | message_type=message_type, |
| 421 | ) |
| 422 | ) |
| 423 | else: |
| 424 | # If it's iterable it may be a repeated field, try each element. |
| 425 | try: |
| 426 | iterator = iter(field) |
| 427 | except TypeError: |
| 428 | # Definitely not a repeated field, just move on. |
| 429 | continue |
| 430 | |
| 431 | for element in iterator: |
| 432 | handlers.extend( |
| 433 | _extract_handlers( |
| 434 | element, |
| 435 | destination, |
| 436 | prefix, |
| 437 | delete, |
| 438 | reset, |
| 439 | field_name=new_field_name, |
| 440 | message_type=message_type, |
| 441 | ) |
| 442 | ) |
| 443 | |
| 444 | return handlers |