blob: 9c6abaf8c1bd25242d246443092fa7ce29b1de93 [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 Klein1f67cf32019-10-09 11:13:42 -060010from chromite.api.gen.chromiumos import common_pb2
Alex Klein566d80e2019-09-24 12:27:58 -060011from chromite.cbuildbot import goma_util
Alex Kleina9d500b2019-04-22 15:37:51 -060012from chromite.lib import portage_util
Alex Klein171da612019-08-06 14:00:42 -060013from chromite.lib.build_target_util import BuildTarget
14from chromite.lib.chroot_lib import Chroot
15
16
17class Error(Exception):
18 """Base error class for the module."""
19
20
21class InvalidMessageError(Error):
22 """Invalid message."""
Alex Kleina9d500b2019-04-22 15:37:51 -060023
24
Alex Klein4f0eb432019-05-02 13:56:04 -060025def ParseChroot(chroot_message):
Alex Klein171da612019-08-06 14:00:42 -060026 """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 Klein4f0eb432019-05-02 13:56:04 -060039 path = chroot_message.path
40 cache_dir = chroot_message.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060041 chrome_root = chroot_message.chrome_dir
Alex Klein4f0eb432019-05-02 13:56:04 -060042
Alex Klein38c7d9e2019-05-08 09:31:19 -060043 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
Alex Kleinb7485bb2019-09-19 13:23:37 -060050 # Make sure it'll use the local source to build chrome when we have it.
51 if chrome_root:
52 env['CHROME_ORIGIN'] = 'LOCAL_SOURCE'
53
Alex Klein38c7d9e2019-05-08 09:31:19 -060054 # TODO(saklein) Remove the default when fully integrated in recipes.
55 env['FEATURES'] = 'separatedebug'
56 if features:
57 env['FEATURES'] = ' '.join(features)
58
Alex Klein566d80e2019-09-24 12:27:58 -060059 goma = None
60 if chroot_message.goma.goma_dir:
Michael Mortensen9a73c322019-10-03 17:14:37 -060061 chromeos_goma_dir = chroot_message.goma.chromeos_goma_dir or None
David Burgere3dcc752019-10-18 09:55:32 -060062 goma_approach = None
63 if chroot_message.goma.goma_approach == common_pb2.GomaConfig.RBE_PROD:
64 goma_approach = goma_util.GomaApproach('?prod', 'goma.chromium.org', True)
65 elif chroot_message.goma.goma_approach == common_pb2.GomaConfig.RBE_STAGING:
66 goma_approach = goma_util.GomaApproach('?staging',
67 'staging-goma.chromium.org', True)
Alex Kleinb37d39e2019-12-09 14:31:53 -070068
69 log_dir = chroot_message.goma.log_dir.dir or None
70 stats_filename = chroot_message.goma.stats_file or None
71 counterz_filename = chroot_message.goma.counterz_file or None
72
Alex Klein566d80e2019-09-24 12:27:58 -060073 goma = goma_util.Goma(chroot_message.goma.goma_dir,
74 chroot_message.goma.goma_client_json,
Michael Mortensen9a73c322019-10-03 17:14:37 -060075 stage_name='BuildAPI',
Alex Klein1f67cf32019-10-09 11:13:42 -060076 chromeos_goma_dir=chromeos_goma_dir,
David Burgere3dcc752019-10-18 09:55:32 -060077 chroot_dir=path,
Alex Kleinb37d39e2019-12-09 14:31:53 -070078 goma_approach=goma_approach,
79 log_dir=log_dir,
80 stats_filename=stats_filename,
81 counterz_filename=counterz_filename)
Alex Klein566d80e2019-09-24 12:27:58 -060082
Alex Klein171da612019-08-06 14:00:42 -060083 return Chroot(path=path, cache_dir=cache_dir, chrome_root=chrome_root,
Alex Klein566d80e2019-09-24 12:27:58 -060084 env=env, goma=goma)
Alex Klein171da612019-08-06 14:00:42 -060085
86
87def ParseBuildTarget(build_target_message):
88 """Create a BuildTarget object from a build_target message.
89
90 Args:
91 build_target_message (common_pb2.BuildTarget): The BuildTarget message.
92
93 Returns:
94 BuildTarget: The parsed instance.
95
96 Raises:
97 AssertionError: When the field is not a BuildTarget message.
98 """
99 assert isinstance(build_target_message, common_pb2.BuildTarget)
100
101 return BuildTarget(build_target_message.name)
102
103
104def ParseBuildTargets(repeated_build_target_field):
105 """Create a BuildTarget for each entry in the repeated field.
106
107 Args:
108 repeated_build_target_field: The repeated BuildTarget field.
109
110 Returns:
111 list[BuildTarget]: The parsed BuildTargets.
112
113 Raises:
114 AssertionError: When the field contains non-BuildTarget messages.
115 """
116 return [ParseBuildTarget(target) for target in repeated_build_target_field]
Alex Klein4f0eb432019-05-02 13:56:04 -0600117
118
Alex Kleina9d500b2019-04-22 15:37:51 -0600119def CPVToPackageInfo(cpv, package_info):
120 """Helper to translate CPVs into a PackageInfo message."""
121 package_info.package_name = cpv.package
122 if cpv.category:
123 package_info.category = cpv.category
124 if cpv.version:
125 package_info.version = cpv.version
126
127
128def PackageInfoToCPV(package_info):
129 """Helper to translate a PackageInfo message into a CPV."""
130 if not package_info or not package_info.package_name:
131 return None
132
133 return portage_util.SplitCPV(PackageInfoToString(package_info), strict=False)
134
135
136def PackageInfoToString(package_info):
137 # Combine the components into the full CPV string that SplitCPV parses.
138 # TODO: Turn portage_util.CPV into a class that can handle building out an
139 # instance from components.
140 if not package_info.package_name:
141 raise ValueError('Invalid package_info.')
142
143 c = ('%s/' % package_info.category) if package_info.category else ''
144 p = package_info.package_name
145 v = ('-%s' % package_info.version) if package_info.version else ''
146 return '%s%s%s' % (c, p, v)
147
148
149def CPVToString(cpv):
150 """Get the most useful string representation from a CPV.
151
152 Args:
153 cpv (portage_util.CPV): The CPV object.
154
155 Returns:
156 str
157
158 Raises:
159 ValueError - when the CPV has no useful fields set.
160 """
161 if cpv.cpf:
162 return cpv.cpf
163 elif cpv.cpv:
164 return cpv.cpv
165 elif cpv.cp:
166 return cpv.cp
167 elif cpv.package:
168 return cpv.package
169 else:
170 raise ValueError('Invalid CPV provided.')