Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 1 | # 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 Klein | 28e59a6 | 2021-09-09 15:45:14 -0600 | [diff] [blame] | 6 | import logging |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 7 | |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 8 | from chromite.api.gen.chromite.api import sysroot_pb2 |
Alex Klein | 1f67cf3 | 2019-10-09 11:13:42 -0600 | [diff] [blame] | 9 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 10 | from chromite.cbuildbot import goma_util |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 11 | from chromite.lib import build_target_lib |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 12 | from chromite.lib import constants |
| 13 | from chromite.lib.parser import package_info |
George Engelbrecht | 35b6e8d | 2021-06-18 09:43:28 -0600 | [diff] [blame] | 14 | from chromite.lib import chroot_lib |
| 15 | from chromite.lib import sysroot_lib |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 16 | |
| 17 | class Error(Exception): |
| 18 | """Base error class for the module.""" |
| 19 | |
| 20 | |
| 21 | class InvalidMessageError(Error): |
| 22 | """Invalid message.""" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 23 | |
| 24 | |
Alex Klein | c7d647f | 2020-01-06 12:00:48 -0700 | [diff] [blame] | 25 | def ParseChroot(chroot_message): |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 26 | """Create a chroot object from the chroot message. |
| 27 | |
| 28 | Args: |
| 29 | chroot_message (common_pb2.Chroot): The chroot message. |
| 30 | |
| 31 | Returns: |
| 32 | Chroot: The parsed chroot object. |
| 33 | |
| 34 | Raises: |
| 35 | AssertionError: When the message is not a Chroot message. |
| 36 | """ |
| 37 | assert isinstance(chroot_message, common_pb2.Chroot) |
| 38 | |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 39 | path = chroot_message.path or constants.DEFAULT_CHROOT_PATH |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 40 | cache_dir = chroot_message.cache_dir |
Alex Klein | 5e4b1bc | 2019-07-02 12:27:06 -0600 | [diff] [blame] | 41 | chrome_root = chroot_message.chrome_dir |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 42 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 43 | use_flags = [u.flag for u in chroot_message.env.use_flags] |
| 44 | features = [f.feature for f in chroot_message.env.features] |
| 45 | |
| 46 | env = {} |
| 47 | if use_flags: |
| 48 | env['USE'] = ' '.join(use_flags) |
| 49 | |
Alex Klein | b7485bb | 2019-09-19 13:23:37 -0600 | [diff] [blame] | 50 | # Make sure it'll use the local source to build chrome when we have it. |
| 51 | if chrome_root: |
| 52 | env['CHROME_ORIGIN'] = 'LOCAL_SOURCE' |
| 53 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 54 | if features: |
| 55 | env['FEATURES'] = ' '.join(features) |
| 56 | |
George Engelbrecht | 35b6e8d | 2021-06-18 09:43:28 -0600 | [diff] [blame] | 57 | chroot = chroot_lib.Chroot( |
Alex Klein | 9b7331e | 2019-12-30 14:37:21 -0700 | [diff] [blame] | 58 | path=path, cache_dir=cache_dir, chrome_root=chrome_root, env=env) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 59 | |
Alex Klein | 9b7331e | 2019-12-30 14:37:21 -0700 | [diff] [blame] | 60 | return chroot |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 61 | |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 62 | |
| 63 | def ParseSysroot(sysroot_message): |
| 64 | """Create a sysroot object from the sysroot message. |
| 65 | |
| 66 | Args: |
| 67 | sysroot_message (commmon_pb2.Sysroot): The sysroot message. |
| 68 | |
| 69 | Returns: |
| 70 | Sysroot: The parsed sysroot object. |
| 71 | |
| 72 | Raises: |
| 73 | AssertionError: When the message is not a Sysroot message. |
| 74 | """ |
| 75 | assert isinstance(sysroot_message, sysroot_pb2.Sysroot) |
| 76 | |
George Engelbrecht | 35b6e8d | 2021-06-18 09:43:28 -0600 | [diff] [blame] | 77 | return sysroot_lib.Sysroot(sysroot_message.path) |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 78 | |
| 79 | |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 80 | def ParseGomaConfig(goma_message, chroot_path): |
| 81 | """Parse a goma config message.""" |
| 82 | assert isinstance(goma_message, common_pb2.GomaConfig) |
| 83 | |
| 84 | if not goma_message.goma_dir: |
| 85 | return None |
| 86 | |
| 87 | # Parse the goma config. |
| 88 | chromeos_goma_dir = goma_message.chromeos_goma_dir or None |
David Burger | ec676f6 | 2020-07-03 09:09:31 -0600 | [diff] [blame] | 89 | if goma_message.goma_approach == common_pb2.GomaConfig.RBE_STAGING: |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 90 | goma_approach = goma_util.GomaApproach('?staging', |
| 91 | 'staging-goma.chromium.org', True) |
Yoshisato Yanagisawa | 57f7f67 | 2021-01-08 02:42:42 +0000 | [diff] [blame] | 92 | elif goma_message.goma_approach == common_pb2.GomaConfig.RBE_PROD: |
David Burger | ec676f6 | 2020-07-03 09:09:31 -0600 | [diff] [blame] | 93 | goma_approach = goma_util.GomaApproach('?prod', 'goma.chromium.org', True) |
Yoshisato Yanagisawa | 57f7f67 | 2021-01-08 02:42:42 +0000 | [diff] [blame] | 94 | else: |
| 95 | goma_approach = goma_util.GomaApproach('?cros', 'goma.chromium.org', True) |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 96 | |
Michael Mortensen | 4ccfb08 | 2020-01-22 16:24:03 -0700 | [diff] [blame] | 97 | # Note that we are not specifying the goma log_dir so that goma will create |
| 98 | # and use a tmp dir for the logs. |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 99 | stats_filename = goma_message.stats_file or None |
| 100 | counterz_filename = goma_message.counterz_file or None |
| 101 | |
| 102 | return goma_util.Goma(goma_message.goma_dir, |
| 103 | goma_message.goma_client_json, |
| 104 | stage_name='BuildAPI', |
| 105 | chromeos_goma_dir=chromeos_goma_dir, |
| 106 | chroot_dir=chroot_path, |
| 107 | goma_approach=goma_approach, |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 108 | stats_filename=stats_filename, |
| 109 | counterz_filename=counterz_filename) |
| 110 | |
| 111 | |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 112 | def ParseBuildTarget(build_target_message, profile_message=None): |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 113 | """Create a BuildTarget object from a build_target message. |
| 114 | |
| 115 | Args: |
| 116 | build_target_message (common_pb2.BuildTarget): The BuildTarget message. |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 117 | profile_message (sysroot_pb2.Profile|None): The profile message. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 118 | |
| 119 | Returns: |
| 120 | BuildTarget: The parsed instance. |
| 121 | |
| 122 | Raises: |
| 123 | AssertionError: When the field is not a BuildTarget message. |
| 124 | """ |
| 125 | assert isinstance(build_target_message, common_pb2.BuildTarget) |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 126 | assert (profile_message is None or |
| 127 | isinstance(profile_message, sysroot_pb2.Profile)) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 128 | |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 129 | profile_name = profile_message.name if profile_message else None |
| 130 | return build_target_lib.BuildTarget( |
| 131 | build_target_message.name, profile=profile_name) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 132 | |
| 133 | |
| 134 | def ParseBuildTargets(repeated_build_target_field): |
| 135 | """Create a BuildTarget for each entry in the repeated field. |
| 136 | |
| 137 | Args: |
| 138 | repeated_build_target_field: The repeated BuildTarget field. |
| 139 | |
| 140 | Returns: |
| 141 | list[BuildTarget]: The parsed BuildTargets. |
| 142 | |
| 143 | Raises: |
| 144 | AssertionError: When the field contains non-BuildTarget messages. |
| 145 | """ |
| 146 | return [ParseBuildTarget(target) for target in repeated_build_target_field] |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 147 | |
| 148 | |
Alex Klein | 28e59a6 | 2021-09-09 15:45:14 -0600 | [diff] [blame] | 149 | def serialize_package_info(pkg_info: package_info.PackageInfo, |
| 150 | pkg_info_msg: common_pb2.PackageInfo): |
Alex Klein | 1e68a8e | 2020-10-06 17:25:11 -0600 | [diff] [blame] | 151 | """Serialize a PackageInfo object to a PackageInfo proto.""" |
Alex Klein | 28e59a6 | 2021-09-09 15:45:14 -0600 | [diff] [blame] | 152 | if not isinstance(pkg_info, package_info.PackageInfo): |
| 153 | # Allows us to swap everything to serialize_package_info, and search the |
| 154 | # logs for usages that aren't passing though a PackageInfo yet. |
| 155 | logging.warning( |
| 156 | 'serialize_package_info: Got a %s instead of a PackageInfo.', |
| 157 | type(pkg_info)) |
| 158 | pkg_info = package_info.parse(pkg_info) |
Alex Klein | 1e68a8e | 2020-10-06 17:25:11 -0600 | [diff] [blame] | 159 | pkg_info_msg.package_name = pkg_info.package |
| 160 | if pkg_info.category: |
| 161 | pkg_info_msg.category = pkg_info.category |
| 162 | if pkg_info.vr: |
| 163 | pkg_info_msg.version = pkg_info.vr |
| 164 | |
| 165 | |
| 166 | def deserialize_package_info(pkg_info_msg): |
| 167 | """Deserialize a PackageInfo message to a PackageInfo object.""" |
| 168 | return package_info.parse(PackageInfoToString(pkg_info_msg)) |
| 169 | |
| 170 | |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 171 | def PackageInfoToCPV(package_info_msg): |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 172 | """Helper to translate a PackageInfo message into a CPV.""" |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 173 | if not package_info_msg or not package_info_msg.package_name: |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 174 | return None |
| 175 | |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 176 | return package_info.SplitCPV(PackageInfoToString(package_info_msg), |
| 177 | strict=False) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 178 | |
| 179 | |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 180 | def PackageInfoToString(package_info_msg): |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 181 | # Combine the components into the full CPV string that SplitCPV parses. |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 182 | # TODO: Use the lib.parser.package_info.PackageInfo class instead. |
| 183 | if not package_info_msg.package_name: |
| 184 | raise ValueError('Invalid PackageInfo message.') |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 185 | |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 186 | c = ('%s/' % package_info_msg.category) if package_info_msg.category else '' |
| 187 | p = package_info_msg.package_name |
| 188 | v = ('-%s' % package_info_msg.version) if package_info_msg.version else '' |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 189 | return '%s%s%s' % (c, p, v) |
| 190 | |
| 191 | |
| 192 | def CPVToString(cpv): |
| 193 | """Get the most useful string representation from a CPV. |
| 194 | |
| 195 | Args: |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 196 | cpv (package_info.CPV): The CPV object. |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 197 | |
| 198 | Returns: |
| 199 | str |
| 200 | |
| 201 | Raises: |
| 202 | ValueError - when the CPV has no useful fields set. |
| 203 | """ |
| 204 | if cpv.cpf: |
| 205 | return cpv.cpf |
| 206 | elif cpv.cpv: |
| 207 | return cpv.cpv |
| 208 | elif cpv.cp: |
| 209 | return cpv.cp |
| 210 | elif cpv.package: |
| 211 | return cpv.package |
| 212 | else: |
| 213 | raise ValueError('Invalid CPV provided.') |