blob: 5eb44cb0d97840d08c43c8601d060758d7f4aba8 [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 -060010import os
Alex Klein171da612019-08-06 14:00:42 -060011
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 Kleina9d500b2019-04-22 15:37:51 -060014from chromite.lib import portage_util
Alex Klein171da612019-08-06 14:00:42 -060015from chromite.lib.build_target_util import BuildTarget
16from 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 Klein4f0eb432019-05-02 13:56:04 -060027def 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 Klein4f0eb432019-05-02 13:56:04 -060041 path = chroot_message.path
42 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 # TODO(saklein) Remove the default when fully integrated in recipes.
57 env['FEATURES'] = 'separatedebug'
58 if features:
59 env['FEATURES'] = ' '.join(features)
60
Alex Klein566d80e2019-09-24 12:27:58 -060061 goma = None
62 if chroot_message.goma.goma_dir:
Michael Mortensen9a73c322019-10-03 17:14:37 -060063 chromeos_goma_dir = chroot_message.goma.chromeos_goma_dir or None
Alex Klein566d80e2019-09-24 12:27:58 -060064 goma = goma_util.Goma(chroot_message.goma.goma_dir,
65 chroot_message.goma.goma_client_json,
Michael Mortensen9a73c322019-10-03 17:14:37 -060066 stage_name='BuildAPI',
Alex Klein1f67cf32019-10-09 11:13:42 -060067 chromeos_goma_dir=chromeos_goma_dir,
68 chroot_tmp=os.path.join(path, 'tmp'))
Alex Klein566d80e2019-09-24 12:27:58 -060069
Alex Klein171da612019-08-06 14:00:42 -060070 return Chroot(path=path, cache_dir=cache_dir, chrome_root=chrome_root,
Alex Klein566d80e2019-09-24 12:27:58 -060071 env=env, goma=goma)
Alex Klein171da612019-08-06 14:00:42 -060072
73
74def ParseBuildTarget(build_target_message):
75 """Create a BuildTarget object from a build_target message.
76
77 Args:
78 build_target_message (common_pb2.BuildTarget): The BuildTarget message.
79
80 Returns:
81 BuildTarget: The parsed instance.
82
83 Raises:
84 AssertionError: When the field is not a BuildTarget message.
85 """
86 assert isinstance(build_target_message, common_pb2.BuildTarget)
87
88 return BuildTarget(build_target_message.name)
89
90
91def ParseBuildTargets(repeated_build_target_field):
92 """Create a BuildTarget for each entry in the repeated field.
93
94 Args:
95 repeated_build_target_field: The repeated BuildTarget field.
96
97 Returns:
98 list[BuildTarget]: The parsed BuildTargets.
99
100 Raises:
101 AssertionError: When the field contains non-BuildTarget messages.
102 """
103 return [ParseBuildTarget(target) for target in repeated_build_target_field]
Alex Klein4f0eb432019-05-02 13:56:04 -0600104
105
Alex Kleina9d500b2019-04-22 15:37:51 -0600106def CPVToPackageInfo(cpv, package_info):
107 """Helper to translate CPVs into a PackageInfo message."""
108 package_info.package_name = cpv.package
109 if cpv.category:
110 package_info.category = cpv.category
111 if cpv.version:
112 package_info.version = cpv.version
113
114
115def PackageInfoToCPV(package_info):
116 """Helper to translate a PackageInfo message into a CPV."""
117 if not package_info or not package_info.package_name:
118 return None
119
120 return portage_util.SplitCPV(PackageInfoToString(package_info), strict=False)
121
122
123def PackageInfoToString(package_info):
124 # Combine the components into the full CPV string that SplitCPV parses.
125 # TODO: Turn portage_util.CPV into a class that can handle building out an
126 # instance from components.
127 if not package_info.package_name:
128 raise ValueError('Invalid package_info.')
129
130 c = ('%s/' % package_info.category) if package_info.category else ''
131 p = package_info.package_name
132 v = ('-%s' % package_info.version) if package_info.version else ''
133 return '%s%s%s' % (c, p, v)
134
135
136def CPVToString(cpv):
137 """Get the most useful string representation from a CPV.
138
139 Args:
140 cpv (portage_util.CPV): The CPV object.
141
142 Returns:
143 str
144
145 Raises:
146 ValueError - when the CPV has no useful fields set.
147 """
148 if cpv.cpf:
149 return cpv.cpf
150 elif cpv.cpv:
151 return cpv.cpv
152 elif cpv.cp:
153 return cpv.cp
154 elif cpv.package:
155 return cpv.package
156 else:
157 raise ValueError('Invalid CPV provided.')