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 | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 12 | from chromite.lib import portage_util |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame^] | 13 | from chromite.lib.build_target_util import BuildTarget |
| 14 | from chromite.lib.chroot_lib import Chroot |
| 15 | |
| 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 | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [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 | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 39 | path = chroot_message.path |
| 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 | |
| 50 | # TODO(saklein) Remove the default when fully integrated in recipes. |
| 51 | env['FEATURES'] = 'separatedebug' |
| 52 | if features: |
| 53 | env['FEATURES'] = ' '.join(features) |
| 54 | |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame^] | 55 | return Chroot(path=path, cache_dir=cache_dir, chrome_root=chrome_root, |
| 56 | env=env) |
| 57 | |
| 58 | |
| 59 | def ParseBuildTarget(build_target_message): |
| 60 | """Create a BuildTarget object from a build_target message. |
| 61 | |
| 62 | Args: |
| 63 | build_target_message (common_pb2.BuildTarget): The BuildTarget message. |
| 64 | |
| 65 | Returns: |
| 66 | BuildTarget: The parsed instance. |
| 67 | |
| 68 | Raises: |
| 69 | AssertionError: When the field is not a BuildTarget message. |
| 70 | """ |
| 71 | assert isinstance(build_target_message, common_pb2.BuildTarget) |
| 72 | |
| 73 | return BuildTarget(build_target_message.name) |
| 74 | |
| 75 | |
| 76 | def ParseBuildTargets(repeated_build_target_field): |
| 77 | """Create a BuildTarget for each entry in the repeated field. |
| 78 | |
| 79 | Args: |
| 80 | repeated_build_target_field: The repeated BuildTarget field. |
| 81 | |
| 82 | Returns: |
| 83 | list[BuildTarget]: The parsed BuildTargets. |
| 84 | |
| 85 | Raises: |
| 86 | AssertionError: When the field contains non-BuildTarget messages. |
| 87 | """ |
| 88 | return [ParseBuildTarget(target) for target in repeated_build_target_field] |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 89 | |
| 90 | |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 91 | def CPVToPackageInfo(cpv, package_info): |
| 92 | """Helper to translate CPVs into a PackageInfo message.""" |
| 93 | package_info.package_name = cpv.package |
| 94 | if cpv.category: |
| 95 | package_info.category = cpv.category |
| 96 | if cpv.version: |
| 97 | package_info.version = cpv.version |
| 98 | |
| 99 | |
| 100 | def PackageInfoToCPV(package_info): |
| 101 | """Helper to translate a PackageInfo message into a CPV.""" |
| 102 | if not package_info or not package_info.package_name: |
| 103 | return None |
| 104 | |
| 105 | return portage_util.SplitCPV(PackageInfoToString(package_info), strict=False) |
| 106 | |
| 107 | |
| 108 | def PackageInfoToString(package_info): |
| 109 | # Combine the components into the full CPV string that SplitCPV parses. |
| 110 | # TODO: Turn portage_util.CPV into a class that can handle building out an |
| 111 | # instance from components. |
| 112 | if not package_info.package_name: |
| 113 | raise ValueError('Invalid package_info.') |
| 114 | |
| 115 | c = ('%s/' % package_info.category) if package_info.category else '' |
| 116 | p = package_info.package_name |
| 117 | v = ('-%s' % package_info.version) if package_info.version else '' |
| 118 | return '%s%s%s' % (c, p, v) |
| 119 | |
| 120 | |
| 121 | def CPVToString(cpv): |
| 122 | """Get the most useful string representation from a CPV. |
| 123 | |
| 124 | Args: |
| 125 | cpv (portage_util.CPV): The CPV object. |
| 126 | |
| 127 | Returns: |
| 128 | str |
| 129 | |
| 130 | Raises: |
| 131 | ValueError - when the CPV has no useful fields set. |
| 132 | """ |
| 133 | if cpv.cpf: |
| 134 | return cpv.cpf |
| 135 | elif cpv.cpv: |
| 136 | return cpv.cpv |
| 137 | elif cpv.cp: |
| 138 | return cpv.cp |
| 139 | elif cpv.package: |
| 140 | return cpv.package |
| 141 | else: |
| 142 | raise ValueError('Invalid CPV provided.') |