blob: ccfac338460ddd2fb16ccd734a90130e2fc54bc3 [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
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
Michael Mortensen4ccfb082020-01-22 16:24:03 -070085 # Note that we are not specifying the goma log_dir so that goma will create
86 # and use a tmp dir for the logs.
Alex Klein915cce92019-12-17 14:19:50 -070087 stats_filename = goma_message.stats_file or None
88 counterz_filename = goma_message.counterz_file or None
89
90 return goma_util.Goma(goma_message.goma_dir,
91 goma_message.goma_client_json,
92 stage_name='BuildAPI',
93 chromeos_goma_dir=chromeos_goma_dir,
94 chroot_dir=chroot_path,
95 goma_approach=goma_approach,
Alex Klein915cce92019-12-17 14:19:50 -070096 stats_filename=stats_filename,
97 counterz_filename=counterz_filename)
98
99
Alex Klein26e472b2020-03-10 14:35:01 -0600100def ParseBuildTarget(build_target_message, profile_message=None):
Alex Klein171da612019-08-06 14:00:42 -0600101 """Create a BuildTarget object from a build_target message.
102
103 Args:
104 build_target_message (common_pb2.BuildTarget): The BuildTarget message.
Alex Klein26e472b2020-03-10 14:35:01 -0600105 profile_message (sysroot_pb2.Profile|None): The profile message.
Alex Klein171da612019-08-06 14:00:42 -0600106
107 Returns:
108 BuildTarget: The parsed instance.
109
110 Raises:
111 AssertionError: When the field is not a BuildTarget message.
112 """
113 assert isinstance(build_target_message, common_pb2.BuildTarget)
Alex Klein26e472b2020-03-10 14:35:01 -0600114 assert (profile_message is None or
115 isinstance(profile_message, sysroot_pb2.Profile))
Alex Klein171da612019-08-06 14:00:42 -0600116
Alex Klein26e472b2020-03-10 14:35:01 -0600117 profile_name = profile_message.name if profile_message else None
118 return build_target_lib.BuildTarget(
119 build_target_message.name, profile=profile_name)
Alex Klein171da612019-08-06 14:00:42 -0600120
121
122def ParseBuildTargets(repeated_build_target_field):
123 """Create a BuildTarget for each entry in the repeated field.
124
125 Args:
126 repeated_build_target_field: The repeated BuildTarget field.
127
128 Returns:
129 list[BuildTarget]: The parsed BuildTargets.
130
131 Raises:
132 AssertionError: When the field contains non-BuildTarget messages.
133 """
134 return [ParseBuildTarget(target) for target in repeated_build_target_field]
Alex Klein4f0eb432019-05-02 13:56:04 -0600135
136
Alex Kleina9d500b2019-04-22 15:37:51 -0600137def CPVToPackageInfo(cpv, package_info):
138 """Helper to translate CPVs into a PackageInfo message."""
139 package_info.package_name = cpv.package
140 if cpv.category:
141 package_info.category = cpv.category
142 if cpv.version:
143 package_info.version = cpv.version
144
145
146def PackageInfoToCPV(package_info):
147 """Helper to translate a PackageInfo message into a CPV."""
148 if not package_info or not package_info.package_name:
149 return None
150
151 return portage_util.SplitCPV(PackageInfoToString(package_info), strict=False)
152
153
154def PackageInfoToString(package_info):
155 # Combine the components into the full CPV string that SplitCPV parses.
156 # TODO: Turn portage_util.CPV into a class that can handle building out an
157 # instance from components.
158 if not package_info.package_name:
159 raise ValueError('Invalid package_info.')
160
161 c = ('%s/' % package_info.category) if package_info.category else ''
162 p = package_info.package_name
163 v = ('-%s' % package_info.version) if package_info.version else ''
164 return '%s%s%s' % (c, p, v)
165
166
167def CPVToString(cpv):
168 """Get the most useful string representation from a CPV.
169
170 Args:
171 cpv (portage_util.CPV): The CPV object.
172
173 Returns:
174 str
175
176 Raises:
177 ValueError - when the CPV has no useful fields set.
178 """
179 if cpv.cpf:
180 return cpv.cpf
181 elif cpv.cpv:
182 return cpv.cpv
183 elif cpv.cp:
184 return cpv.cp
185 elif cpv.package:
186 return cpv.package
187 else:
188 raise ValueError('Invalid CPV provided.')