blob: 630be1f620773f80e8a3fb06f846dc969055bf8c [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 Klein171da612019-08-06 14:00:42 -060010from chromite.api.gen.chromiumos import common_pb2
11
Alex Klein566d80e2019-09-24 12:27:58 -060012from chromite.cbuildbot import goma_util
Alex Kleina9d500b2019-04-22 15:37:51 -060013from chromite.lib import portage_util
Alex Klein171da612019-08-06 14:00:42 -060014from chromite.lib.build_target_util import BuildTarget
15from chromite.lib.chroot_lib import Chroot
16
17
18class Error(Exception):
19 """Base error class for the module."""
20
21
22class InvalidMessageError(Error):
23 """Invalid message."""
Alex Kleina9d500b2019-04-22 15:37:51 -060024
25
Alex Klein4f0eb432019-05-02 13:56:04 -060026def ParseChroot(chroot_message):
Alex Klein171da612019-08-06 14:00:42 -060027 """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 Klein4f0eb432019-05-02 13:56:04 -060040 path = chroot_message.path
41 cache_dir = chroot_message.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060042 chrome_root = chroot_message.chrome_dir
Alex Klein4f0eb432019-05-02 13:56:04 -060043
Alex Klein38c7d9e2019-05-08 09:31:19 -060044 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 Kleinb7485bb2019-09-19 13:23:37 -060051 # 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 Klein38c7d9e2019-05-08 09:31:19 -060055 # TODO(saklein) Remove the default when fully integrated in recipes.
56 env['FEATURES'] = 'separatedebug'
57 if features:
58 env['FEATURES'] = ' '.join(features)
59
Alex Klein566d80e2019-09-24 12:27:58 -060060 goma = None
61 if chroot_message.goma.goma_dir:
62 goma = goma_util.Goma(chroot_message.goma.goma_dir,
63 chroot_message.goma.goma_client_json,
64 stage_name='BuildAPI')
65
Alex Klein171da612019-08-06 14:00:42 -060066 return Chroot(path=path, cache_dir=cache_dir, chrome_root=chrome_root,
Alex Klein566d80e2019-09-24 12:27:58 -060067 env=env, goma=goma)
Alex Klein171da612019-08-06 14:00:42 -060068
69
70def ParseBuildTarget(build_target_message):
71 """Create a BuildTarget object from a build_target message.
72
73 Args:
74 build_target_message (common_pb2.BuildTarget): The BuildTarget message.
75
76 Returns:
77 BuildTarget: The parsed instance.
78
79 Raises:
80 AssertionError: When the field is not a BuildTarget message.
81 """
82 assert isinstance(build_target_message, common_pb2.BuildTarget)
83
84 return BuildTarget(build_target_message.name)
85
86
87def ParseBuildTargets(repeated_build_target_field):
88 """Create a BuildTarget for each entry in the repeated field.
89
90 Args:
91 repeated_build_target_field: The repeated BuildTarget field.
92
93 Returns:
94 list[BuildTarget]: The parsed BuildTargets.
95
96 Raises:
97 AssertionError: When the field contains non-BuildTarget messages.
98 """
99 return [ParseBuildTarget(target) for target in repeated_build_target_field]
Alex Klein4f0eb432019-05-02 13:56:04 -0600100
101
Alex Kleina9d500b2019-04-22 15:37:51 -0600102def CPVToPackageInfo(cpv, package_info):
103 """Helper to translate CPVs into a PackageInfo message."""
104 package_info.package_name = cpv.package
105 if cpv.category:
106 package_info.category = cpv.category
107 if cpv.version:
108 package_info.version = cpv.version
109
110
111def PackageInfoToCPV(package_info):
112 """Helper to translate a PackageInfo message into a CPV."""
113 if not package_info or not package_info.package_name:
114 return None
115
116 return portage_util.SplitCPV(PackageInfoToString(package_info), strict=False)
117
118
119def PackageInfoToString(package_info):
120 # Combine the components into the full CPV string that SplitCPV parses.
121 # TODO: Turn portage_util.CPV into a class that can handle building out an
122 # instance from components.
123 if not package_info.package_name:
124 raise ValueError('Invalid package_info.')
125
126 c = ('%s/' % package_info.category) if package_info.category else ''
127 p = package_info.package_name
128 v = ('-%s' % package_info.version) if package_info.version else ''
129 return '%s%s%s' % (c, p, v)
130
131
132def CPVToString(cpv):
133 """Get the most useful string representation from a CPV.
134
135 Args:
136 cpv (portage_util.CPV): The CPV object.
137
138 Returns:
139 str
140
141 Raises:
142 ValueError - when the CPV has no useful fields set.
143 """
144 if cpv.cpf:
145 return cpv.cpf
146 elif cpv.cpv:
147 return cpv.cpv
148 elif cpv.cp:
149 return cpv.cp
150 elif cpv.package:
151 return cpv.package
152 else:
153 raise ValueError('Invalid CPV provided.')