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 | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 10 | from chromite.lib import chroot_lib |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 11 | from chromite.lib import portage_util |
| 12 | |
| 13 | |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 14 | def ParseChroot(chroot_message): |
| 15 | """Create a chroot object from the chroot message.""" |
| 16 | path = chroot_message.path |
| 17 | cache_dir = chroot_message.cache_dir |
Alex Klein | 5e4b1bc | 2019-07-02 12:27:06 -0600 | [diff] [blame] | 18 | chrome_root = chroot_message.chrome_dir |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 19 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 20 | use_flags = [u.flag for u in chroot_message.env.use_flags] |
| 21 | features = [f.feature for f in chroot_message.env.features] |
| 22 | |
| 23 | env = {} |
| 24 | if use_flags: |
| 25 | env['USE'] = ' '.join(use_flags) |
| 26 | |
| 27 | # TODO(saklein) Remove the default when fully integrated in recipes. |
| 28 | env['FEATURES'] = 'separatedebug' |
| 29 | if features: |
| 30 | env['FEATURES'] = ' '.join(features) |
| 31 | |
Alex Klein | 5e4b1bc | 2019-07-02 12:27:06 -0600 | [diff] [blame] | 32 | return chroot_lib.Chroot(path=path, cache_dir=cache_dir, |
| 33 | chrome_root=chrome_root, env=env) |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 34 | |
| 35 | |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 36 | def CPVToPackageInfo(cpv, package_info): |
| 37 | """Helper to translate CPVs into a PackageInfo message.""" |
| 38 | package_info.package_name = cpv.package |
| 39 | if cpv.category: |
| 40 | package_info.category = cpv.category |
| 41 | if cpv.version: |
| 42 | package_info.version = cpv.version |
| 43 | |
| 44 | |
| 45 | def PackageInfoToCPV(package_info): |
| 46 | """Helper to translate a PackageInfo message into a CPV.""" |
| 47 | if not package_info or not package_info.package_name: |
| 48 | return None |
| 49 | |
| 50 | return portage_util.SplitCPV(PackageInfoToString(package_info), strict=False) |
| 51 | |
| 52 | |
| 53 | def PackageInfoToString(package_info): |
| 54 | # Combine the components into the full CPV string that SplitCPV parses. |
| 55 | # TODO: Turn portage_util.CPV into a class that can handle building out an |
| 56 | # instance from components. |
| 57 | if not package_info.package_name: |
| 58 | raise ValueError('Invalid package_info.') |
| 59 | |
| 60 | c = ('%s/' % package_info.category) if package_info.category else '' |
| 61 | p = package_info.package_name |
| 62 | v = ('-%s' % package_info.version) if package_info.version else '' |
| 63 | return '%s%s%s' % (c, p, v) |
| 64 | |
| 65 | |
| 66 | def CPVToString(cpv): |
| 67 | """Get the most useful string representation from a CPV. |
| 68 | |
| 69 | Args: |
| 70 | cpv (portage_util.CPV): The CPV object. |
| 71 | |
| 72 | Returns: |
| 73 | str |
| 74 | |
| 75 | Raises: |
| 76 | ValueError - when the CPV has no useful fields set. |
| 77 | """ |
| 78 | if cpv.cpf: |
| 79 | return cpv.cpf |
| 80 | elif cpv.cpv: |
| 81 | return cpv.cpv |
| 82 | elif cpv.cp: |
| 83 | return cpv.cp |
| 84 | elif cpv.package: |
| 85 | return cpv.package |
| 86 | else: |
| 87 | raise ValueError('Invalid CPV provided.') |