blob: 5306f80905abf1e3e688d6c7abd89a57e97e7715 [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 Klein915cce92019-12-17 14:19:50 -070010import os
11
Alex Klein1f67cf32019-10-09 11:13:42 -060012from chromite.api.gen.chromiumos import common_pb2
Alex Klein566d80e2019-09-24 12:27:58 -060013from chromite.cbuildbot import goma_util
Alex Klein915cce92019-12-17 14:19:50 -070014from chromite.lib import constants
Alex Kleina9d500b2019-04-22 15:37:51 -060015from chromite.lib import portage_util
Alex Klein171da612019-08-06 14:00:42 -060016from chromite.lib.build_target_util import BuildTarget
17from chromite.lib.chroot_lib import Chroot
18
19
20class Error(Exception):
21 """Base error class for the module."""
22
23
24class InvalidMessageError(Error):
25 """Invalid message."""
Alex Kleina9d500b2019-04-22 15:37:51 -060026
27
Alex Klein915cce92019-12-17 14:19:50 -070028def ParseChroot(chroot_message, parse_goma=True):
Alex Klein171da612019-08-06 14:00:42 -060029 """Create a chroot object from the chroot message.
30
31 Args:
32 chroot_message (common_pb2.Chroot): The chroot message.
Alex Klein915cce92019-12-17 14:19:50 -070033 parse_goma (bool): Whether to try to parse the goma configs.
Alex Klein171da612019-08-06 14:00:42 -060034
35 Returns:
36 Chroot: The parsed chroot object.
37
38 Raises:
39 AssertionError: When the message is not a Chroot message.
40 """
41 assert isinstance(chroot_message, common_pb2.Chroot)
42
Alex Klein915cce92019-12-17 14:19:50 -070043 path = chroot_message.path or constants.DEFAULT_CHROOT_PATH
Alex Klein4f0eb432019-05-02 13:56:04 -060044 cache_dir = chroot_message.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060045 chrome_root = chroot_message.chrome_dir
Alex Klein4f0eb432019-05-02 13:56:04 -060046
Alex Klein38c7d9e2019-05-08 09:31:19 -060047 use_flags = [u.flag for u in chroot_message.env.use_flags]
48 features = [f.feature for f in chroot_message.env.features]
49
50 env = {}
51 if use_flags:
52 env['USE'] = ' '.join(use_flags)
53
Alex Kleinb7485bb2019-09-19 13:23:37 -060054 # Make sure it'll use the local source to build chrome when we have it.
55 if chrome_root:
56 env['CHROME_ORIGIN'] = 'LOCAL_SOURCE'
57
Alex Klein38c7d9e2019-05-08 09:31:19 -060058 if features:
59 env['FEATURES'] = ' '.join(features)
60
Alex Klein915cce92019-12-17 14:19:50 -070061 goma = ParseGomaConfig(chroot_message.goma, path) if parse_goma else None
Alex Klein566d80e2019-09-24 12:27:58 -060062
Alex Klein9b7331e2019-12-30 14:37:21 -070063 chroot = Chroot(
64 path=path, cache_dir=cache_dir, chrome_root=chrome_root, env=env)
65 chroot.goma = goma
Alex Klein171da612019-08-06 14:00:42 -060066
Alex Klein9b7331e2019-12-30 14:37:21 -070067 return chroot
Alex Klein171da612019-08-06 14:00:42 -060068
Alex Klein915cce92019-12-17 14:19:50 -070069def ParseGomaConfig(goma_message, chroot_path):
70 """Parse a goma config message."""
71 assert isinstance(goma_message, common_pb2.GomaConfig)
72
73 if not goma_message.goma_dir:
74 return None
75
76 # Parse the goma config.
77 chromeos_goma_dir = goma_message.chromeos_goma_dir or None
78 goma_approach = None
79 if goma_message.goma_approach == common_pb2.GomaConfig.RBE_PROD:
80 goma_approach = goma_util.GomaApproach('?prod', 'goma.chromium.org', True)
81 elif goma_message.goma_approach == common_pb2.GomaConfig.RBE_STAGING:
82 goma_approach = goma_util.GomaApproach('?staging',
83 'staging-goma.chromium.org', True)
84
85 log_dir = goma_message.log_dir.dir or None
86 if log_dir:
87 log_dir = os.path.join(chroot_path, log_dir.lstrip(os.sep))
88
89 stats_filename = goma_message.stats_file or None
90 counterz_filename = goma_message.counterz_file or None
91
92 return goma_util.Goma(goma_message.goma_dir,
93 goma_message.goma_client_json,
94 stage_name='BuildAPI',
95 chromeos_goma_dir=chromeos_goma_dir,
96 chroot_dir=chroot_path,
97 goma_approach=goma_approach,
98 log_dir=log_dir,
99 stats_filename=stats_filename,
100 counterz_filename=counterz_filename)
101
102
Alex Klein171da612019-08-06 14:00:42 -0600103def ParseBuildTarget(build_target_message):
104 """Create a BuildTarget object from a build_target message.
105
106 Args:
107 build_target_message (common_pb2.BuildTarget): The BuildTarget message.
108
109 Returns:
110 BuildTarget: The parsed instance.
111
112 Raises:
113 AssertionError: When the field is not a BuildTarget message.
114 """
115 assert isinstance(build_target_message, common_pb2.BuildTarget)
116
117 return BuildTarget(build_target_message.name)
118
119
120def ParseBuildTargets(repeated_build_target_field):
121 """Create a BuildTarget for each entry in the repeated field.
122
123 Args:
124 repeated_build_target_field: The repeated BuildTarget field.
125
126 Returns:
127 list[BuildTarget]: The parsed BuildTargets.
128
129 Raises:
130 AssertionError: When the field contains non-BuildTarget messages.
131 """
132 return [ParseBuildTarget(target) for target in repeated_build_target_field]
Alex Klein4f0eb432019-05-02 13:56:04 -0600133
134
Alex Kleina9d500b2019-04-22 15:37:51 -0600135def CPVToPackageInfo(cpv, package_info):
136 """Helper to translate CPVs into a PackageInfo message."""
137 package_info.package_name = cpv.package
138 if cpv.category:
139 package_info.category = cpv.category
140 if cpv.version:
141 package_info.version = cpv.version
142
143
144def PackageInfoToCPV(package_info):
145 """Helper to translate a PackageInfo message into a CPV."""
146 if not package_info or not package_info.package_name:
147 return None
148
149 return portage_util.SplitCPV(PackageInfoToString(package_info), strict=False)
150
151
152def PackageInfoToString(package_info):
153 # Combine the components into the full CPV string that SplitCPV parses.
154 # TODO: Turn portage_util.CPV into a class that can handle building out an
155 # instance from components.
156 if not package_info.package_name:
157 raise ValueError('Invalid package_info.')
158
159 c = ('%s/' % package_info.category) if package_info.category else ''
160 p = package_info.package_name
161 v = ('-%s' % package_info.version) if package_info.version else ''
162 return '%s%s%s' % (c, p, v)
163
164
165def CPVToString(cpv):
166 """Get the most useful string representation from a CPV.
167
168 Args:
169 cpv (portage_util.CPV): The CPV object.
170
171 Returns:
172 str
173
174 Raises:
175 ValueError - when the CPV has no useful fields set.
176 """
177 if cpv.cpf:
178 return cpv.cpf
179 elif cpv.cpv:
180 return cpv.cpv
181 elif cpv.cp:
182 return cpv.cp
183 elif cpv.package:
184 return cpv.package
185 else:
186 raise ValueError('Invalid CPV provided.')