blob: 21f6d84c7a026ebed44b17bc0c793ae0f56af14c [file] [log] [blame]
Alex Kleina9d500b2019-04-22 15:37:51 -06001# -*- 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
8from __future__ import print_function
9
Alex Klein4f0eb432019-05-02 13:56:04 -060010from chromite.lib import chroot_lib
Alex Kleina9d500b2019-04-22 15:37:51 -060011from chromite.lib import portage_util
12
13
Alex Klein4f0eb432019-05-02 13:56:04 -060014def 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 Klein5e4b1bc2019-07-02 12:27:06 -060018 chrome_root = chroot_message.chrome_dir
Alex Klein4f0eb432019-05-02 13:56:04 -060019
Alex Klein38c7d9e2019-05-08 09:31:19 -060020 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 Klein5e4b1bc2019-07-02 12:27:06 -060032 return chroot_lib.Chroot(path=path, cache_dir=cache_dir,
33 chrome_root=chrome_root, env=env)
Alex Klein4f0eb432019-05-02 13:56:04 -060034
35
Alex Kleina9d500b2019-04-22 15:37:51 -060036def 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
45def 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
53def 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
66def 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.')