blob: b131f8e9da645c7b1353543d5c7ad3cd25c035c8 [file] [log] [blame]
Kuang-che Wuacb6efd2018-04-25 18:52:58 +08001#!/usr/bin/env python2
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
29from bisect_kit import cli
Kuang-che Wud1d45b42018-07-05 00:46:45 +080030from bisect_kit import codechange
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080031from bisect_kit import configure
32from bisect_kit import core
33from bisect_kit import cros_util
Kuang-che Wue121fae2018-11-09 16:18:39 +080034from bisect_kit import errors
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080035from bisect_kit import repo_util
36
37logger = logging.getLogger(__name__)
38
39
40def 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
Kuang-che Wue80bb872018-11-15 19:45:25 +080062def generate_action_link(action):
63 if action['action_type'] == 'commit':
64 repo_url = action['repo_url']
65 action['link'] = repo_url + '/+/' + action['rev']
66
67
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080068class AndroidRepoDomain(core.BisectDomain):
69 """BisectDomain for Android code changes."""
70 # Accepts Android build id or ChromeOS version
71 revtype = staticmethod(
72 cli.argtype_multiplexer(cros_util.argtype_cros_version,
73 android_util.argtype_android_build_id))
Kuang-che Wu752228c2018-09-05 13:54:22 +080074 intra_revtype = staticmethod(
75 codechange.argtype_intra_rev(android_util.argtype_android_build_id))
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080076 help = globals()['__doc__']
77
78 @staticmethod
79 def add_init_arguments(parser):
80 parser.add_argument(
81 '--dut',
82 type=cli.argtype_notempty,
83 metavar='DUT',
84 default=configure.get('DUT', ''),
85 help='DUT address')
86 parser.add_argument(
87 '--android_root',
88 metavar='ANDROID_ROOT',
89 type=cli.argtype_dir_path,
90 required=True,
91 default=configure.get('ANDROID_ROOT'),
92 help='Android tree root')
93 parser.add_argument(
Kuang-che Wud8fc9572018-10-03 21:00:41 +080094 '--android_mirror',
Kuang-che Wud1d45b42018-07-05 00:46:45 +080095 type=cli.argtype_dir_path,
96 required=True,
Kuang-che Wud8fc9572018-10-03 21:00:41 +080097 default=configure.get('ANDROID_MIRROR'),
Kuang-che Wud1d45b42018-07-05 00:46:45 +080098 help='Android repo mirror path')
99 parser.add_argument(
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800100 '--branch',
101 metavar='ANDROID_BRANCH',
102 help='branch name like "git_mnc-dr-arc-dev"; '
103 'default is auto detect via DUT')
104 parser.add_argument(
105 '--flavor',
106 metavar='ANDROID_FLAVOR',
107 default=configure.get('ANDROID_FLAVOR'),
108 help='example: cheets_x86-user; default is auto detect via DUT')
109 parser.add_argument(
110 '--board',
111 metavar='BOARD',
112 default=configure.get('BOARD'),
113 help='ChromeOS board name, if ARC++')
114
115 @staticmethod
116 def init(opts):
117 if opts.dut:
118 assert cros_util.is_dut(opts.dut)
119
120 if not opts.flavor:
121 assert opts.dut
122 opts.flavor = arc_util.query_flavor(opts.dut)
123
124 if not opts.board:
125 assert opts.dut
126 opts.board = cros_util.query_dut_board(opts.dut)
127 if not opts.branch:
128 version = cros_util.query_dut_short_version(opts.dut)
Kuang-che Wudd7f6f02018-06-28 18:19:30 +0800129 if not cros_util.is_cros_short_version(version):
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800130 base = '.'.join(version.split('.')[:-1] + ['0'])
Kuang-che Wu74768d32018-09-07 12:03:24 +0800131 logger.info(
132 'ChromeOS version of DUT (%s) is local build, '
133 'use its base version %s to determine Android branch', version,
134 base)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800135 version = base
136 opts.branch = cros_util.query_android_branch(opts.board, version)
137 logger.debug('branch=%s', opts.branch)
138 assert opts.branch
139
140 old = determine_android_build_id(opts, opts.old)
141 new = determine_android_build_id(opts, opts.new)
142
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800143 if int(old) >= int(new):
Kuang-che Wue121fae2018-11-09 16:18:39 +0800144 raise errors.ArgumentError('--old and --new',
145 'bad bisect range (%s, %s)' % (old, new))
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800146
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800147 config = dict(
148 dut=opts.dut,
149 android_root=opts.android_root,
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800150 android_mirror=opts.android_mirror,
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800151 branch=opts.branch,
152 flavor=opts.flavor,
153 old=old,
154 new=new)
155
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800156 spec_manager = android_util.AndroidSpecManager(config)
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800157 cache = repo_util.RepoMirror(opts.android_mirror)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800158
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800159 # Make sure all repos in between are cached
160 float_specs = spec_manager.collect_float_spec(old, new)
161 for spec in reversed(float_specs):
162 spec_manager.parse_spec(spec)
163 if cache.are_spec_commits_available(spec):
164 continue
165 spec_manager.sync_disk_state(spec.name)
166
167 code_manager = codechange.CodeManager(opts.android_root, spec_manager,
168 cache)
169 revlist = code_manager.build_revlist(old, new)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800170 return config, revlist
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 Wud8fc9572018-10-03 21:00:41 +0800180 env['ANDROID_MIRROR'] = self.config['android_mirror']
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800181 env['INTRA_REV'] = rev
182
Kuang-che Wue80bb872018-11-15 19:45:25 +0800183 def fill_candidate_summary(self, summary, interesting_indexes):
Kuang-che Wu948a79c2019-06-19 19:13:56 +0800184 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 Wuaccf9202019-01-04 15:40:42 +0800191
Kuang-che Wu948a79c2019-06-19 19:13:56 +0800192 summary['links'] = [
193 {
194 'name': 'change_list',
195 'url': url_template.format(old=old_base, new=new_next),
Kuang-che Wu948a79c2019-06-19 19:13:56 +0800196 },
197 ]
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800198
199 spec_manager = android_util.AndroidSpecManager(self.config)
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800200 cache = repo_util.RepoMirror(self.config['android_mirror'])
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800201 code_manager = codechange.CodeManager(self.config['android_root'],
202 spec_manager, cache)
Kuang-che Wue80bb872018-11-15 19:45:25 +0800203 for i in interesting_indexes:
204 if i == 0:
205 continue
206 rev_info = summary['rev_info'][i]
207 rev_info.update(code_manager.get_rev_detail(rev_info.rev))
208 for action in rev_info.get('actions', []):
209 generate_action_link(action)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800210
211
212if __name__ == '__main__':
213 cli.BisectorCommandLine(AndroidRepoDomain).main()