Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -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 | """Utility functions that are useful for controllers.""" |
Lizzy Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 6 | |
| 7 | import glob |
Alex Klein | 28e59a6 | 2021-09-09 15:45:14 -0600 | [diff] [blame] | 8 | import logging |
Lizzy Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 9 | import os |
Brian Norris | 0993701 | 2023-03-31 15:16:55 -0700 | [diff] [blame] | 10 | from pathlib import Path |
Lizzy Presland | 084c20f | 2022-03-30 19:13:50 +0000 | [diff] [blame] | 11 | from typing import Iterable, Optional, TYPE_CHECKING, Union |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 12 | |
Trent Apted | bd3cb41 | 2023-08-18 11:37:02 +1000 | [diff] [blame^] | 13 | from chromite.api.gen.chromite.api import sdk_subtools_pb2 |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 14 | from chromite.api.gen.chromite.api import sysroot_pb2 |
| 15 | from chromite.api.gen.chromite.api import test_pb2 |
Alex Klein | 1f67cf3 | 2019-10-09 11:13:42 -0600 | [diff] [blame] | 16 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | 247d792 | 2023-01-18 15:36:02 -0700 | [diff] [blame] | 17 | from chromite.lib import binpkg |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 18 | from chromite.lib import build_target_lib |
George Engelbrecht | 35b6e8d | 2021-06-18 09:43:28 -0600 | [diff] [blame] | 19 | from chromite.lib import chroot_lib |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 20 | from chromite.lib import constants |
Ram Chandrasekar | e08e3ba | 2022-04-04 21:42:27 +0000 | [diff] [blame] | 21 | from chromite.lib import goma_lib |
Joanna Wang | 92cad81 | 2021-11-03 14:52:08 -0700 | [diff] [blame] | 22 | from chromite.lib import remoteexec_util |
George Engelbrecht | 35b6e8d | 2021-06-18 09:43:28 -0600 | [diff] [blame] | 23 | from chromite.lib import sysroot_lib |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 24 | from chromite.lib.parser import package_info |
| 25 | |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 26 | |
Alex Klein | 46c30f3 | 2021-11-10 13:12:50 -0700 | [diff] [blame] | 27 | if TYPE_CHECKING: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 28 | from chromite.api.gen.chromiumos.build.api import portage_pb2 |
| 29 | |
Alex Klein | 46c30f3 | 2021-11-10 13:12:50 -0700 | [diff] [blame] | 30 | |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 31 | class Error(Exception): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 32 | """Base error class for the module.""" |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 33 | |
| 34 | |
| 35 | class InvalidMessageError(Error): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 36 | """Invalid message.""" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 37 | |
| 38 | |
Kevin Shelton | a805636 | 2022-04-04 16:19:23 -0700 | [diff] [blame] | 39 | def ParseChroot(chroot_message: common_pb2.Chroot) -> chroot_lib.Chroot: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 40 | """Create a chroot object from the chroot message. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 41 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 42 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 43 | chroot_message: The chroot message. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 46 | Chroot: The parsed chroot object. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 47 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 48 | Raises: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 49 | AssertionError: When the message is not a Chroot message. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | """ |
| 51 | assert isinstance(chroot_message, common_pb2.Chroot) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 52 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 53 | path = chroot_message.path or constants.DEFAULT_CHROOT_PATH |
| 54 | cache_dir = chroot_message.cache_dir |
| 55 | chrome_root = chroot_message.chrome_dir |
Brian Norris | a8a1e95 | 2023-04-25 15:03:11 -0700 | [diff] [blame] | 56 | out_path = ( |
| 57 | Path(chroot_message.out_path) |
| 58 | if chroot_message.out_path |
| 59 | else constants.DEFAULT_OUT_PATH |
| 60 | ) |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 61 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | use_flags = [u.flag for u in chroot_message.env.use_flags] |
| 63 | features = [f.feature for f in chroot_message.env.features] |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 64 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 65 | env = {} |
| 66 | if use_flags: |
| 67 | env["USE"] = " ".join(use_flags) |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 68 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | # Make sure it'll use the local source to build chrome when we have it. |
| 70 | if chrome_root: |
| 71 | env["CHROME_ORIGIN"] = "LOCAL_SOURCE" |
Alex Klein | b7485bb | 2019-09-19 13:23:37 -0600 | [diff] [blame] | 72 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | if features: |
| 74 | env["FEATURES"] = " ".join(features) |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | chroot = chroot_lib.Chroot( |
Brian Norris | d0dfeae | 2023-03-09 13:06:47 -0800 | [diff] [blame] | 77 | path=path, |
| 78 | out_path=out_path, |
| 79 | cache_dir=cache_dir, |
| 80 | chrome_root=chrome_root, |
| 81 | env=env, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 82 | ) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | return chroot |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 85 | |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 86 | |
Kevin Shelton | a805636 | 2022-04-04 16:19:23 -0700 | [diff] [blame] | 87 | def ParseSysroot(sysroot_message: sysroot_pb2.Sysroot) -> sysroot_lib.Sysroot: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 88 | """Create a sysroot object from the sysroot message. |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 89 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 91 | sysroot_message: The sysroot message. |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 92 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 93 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 94 | Sysroot: The parsed sysroot object. |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 95 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 96 | Raises: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 97 | AssertionError: When the message is not a Sysroot message. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 98 | """ |
| 99 | assert isinstance(sysroot_message, sysroot_pb2.Sysroot) |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 100 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 101 | return sysroot_lib.Sysroot(sysroot_message.path) |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 102 | |
| 103 | |
Joanna Wang | 92cad81 | 2021-11-03 14:52:08 -0700 | [diff] [blame] | 104 | def ParseRemoteexecConfig(remoteexec_message: common_pb2.RemoteexecConfig): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 105 | """Parse a remoteexec config message.""" |
| 106 | assert isinstance(remoteexec_message, common_pb2.RemoteexecConfig) |
Joanna Wang | 92cad81 | 2021-11-03 14:52:08 -0700 | [diff] [blame] | 107 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | if not ( |
| 109 | remoteexec_message.reclient_dir or remoteexec_message.reproxy_cfg_file |
| 110 | ): |
| 111 | return None |
Joanna Wang | 92cad81 | 2021-11-03 14:52:08 -0700 | [diff] [blame] | 112 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 113 | return remoteexec_util.Remoteexec( |
| 114 | remoteexec_message.reclient_dir, remoteexec_message.reproxy_cfg_file |
| 115 | ) |
Joanna Wang | 92cad81 | 2021-11-03 14:52:08 -0700 | [diff] [blame] | 116 | |
| 117 | |
Brian Norris | d0dfeae | 2023-03-09 13:06:47 -0800 | [diff] [blame] | 118 | def ParseGomaConfig(goma_message, chroot_path, out_path): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 119 | """Parse a goma config message.""" |
| 120 | assert isinstance(goma_message, common_pb2.GomaConfig) |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 121 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 122 | if not goma_message.goma_dir: |
| 123 | return None |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | # Parse the goma config. |
| 126 | chromeos_goma_dir = goma_message.chromeos_goma_dir or None |
| 127 | if goma_message.goma_approach == common_pb2.GomaConfig.RBE_STAGING: |
| 128 | goma_approach = goma_lib.GomaApproach( |
| 129 | "?staging", "staging-goma.chromium.org", True |
| 130 | ) |
| 131 | elif goma_message.goma_approach == common_pb2.GomaConfig.RBE_PROD: |
| 132 | goma_approach = goma_lib.GomaApproach( |
| 133 | "?prod", "goma.chromium.org", True |
| 134 | ) |
| 135 | else: |
| 136 | goma_approach = goma_lib.GomaApproach( |
| 137 | "?cros", "goma.chromium.org", True |
| 138 | ) |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 139 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 140 | # Note that we are not specifying the goma log_dir so that goma will create |
| 141 | # and use a tmp dir for the logs. |
| 142 | stats_filename = goma_message.stats_file or None |
| 143 | counterz_filename = goma_message.counterz_file or None |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 144 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | return goma_lib.Goma( |
| 146 | goma_message.goma_dir, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 147 | stage_name="BuildAPI", |
| 148 | chromeos_goma_dir=chromeos_goma_dir, |
| 149 | chroot_dir=chroot_path, |
Brian Norris | d0dfeae | 2023-03-09 13:06:47 -0800 | [diff] [blame] | 150 | out_dir=out_path, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 151 | goma_approach=goma_approach, |
| 152 | stats_filename=stats_filename, |
| 153 | counterz_filename=counterz_filename, |
| 154 | ) |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 155 | |
| 156 | |
Kevin Shelton | a805636 | 2022-04-04 16:19:23 -0700 | [diff] [blame] | 157 | def ParseBuildTarget( |
| 158 | build_target_message: common_pb2.BuildTarget, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 159 | profile_message: Optional[sysroot_pb2.Profile] = None, |
Kevin Shelton | a805636 | 2022-04-04 16:19:23 -0700 | [diff] [blame] | 160 | ) -> build_target_lib.BuildTarget: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 161 | """Create a BuildTarget object from a build_target message. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 162 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 164 | build_target_message: The BuildTarget message. |
| 165 | profile_message: The profile message. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 166 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 167 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 168 | BuildTarget: The parsed instance. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 169 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 170 | Raises: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 171 | AssertionError: When the field is not a BuildTarget message. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 172 | """ |
| 173 | assert isinstance(build_target_message, common_pb2.BuildTarget) |
| 174 | assert profile_message is None or isinstance( |
| 175 | profile_message, sysroot_pb2.Profile |
| 176 | ) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 177 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | profile_name = profile_message.name if profile_message else None |
| 179 | return build_target_lib.BuildTarget( |
| 180 | build_target_message.name, profile=profile_name |
| 181 | ) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 182 | |
| 183 | |
| 184 | def ParseBuildTargets(repeated_build_target_field): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 185 | """Create a BuildTarget for each entry in the repeated field. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 186 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 187 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 188 | repeated_build_target_field: The repeated BuildTarget field. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 189 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 190 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 191 | list[BuildTarget]: The parsed BuildTargets. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 192 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 193 | Raises: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 194 | AssertionError: When the field contains non-BuildTarget messages. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 195 | """ |
| 196 | return [ParseBuildTarget(target) for target in repeated_build_target_field] |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 197 | |
| 198 | |
Alex Klein | 247d792 | 2023-01-18 15:36:02 -0700 | [diff] [blame] | 199 | def deserialize_profile(profile: common_pb2.Profile) -> sysroot_lib.Profile: |
| 200 | """Deserialize a portage profile message to a Profile object.""" |
| 201 | return sysroot_lib.Profile(profile.name) |
| 202 | |
| 203 | |
| 204 | def deserialize_package_index_info( |
| 205 | message: common_pb2.PackageIndexInfo, |
| 206 | ) -> binpkg.PackageIndexInfo: |
| 207 | """Deserialize a PackageIndexInfo message to an object.""" |
| 208 | return binpkg.PackageIndexInfo( |
| 209 | snapshot_sha=message.snapshot_sha, |
| 210 | snapshot_number=message.snapshot_number, |
| 211 | build_target=ParseBuildTarget(message.build_target), |
| 212 | profile=deserialize_profile(message.profile), |
| 213 | location=message.location, |
| 214 | ) |
| 215 | |
| 216 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 217 | def serialize_package_info( |
| 218 | pkg_info: package_info.PackageInfo, |
| 219 | pkg_info_msg: Union[common_pb2.PackageInfo, "portage_pb2.Portage.Package"], |
| 220 | ): |
| 221 | """Serialize a PackageInfo object to a PackageInfo proto.""" |
| 222 | if not isinstance(pkg_info, package_info.PackageInfo): |
| 223 | # Allows us to swap everything to serialize_package_info, and search the |
| 224 | # logs for usages that aren't passing though a PackageInfo yet. |
| 225 | logging.warning( |
| 226 | "serialize_package_info: Got a %s instead of a PackageInfo.", |
| 227 | type(pkg_info), |
| 228 | ) |
| 229 | pkg_info = package_info.parse(pkg_info) |
| 230 | pkg_info_msg.package_name = pkg_info.package |
| 231 | if pkg_info.category: |
| 232 | pkg_info_msg.category = pkg_info.category |
| 233 | if pkg_info.vr: |
| 234 | pkg_info_msg.version = pkg_info.vr |
Alex Klein | 1e68a8e | 2020-10-06 17:25:11 -0600 | [diff] [blame] | 235 | |
| 236 | |
| 237 | def deserialize_package_info(pkg_info_msg): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 238 | """Deserialize a PackageInfo message to a PackageInfo object.""" |
| 239 | return package_info.parse(PackageInfoToString(pkg_info_msg)) |
Alex Klein | 1e68a8e | 2020-10-06 17:25:11 -0600 | [diff] [blame] | 240 | |
| 241 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 242 | def retrieve_package_log_paths( |
| 243 | packages: Iterable[package_info.PackageInfo], |
| 244 | output_proto: Union[ |
| 245 | sysroot_pb2.InstallPackagesResponse, |
| 246 | sysroot_pb2.InstallToolchainResponse, |
Trent Apted | bd3cb41 | 2023-08-18 11:37:02 +1000 | [diff] [blame^] | 247 | sdk_subtools_pb2.BuildSdkSubtoolsResponse, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 248 | test_pb2.BuildTargetUnitTestResponse, |
| 249 | ], |
| 250 | target_sysroot: sysroot_lib.Sysroot, |
| 251 | ) -> None: |
| 252 | """Get the path to the log file for each package that failed to build. |
Lizzy Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 253 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 254 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 255 | packages: A list of packages which failed to build. |
| 256 | output_proto: The Response message for a given API call. This response |
| 257 | proto must contain a failed_package_data field. |
| 258 | target_sysroot: The sysroot used by the build step. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 259 | """ |
| 260 | for pkg_info in packages: |
| 261 | # Grab the paths to the log files for each failed package from the |
| 262 | # sysroot. |
| 263 | # Logs currently exist within the sysroot in the form of: |
| 264 | # /build/${BOARD}/tmp/portage/logs/$CATEGORY:$PF:$TIMESTAMP.log |
| 265 | failed_pkg_data_msg = output_proto.failed_package_data.add() |
| 266 | serialize_package_info(pkg_info, failed_pkg_data_msg.name) |
| 267 | glob_path = os.path.join( |
| 268 | target_sysroot.portage_logdir, |
| 269 | f"{pkg_info.category}:{pkg_info.pvr}:*.log", |
| 270 | ) |
| 271 | log_files = glob.glob(glob_path) |
| 272 | log_files.sort(reverse=True) |
| 273 | # Omit path if files don't exist for some reason. |
| 274 | if not log_files: |
| 275 | logging.warning( |
| 276 | "Log file for %s was not found. Search path: %s", |
| 277 | pkg_info.cpvr, |
| 278 | glob_path, |
| 279 | ) |
| 280 | continue |
| 281 | failed_pkg_data_msg.log_path.path = log_files[0] |
| 282 | failed_pkg_data_msg.log_path.location = common_pb2.Path.INSIDE |
Lizzy Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 283 | |
| 284 | |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 285 | def PackageInfoToString(package_info_msg): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 286 | """Combine the components into the full package string.""" |
| 287 | # TODO: Use the lib.parser.package_info.PackageInfo class instead. |
| 288 | if not package_info_msg.package_name: |
| 289 | raise ValueError("Invalid PackageInfo message.") |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 290 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 291 | c = ("%s/" % package_info_msg.category) if package_info_msg.category else "" |
| 292 | p = package_info_msg.package_name |
| 293 | v = ("-%s" % package_info_msg.version) if package_info_msg.version else "" |
| 294 | return "%s%s%s" % (c, p, v) |
Greg Edelston | 1f5deb6 | 2023-03-31 14:22:08 -0600 | [diff] [blame] | 295 | |
| 296 | |
| 297 | def pb2_path_to_pathlib_path( |
| 298 | pb2_path: "common_pb2.Path", |
| 299 | chroot: Optional["common_pb2.Chroot"] = None, |
| 300 | ) -> Path: |
| 301 | """Convert an absolute pb2 path to a pathlib.Path outside the chroot. |
| 302 | |
| 303 | Args: |
| 304 | pb2_path: An absolute path, which might be inside or outside chroot. |
| 305 | chroot: The chroot that the path might be inside of. |
| 306 | |
| 307 | Returns: |
| 308 | A Path pointing to the same location as pb2_path, originating |
| 309 | outside the chroot. |
| 310 | |
| 311 | Raises: |
| 312 | ValueError: If the given path is relative instead of absolute. |
| 313 | ValueError: If the given path is inside the chroot, but a chroot is not |
| 314 | provided. |
| 315 | """ |
| 316 | if pb2_path.path[0] != "/": |
| 317 | raise ValueError(f"Cannot convert relative path: {pb2_path.path}") |
| 318 | if pb2_path.location is common_pb2.Path.Location.OUTSIDE: |
| 319 | return Path(pb2_path.path) |
| 320 | if chroot is None: |
| 321 | raise ValueError("Cannot convert inside path without a chroot.") |
Brian Norris | 41f247b | 2023-06-30 11:09:40 -0700 | [diff] [blame] | 322 | return Path(ParseChroot(chroot).full_path(pb2_path.path)) |