blob: 263991506d50ddff5d0c81cbe44ce6a222832f9e [file] [log] [blame]
Kuang-che Wu875c89a2020-01-08 14:30:55 +08001#!/usr/bin/env python3
Kuang-che Wu6e4beca2018-06-27 17:45:02 +08002# -*- coding: utf-8 -*-
Kuang-che Wuacb6efd2018-04-25 18:52:58 +08003# 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
8Example:
Kuang-che Wu94f48e52018-07-25 15:28:31 +08009 $ ./bisect_android_repo.py init --old rev1 --new rev2 \\
10 --android_root ~/android \\
Kuang-che Wud8fc9572018-10-03 21:00:41 +080011 --android_mirror $ANDROID_MIRROR
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080012 $ ./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
16When running switcher and evaluator, following environment variables
17will 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
24from __future__ import print_function
25import logging
26
27from bisect_kit import android_util
28from bisect_kit import arc_util
Kuang-che Wu2526a672019-09-10 16:23:59 +080029from bisect_kit import bisector_cli
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080030from bisect_kit import cli
Kuang-che Wud1d45b42018-07-05 00:46:45 +080031from bisect_kit import codechange
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080032from bisect_kit import configure
33from bisect_kit import core
34from bisect_kit import cros_util
Kuang-che Wue121fae2018-11-09 16:18:39 +080035from bisect_kit import errors
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080036from bisect_kit import repo_util
37
38logger = logging.getLogger(__name__)
39
40
41def 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 Wue80bb872018-11-15 19:45:25 +080063def 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 Wuacb6efd2018-04-25 18:52:58 +080069class 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 Wu752228c2018-09-05 13:54:22 +080075 intra_revtype = staticmethod(
76 codechange.argtype_intra_rev(android_util.argtype_android_build_id))
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080077 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',
85 default=configure.get('DUT', ''),
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 Wud8fc9572018-10-03 21:00:41 +080095 '--android_mirror',
Kuang-che Wud1d45b42018-07-05 00:46:45 +080096 type=cli.argtype_dir_path,
97 required=True,
Kuang-che Wud8fc9572018-10-03 21:00:41 +080098 default=configure.get('ANDROID_MIRROR'),
Kuang-che Wud1d45b42018-07-05 00:46:45 +080099 help='Android repo mirror path')
100 parser.add_argument(
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800101 '--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:
122 assert opts.dut
123 opts.flavor = arc_util.query_flavor(opts.dut)
124
125 if not opts.board:
126 assert opts.dut
127 opts.board = cros_util.query_dut_board(opts.dut)
128 if not opts.branch:
129 version = cros_util.query_dut_short_version(opts.dut)
Kuang-che Wudd7f6f02018-06-28 18:19:30 +0800130 if not cros_util.is_cros_short_version(version):
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800131 base = '.'.join(version.split('.')[:-1] + ['0'])
Kuang-che Wu74768d32018-09-07 12:03:24 +0800132 logger.info(
133 'ChromeOS version of DUT (%s) is local build, '
134 'use its base version %s to determine Android branch', version,
135 base)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800136 version = base
137 opts.branch = cros_util.query_android_branch(opts.board, version)
138 logger.debug('branch=%s', opts.branch)
139 assert opts.branch
140
141 old = determine_android_build_id(opts, opts.old)
142 new = determine_android_build_id(opts, opts.new)
143
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800144 if int(old) >= int(new):
Kuang-che Wue121fae2018-11-09 16:18:39 +0800145 raise errors.ArgumentError('--old and --new',
146 'bad bisect range (%s, %s)' % (old, new))
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800147
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800148 config = dict(
149 dut=opts.dut,
150 android_root=opts.android_root,
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800151 android_mirror=opts.android_mirror,
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800152 branch=opts.branch,
153 flavor=opts.flavor,
154 old=old,
155 new=new)
156
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800157 spec_manager = android_util.AndroidSpecManager(config)
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800158 cache = repo_util.RepoMirror(opts.android_mirror)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800159
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800160 # Make sure all repos in between are cached
161 float_specs = spec_manager.collect_float_spec(old, new)
162 for spec in reversed(float_specs):
163 spec_manager.parse_spec(spec)
164 if cache.are_spec_commits_available(spec):
165 continue
166 spec_manager.sync_disk_state(spec.name)
167
168 code_manager = codechange.CodeManager(opts.android_root, spec_manager,
169 cache)
170 revlist = code_manager.build_revlist(old, new)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800171 return config, revlist
172
173 def __init__(self, config):
174 self.config = config
175
176 def setenv(self, env, rev=None):
177 env['DUT'] = self.config['dut']
178 env['ANDROID_ROOT'] = self.config['android_root']
179 env['ANDROID_FLAVOR'] = self.config['flavor']
180 env['ANDROID_BRANCH'] = self.config['branch']
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800181 env['ANDROID_MIRROR'] = self.config['android_mirror']
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800182 env['INTRA_REV'] = rev
183
Kuang-che Wue80bb872018-11-15 19:45:25 +0800184 def fill_candidate_summary(self, summary, interesting_indexes):
Kuang-che Wu948a79c2019-06-19 19:13:56 +0800185 if 'current_range' in summary:
186 old, new = summary['current_range']
187 old_base, _, _ = codechange.parse_intra_rev(old)
188 _, new_next, _ = codechange.parse_intra_rev(new)
189 url_template = ('https://android-build.googleplex.com/'
190 'builds/{new}/branches/%s/targets/%s/cls?end={old}') % (
191 self.config['branch'], self.config['flavor'])
Kuang-che Wuaccf9202019-01-04 15:40:42 +0800192
Kuang-che Wu948a79c2019-06-19 19:13:56 +0800193 summary['links'] = [
194 {
195 'name': 'change_list',
196 'url': url_template.format(old=old_base, new=new_next),
Kuang-che Wu948a79c2019-06-19 19:13:56 +0800197 },
198 ]
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800199
200 spec_manager = android_util.AndroidSpecManager(self.config)
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800201 cache = repo_util.RepoMirror(self.config['android_mirror'])
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800202 code_manager = codechange.CodeManager(self.config['android_root'],
203 spec_manager, cache)
Kuang-che Wue80bb872018-11-15 19:45:25 +0800204 for i in interesting_indexes:
205 if i == 0:
206 continue
207 rev_info = summary['rev_info'][i]
208 rev_info.update(code_manager.get_rev_detail(rev_info.rev))
209 for action in rev_info.get('actions', []):
210 generate_action_link(action)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800211
212
213if __name__ == '__main__':
Kuang-che Wu2526a672019-09-10 16:23:59 +0800214 bisector_cli.BisectorCommandLine(AndroidRepoDomain).main()