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