blob: d10544484b48ab563bcc73963097657e2374e161 [file] [log] [blame]
Alex Kleina9d500b2019-04-22 15:37:51 -06001# Copyright 2019 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"""Utility functions that are useful for controllers."""
Lizzy Presland29e62452022-01-05 21:58:21 +00006
7import glob
Alex Klein28e59a62021-09-09 15:45:14 -06008import logging
Lizzy Presland29e62452022-01-05 21:58:21 +00009import os
Lizzy Presland084c20f2022-03-30 19:13:50 +000010from typing import Iterable, Optional, TYPE_CHECKING, Union
Alex Kleina9d500b2019-04-22 15:37:51 -060011
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040012from chromite.api.gen.chromite.api import sysroot_pb2
13from chromite.api.gen.chromite.api import test_pb2
Alex Klein1f67cf32019-10-09 11:13:42 -060014from chromite.api.gen.chromiumos import common_pb2
Alex Klein26e472b2020-03-10 14:35:01 -060015from chromite.lib import build_target_lib
George Engelbrecht35b6e8d2021-06-18 09:43:28 -060016from chromite.lib import chroot_lib
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040017from chromite.lib import constants
Ram Chandrasekare08e3ba2022-04-04 21:42:27 +000018from chromite.lib import goma_lib
Joanna Wang92cad812021-11-03 14:52:08 -070019from chromite.lib import remoteexec_util
George Engelbrecht35b6e8d2021-06-18 09:43:28 -060020from chromite.lib import sysroot_lib
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040021from chromite.lib.parser import package_info
22
Alex Klein171da612019-08-06 14:00:42 -060023
Alex Klein46c30f32021-11-10 13:12:50 -070024if TYPE_CHECKING:
25 from chromite.api.gen.chromiumos.build.api import portage_pb2
26
Alex Klein171da612019-08-06 14:00:42 -060027class Error(Exception):
28 """Base error class for the module."""
29
30
31class InvalidMessageError(Error):
32 """Invalid message."""
Alex Kleina9d500b2019-04-22 15:37:51 -060033
34
Kevin Sheltona8056362022-04-04 16:19:23 -070035def ParseChroot(chroot_message: common_pb2.Chroot) -> chroot_lib.Chroot:
Alex Klein171da612019-08-06 14:00:42 -060036 """Create a chroot object from the chroot message.
37
38 Args:
Kevin Sheltona8056362022-04-04 16:19:23 -070039 chroot_message: The chroot message.
Alex Klein171da612019-08-06 14:00:42 -060040
41 Returns:
42 Chroot: The parsed chroot object.
43
44 Raises:
45 AssertionError: When the message is not a Chroot message.
46 """
47 assert isinstance(chroot_message, common_pb2.Chroot)
48
Alex Klein915cce92019-12-17 14:19:50 -070049 path = chroot_message.path or constants.DEFAULT_CHROOT_PATH
Alex Klein4f0eb432019-05-02 13:56:04 -060050 cache_dir = chroot_message.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060051 chrome_root = chroot_message.chrome_dir
Alex Klein4f0eb432019-05-02 13:56:04 -060052
Alex Klein38c7d9e2019-05-08 09:31:19 -060053 use_flags = [u.flag for u in chroot_message.env.use_flags]
54 features = [f.feature for f in chroot_message.env.features]
55
56 env = {}
57 if use_flags:
58 env['USE'] = ' '.join(use_flags)
59
Alex Kleinb7485bb2019-09-19 13:23:37 -060060 # Make sure it'll use the local source to build chrome when we have it.
61 if chrome_root:
62 env['CHROME_ORIGIN'] = 'LOCAL_SOURCE'
63
Alex Klein38c7d9e2019-05-08 09:31:19 -060064 if features:
65 env['FEATURES'] = ' '.join(features)
66
George Engelbrecht35b6e8d2021-06-18 09:43:28 -060067 chroot = chroot_lib.Chroot(
Alex Klein9b7331e2019-12-30 14:37:21 -070068 path=path, cache_dir=cache_dir, chrome_root=chrome_root, env=env)
Alex Klein171da612019-08-06 14:00:42 -060069
Alex Klein9b7331e2019-12-30 14:37:21 -070070 return chroot
Alex Klein171da612019-08-06 14:00:42 -060071
George Engelbrechtc9a8e812021-06-16 18:14:17 -060072
Kevin Sheltona8056362022-04-04 16:19:23 -070073def ParseSysroot(sysroot_message: sysroot_pb2.Sysroot) -> sysroot_lib.Sysroot:
George Engelbrechtc9a8e812021-06-16 18:14:17 -060074 """Create a sysroot object from the sysroot message.
75
76 Args:
Kevin Sheltona8056362022-04-04 16:19:23 -070077 sysroot_message: The sysroot message.
George Engelbrechtc9a8e812021-06-16 18:14:17 -060078
79 Returns:
80 Sysroot: The parsed sysroot object.
81
82 Raises:
83 AssertionError: When the message is not a Sysroot message.
84 """
85 assert isinstance(sysroot_message, sysroot_pb2.Sysroot)
86
George Engelbrecht35b6e8d2021-06-18 09:43:28 -060087 return sysroot_lib.Sysroot(sysroot_message.path)
George Engelbrechtc9a8e812021-06-16 18:14:17 -060088
89
Joanna Wang92cad812021-11-03 14:52:08 -070090def ParseRemoteexecConfig(remoteexec_message: common_pb2.RemoteexecConfig):
91 """Parse a remoteexec config message."""
92 assert isinstance(remoteexec_message, common_pb2.RemoteexecConfig)
93
94 if not (remoteexec_message.reclient_dir or
95 remoteexec_message.reproxy_cfg_file):
96 return None
97
98 return remoteexec_util.Remoteexec(remoteexec_message.reclient_dir,
99 remoteexec_message.reproxy_cfg_file)
100
101
Alex Klein915cce92019-12-17 14:19:50 -0700102def ParseGomaConfig(goma_message, chroot_path):
103 """Parse a goma config message."""
104 assert isinstance(goma_message, common_pb2.GomaConfig)
105
106 if not goma_message.goma_dir:
107 return None
108
109 # Parse the goma config.
110 chromeos_goma_dir = goma_message.chromeos_goma_dir or None
David Burgerec676f62020-07-03 09:09:31 -0600111 if goma_message.goma_approach == common_pb2.GomaConfig.RBE_STAGING:
Ram Chandrasekare08e3ba2022-04-04 21:42:27 +0000112 goma_approach = goma_lib.GomaApproach('?staging',
113 'staging-goma.chromium.org', True)
Yoshisato Yanagisawa57f7f672021-01-08 02:42:42 +0000114 elif goma_message.goma_approach == common_pb2.GomaConfig.RBE_PROD:
Ram Chandrasekare08e3ba2022-04-04 21:42:27 +0000115 goma_approach = goma_lib.GomaApproach('?prod', 'goma.chromium.org', True)
Yoshisato Yanagisawa57f7f672021-01-08 02:42:42 +0000116 else:
Ram Chandrasekare08e3ba2022-04-04 21:42:27 +0000117 goma_approach = goma_lib.GomaApproach('?cros', 'goma.chromium.org', True)
Alex Klein915cce92019-12-17 14:19:50 -0700118
Michael Mortensen4ccfb082020-01-22 16:24:03 -0700119 # Note that we are not specifying the goma log_dir so that goma will create
120 # and use a tmp dir for the logs.
Alex Klein915cce92019-12-17 14:19:50 -0700121 stats_filename = goma_message.stats_file or None
122 counterz_filename = goma_message.counterz_file or None
123
Ram Chandrasekare08e3ba2022-04-04 21:42:27 +0000124 return goma_lib.Goma(
125 goma_message.goma_dir,
126 goma_message.goma_client_json,
127 stage_name='BuildAPI',
128 chromeos_goma_dir=chromeos_goma_dir,
129 chroot_dir=chroot_path,
130 goma_approach=goma_approach,
131 stats_filename=stats_filename,
132 counterz_filename=counterz_filename)
Alex Klein915cce92019-12-17 14:19:50 -0700133
134
Kevin Sheltona8056362022-04-04 16:19:23 -0700135def ParseBuildTarget(
136 build_target_message: common_pb2.BuildTarget,
137 profile_message: Optional[sysroot_pb2.Profile] = None
138) -> build_target_lib.BuildTarget:
Alex Klein171da612019-08-06 14:00:42 -0600139 """Create a BuildTarget object from a build_target message.
140
141 Args:
Kevin Sheltona8056362022-04-04 16:19:23 -0700142 build_target_message: The BuildTarget message.
143 profile_message: The profile message.
Alex Klein171da612019-08-06 14:00:42 -0600144
145 Returns:
146 BuildTarget: The parsed instance.
147
148 Raises:
149 AssertionError: When the field is not a BuildTarget message.
150 """
151 assert isinstance(build_target_message, common_pb2.BuildTarget)
Alex Klein26e472b2020-03-10 14:35:01 -0600152 assert (profile_message is None or
153 isinstance(profile_message, sysroot_pb2.Profile))
Alex Klein171da612019-08-06 14:00:42 -0600154
Alex Klein26e472b2020-03-10 14:35:01 -0600155 profile_name = profile_message.name if profile_message else None
156 return build_target_lib.BuildTarget(
157 build_target_message.name, profile=profile_name)
Alex Klein171da612019-08-06 14:00:42 -0600158
159
160def ParseBuildTargets(repeated_build_target_field):
161 """Create a BuildTarget for each entry in the repeated field.
162
163 Args:
164 repeated_build_target_field: The repeated BuildTarget field.
165
166 Returns:
167 list[BuildTarget]: The parsed BuildTargets.
168
169 Raises:
170 AssertionError: When the field contains non-BuildTarget messages.
171 """
172 return [ParseBuildTarget(target) for target in repeated_build_target_field]
Alex Klein4f0eb432019-05-02 13:56:04 -0600173
174
Alex Klein28e59a62021-09-09 15:45:14 -0600175def serialize_package_info(pkg_info: package_info.PackageInfo,
Alex Klein46c30f32021-11-10 13:12:50 -0700176 pkg_info_msg: Union[common_pb2.PackageInfo,
177 'portage_pb2.Portage.Package']):
Alex Klein1e68a8e2020-10-06 17:25:11 -0600178 """Serialize a PackageInfo object to a PackageInfo proto."""
Alex Klein28e59a62021-09-09 15:45:14 -0600179 if not isinstance(pkg_info, package_info.PackageInfo):
180 # Allows us to swap everything to serialize_package_info, and search the
181 # logs for usages that aren't passing though a PackageInfo yet.
182 logging.warning(
183 'serialize_package_info: Got a %s instead of a PackageInfo.',
184 type(pkg_info))
185 pkg_info = package_info.parse(pkg_info)
Alex Klein1e68a8e2020-10-06 17:25:11 -0600186 pkg_info_msg.package_name = pkg_info.package
187 if pkg_info.category:
188 pkg_info_msg.category = pkg_info.category
189 if pkg_info.vr:
190 pkg_info_msg.version = pkg_info.vr
191
192
193def deserialize_package_info(pkg_info_msg):
194 """Deserialize a PackageInfo message to a PackageInfo object."""
195 return package_info.parse(PackageInfoToString(pkg_info_msg))
196
197
Lizzy Presland084c20f2022-03-30 19:13:50 +0000198def retrieve_package_log_paths(packages: Iterable[package_info.PackageInfo],
Lizzy Presland29e62452022-01-05 21:58:21 +0000199 output_proto: Union[
200 sysroot_pb2.InstallPackagesResponse,
Lizzy Presland4feb2372022-01-20 05:16:30 +0000201 sysroot_pb2.InstallToolchainResponse,
202 test_pb2.BuildTargetUnitTestResponse
203 ],
Lizzy Presland29e62452022-01-05 21:58:21 +0000204 target_sysroot: sysroot_lib.Sysroot) -> None:
205 """Get the path to the log file for each package that failed to build.
206
207 Args:
Lizzy Presland084c20f2022-03-30 19:13:50 +0000208 packages: A list of packages which failed to build.
Lizzy Presland29e62452022-01-05 21:58:21 +0000209 output_proto: The Response message for a given API call. This response proto
210 must contain a failed_package_data field.
211 target_sysroot: The sysroot used by the build step.
212 """
Lizzy Presland084c20f2022-03-30 19:13:50 +0000213 for pkg_info in packages:
Lizzy Presland29e62452022-01-05 21:58:21 +0000214 # Grab the paths to the log files for each failed package from the
215 # sysroot.
216 # Logs currently exist within the sysroot in the form of:
217 # /build/${BOARD}/tmp/portage/logs/$CATEGORY:$PF:$TIMESTAMP.log
218 failed_pkg_data_msg = output_proto.failed_package_data.add()
219 serialize_package_info(pkg_info, failed_pkg_data_msg.name)
220 glob_path = os.path.join(target_sysroot.portage_logdir,
221 f'{pkg_info.category}:{pkg_info.pvr}:*.log')
222 log_files = glob.glob(glob_path)
223 log_files.sort(reverse=True)
224 # Omit path if files don't exist for some reason.
225 if not log_files:
226 logging.warning('Log file for %s was not found. Search path: %s',
227 pkg_info.cpvr, glob_path)
228 continue
229 failed_pkg_data_msg.log_path.path = log_files[0]
230 failed_pkg_data_msg.log_path.location = common_pb2.Path.INSIDE
231
232
Alex Klein18a60af2020-06-11 12:08:47 -0600233def PackageInfoToCPV(package_info_msg):
Alex Kleina9d500b2019-04-22 15:37:51 -0600234 """Helper to translate a PackageInfo message into a CPV."""
Alex Klein18a60af2020-06-11 12:08:47 -0600235 if not package_info_msg or not package_info_msg.package_name:
Alex Kleina9d500b2019-04-22 15:37:51 -0600236 return None
237
Alex Klein18a60af2020-06-11 12:08:47 -0600238 return package_info.SplitCPV(PackageInfoToString(package_info_msg),
239 strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600240
241
Alex Klein18a60af2020-06-11 12:08:47 -0600242def PackageInfoToString(package_info_msg):
Alex Kleina9d500b2019-04-22 15:37:51 -0600243 # Combine the components into the full CPV string that SplitCPV parses.
Alex Klein18a60af2020-06-11 12:08:47 -0600244 # TODO: Use the lib.parser.package_info.PackageInfo class instead.
245 if not package_info_msg.package_name:
246 raise ValueError('Invalid PackageInfo message.')
Alex Kleina9d500b2019-04-22 15:37:51 -0600247
Alex Klein18a60af2020-06-11 12:08:47 -0600248 c = ('%s/' % package_info_msg.category) if package_info_msg.category else ''
249 p = package_info_msg.package_name
250 v = ('-%s' % package_info_msg.version) if package_info_msg.version else ''
Alex Kleina9d500b2019-04-22 15:37:51 -0600251 return '%s%s%s' % (c, p, v)
252
253
Kevin Sheltona8056362022-04-04 16:19:23 -0700254def CPVToString(cpv: package_info.CPV) -> str:
Alex Kleina9d500b2019-04-22 15:37:51 -0600255 """Get the most useful string representation from a CPV.
256
257 Args:
Kevin Sheltona8056362022-04-04 16:19:23 -0700258 cpv: The CPV object.
Alex Kleina9d500b2019-04-22 15:37:51 -0600259
260 Raises:
261 ValueError - when the CPV has no useful fields set.
262 """
263 if cpv.cpf:
264 return cpv.cpf
265 elif cpv.cpv:
266 return cpv.cpv
267 elif cpv.cp:
268 return cpv.cp
269 elif cpv.package:
270 return cpv.package
271 else:
272 raise ValueError('Invalid CPV provided.')