blob: f00342045b46a7b4cb87888b1c7b8ed7f18120fd [file] [log] [blame]
LaMont Jones8a1644f2019-04-16 14:30:17 -06001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Android operations."""
6
LaMont Jones8a1644f2019-04-16 14:30:17 -06007import os
Eric Lin63d39712021-08-19 10:11:27 +00008from typing import TYPE_CHECKING
LaMont Jones8a1644f2019-04-16 14:30:17 -06009
Alex Klein076841b2019-08-29 15:19:39 -060010from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060011from chromite.api import validate
Alex Klein4de25e82019-08-05 15:58:39 -060012from chromite.api.controller import controller_util
LaMont Jones8a1644f2019-04-16 14:30:17 -060013from chromite.api.gen.chromite.api import android_pb2
LaMont Jones8a1644f2019-04-16 14:30:17 -060014from chromite.lib import constants
LaMont Jones8a1644f2019-04-16 14:30:17 -060015from chromite.lib import osutils
Alex Klein18a60af2020-06-11 12:08:47 -060016from chromite.lib.parser import package_info
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090017from chromite.service import android
Alex Klein4de25e82019-08-05 15:58:39 -060018from chromite.service import packages
LaMont Jones8a1644f2019-04-16 14:30:17 -060019
Eric Lin63d39712021-08-19 10:11:27 +000020if TYPE_CHECKING:
21 from chromite.api import api_config
22
LaMont Jones8a1644f2019-04-16 14:30:17 -060023
24ANDROIDPIN_MASK_PATH = os.path.join(constants.SOURCE_ROOT,
25 constants.CHROMIUMOS_OVERLAY_DIR,
26 'profiles', 'default', 'linux',
27 'package.mask', 'androidpin')
28
29
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090030def _GetLatestBuildResponse(_input_proto, output_proto, _config):
31 """Fake GetLatestBuild response."""
32 output_proto.android_version = '7123456'
33
34
35@faux.success(_GetLatestBuildResponse)
36@faux.empty_error
Shao-Chuan Lee6e8784a2021-05-13 09:34:46 +090037@validate.require_any('android_build_branch', 'android_package')
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090038@validate.validation_complete
39def GetLatestBuild(input_proto, output_proto, _config):
Shao-Chuan Lee6e8784a2021-05-13 09:34:46 +090040 branch = (input_proto.android_build_branch or
41 android.GetAndroidBranchForPackage(input_proto.android_package))
42 build_id, _ = android.GetLatestBuild(branch)
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090043 output_proto.android_version = build_id
44
45
Michael Mortensen25626442019-11-22 10:06:59 -070046def _MarkStableResponse(_input_proto, output_proto, _config):
47 """Add fake status to a successful response."""
48 output_proto.android_atom.category = 'category'
49 output_proto.android_atom.package_name = 'android-package-name'
50 output_proto.android_atom.version = '1.2'
51 output_proto.status = android_pb2.MARK_STABLE_STATUS_SUCCESS
52
53
54@faux.success(_MarkStableResponse)
55@faux.empty_error
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +090056@validate.require('package_name')
Alex Klein231d2da2019-07-22 16:44:45 -060057@validate.validation_complete
Eric Lin63d39712021-08-19 10:11:27 +000058def MarkStable(input_proto: android_pb2.MarkStableRequest,
59 output_proto: android_pb2.MarkStableResponse,
60 _config: 'api_config.ApiConfig') -> None:
LaMont Jones8a1644f2019-04-16 14:30:17 -060061 """Uprev Android, if able.
62
63 Uprev Android, verify that the newly uprevved package can be emerged, and
64 return the new package info.
65
66 See AndroidService documentation in api/proto/android.proto.
67
68 Args:
Eric Lin63d39712021-08-19 10:11:27 +000069 input_proto: The input proto.
70 output_proto: The output proto.
71 _config: The call config.
LaMont Jones8a1644f2019-04-16 14:30:17 -060072 """
Alex Klein4de25e82019-08-05 15:58:39 -060073 chroot = controller_util.ParseChroot(input_proto.chroot)
74 build_targets = controller_util.ParseBuildTargets(input_proto.build_targets)
LaMont Jones8a1644f2019-04-16 14:30:17 -060075 package_name = input_proto.package_name
76 android_build_branch = input_proto.android_build_branch
77 android_version = input_proto.android_version
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +090078 skip_commit = input_proto.skip_commit
LaMont Jones8a1644f2019-04-16 14:30:17 -060079
LaMont Jones8a1644f2019-04-16 14:30:17 -060080 # Assume success.
81 output_proto.status = android_pb2.MARK_STABLE_STATUS_SUCCESS
David Burgerff1a92c2019-07-09 00:27:54 +000082 # TODO(crbug/904939): This should move to service/android.py and the port
83 # should be finished.
LaMont Jones8a1644f2019-04-16 14:30:17 -060084 try:
Alex Klein4de25e82019-08-05 15:58:39 -060085 android_atom_to_build = packages.uprev_android(
LaMont Jones8a1644f2019-04-16 14:30:17 -060086 android_package=package_name,
Alex Klein4de25e82019-08-05 15:58:39 -060087 chroot=chroot,
88 build_targets=build_targets,
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +090089 android_build_branch=android_build_branch,
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +090090 android_version=android_version,
91 skip_commit=skip_commit,
92 )
Alex Klein4de25e82019-08-05 15:58:39 -060093 except packages.AndroidIsPinnedUprevError as e:
LaMont Jones8a1644f2019-04-16 14:30:17 -060094 # If the uprev failed due to a pin, CI needs to unpin and retry.
95 android_atom_to_build = e.new_android_atom
96 output_proto.status = android_pb2.MARK_STABLE_STATUS_PINNED
97
98 if android_atom_to_build:
Alex Klein16c0f302020-10-06 17:28:14 -060099 pkg = package_info.parse(android_atom_to_build)
100 controller_util.serialize_package_info(pkg, output_proto.android_atom)
LaMont Jones8a1644f2019-04-16 14:30:17 -0600101 else:
102 output_proto.status = android_pb2.MARK_STABLE_STATUS_EARLY_EXIT
103
Alex Klein2b236722019-06-19 15:44:26 -0600104
Michael Mortensen25626442019-11-22 10:06:59 -0700105# We don't use @faux.success for UnpinVersion because output_proto is unused.
Alex Klein076841b2019-08-29 15:19:39 -0600106@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600107@validate.validation_complete
Eric Lin63d39712021-08-19 10:11:27 +0000108def UnpinVersion(_input_proto: android_pb2.UnpinVersionRequest,
109 _output_proto: android_pb2.UnpinVersionResponse,
110 _config: 'api_config.ApiConfig') -> None:
LaMont Jones8a1644f2019-04-16 14:30:17 -0600111 """Unpin the Android version.
112
113 See AndroidService documentation in api/proto/android.proto.
114
115 Args:
Eric Lin63d39712021-08-19 10:11:27 +0000116 _input_proto: The input proto. (not used.)
117 _output_proto: The output proto. (not used.)
118 _config: The call config.
LaMont Jones8a1644f2019-04-16 14:30:17 -0600119 """
LaMont Jones8a1644f2019-04-16 14:30:17 -0600120 osutils.SafeUnlink(ANDROIDPIN_MASK_PATH)