Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 1 | # -*- 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 | """Package related functionality.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 10 | import sys |
| 11 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 12 | from chromite.api import faux |
David Burger | 1e0fe23 | 2019-07-01 14:52:07 -0600 | [diff] [blame] | 13 | from chromite.api import validate |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 14 | from chromite.api.controller import controller_util |
| 15 | from chromite.api.gen.chromite.api import binhost_pb2 |
David Burger | 1e0fe23 | 2019-07-01 14:52:07 -0600 | [diff] [blame] | 16 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 17 | from chromite.lib import constants |
| 18 | from chromite.lib import cros_build_lib |
Alex Klein | 445ae3c | 2020-01-08 16:40:43 -0700 | [diff] [blame] | 19 | from chromite.lib import cros_logging as logging |
Michael Mortensen | bbce7e4 | 2020-05-01 15:03:49 -0600 | [diff] [blame^] | 20 | from chromite.lib import portage_util |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 21 | from chromite.lib.uprev_lib import GitRef |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 22 | from chromite.service import packages |
| 23 | |
| 24 | |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 25 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 26 | |
| 27 | |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 28 | _OVERLAY_TYPE_TO_NAME = { |
| 29 | binhost_pb2.OVERLAYTYPE_PUBLIC: constants.PUBLIC_OVERLAYS, |
| 30 | binhost_pb2.OVERLAYTYPE_PRIVATE: constants.PRIVATE_OVERLAYS, |
| 31 | binhost_pb2.OVERLAYTYPE_BOTH: constants.BOTH_OVERLAYS, |
| 32 | } |
| 33 | |
Michael Mortensen | 2677bf6 | 2019-10-29 08:31:25 -0600 | [diff] [blame] | 34 | def _UprevResponse(_input_proto, output_proto, _config): |
| 35 | """Add fake paths to a successful uprev response.""" |
| 36 | output_proto.modified_ebuilds.add().path = '/fake/path1' |
| 37 | output_proto.modified_ebuilds.add().path = '/fake/path2' |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 38 | |
Michael Mortensen | 2677bf6 | 2019-10-29 08:31:25 -0600 | [diff] [blame] | 39 | @faux.success(_UprevResponse) |
| 40 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 41 | @validate.require('overlay_type') |
| 42 | @validate.is_in('overlay_type', _OVERLAY_TYPE_TO_NAME) |
| 43 | @validate.validation_complete |
| 44 | def Uprev(input_proto, output_proto, _config): |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 45 | """Uprev all cros workon ebuilds that have changes.""" |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 46 | build_targets = controller_util.ParseBuildTargets(input_proto.build_targets) |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 47 | overlay_type = _OVERLAY_TYPE_TO_NAME[input_proto.overlay_type] |
| 48 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 49 | output_dir = input_proto.output_dir or None |
| 50 | |
| 51 | try: |
Alex Klein | 5dd6b1e | 2019-07-31 15:45:24 -0600 | [diff] [blame] | 52 | uprevved = packages.uprev_build_targets(build_targets, overlay_type, chroot, |
| 53 | output_dir) |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 54 | except packages.Error as e: |
| 55 | # Handle module errors nicely, let everything else bubble up. |
Mike Frysinger | 6b5c3cd | 2019-08-27 16:51:00 -0400 | [diff] [blame] | 56 | cros_build_lib.Die(e) |
David Burger | 1e0fe23 | 2019-07-01 14:52:07 -0600 | [diff] [blame] | 57 | |
Alex Klein | 5dd6b1e | 2019-07-31 15:45:24 -0600 | [diff] [blame] | 58 | for path in uprevved: |
| 59 | output_proto.modified_ebuilds.add().path = path |
| 60 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 61 | |
Michael Mortensen | 2677bf6 | 2019-10-29 08:31:25 -0600 | [diff] [blame] | 62 | def _UprevVersionedPackageResponse(_input_proto, output_proto, _config): |
| 63 | """Add fake paths to a successful uprev versioned package response.""" |
| 64 | uprev_response = output_proto.responses.add() |
| 65 | uprev_response.modified_ebuilds.add().path = '/uprev/response/path' |
| 66 | |
| 67 | |
| 68 | @faux.success(_UprevVersionedPackageResponse) |
| 69 | @faux.empty_error |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 70 | @validate.require('versions') |
| 71 | @validate.require('package_info.package_name', 'package_info.category') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 72 | @validate.validation_complete |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 73 | def UprevVersionedPackage(input_proto, output_proto, _config): |
Evan Hernandez | 38555d4 | 2019-08-05 15:11:54 -0600 | [diff] [blame] | 74 | """Uprev a versioned package. |
| 75 | |
| 76 | See go/pupr-generator for details about this endpoint. |
| 77 | """ |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 78 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 79 | build_targets = controller_util.ParseBuildTargets(input_proto.build_targets) |
| 80 | package = controller_util.PackageInfoToCPV(input_proto.package_info) |
| 81 | refs = [] |
| 82 | for ref in input_proto.versions: |
| 83 | refs.append(GitRef(path=ref.repository, ref=ref.ref, revision=ref.revision)) |
| 84 | |
| 85 | try: |
Alex Klein | 34afcbc | 2019-08-22 16:14:31 -0600 | [diff] [blame] | 86 | result = packages.uprev_versioned_package(package, build_targets, refs, |
| 87 | chroot) |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 88 | except packages.Error as e: |
| 89 | # Handle module errors nicely, let everything else bubble up. |
Mike Frysinger | 6b5c3cd | 2019-08-27 16:51:00 -0400 | [diff] [blame] | 90 | cros_build_lib.Die(e) |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 91 | |
Alex Klein | 34afcbc | 2019-08-22 16:14:31 -0600 | [diff] [blame] | 92 | if not result.uprevved: |
| 93 | # No uprevs executed, skip the output population. |
| 94 | return |
| 95 | |
Yaakov Shaul | 730814a | 2019-09-10 13:58:25 -0600 | [diff] [blame] | 96 | for modified in result.modified: |
| 97 | uprev_response = output_proto.responses.add() |
| 98 | uprev_response.version = modified.new_version |
| 99 | for path in modified.files: |
| 100 | uprev_response.modified_ebuilds.add().path = path |
| 101 | |
Alex Klein | 8753118 | 2019-08-12 15:23:37 -0600 | [diff] [blame] | 102 | |
Michael Mortensen | 2677bf6 | 2019-10-29 08:31:25 -0600 | [diff] [blame] | 103 | def _GetBestVisibleResponse(_input_proto, output_proto, _config): |
| 104 | """Add fake paths to a successful GetBestVisible response.""" |
| 105 | package_info = common_pb2.PackageInfo( |
| 106 | category='category', |
| 107 | package_name='name', |
| 108 | version='1.01', |
| 109 | ) |
| 110 | output_proto.package_info.CopyFrom(package_info) |
| 111 | |
| 112 | |
| 113 | @faux.success(_GetBestVisibleResponse) |
| 114 | @faux.empty_error |
David Burger | 1e0fe23 | 2019-07-01 14:52:07 -0600 | [diff] [blame] | 115 | @validate.require('atom') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 116 | @validate.validation_complete |
| 117 | def GetBestVisible(input_proto, output_proto, _config): |
David Burger | 1e0fe23 | 2019-07-01 14:52:07 -0600 | [diff] [blame] | 118 | """Returns the best visible PackageInfo for the indicated atom.""" |
Alex Klein | bbef2b3 | 2019-08-27 10:38:50 -0600 | [diff] [blame] | 119 | build_target = None |
| 120 | if input_proto.build_target.name: |
| 121 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
| 122 | |
| 123 | cpv = packages.get_best_visible(input_proto.atom, build_target=build_target) |
David Burger | 1e0fe23 | 2019-07-01 14:52:07 -0600 | [diff] [blame] | 124 | package_info = common_pb2.PackageInfo() |
| 125 | controller_util.CPVToPackageInfo(cpv, package_info) |
| 126 | output_proto.package_info.CopyFrom(package_info) |
Alex Klein | 551e805 | 2019-08-29 11:23:48 -0600 | [diff] [blame] | 127 | |
| 128 | |
Michael Mortensen | 68abdb7 | 2019-10-28 09:43:52 -0600 | [diff] [blame] | 129 | def _ChromeVersionResponse(_input_proto, output_proto, _config): |
| 130 | """Add a fake chrome version to a successful response.""" |
| 131 | output_proto.version = '78.0.3900.0' |
| 132 | |
| 133 | |
| 134 | @faux.success(_ChromeVersionResponse) |
| 135 | @faux.empty_error |
Alex Klein | 551e805 | 2019-08-29 11:23:48 -0600 | [diff] [blame] | 136 | @validate.require('build_target.name') |
| 137 | @validate.validation_complete |
| 138 | def GetChromeVersion(input_proto, output_proto, _config): |
| 139 | """Returns the chrome version.""" |
| 140 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
Michael Mortensen | 9fe740c | 2019-10-29 14:42:48 -0600 | [diff] [blame] | 141 | chrome_version = packages.determine_chrome_version(build_target) |
| 142 | if chrome_version: |
| 143 | output_proto.version = chrome_version |
Alex Klein | da39c6d | 2019-09-16 14:36:36 -0600 | [diff] [blame] | 144 | |
| 145 | |
Michael Mortensen | 2677bf6 | 2019-10-29 08:31:25 -0600 | [diff] [blame] | 146 | def _GetTargetVersionsResponse(_input_proto, output_proto, _config): |
| 147 | """Add fake target version fields to a successful response.""" |
| 148 | output_proto.android_version = '5812377' |
| 149 | output_proto.android_branch_version = 'git_nyc-mr1-arc' |
| 150 | output_proto.android_target_version = 'cheets' |
| 151 | output_proto.chrome_version = '78.0.3900.0' |
| 152 | output_proto.platform_version = '12438.0.0' |
| 153 | output_proto.milestone_version = '78' |
| 154 | output_proto.full_version = 'R78-12438.0.0' |
| 155 | |
| 156 | |
| 157 | @faux.success(_GetTargetVersionsResponse) |
| 158 | @faux.empty_error |
Michael Mortensen | b70e8a8 | 2019-10-10 18:43:41 -0600 | [diff] [blame] | 159 | @validate.require('build_target.name') |
| 160 | @validate.validation_complete |
| 161 | def GetTargetVersions(input_proto, output_proto, _config): |
| 162 | """Returns the target versions.""" |
| 163 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
Alex Klein | 445ae3c | 2020-01-08 16:40:43 -0700 | [diff] [blame] | 164 | # Android version. |
Michael Mortensen | edf7653 | 2019-10-16 14:22:37 -0600 | [diff] [blame] | 165 | android_version = packages.determine_android_version([build_target]) |
Alex Klein | 445ae3c | 2020-01-08 16:40:43 -0700 | [diff] [blame] | 166 | logging.info('Found android version: %s', android_version) |
Michael Mortensen | edf7653 | 2019-10-16 14:22:37 -0600 | [diff] [blame] | 167 | if android_version: |
| 168 | output_proto.android_version = android_version |
Alex Klein | 445ae3c | 2020-01-08 16:40:43 -0700 | [diff] [blame] | 169 | # Android branch version. |
Michael Mortensen | edf7653 | 2019-10-16 14:22:37 -0600 | [diff] [blame] | 170 | android_branch_version = packages.determine_android_branch(build_target) |
Alex Klein | 445ae3c | 2020-01-08 16:40:43 -0700 | [diff] [blame] | 171 | logging.info('Found android branch version: %s', android_branch_version) |
Michael Mortensen | edf7653 | 2019-10-16 14:22:37 -0600 | [diff] [blame] | 172 | if android_branch_version: |
| 173 | output_proto.android_branch_version = android_branch_version |
Alex Klein | 445ae3c | 2020-01-08 16:40:43 -0700 | [diff] [blame] | 174 | # Android target version. |
Michael Mortensen | edf7653 | 2019-10-16 14:22:37 -0600 | [diff] [blame] | 175 | android_target_version = packages.determine_android_target(build_target) |
Alex Klein | 445ae3c | 2020-01-08 16:40:43 -0700 | [diff] [blame] | 176 | logging.info('Found android target version: %s', android_target_version) |
Michael Mortensen | edf7653 | 2019-10-16 14:22:37 -0600 | [diff] [blame] | 177 | if android_target_version: |
| 178 | output_proto.android_target_version = android_target_version |
Alex Klein | 445ae3c | 2020-01-08 16:40:43 -0700 | [diff] [blame] | 179 | |
Michael Mortensen | 9fe740c | 2019-10-29 14:42:48 -0600 | [diff] [blame] | 180 | # TODO(crbug/1019770): Investigate cases where builds_chrome is true but |
| 181 | # chrome_version is None. |
Michael Mortensen | 1d2ab0d | 2019-10-17 13:19:25 -0600 | [diff] [blame] | 182 | builds_chrome = packages.builds(constants.CHROME_CP, build_target) |
| 183 | if builds_chrome: |
Alex Klein | 445ae3c | 2020-01-08 16:40:43 -0700 | [diff] [blame] | 184 | # Chrome version fetch. |
Michael Mortensen | 9fe740c | 2019-10-29 14:42:48 -0600 | [diff] [blame] | 185 | chrome_version = packages.determine_chrome_version(build_target) |
Alex Klein | 445ae3c | 2020-01-08 16:40:43 -0700 | [diff] [blame] | 186 | logging.info('Found chrome version: %s', chrome_version) |
Michael Mortensen | 9fe740c | 2019-10-29 14:42:48 -0600 | [diff] [blame] | 187 | if chrome_version: |
| 188 | output_proto.chrome_version = chrome_version |
Alex Klein | 445ae3c | 2020-01-08 16:40:43 -0700 | [diff] [blame] | 189 | |
| 190 | # The ChromeOS version info. |
Michael Mortensen | 9fdb14b | 2019-10-17 11:17:30 -0600 | [diff] [blame] | 191 | output_proto.platform_version = packages.determine_platform_version() |
Michael Mortensen | 009cb66 | 2019-10-21 11:38:43 -0600 | [diff] [blame] | 192 | output_proto.milestone_version = packages.determine_milestone_version() |
| 193 | output_proto.full_version = packages.determine_full_version() |
Michael Mortensen | b70e8a8 | 2019-10-10 18:43:41 -0600 | [diff] [blame] | 194 | |
| 195 | |
Michael Mortensen | bfc5639 | 2020-04-30 15:23:47 -0600 | [diff] [blame] | 196 | def _GetBuilderMetadataResponse(input_proto, output_proto, _config): |
| 197 | """Add fake metadata fields to a successful response.""" |
| 198 | # Populate only a few fields to validate faux testing. |
| 199 | build_target_metadata = output_proto.build_target_metadata.add() |
| 200 | build_target_metadata.build_target = input_proto.build_target.name |
| 201 | build_target_metadata.android_container_branch = 'git_pi-arc' |
| 202 | model_metadata = output_proto.model_metadata.add() |
| 203 | model_metadata.model_name = 'astronaut' |
| 204 | model_metadata.ec_firmware_version = 'coral_v1.1.1234-56789f' |
| 205 | |
| 206 | |
| 207 | @faux.success(_GetBuilderMetadataResponse) |
| 208 | @faux.empty_error |
| 209 | @validate.require('build_target.name') |
| 210 | @validate.validation_complete |
| 211 | def GetBuilderMetadata(input_proto, output_proto, _config): |
| 212 | """Returns the target builder metadata.""" |
| 213 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
| 214 | build_target_metadata = output_proto.build_target_metadata.add() |
| 215 | build_target_metadata.build_target = build_target.name |
Michael Mortensen | bbce7e4 | 2020-05-01 15:03:49 -0600 | [diff] [blame^] | 216 | # Android version. |
| 217 | android_version = packages.determine_android_version([build_target]) |
| 218 | logging.info('Found android version: %s', android_version) |
| 219 | if android_version: |
| 220 | build_target_metadata.android_container_version = android_version |
| 221 | # Android branch version. |
| 222 | android_branch_version = packages.determine_android_branch(build_target) |
| 223 | logging.info('Found android branch version: %s', android_branch_version) |
| 224 | if android_branch_version: |
| 225 | build_target_metadata.android_container_branch = android_branch_version |
| 226 | # Android target version. |
| 227 | android_target_version = packages.determine_android_target(build_target) |
| 228 | logging.info('Found android target version: %s', android_target_version) |
| 229 | if android_target_version: |
| 230 | build_target_metadata.android_container_target = android_target_version |
| 231 | |
| 232 | build_target_metadata.arc_use_set = 'arc' in portage_util.GetBoardUseFlags( |
| 233 | build_target.name) |
| 234 | |
Michael Mortensen | bfc5639 | 2020-04-30 15:23:47 -0600 | [diff] [blame] | 235 | # TODO(crbug/1071620): Add service layer calls to fill out the rest of |
| 236 | # build_target_metadata and model_metadata. |
| 237 | |
Alex Klein | 7d66c09 | 2020-03-23 15:13:18 -0600 | [diff] [blame] | 238 | def _HasPrebuiltSuccess(_input_proto, output_proto, _config): |
Michael Mortensen | 68abdb7 | 2019-10-28 09:43:52 -0600 | [diff] [blame] | 239 | """The mock success case for HasChromePrebuilt.""" |
| 240 | output_proto.has_prebuilt = True |
| 241 | |
| 242 | |
Alex Klein | 7d66c09 | 2020-03-23 15:13:18 -0600 | [diff] [blame] | 243 | @faux.success(_HasPrebuiltSuccess) |
Alex Klein | da39c6d | 2019-09-16 14:36:36 -0600 | [diff] [blame] | 244 | @faux.empty_error |
| 245 | @validate.require('build_target.name') |
| 246 | @validate.validation_complete |
| 247 | def HasChromePrebuilt(input_proto, output_proto, _config): |
| 248 | """Checks if the most recent version of Chrome has a prebuilt.""" |
| 249 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
Alex Klein | 149fd3b | 2019-12-16 16:01:05 -0700 | [diff] [blame] | 250 | useflags = 'chrome_internal' if input_proto.chrome else None |
| 251 | exists = packages.has_prebuilt(constants.CHROME_CP, build_target=build_target, |
| 252 | useflags=useflags) |
Alex Klein | da39c6d | 2019-09-16 14:36:36 -0600 | [diff] [blame] | 253 | |
| 254 | output_proto.has_prebuilt = exists |
Alex Klein | 73fb657 | 2019-09-30 16:55:23 -0600 | [diff] [blame] | 255 | |
| 256 | |
Alex Klein | 7d66c09 | 2020-03-23 15:13:18 -0600 | [diff] [blame] | 257 | @faux.success(_HasPrebuiltSuccess) |
| 258 | @faux.empty_error |
George Engelbrecht | fce26aa | 2020-03-23 20:53:41 -0600 | [diff] [blame] | 259 | @validate.require('build_target.name', 'package_info.category', |
| 260 | 'package_info.package_name') |
Alex Klein | 7d66c09 | 2020-03-23 15:13:18 -0600 | [diff] [blame] | 261 | @validate.validation_complete |
| 262 | def HasPrebuilt(input_proto, output_proto, _config): |
| 263 | """Checks if the most recent version of Chrome has a prebuilt.""" |
| 264 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
| 265 | package = controller_util.PackageInfoToCPV(input_proto.package_info).cp |
| 266 | useflags = 'chrome_internal' if input_proto.chrome else None |
| 267 | exists = packages.has_prebuilt( |
| 268 | package, build_target=build_target, useflags=useflags) |
| 269 | |
| 270 | output_proto.has_prebuilt = exists |
| 271 | |
| 272 | |
Alex Klein | 73fb657 | 2019-09-30 16:55:23 -0600 | [diff] [blame] | 273 | def _BuildsChromeSuccess(_input_proto, output_proto, _config): |
| 274 | """Mock success case for BuildsChrome.""" |
| 275 | output_proto.builds_chrome = True |
| 276 | |
| 277 | |
| 278 | @faux.success(_BuildsChromeSuccess) |
| 279 | @faux.empty_error |
| 280 | @validate.require('build_target.name') |
| 281 | @validate.validation_complete |
| 282 | def BuildsChrome(input_proto, output_proto, _config): |
| 283 | """Check if the board builds chrome.""" |
Alex Klein | e65131f | 2019-10-03 10:34:01 -0600 | [diff] [blame] | 284 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
David Burger | 0f9dd4e | 2019-10-08 12:33:42 -0600 | [diff] [blame] | 285 | cpvs = [controller_util.PackageInfoToCPV(pi) for pi in input_proto.packages] |
| 286 | builds_chrome = packages.builds(constants.CHROME_CP, build_target, cpvs) |
Alex Klein | e65131f | 2019-10-03 10:34:01 -0600 | [diff] [blame] | 287 | output_proto.builds_chrome = builds_chrome |