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