blob: 8ea233dc82e6ecb6d84f8968e3d0501d8a17016e [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
Mike Frysingeref94e4c2020-02-10 23:59:54 -050010import sys
11
Alex Klein26e472b2020-03-10 14:35:01 -060012from chromite.api.gen.chromite.api import sysroot_pb2
Alex Klein1f67cf32019-10-09 11:13:42 -060013from chromite.api.gen.chromiumos import common_pb2
Alex Klein566d80e2019-09-24 12:27:58 -060014from chromite.cbuildbot import goma_util
Alex Klein915cce92019-12-17 14:19:50 -070015from chromite.lib import constants
Alex Kleina9d500b2019-04-22 15:37:51 -060016from chromite.lib import portage_util
Alex Klein26e472b2020-03-10 14:35:01 -060017from chromite.lib import build_target_lib
Alex Klein171da612019-08-06 14:00:42 -060018from chromite.lib.chroot_lib import Chroot
19
20
Mike Frysingeref94e4c2020-02-10 23:59:54 -050021assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
22
23
Alex Klein171da612019-08-06 14:00:42 -060024class Error(Exception):
25 """Base error class for the module."""
26
27
28class InvalidMessageError(Error):
29 """Invalid message."""
Alex Kleina9d500b2019-04-22 15:37:51 -060030
31
Alex Kleinc7d647f2020-01-06 12:00:48 -070032def ParseChroot(chroot_message):
Alex Klein171da612019-08-06 14:00:42 -060033 """Create a chroot object from the chroot message.
34
35 Args:
36 chroot_message (common_pb2.Chroot): The chroot message.
37
38 Returns:
39 Chroot: The parsed chroot object.
40
41 Raises:
42 AssertionError: When the message is not a Chroot message.
43 """
44 assert isinstance(chroot_message, common_pb2.Chroot)
45
Alex Klein915cce92019-12-17 14:19:50 -070046 path = chroot_message.path or constants.DEFAULT_CHROOT_PATH
Alex Klein4f0eb432019-05-02 13:56:04 -060047 cache_dir = chroot_message.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060048 chrome_root = chroot_message.chrome_dir
Alex Klein4f0eb432019-05-02 13:56:04 -060049
Alex Klein38c7d9e2019-05-08 09:31:19 -060050 use_flags = [u.flag for u in chroot_message.env.use_flags]
51 features = [f.feature for f in chroot_message.env.features]
52
53 env = {}
54 if use_flags:
55 env['USE'] = ' '.join(use_flags)
56
Alex Kleinb7485bb2019-09-19 13:23:37 -060057 # Make sure it'll use the local source to build chrome when we have it.
58 if chrome_root:
59 env['CHROME_ORIGIN'] = 'LOCAL_SOURCE'
60
Alex Klein38c7d9e2019-05-08 09:31:19 -060061 if features:
62 env['FEATURES'] = ' '.join(features)
63
Alex Klein9b7331e2019-12-30 14:37:21 -070064 chroot = Chroot(
65 path=path, cache_dir=cache_dir, chrome_root=chrome_root, env=env)
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
David Burgerec676f62020-07-03 09:09:31 -060078 if goma_message.goma_approach == common_pb2.GomaConfig.RBE_STAGING:
Alex Klein915cce92019-12-17 14:19:50 -070079 goma_approach = goma_util.GomaApproach('?staging',
80 'staging-goma.chromium.org', True)
David Burgerec676f62020-07-03 09:09:31 -060081 else:
82 goma_approach = goma_util.GomaApproach('?prod', 'goma.chromium.org', True)
Alex Klein915cce92019-12-17 14:19:50 -070083
Michael Mortensen4ccfb082020-01-22 16:24:03 -070084 # Note that we are not specifying the goma log_dir so that goma will create
85 # and use a tmp dir for the logs.
Alex Klein915cce92019-12-17 14:19:50 -070086 stats_filename = goma_message.stats_file or None
87 counterz_filename = goma_message.counterz_file or None
88
89 return goma_util.Goma(goma_message.goma_dir,
90 goma_message.goma_client_json,
91 stage_name='BuildAPI',
92 chromeos_goma_dir=chromeos_goma_dir,
93 chroot_dir=chroot_path,
94 goma_approach=goma_approach,
Alex Klein915cce92019-12-17 14:19:50 -070095 stats_filename=stats_filename,
96 counterz_filename=counterz_filename)
97
98
Alex Klein26e472b2020-03-10 14:35:01 -060099def ParseBuildTarget(build_target_message, profile_message=None):
Alex Klein171da612019-08-06 14:00:42 -0600100 """Create a BuildTarget object from a build_target message.
101
102 Args:
103 build_target_message (common_pb2.BuildTarget): The BuildTarget message.
Alex Klein26e472b2020-03-10 14:35:01 -0600104 profile_message (sysroot_pb2.Profile|None): The profile message.
Alex Klein171da612019-08-06 14:00:42 -0600105
106 Returns:
107 BuildTarget: The parsed instance.
108
109 Raises:
110 AssertionError: When the field is not a BuildTarget message.
111 """
112 assert isinstance(build_target_message, common_pb2.BuildTarget)
Alex Klein26e472b2020-03-10 14:35:01 -0600113 assert (profile_message is None or
114 isinstance(profile_message, sysroot_pb2.Profile))
Alex Klein171da612019-08-06 14:00:42 -0600115
Alex Klein26e472b2020-03-10 14:35:01 -0600116 profile_name = profile_message.name if profile_message else None
117 return build_target_lib.BuildTarget(
118 build_target_message.name, profile=profile_name)
Alex Klein171da612019-08-06 14:00:42 -0600119
120
121def ParseBuildTargets(repeated_build_target_field):
122 """Create a BuildTarget for each entry in the repeated field.
123
124 Args:
125 repeated_build_target_field: The repeated BuildTarget field.
126
127 Returns:
128 list[BuildTarget]: The parsed BuildTargets.
129
130 Raises:
131 AssertionError: When the field contains non-BuildTarget messages.
132 """
133 return [ParseBuildTarget(target) for target in repeated_build_target_field]
Alex Klein4f0eb432019-05-02 13:56:04 -0600134
135
Alex Kleina9d500b2019-04-22 15:37:51 -0600136def CPVToPackageInfo(cpv, package_info):
137 """Helper to translate CPVs into a PackageInfo message."""
138 package_info.package_name = cpv.package
139 if cpv.category:
140 package_info.category = cpv.category
141 if cpv.version:
142 package_info.version = cpv.version
143
144
145def PackageInfoToCPV(package_info):
146 """Helper to translate a PackageInfo message into a CPV."""
147 if not package_info or not package_info.package_name:
148 return None
149
150 return portage_util.SplitCPV(PackageInfoToString(package_info), strict=False)
151
152
153def PackageInfoToString(package_info):
154 # Combine the components into the full CPV string that SplitCPV parses.
155 # TODO: Turn portage_util.CPV into a class that can handle building out an
156 # instance from components.
157 if not package_info.package_name:
158 raise ValueError('Invalid package_info.')
159
160 c = ('%s/' % package_info.category) if package_info.category else ''
161 p = package_info.package_name
162 v = ('-%s' % package_info.version) if package_info.version else ''
163 return '%s%s%s' % (c, p, v)
164
165
166def CPVToString(cpv):
167 """Get the most useful string representation from a CPV.
168
169 Args:
170 cpv (portage_util.CPV): The CPV object.
171
172 Returns:
173 str
174
175 Raises:
176 ValueError - when the CPV has no useful fields set.
177 """
178 if cpv.cpf:
179 return cpv.cpf
180 elif cpv.cpv:
181 return cpv.cpv
182 elif cpv.cp:
183 return cpv.cp
184 elif cpv.package:
185 return cpv.package
186 else:
187 raise ValueError('Invalid CPV provided.')