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