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 | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 10 | from chromite.api.gen.chromiumos import common_pb2 |
| 11 | |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 12 | from chromite.cbuildbot import goma_util |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 13 | from chromite.lib import portage_util |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 14 | from chromite.lib.build_target_util import BuildTarget |
| 15 | from chromite.lib.chroot_lib import Chroot |
| 16 | |
| 17 | |
| 18 | class Error(Exception): |
| 19 | """Base error class for the module.""" |
| 20 | |
| 21 | |
| 22 | class InvalidMessageError(Error): |
| 23 | """Invalid message.""" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 24 | |
| 25 | |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 26 | def ParseChroot(chroot_message): |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 27 | """Create a chroot object from the chroot message. |
| 28 | |
| 29 | Args: |
| 30 | chroot_message (common_pb2.Chroot): The chroot message. |
| 31 | |
| 32 | Returns: |
| 33 | Chroot: The parsed chroot object. |
| 34 | |
| 35 | Raises: |
| 36 | AssertionError: When the message is not a Chroot message. |
| 37 | """ |
| 38 | assert isinstance(chroot_message, common_pb2.Chroot) |
| 39 | |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 40 | path = chroot_message.path |
| 41 | cache_dir = chroot_message.cache_dir |
Alex Klein | 5e4b1bc | 2019-07-02 12:27:06 -0600 | [diff] [blame] | 42 | chrome_root = chroot_message.chrome_dir |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 43 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 44 | use_flags = [u.flag for u in chroot_message.env.use_flags] |
| 45 | features = [f.feature for f in chroot_message.env.features] |
| 46 | |
| 47 | env = {} |
| 48 | if use_flags: |
| 49 | env['USE'] = ' '.join(use_flags) |
| 50 | |
Alex Klein | b7485bb | 2019-09-19 13:23:37 -0600 | [diff] [blame] | 51 | # Make sure it'll use the local source to build chrome when we have it. |
| 52 | if chrome_root: |
| 53 | env['CHROME_ORIGIN'] = 'LOCAL_SOURCE' |
| 54 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 55 | # TODO(saklein) Remove the default when fully integrated in recipes. |
| 56 | env['FEATURES'] = 'separatedebug' |
| 57 | if features: |
| 58 | env['FEATURES'] = ' '.join(features) |
| 59 | |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 60 | goma = None |
| 61 | if chroot_message.goma.goma_dir: |
Michael Mortensen | 9a73c32 | 2019-10-03 17:14:37 -0600 | [diff] [blame] | 62 | chromeos_goma_dir = chroot_message.goma.chromeos_goma_dir or None |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 63 | goma = goma_util.Goma(chroot_message.goma.goma_dir, |
| 64 | chroot_message.goma.goma_client_json, |
Michael Mortensen | 9a73c32 | 2019-10-03 17:14:37 -0600 | [diff] [blame] | 65 | stage_name='BuildAPI', |
| 66 | chromeos_goma_dir=chromeos_goma_dir) |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 67 | |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 68 | return Chroot(path=path, cache_dir=cache_dir, chrome_root=chrome_root, |
Alex Klein | 566d80e | 2019-09-24 12:27:58 -0600 | [diff] [blame] | 69 | env=env, goma=goma) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 70 | |
| 71 | |
| 72 | def ParseBuildTarget(build_target_message): |
| 73 | """Create a BuildTarget object from a build_target message. |
| 74 | |
| 75 | Args: |
| 76 | build_target_message (common_pb2.BuildTarget): The BuildTarget message. |
| 77 | |
| 78 | Returns: |
| 79 | BuildTarget: The parsed instance. |
| 80 | |
| 81 | Raises: |
| 82 | AssertionError: When the field is not a BuildTarget message. |
| 83 | """ |
| 84 | assert isinstance(build_target_message, common_pb2.BuildTarget) |
| 85 | |
| 86 | return BuildTarget(build_target_message.name) |
| 87 | |
| 88 | |
| 89 | def ParseBuildTargets(repeated_build_target_field): |
| 90 | """Create a BuildTarget for each entry in the repeated field. |
| 91 | |
| 92 | Args: |
| 93 | repeated_build_target_field: The repeated BuildTarget field. |
| 94 | |
| 95 | Returns: |
| 96 | list[BuildTarget]: The parsed BuildTargets. |
| 97 | |
| 98 | Raises: |
| 99 | AssertionError: When the field contains non-BuildTarget messages. |
| 100 | """ |
| 101 | return [ParseBuildTarget(target) for target in repeated_build_target_field] |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 102 | |
| 103 | |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 104 | def CPVToPackageInfo(cpv, package_info): |
| 105 | """Helper to translate CPVs into a PackageInfo message.""" |
| 106 | package_info.package_name = cpv.package |
| 107 | if cpv.category: |
| 108 | package_info.category = cpv.category |
| 109 | if cpv.version: |
| 110 | package_info.version = cpv.version |
| 111 | |
| 112 | |
| 113 | def PackageInfoToCPV(package_info): |
| 114 | """Helper to translate a PackageInfo message into a CPV.""" |
| 115 | if not package_info or not package_info.package_name: |
| 116 | return None |
| 117 | |
| 118 | return portage_util.SplitCPV(PackageInfoToString(package_info), strict=False) |
| 119 | |
| 120 | |
| 121 | def PackageInfoToString(package_info): |
| 122 | # Combine the components into the full CPV string that SplitCPV parses. |
| 123 | # TODO: Turn portage_util.CPV into a class that can handle building out an |
| 124 | # instance from components. |
| 125 | if not package_info.package_name: |
| 126 | raise ValueError('Invalid package_info.') |
| 127 | |
| 128 | c = ('%s/' % package_info.category) if package_info.category else '' |
| 129 | p = package_info.package_name |
| 130 | v = ('-%s' % package_info.version) if package_info.version else '' |
| 131 | return '%s%s%s' % (c, p, v) |
| 132 | |
| 133 | |
| 134 | def CPVToString(cpv): |
| 135 | """Get the most useful string representation from a CPV. |
| 136 | |
| 137 | Args: |
| 138 | cpv (portage_util.CPV): The CPV object. |
| 139 | |
| 140 | Returns: |
| 141 | str |
| 142 | |
| 143 | Raises: |
| 144 | ValueError - when the CPV has no useful fields set. |
| 145 | """ |
| 146 | if cpv.cpf: |
| 147 | return cpv.cpf |
| 148 | elif cpv.cpv: |
| 149 | return cpv.cpv |
| 150 | elif cpv.cp: |
| 151 | return cpv.cp |
| 152 | elif cpv.package: |
| 153 | return cpv.package |
| 154 | else: |
| 155 | raise ValueError('Invalid CPV provided.') |