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