blob: ba4031b22a5a94032d577534ac35f9aa452a3cbf [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 Klein915cce92019-12-17 14:19:50 -070012from chromite.lib import constants
Alex Kleina9d500b2019-04-22 15:37:51 -060013from chromite.lib import portage_util
Alex Klein171da612019-08-06 14:00:42 -060014from chromite.lib.build_target_util import BuildTarget
15from chromite.lib.chroot_lib import Chroot
16
17
18class Error(Exception):
19 """Base error class for the module."""
20
21
22class InvalidMessageError(Error):
23 """Invalid message."""
Alex Kleina9d500b2019-04-22 15:37:51 -060024
25
Alex Kleinc7d647f2020-01-06 12:00:48 -070026def ParseChroot(chroot_message):
Alex Klein171da612019-08-06 14:00:42 -060027 """Create a chroot object from the chroot message.
28
29 Args:
30 chroot_message (common_pb2.Chroot): The chroot message.
31
32 Returns:
33 Chroot: The parsed chroot object.
34
35 Raises:
36 AssertionError: When the message is not a Chroot message.
37 """
38 assert isinstance(chroot_message, common_pb2.Chroot)
39
Alex Klein915cce92019-12-17 14:19:50 -070040 path = chroot_message.path or constants.DEFAULT_CHROOT_PATH
Alex Klein4f0eb432019-05-02 13:56:04 -060041 cache_dir = chroot_message.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060042 chrome_root = chroot_message.chrome_dir
Alex Klein4f0eb432019-05-02 13:56:04 -060043
Alex Klein38c7d9e2019-05-08 09:31:19 -060044 use_flags = [u.flag for u in chroot_message.env.use_flags]
45 features = [f.feature for f in chroot_message.env.features]
46
47 env = {}
48 if use_flags:
49 env['USE'] = ' '.join(use_flags)
50
Alex Kleinb7485bb2019-09-19 13:23:37 -060051 # Make sure it'll use the local source to build chrome when we have it.
52 if chrome_root:
53 env['CHROME_ORIGIN'] = 'LOCAL_SOURCE'
54
Alex Klein38c7d9e2019-05-08 09:31:19 -060055 if features:
56 env['FEATURES'] = ' '.join(features)
57
Alex Klein9b7331e2019-12-30 14:37:21 -070058 chroot = Chroot(
59 path=path, cache_dir=cache_dir, chrome_root=chrome_root, env=env)
Alex Klein171da612019-08-06 14:00:42 -060060
Alex Klein9b7331e2019-12-30 14:37:21 -070061 return chroot
Alex Klein171da612019-08-06 14:00:42 -060062
Alex Klein915cce92019-12-17 14:19:50 -070063def ParseGomaConfig(goma_message, chroot_path):
64 """Parse a goma config message."""
65 assert isinstance(goma_message, common_pb2.GomaConfig)
66
67 if not goma_message.goma_dir:
68 return None
69
70 # Parse the goma config.
71 chromeos_goma_dir = goma_message.chromeos_goma_dir or None
72 goma_approach = None
73 if goma_message.goma_approach == common_pb2.GomaConfig.RBE_PROD:
74 goma_approach = goma_util.GomaApproach('?prod', 'goma.chromium.org', True)
75 elif goma_message.goma_approach == common_pb2.GomaConfig.RBE_STAGING:
76 goma_approach = goma_util.GomaApproach('?staging',
77 'staging-goma.chromium.org', True)
78
Michael Mortensen4ccfb082020-01-22 16:24:03 -070079 # Note that we are not specifying the goma log_dir so that goma will create
80 # and use a tmp dir for the logs.
Alex Klein915cce92019-12-17 14:19:50 -070081 stats_filename = goma_message.stats_file or None
82 counterz_filename = goma_message.counterz_file or None
83
84 return goma_util.Goma(goma_message.goma_dir,
85 goma_message.goma_client_json,
86 stage_name='BuildAPI',
87 chromeos_goma_dir=chromeos_goma_dir,
88 chroot_dir=chroot_path,
89 goma_approach=goma_approach,
Alex Klein915cce92019-12-17 14:19:50 -070090 stats_filename=stats_filename,
91 counterz_filename=counterz_filename)
92
93
Alex Klein171da612019-08-06 14:00:42 -060094def ParseBuildTarget(build_target_message):
95 """Create a BuildTarget object from a build_target message.
96
97 Args:
98 build_target_message (common_pb2.BuildTarget): The BuildTarget message.
99
100 Returns:
101 BuildTarget: The parsed instance.
102
103 Raises:
104 AssertionError: When the field is not a BuildTarget message.
105 """
106 assert isinstance(build_target_message, common_pb2.BuildTarget)
107
108 return BuildTarget(build_target_message.name)
109
110
111def ParseBuildTargets(repeated_build_target_field):
112 """Create a BuildTarget for each entry in the repeated field.
113
114 Args:
115 repeated_build_target_field: The repeated BuildTarget field.
116
117 Returns:
118 list[BuildTarget]: The parsed BuildTargets.
119
120 Raises:
121 AssertionError: When the field contains non-BuildTarget messages.
122 """
123 return [ParseBuildTarget(target) for target in repeated_build_target_field]
Alex Klein4f0eb432019-05-02 13:56:04 -0600124
125
Alex Kleina9d500b2019-04-22 15:37:51 -0600126def CPVToPackageInfo(cpv, package_info):
127 """Helper to translate CPVs into a PackageInfo message."""
128 package_info.package_name = cpv.package
129 if cpv.category:
130 package_info.category = cpv.category
131 if cpv.version:
132 package_info.version = cpv.version
133
134
135def PackageInfoToCPV(package_info):
136 """Helper to translate a PackageInfo message into a CPV."""
137 if not package_info or not package_info.package_name:
138 return None
139
140 return portage_util.SplitCPV(PackageInfoToString(package_info), strict=False)
141
142
143def PackageInfoToString(package_info):
144 # Combine the components into the full CPV string that SplitCPV parses.
145 # TODO: Turn portage_util.CPV into a class that can handle building out an
146 # instance from components.
147 if not package_info.package_name:
148 raise ValueError('Invalid package_info.')
149
150 c = ('%s/' % package_info.category) if package_info.category else ''
151 p = package_info.package_name
152 v = ('-%s' % package_info.version) if package_info.version else ''
153 return '%s%s%s' % (c, p, v)
154
155
156def CPVToString(cpv):
157 """Get the most useful string representation from a CPV.
158
159 Args:
160 cpv (portage_util.CPV): The CPV object.
161
162 Returns:
163 str
164
165 Raises:
166 ValueError - when the CPV has no useful fields set.
167 """
168 if cpv.cpf:
169 return cpv.cpf
170 elif cpv.cpv:
171 return cpv.cpv
172 elif cpv.cp:
173 return cpv.cp
174 elif cpv.package:
175 return cpv.package
176 else:
177 raise ValueError('Invalid CPV provided.')