Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
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 |
| 29 | from bisect_kit import cli |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 30 | from bisect_kit import codechange |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 31 | from bisect_kit import configure |
| 32 | from bisect_kit import core |
| 33 | from bisect_kit import cros_util |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 34 | from bisect_kit import errors |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 35 | from bisect_kit import repo_util |
| 36 | |
| 37 | logger = logging.getLogger(__name__) |
| 38 | |
| 39 | |
| 40 | def determine_android_build_id(opts, rev): |
| 41 | """Determine android build id. |
| 42 | |
| 43 | If `rev` is ChromeOS version, query its corresponding Android build id. |
| 44 | |
| 45 | Args: |
| 46 | opts: parse result of argparse |
| 47 | rev: Android build id or ChromeOS version |
| 48 | |
| 49 | Returns: |
| 50 | Android build id |
| 51 | """ |
| 52 | if cros_util.is_cros_version(rev): |
| 53 | assert opts.board, 'need to specify BOARD for cros version' |
| 54 | android_build_id = cros_util.query_android_build_id(opts.board, rev) |
| 55 | assert android_util.is_android_build_id(android_build_id) |
| 56 | logger.info('Converted given CrOS version %s to Android build id %s', rev, |
| 57 | android_build_id) |
| 58 | rev = android_build_id |
| 59 | return rev |
| 60 | |
| 61 | |
| 62 | class AndroidRepoDomain(core.BisectDomain): |
| 63 | """BisectDomain for Android code changes.""" |
| 64 | # Accepts Android build id or ChromeOS version |
| 65 | revtype = staticmethod( |
| 66 | cli.argtype_multiplexer(cros_util.argtype_cros_version, |
| 67 | android_util.argtype_android_build_id)) |
Kuang-che Wu | 752228c | 2018-09-05 13:54:22 +0800 | [diff] [blame] | 68 | intra_revtype = staticmethod( |
| 69 | codechange.argtype_intra_rev(android_util.argtype_android_build_id)) |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 70 | help = globals()['__doc__'] |
| 71 | |
| 72 | @staticmethod |
| 73 | def add_init_arguments(parser): |
| 74 | parser.add_argument( |
| 75 | '--dut', |
| 76 | type=cli.argtype_notempty, |
| 77 | metavar='DUT', |
| 78 | default=configure.get('DUT', ''), |
| 79 | help='DUT address') |
| 80 | parser.add_argument( |
| 81 | '--android_root', |
| 82 | metavar='ANDROID_ROOT', |
| 83 | type=cli.argtype_dir_path, |
| 84 | required=True, |
| 85 | default=configure.get('ANDROID_ROOT'), |
| 86 | help='Android tree root') |
| 87 | parser.add_argument( |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 88 | '--android_mirror', |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 89 | type=cli.argtype_dir_path, |
| 90 | required=True, |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 91 | default=configure.get('ANDROID_MIRROR'), |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 92 | help='Android repo mirror path') |
| 93 | parser.add_argument( |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 94 | '--branch', |
| 95 | metavar='ANDROID_BRANCH', |
| 96 | help='branch name like "git_mnc-dr-arc-dev"; ' |
| 97 | 'default is auto detect via DUT') |
| 98 | parser.add_argument( |
| 99 | '--flavor', |
| 100 | metavar='ANDROID_FLAVOR', |
| 101 | default=configure.get('ANDROID_FLAVOR'), |
| 102 | help='example: cheets_x86-user; default is auto detect via DUT') |
| 103 | parser.add_argument( |
| 104 | '--board', |
| 105 | metavar='BOARD', |
| 106 | default=configure.get('BOARD'), |
| 107 | help='ChromeOS board name, if ARC++') |
| 108 | |
| 109 | @staticmethod |
| 110 | def init(opts): |
| 111 | if opts.dut: |
| 112 | assert cros_util.is_dut(opts.dut) |
| 113 | |
| 114 | if not opts.flavor: |
| 115 | assert opts.dut |
| 116 | opts.flavor = arc_util.query_flavor(opts.dut) |
| 117 | |
| 118 | if not opts.board: |
| 119 | assert opts.dut |
| 120 | opts.board = cros_util.query_dut_board(opts.dut) |
| 121 | if not opts.branch: |
| 122 | version = cros_util.query_dut_short_version(opts.dut) |
Kuang-che Wu | dd7f6f0 | 2018-06-28 18:19:30 +0800 | [diff] [blame] | 123 | if not cros_util.is_cros_short_version(version): |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 124 | base = '.'.join(version.split('.')[:-1] + ['0']) |
Kuang-che Wu | 74768d3 | 2018-09-07 12:03:24 +0800 | [diff] [blame] | 125 | logger.info( |
| 126 | 'ChromeOS version of DUT (%s) is local build, ' |
| 127 | 'use its base version %s to determine Android branch', version, |
| 128 | base) |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 129 | version = base |
| 130 | opts.branch = cros_util.query_android_branch(opts.board, version) |
| 131 | logger.debug('branch=%s', opts.branch) |
| 132 | assert opts.branch |
| 133 | |
| 134 | old = determine_android_build_id(opts, opts.old) |
| 135 | new = determine_android_build_id(opts, opts.new) |
| 136 | |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 137 | if int(old) >= int(new): |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 138 | raise errors.ArgumentError('--old and --new', |
| 139 | 'bad bisect range (%s, %s)' % (old, new)) |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 140 | |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 141 | config = dict( |
| 142 | dut=opts.dut, |
| 143 | android_root=opts.android_root, |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 144 | android_mirror=opts.android_mirror, |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 145 | branch=opts.branch, |
| 146 | flavor=opts.flavor, |
| 147 | old=old, |
| 148 | new=new) |
| 149 | |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 150 | spec_manager = android_util.AndroidSpecManager(config) |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 151 | cache = repo_util.RepoMirror(opts.android_mirror) |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 152 | |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 153 | # Make sure all repos in between are cached |
| 154 | float_specs = spec_manager.collect_float_spec(old, new) |
| 155 | for spec in reversed(float_specs): |
| 156 | spec_manager.parse_spec(spec) |
| 157 | if cache.are_spec_commits_available(spec): |
| 158 | continue |
| 159 | spec_manager.sync_disk_state(spec.name) |
| 160 | |
| 161 | code_manager = codechange.CodeManager(opts.android_root, spec_manager, |
| 162 | cache) |
| 163 | revlist = code_manager.build_revlist(old, new) |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 164 | return config, revlist |
| 165 | |
| 166 | def __init__(self, config): |
| 167 | self.config = config |
| 168 | |
| 169 | def setenv(self, env, rev=None): |
| 170 | env['DUT'] = self.config['dut'] |
| 171 | env['ANDROID_ROOT'] = self.config['android_root'] |
| 172 | env['ANDROID_FLAVOR'] = self.config['flavor'] |
| 173 | env['ANDROID_BRANCH'] = self.config['branch'] |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 174 | env['ANDROID_MIRROR'] = self.config['android_mirror'] |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 175 | env['INTRA_REV'] = rev |
| 176 | |
Kuang-che Wu | 81aecc0 | 2018-10-31 19:37:32 +0800 | [diff] [blame] | 177 | def view(self, revlist, old, new): |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 178 | print('old', old) |
| 179 | print('new', new) |
| 180 | |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 181 | old_base, old_next, _ = codechange.parse_intra_rev(old) |
| 182 | new_base, new_next, _ = codechange.parse_intra_rev(new) |
| 183 | # Only print log url if the range is within two releases. |
| 184 | if old_next in (new_base, new_next): |
| 185 | url_template = ( |
| 186 | 'https://android-build.googleplex.com/' |
| 187 | 'builds/{new}/branches/{branch}/targets/{flavor}/cls?end={old}') |
| 188 | print(url_template.format( |
| 189 | old=old_base, |
| 190 | new=new_next, |
| 191 | branch=self.config['branch'], |
| 192 | flavor=self.config['flavor'])) |
| 193 | |
| 194 | spec_manager = android_util.AndroidSpecManager(self.config) |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 195 | cache = repo_util.RepoMirror(self.config['android_mirror']) |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 196 | code_manager = codechange.CodeManager(self.config['android_root'], |
| 197 | spec_manager, cache) |
Kuang-che Wu | 81aecc0 | 2018-10-31 19:37:32 +0800 | [diff] [blame] | 198 | code_manager.view_rev_diff(revlist, old, new) |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 199 | |
| 200 | |
| 201 | if __name__ == '__main__': |
| 202 | cli.BisectorCommandLine(AndroidRepoDomain).main() |