blob: b7b7de1b89ffcfee0ad78c4bbbddf84cca4f43e0 [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."""
Alex Klein28e59a62021-09-09 15:45:14 -06006import logging
Alex Klein46c30f32021-11-10 13:12:50 -07007from typing import TYPE_CHECKING, Union
Alex Kleina9d500b2019-04-22 15:37:51 -06008
Alex Klein26e472b2020-03-10 14:35:01 -06009from chromite.api.gen.chromite.api import sysroot_pb2
Alex Klein1f67cf32019-10-09 11:13:42 -060010from chromite.api.gen.chromiumos import common_pb2
Alex Klein566d80e2019-09-24 12:27:58 -060011from chromite.cbuildbot import goma_util
Alex Klein26e472b2020-03-10 14:35:01 -060012from chromite.lib import build_target_lib
Alex Klein18a60af2020-06-11 12:08:47 -060013from chromite.lib import constants
14from chromite.lib.parser import package_info
George Engelbrecht35b6e8d2021-06-18 09:43:28 -060015from chromite.lib import chroot_lib
Joanna Wang92cad812021-11-03 14:52:08 -070016from chromite.lib import remoteexec_util
George Engelbrecht35b6e8d2021-06-18 09:43:28 -060017from chromite.lib import sysroot_lib
Alex Klein171da612019-08-06 14:00:42 -060018
Alex Klein46c30f32021-11-10 13:12:50 -070019if TYPE_CHECKING:
20 from chromite.api.gen.chromiumos.build.api import portage_pb2
21
Alex Klein171da612019-08-06 14:00:42 -060022class Error(Exception):
23 """Base error class for the module."""
24
25
26class InvalidMessageError(Error):
27 """Invalid message."""
Alex Kleina9d500b2019-04-22 15:37:51 -060028
29
Alex Kleinc7d647f2020-01-06 12:00:48 -070030def ParseChroot(chroot_message):
Alex Klein171da612019-08-06 14:00:42 -060031 """Create a chroot object from the chroot message.
32
33 Args:
34 chroot_message (common_pb2.Chroot): The chroot message.
35
36 Returns:
37 Chroot: The parsed chroot object.
38
39 Raises:
40 AssertionError: When the message is not a Chroot message.
41 """
42 assert isinstance(chroot_message, common_pb2.Chroot)
43
Alex Klein915cce92019-12-17 14:19:50 -070044 path = chroot_message.path or constants.DEFAULT_CHROOT_PATH
Alex Klein4f0eb432019-05-02 13:56:04 -060045 cache_dir = chroot_message.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060046 chrome_root = chroot_message.chrome_dir
Alex Klein4f0eb432019-05-02 13:56:04 -060047
Alex Klein38c7d9e2019-05-08 09:31:19 -060048 use_flags = [u.flag for u in chroot_message.env.use_flags]
49 features = [f.feature for f in chroot_message.env.features]
50
51 env = {}
52 if use_flags:
53 env['USE'] = ' '.join(use_flags)
54
Alex Kleinb7485bb2019-09-19 13:23:37 -060055 # Make sure it'll use the local source to build chrome when we have it.
56 if chrome_root:
57 env['CHROME_ORIGIN'] = 'LOCAL_SOURCE'
58
Alex Klein38c7d9e2019-05-08 09:31:19 -060059 if features:
60 env['FEATURES'] = ' '.join(features)
61
George Engelbrecht35b6e8d2021-06-18 09:43:28 -060062 chroot = chroot_lib.Chroot(
Alex Klein9b7331e2019-12-30 14:37:21 -070063 path=path, cache_dir=cache_dir, chrome_root=chrome_root, env=env)
Alex Klein171da612019-08-06 14:00:42 -060064
Alex Klein9b7331e2019-12-30 14:37:21 -070065 return chroot
Alex Klein171da612019-08-06 14:00:42 -060066
George Engelbrechtc9a8e812021-06-16 18:14:17 -060067
68def ParseSysroot(sysroot_message):
69 """Create a sysroot object from the sysroot message.
70
71 Args:
72 sysroot_message (commmon_pb2.Sysroot): The sysroot message.
73
74 Returns:
75 Sysroot: The parsed sysroot object.
76
77 Raises:
78 AssertionError: When the message is not a Sysroot message.
79 """
80 assert isinstance(sysroot_message, sysroot_pb2.Sysroot)
81
George Engelbrecht35b6e8d2021-06-18 09:43:28 -060082 return sysroot_lib.Sysroot(sysroot_message.path)
George Engelbrechtc9a8e812021-06-16 18:14:17 -060083
84
Joanna Wang92cad812021-11-03 14:52:08 -070085def ParseRemoteexecConfig(remoteexec_message: common_pb2.RemoteexecConfig):
86 """Parse a remoteexec config message."""
87 assert isinstance(remoteexec_message, common_pb2.RemoteexecConfig)
88
89 if not (remoteexec_message.reclient_dir or
90 remoteexec_message.reproxy_cfg_file):
91 return None
92
93 return remoteexec_util.Remoteexec(remoteexec_message.reclient_dir,
94 remoteexec_message.reproxy_cfg_file)
95
96
Alex Klein915cce92019-12-17 14:19:50 -070097def ParseGomaConfig(goma_message, chroot_path):
98 """Parse a goma config message."""
99 assert isinstance(goma_message, common_pb2.GomaConfig)
100
101 if not goma_message.goma_dir:
102 return None
103
104 # Parse the goma config.
105 chromeos_goma_dir = goma_message.chromeos_goma_dir or None
David Burgerec676f62020-07-03 09:09:31 -0600106 if goma_message.goma_approach == common_pb2.GomaConfig.RBE_STAGING:
Alex Klein915cce92019-12-17 14:19:50 -0700107 goma_approach = goma_util.GomaApproach('?staging',
108 'staging-goma.chromium.org', True)
Yoshisato Yanagisawa57f7f672021-01-08 02:42:42 +0000109 elif goma_message.goma_approach == common_pb2.GomaConfig.RBE_PROD:
David Burgerec676f62020-07-03 09:09:31 -0600110 goma_approach = goma_util.GomaApproach('?prod', 'goma.chromium.org', True)
Yoshisato Yanagisawa57f7f672021-01-08 02:42:42 +0000111 else:
112 goma_approach = goma_util.GomaApproach('?cros', 'goma.chromium.org', True)
Alex Klein915cce92019-12-17 14:19:50 -0700113
Michael Mortensen4ccfb082020-01-22 16:24:03 -0700114 # Note that we are not specifying the goma log_dir so that goma will create
115 # and use a tmp dir for the logs.
Alex Klein915cce92019-12-17 14:19:50 -0700116 stats_filename = goma_message.stats_file or None
117 counterz_filename = goma_message.counterz_file or None
118
119 return goma_util.Goma(goma_message.goma_dir,
120 goma_message.goma_client_json,
121 stage_name='BuildAPI',
122 chromeos_goma_dir=chromeos_goma_dir,
123 chroot_dir=chroot_path,
124 goma_approach=goma_approach,
Alex Klein915cce92019-12-17 14:19:50 -0700125 stats_filename=stats_filename,
126 counterz_filename=counterz_filename)
127
128
Alex Klein26e472b2020-03-10 14:35:01 -0600129def ParseBuildTarget(build_target_message, profile_message=None):
Alex Klein171da612019-08-06 14:00:42 -0600130 """Create a BuildTarget object from a build_target message.
131
132 Args:
133 build_target_message (common_pb2.BuildTarget): The BuildTarget message.
Alex Klein26e472b2020-03-10 14:35:01 -0600134 profile_message (sysroot_pb2.Profile|None): The profile message.
Alex Klein171da612019-08-06 14:00:42 -0600135
136 Returns:
137 BuildTarget: The parsed instance.
138
139 Raises:
140 AssertionError: When the field is not a BuildTarget message.
141 """
142 assert isinstance(build_target_message, common_pb2.BuildTarget)
Alex Klein26e472b2020-03-10 14:35:01 -0600143 assert (profile_message is None or
144 isinstance(profile_message, sysroot_pb2.Profile))
Alex Klein171da612019-08-06 14:00:42 -0600145
Alex Klein26e472b2020-03-10 14:35:01 -0600146 profile_name = profile_message.name if profile_message else None
147 return build_target_lib.BuildTarget(
148 build_target_message.name, profile=profile_name)
Alex Klein171da612019-08-06 14:00:42 -0600149
150
151def ParseBuildTargets(repeated_build_target_field):
152 """Create a BuildTarget for each entry in the repeated field.
153
154 Args:
155 repeated_build_target_field: The repeated BuildTarget field.
156
157 Returns:
158 list[BuildTarget]: The parsed BuildTargets.
159
160 Raises:
161 AssertionError: When the field contains non-BuildTarget messages.
162 """
163 return [ParseBuildTarget(target) for target in repeated_build_target_field]
Alex Klein4f0eb432019-05-02 13:56:04 -0600164
165
Alex Klein28e59a62021-09-09 15:45:14 -0600166def serialize_package_info(pkg_info: package_info.PackageInfo,
Alex Klein46c30f32021-11-10 13:12:50 -0700167 pkg_info_msg: Union[common_pb2.PackageInfo,
168 'portage_pb2.Portage.Package']):
Alex Klein1e68a8e2020-10-06 17:25:11 -0600169 """Serialize a PackageInfo object to a PackageInfo proto."""
Alex Klein28e59a62021-09-09 15:45:14 -0600170 if not isinstance(pkg_info, package_info.PackageInfo):
171 # Allows us to swap everything to serialize_package_info, and search the
172 # logs for usages that aren't passing though a PackageInfo yet.
173 logging.warning(
174 'serialize_package_info: Got a %s instead of a PackageInfo.',
175 type(pkg_info))
176 pkg_info = package_info.parse(pkg_info)
Alex Klein1e68a8e2020-10-06 17:25:11 -0600177 pkg_info_msg.package_name = pkg_info.package
178 if pkg_info.category:
179 pkg_info_msg.category = pkg_info.category
180 if pkg_info.vr:
181 pkg_info_msg.version = pkg_info.vr
182
183
184def deserialize_package_info(pkg_info_msg):
185 """Deserialize a PackageInfo message to a PackageInfo object."""
186 return package_info.parse(PackageInfoToString(pkg_info_msg))
187
188
Alex Klein18a60af2020-06-11 12:08:47 -0600189def PackageInfoToCPV(package_info_msg):
Alex Kleina9d500b2019-04-22 15:37:51 -0600190 """Helper to translate a PackageInfo message into a CPV."""
Alex Klein18a60af2020-06-11 12:08:47 -0600191 if not package_info_msg or not package_info_msg.package_name:
Alex Kleina9d500b2019-04-22 15:37:51 -0600192 return None
193
Alex Klein18a60af2020-06-11 12:08:47 -0600194 return package_info.SplitCPV(PackageInfoToString(package_info_msg),
195 strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600196
197
Alex Klein18a60af2020-06-11 12:08:47 -0600198def PackageInfoToString(package_info_msg):
Alex Kleina9d500b2019-04-22 15:37:51 -0600199 # Combine the components into the full CPV string that SplitCPV parses.
Alex Klein18a60af2020-06-11 12:08:47 -0600200 # TODO: Use the lib.parser.package_info.PackageInfo class instead.
201 if not package_info_msg.package_name:
202 raise ValueError('Invalid PackageInfo message.')
Alex Kleina9d500b2019-04-22 15:37:51 -0600203
Alex Klein18a60af2020-06-11 12:08:47 -0600204 c = ('%s/' % package_info_msg.category) if package_info_msg.category else ''
205 p = package_info_msg.package_name
206 v = ('-%s' % package_info_msg.version) if package_info_msg.version else ''
Alex Kleina9d500b2019-04-22 15:37:51 -0600207 return '%s%s%s' % (c, p, v)
208
209
210def CPVToString(cpv):
211 """Get the most useful string representation from a CPV.
212
213 Args:
Alex Klein18a60af2020-06-11 12:08:47 -0600214 cpv (package_info.CPV): The CPV object.
Alex Kleina9d500b2019-04-22 15:37:51 -0600215
216 Returns:
217 str
218
219 Raises:
220 ValueError - when the CPV has no useful fields set.
221 """
222 if cpv.cpf:
223 return cpv.cpf
224 elif cpv.cpv:
225 return cpv.cpv
226 elif cpv.cp:
227 return cpv.cp
228 elif cpv.package:
229 return cpv.package
230 else:
231 raise ValueError('Invalid CPV provided.')