Kuang-che Wu | 875c89a | 2020-01-08 14:30:55 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Kuang-che Wu | 6e4beca | 2018-06-27 17:45:02 +0800 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 3 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | """Android bisector to bisect local build commits. |
| 7 | |
| 8 | Example: |
Kuang-che Wu | 94f48e5 | 2018-07-25 15:28:31 +0800 | [diff] [blame] | 9 | $ ./bisect_android_repo.py init --old rev1 --new rev2 \\ |
| 10 | --android_root ~/android \\ |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 11 | --android_mirror $ANDROID_MIRROR |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 12 | $ ./bisect_android_repo.py config switch ./switch_arc_localbuild.py |
| 13 | $ ./bisect_android_repo.py config eval ./eval-manually.sh |
| 14 | $ ./bisect_android_repo.py run |
| 15 | |
| 16 | When running switcher and evaluator, following environment variables |
| 17 | will be set: |
| 18 | ANDROID_BRANCH (e.g. git_mnc-dr-arc-dev), |
| 19 | ANDROID_FLAVOR (e.g. cheets_x86-user), |
| 20 | ANDROID_ROOT, |
| 21 | DUT (e.g. samus-dut, if available). |
| 22 | """ |
| 23 | |
| 24 | from __future__ import print_function |
| 25 | import logging |
| 26 | |
| 27 | from bisect_kit import android_util |
| 28 | from bisect_kit import arc_util |
Kuang-che Wu | 2526a67 | 2019-09-10 16:23:59 +0800 | [diff] [blame] | 29 | from bisect_kit import bisector_cli |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 30 | from bisect_kit import cli |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 31 | from bisect_kit import codechange |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 32 | from bisect_kit import configure |
| 33 | from bisect_kit import core |
| 34 | from bisect_kit import cros_util |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 35 | from bisect_kit import errors |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 36 | from bisect_kit import repo_util |
| 37 | |
| 38 | logger = logging.getLogger(__name__) |
| 39 | |
| 40 | |
| 41 | def determine_android_build_id(opts, rev): |
| 42 | """Determine android build id. |
| 43 | |
| 44 | If `rev` is ChromeOS version, query its corresponding Android build id. |
| 45 | |
| 46 | Args: |
| 47 | opts: parse result of argparse |
| 48 | rev: Android build id or ChromeOS version |
| 49 | |
| 50 | Returns: |
| 51 | Android build id |
| 52 | """ |
| 53 | if cros_util.is_cros_version(rev): |
| 54 | assert opts.board, 'need to specify BOARD for cros version' |
| 55 | android_build_id = cros_util.query_android_build_id(opts.board, rev) |
| 56 | assert android_util.is_android_build_id(android_build_id) |
| 57 | logger.info('Converted given CrOS version %s to Android build id %s', rev, |
| 58 | android_build_id) |
| 59 | rev = android_build_id |
| 60 | return rev |
| 61 | |
| 62 | |
Kuang-che Wu | e80bb87 | 2018-11-15 19:45:25 +0800 | [diff] [blame] | 63 | def generate_action_link(action): |
| 64 | if action['action_type'] == 'commit': |
| 65 | repo_url = action['repo_url'] |
| 66 | action['link'] = repo_url + '/+/' + action['rev'] |
| 67 | |
| 68 | |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 69 | class AndroidRepoDomain(core.BisectDomain): |
| 70 | """BisectDomain for Android code changes.""" |
| 71 | # Accepts Android build id or ChromeOS version |
| 72 | revtype = staticmethod( |
| 73 | cli.argtype_multiplexer(cros_util.argtype_cros_version, |
| 74 | android_util.argtype_android_build_id)) |
Kuang-che Wu | 752228c | 2018-09-05 13:54:22 +0800 | [diff] [blame] | 75 | intra_revtype = staticmethod( |
| 76 | codechange.argtype_intra_rev(android_util.argtype_android_build_id)) |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 77 | help = globals()['__doc__'] |
| 78 | |
| 79 | @staticmethod |
| 80 | def add_init_arguments(parser): |
| 81 | parser.add_argument( |
| 82 | '--dut', |
| 83 | type=cli.argtype_notempty, |
| 84 | metavar='DUT', |
Kuang-che Wu | 13acc7b | 2020-06-15 10:45:35 +0800 | [diff] [blame] | 85 | default=configure.get('DUT'), |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 86 | help='DUT address') |
| 87 | parser.add_argument( |
| 88 | '--android_root', |
| 89 | metavar='ANDROID_ROOT', |
| 90 | type=cli.argtype_dir_path, |
| 91 | required=True, |
| 92 | default=configure.get('ANDROID_ROOT'), |
| 93 | help='Android tree root') |
| 94 | parser.add_argument( |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 95 | '--android_mirror', |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 96 | type=cli.argtype_dir_path, |
| 97 | required=True, |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 98 | default=configure.get('ANDROID_MIRROR'), |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 99 | help='Android repo mirror path') |
| 100 | parser.add_argument( |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 101 | '--branch', |
| 102 | metavar='ANDROID_BRANCH', |
| 103 | help='branch name like "git_mnc-dr-arc-dev"; ' |
| 104 | 'default is auto detect via DUT') |
| 105 | parser.add_argument( |
| 106 | '--flavor', |
| 107 | metavar='ANDROID_FLAVOR', |
| 108 | default=configure.get('ANDROID_FLAVOR'), |
| 109 | help='example: cheets_x86-user; default is auto detect via DUT') |
| 110 | parser.add_argument( |
| 111 | '--board', |
| 112 | metavar='BOARD', |
| 113 | default=configure.get('BOARD'), |
| 114 | help='ChromeOS board name, if ARC++') |
| 115 | |
| 116 | @staticmethod |
| 117 | def init(opts): |
| 118 | if opts.dut: |
| 119 | assert cros_util.is_dut(opts.dut) |
| 120 | |
| 121 | if not opts.flavor: |
Kuang-che Wu | 13acc7b | 2020-06-15 10:45:35 +0800 | [diff] [blame] | 122 | if not opts.dut: |
| 123 | raise errors.ArgumentError('--dut and --flavor', 'Neither is specified') |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 124 | opts.flavor = arc_util.query_flavor(opts.dut) |
| 125 | |
| 126 | if not opts.board: |
Kuang-che Wu | 13acc7b | 2020-06-15 10:45:35 +0800 | [diff] [blame] | 127 | if not opts.dut: |
| 128 | raise errors.ArgumentError('--dut and --board', 'Neither is specified') |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 129 | opts.board = cros_util.query_dut_board(opts.dut) |
| 130 | if not opts.branch: |
Kuang-che Wu | 13acc7b | 2020-06-15 10:45:35 +0800 | [diff] [blame] | 131 | if not opts.dut: |
| 132 | raise errors.ArgumentError('--dut and --branch', 'Neither is specified') |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 133 | version = cros_util.query_dut_short_version(opts.dut) |
Kuang-che Wu | dd7f6f0 | 2018-06-28 18:19:30 +0800 | [diff] [blame] | 134 | if not cros_util.is_cros_short_version(version): |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 135 | base = '.'.join(version.split('.')[:-1] + ['0']) |
Kuang-che Wu | 74768d3 | 2018-09-07 12:03:24 +0800 | [diff] [blame] | 136 | logger.info( |
| 137 | 'ChromeOS version of DUT (%s) is local build, ' |
| 138 | 'use its base version %s to determine Android branch', version, |
| 139 | base) |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 140 | version = base |
| 141 | opts.branch = cros_util.query_android_branch(opts.board, version) |
| 142 | logger.debug('branch=%s', opts.branch) |
| 143 | assert opts.branch |
| 144 | |
| 145 | old = determine_android_build_id(opts, opts.old) |
| 146 | new = determine_android_build_id(opts, opts.new) |
| 147 | |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 148 | if int(old) >= int(new): |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 149 | raise errors.ArgumentError('--old and --new', |
| 150 | 'bad bisect range (%s, %s)' % (old, new)) |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 151 | |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 152 | config = dict( |
| 153 | dut=opts.dut, |
| 154 | android_root=opts.android_root, |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 155 | android_mirror=opts.android_mirror, |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 156 | branch=opts.branch, |
| 157 | flavor=opts.flavor, |
| 158 | old=old, |
| 159 | new=new) |
| 160 | |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 161 | spec_manager = android_util.AndroidSpecManager(config) |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 162 | cache = repo_util.RepoMirror(opts.android_mirror) |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 163 | code_manager = codechange.CodeManager(opts.android_root, spec_manager, |
| 164 | cache) |
Kuang-che Wu | 13acc7b | 2020-06-15 10:45:35 +0800 | [diff] [blame] | 165 | revlist, details = code_manager.build_revlist(old, new) |
| 166 | for detail in details.values(): |
| 167 | for action in detail.get('actions', []): |
| 168 | generate_action_link(action) |
| 169 | |
| 170 | return config, {'revlist': revlist, 'details': details} |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 171 | |
| 172 | def __init__(self, config): |
| 173 | self.config = config |
| 174 | |
| 175 | def setenv(self, env, rev=None): |
| 176 | env['DUT'] = self.config['dut'] |
| 177 | env['ANDROID_ROOT'] = self.config['android_root'] |
| 178 | env['ANDROID_FLAVOR'] = self.config['flavor'] |
| 179 | env['ANDROID_BRANCH'] = self.config['branch'] |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 180 | env['ANDROID_MIRROR'] = self.config['android_mirror'] |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 181 | env['INTRA_REV'] = rev |
| 182 | |
Kuang-che Wu | 13acc7b | 2020-06-15 10:45:35 +0800 | [diff] [blame] | 183 | def fill_candidate_summary(self, summary): |
Kuang-che Wu | 948a79c | 2019-06-19 19:13:56 +0800 | [diff] [blame] | 184 | if 'current_range' in summary: |
| 185 | old, new = summary['current_range'] |
| 186 | old_base, _, _ = codechange.parse_intra_rev(old) |
| 187 | _, new_next, _ = codechange.parse_intra_rev(new) |
| 188 | url_template = ('https://android-build.googleplex.com/' |
| 189 | 'builds/{new}/branches/%s/targets/%s/cls?end={old}') % ( |
| 190 | self.config['branch'], self.config['flavor']) |
Kuang-che Wu | accf920 | 2019-01-04 15:40:42 +0800 | [diff] [blame] | 191 | |
Kuang-che Wu | 948a79c | 2019-06-19 19:13:56 +0800 | [diff] [blame] | 192 | summary['links'] = [ |
| 193 | { |
| 194 | 'name': 'change_list', |
| 195 | 'url': url_template.format(old=old_base, new=new_next), |
Kuang-che Wu | 948a79c | 2019-06-19 19:13:56 +0800 | [diff] [blame] | 196 | }, |
| 197 | ] |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 198 | |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 199 | |
| 200 | if __name__ == '__main__': |
Kuang-che Wu | 2526a67 | 2019-09-10 16:23:59 +0800 | [diff] [blame] | 201 | bisector_cli.BisectorCommandLine(AndroidRepoDomain).main() |