blob: ec46ebf2c4f0d70bab7de0f3ced84fb479a02ec2 [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 Klein26e472b2020-03-10 14:35:01 -060010from chromite.api.gen.chromite.api import sysroot_pb2
Alex Klein1f67cf32019-10-09 11:13:42 -060011from chromite.api.gen.chromiumos import common_pb2
Alex Klein566d80e2019-09-24 12:27:58 -060012from chromite.cbuildbot import goma_util
Alex Klein26e472b2020-03-10 14:35:01 -060013from chromite.lib import build_target_lib
Alex Klein18a60af2020-06-11 12:08:47 -060014from chromite.lib import constants
15from chromite.lib.parser import package_info
Alex Klein171da612019-08-06 14:00:42 -060016from chromite.lib.chroot_lib import Chroot
17
18
19class Error(Exception):
20 """Base error class for the module."""
21
22
23class InvalidMessageError(Error):
24 """Invalid message."""
Alex Kleina9d500b2019-04-22 15:37:51 -060025
26
Alex Kleinc7d647f2020-01-06 12:00:48 -070027def ParseChroot(chroot_message):
Alex Klein171da612019-08-06 14:00:42 -060028 """Create a chroot object from the chroot message.
29
30 Args:
31 chroot_message (common_pb2.Chroot): The chroot message.
32
33 Returns:
34 Chroot: The parsed chroot object.
35
36 Raises:
37 AssertionError: When the message is not a Chroot message.
38 """
39 assert isinstance(chroot_message, common_pb2.Chroot)
40
Alex Klein915cce92019-12-17 14:19:50 -070041 path = chroot_message.path or constants.DEFAULT_CHROOT_PATH
Alex Klein4f0eb432019-05-02 13:56:04 -060042 cache_dir = chroot_message.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060043 chrome_root = chroot_message.chrome_dir
Alex Klein4f0eb432019-05-02 13:56:04 -060044
Alex Klein38c7d9e2019-05-08 09:31:19 -060045 use_flags = [u.flag for u in chroot_message.env.use_flags]
46 features = [f.feature for f in chroot_message.env.features]
47
48 env = {}
49 if use_flags:
50 env['USE'] = ' '.join(use_flags)
51
Alex Kleinb7485bb2019-09-19 13:23:37 -060052 # Make sure it'll use the local source to build chrome when we have it.
53 if chrome_root:
54 env['CHROME_ORIGIN'] = 'LOCAL_SOURCE'
55
Alex Klein38c7d9e2019-05-08 09:31:19 -060056 if features:
57 env['FEATURES'] = ' '.join(features)
58
Alex Klein9b7331e2019-12-30 14:37:21 -070059 chroot = Chroot(
60 path=path, cache_dir=cache_dir, chrome_root=chrome_root, env=env)
Alex Klein171da612019-08-06 14:00:42 -060061
Alex Klein9b7331e2019-12-30 14:37:21 -070062 return chroot
Alex Klein171da612019-08-06 14:00:42 -060063
Alex Klein915cce92019-12-17 14:19:50 -070064def ParseGomaConfig(goma_message, chroot_path):
65 """Parse a goma config message."""
66 assert isinstance(goma_message, common_pb2.GomaConfig)
67
68 if not goma_message.goma_dir:
69 return None
70
71 # Parse the goma config.
72 chromeos_goma_dir = goma_message.chromeos_goma_dir or None
David Burgerec676f62020-07-03 09:09:31 -060073 if goma_message.goma_approach == common_pb2.GomaConfig.RBE_STAGING:
Alex Klein915cce92019-12-17 14:19:50 -070074 goma_approach = goma_util.GomaApproach('?staging',
75 'staging-goma.chromium.org', True)
Yoshisato Yanagisawa57f7f672021-01-08 02:42:42 +000076 elif goma_message.goma_approach == common_pb2.GomaConfig.RBE_PROD:
David Burgerec676f62020-07-03 09:09:31 -060077 goma_approach = goma_util.GomaApproach('?prod', 'goma.chromium.org', True)
Yoshisato Yanagisawa57f7f672021-01-08 02:42:42 +000078 else:
79 goma_approach = goma_util.GomaApproach('?cros', 'goma.chromium.org', True)
Alex Klein915cce92019-12-17 14:19:50 -070080
Michael Mortensen4ccfb082020-01-22 16:24:03 -070081 # Note that we are not specifying the goma log_dir so that goma will create
82 # and use a tmp dir for the logs.
Alex Klein915cce92019-12-17 14:19:50 -070083 stats_filename = goma_message.stats_file or None
84 counterz_filename = goma_message.counterz_file or None
85
86 return goma_util.Goma(goma_message.goma_dir,
87 goma_message.goma_client_json,
88 stage_name='BuildAPI',
89 chromeos_goma_dir=chromeos_goma_dir,
90 chroot_dir=chroot_path,
91 goma_approach=goma_approach,
Alex Klein915cce92019-12-17 14:19:50 -070092 stats_filename=stats_filename,
93 counterz_filename=counterz_filename)
94
95
Alex Klein26e472b2020-03-10 14:35:01 -060096def ParseBuildTarget(build_target_message, profile_message=None):
Alex Klein171da612019-08-06 14:00:42 -060097 """Create a BuildTarget object from a build_target message.
98
99 Args:
100 build_target_message (common_pb2.BuildTarget): The BuildTarget message.
Alex Klein26e472b2020-03-10 14:35:01 -0600101 profile_message (sysroot_pb2.Profile|None): The profile message.
Alex Klein171da612019-08-06 14:00:42 -0600102
103 Returns:
104 BuildTarget: The parsed instance.
105
106 Raises:
107 AssertionError: When the field is not a BuildTarget message.
108 """
109 assert isinstance(build_target_message, common_pb2.BuildTarget)
Alex Klein26e472b2020-03-10 14:35:01 -0600110 assert (profile_message is None or
111 isinstance(profile_message, sysroot_pb2.Profile))
Alex Klein171da612019-08-06 14:00:42 -0600112
Alex Klein26e472b2020-03-10 14:35:01 -0600113 profile_name = profile_message.name if profile_message else None
114 return build_target_lib.BuildTarget(
115 build_target_message.name, profile=profile_name)
Alex Klein171da612019-08-06 14:00:42 -0600116
117
118def ParseBuildTargets(repeated_build_target_field):
119 """Create a BuildTarget for each entry in the repeated field.
120
121 Args:
122 repeated_build_target_field: The repeated BuildTarget field.
123
124 Returns:
125 list[BuildTarget]: The parsed BuildTargets.
126
127 Raises:
128 AssertionError: When the field contains non-BuildTarget messages.
129 """
130 return [ParseBuildTarget(target) for target in repeated_build_target_field]
Alex Klein4f0eb432019-05-02 13:56:04 -0600131
132
Alex Klein18a60af2020-06-11 12:08:47 -0600133def CPVToPackageInfo(cpv, package_info_msg):
Alex Kleina9d500b2019-04-22 15:37:51 -0600134 """Helper to translate CPVs into a PackageInfo message."""
Alex Klein18a60af2020-06-11 12:08:47 -0600135 package_info_msg.package_name = cpv.package
Alex Kleina9d500b2019-04-22 15:37:51 -0600136 if cpv.category:
Alex Klein18a60af2020-06-11 12:08:47 -0600137 package_info_msg.category = cpv.category
Alex Kleina9d500b2019-04-22 15:37:51 -0600138 if cpv.version:
Alex Klein18a60af2020-06-11 12:08:47 -0600139 package_info_msg.version = cpv.version
Alex Kleina9d500b2019-04-22 15:37:51 -0600140
141
Alex Klein1e68a8e2020-10-06 17:25:11 -0600142def serialize_package_info(pkg_info: 'package_info.PackageInfo', pkg_info_msg):
143 """Serialize a PackageInfo object to a PackageInfo proto."""
144 pkg_info_msg.package_name = pkg_info.package
145 if pkg_info.category:
146 pkg_info_msg.category = pkg_info.category
147 if pkg_info.vr:
148 pkg_info_msg.version = pkg_info.vr
149
150
151def deserialize_package_info(pkg_info_msg):
152 """Deserialize a PackageInfo message to a PackageInfo object."""
153 return package_info.parse(PackageInfoToString(pkg_info_msg))
154
155
Alex Klein18a60af2020-06-11 12:08:47 -0600156def PackageInfoToCPV(package_info_msg):
Alex Kleina9d500b2019-04-22 15:37:51 -0600157 """Helper to translate a PackageInfo message into a CPV."""
Alex Klein18a60af2020-06-11 12:08:47 -0600158 if not package_info_msg or not package_info_msg.package_name:
Alex Kleina9d500b2019-04-22 15:37:51 -0600159 return None
160
Alex Klein18a60af2020-06-11 12:08:47 -0600161 return package_info.SplitCPV(PackageInfoToString(package_info_msg),
162 strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600163
164
Alex Klein18a60af2020-06-11 12:08:47 -0600165def PackageInfoToString(package_info_msg):
Alex Kleina9d500b2019-04-22 15:37:51 -0600166 # Combine the components into the full CPV string that SplitCPV parses.
Alex Klein18a60af2020-06-11 12:08:47 -0600167 # TODO: Use the lib.parser.package_info.PackageInfo class instead.
168 if not package_info_msg.package_name:
169 raise ValueError('Invalid PackageInfo message.')
Alex Kleina9d500b2019-04-22 15:37:51 -0600170
Alex Klein18a60af2020-06-11 12:08:47 -0600171 c = ('%s/' % package_info_msg.category) if package_info_msg.category else ''
172 p = package_info_msg.package_name
173 v = ('-%s' % package_info_msg.version) if package_info_msg.version else ''
Alex Kleina9d500b2019-04-22 15:37:51 -0600174 return '%s%s%s' % (c, p, v)
175
176
177def CPVToString(cpv):
178 """Get the most useful string representation from a CPV.
179
180 Args:
Alex Klein18a60af2020-06-11 12:08:47 -0600181 cpv (package_info.CPV): The CPV object.
Alex Kleina9d500b2019-04-22 15:37:51 -0600182
183 Returns:
184 str
185
186 Raises:
187 ValueError - when the CPV has no useful fields set.
188 """
189 if cpv.cpf:
190 return cpv.cpf
191 elif cpv.cpv:
192 return cpv.cpv
193 elif cpv.cp:
194 return cpv.cp
195 elif cpv.package:
196 return cpv.package
197 else:
198 raise ValueError('Invalid CPV provided.')