blob: e7088c6a2806b4a5c2739ab8d9c0a521d385f9d8 [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',
Kuang-che Wu13acc7b2020-06-15 10:45:35 +080085 default=configure.get('DUT'),
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080086 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:
Kuang-che Wu13acc7b2020-06-15 10:45:35 +0800122 if not opts.dut:
123 raise errors.ArgumentError('--dut and --flavor', 'Neither is specified')
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800124 opts.flavor = arc_util.query_flavor(opts.dut)
125
126 if not opts.board:
Kuang-che Wu13acc7b2020-06-15 10:45:35 +0800127 if not opts.dut:
128 raise errors.ArgumentError('--dut and --board', 'Neither is specified')
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800129 opts.board = cros_util.query_dut_board(opts.dut)
130 if not opts.branch:
Kuang-che Wu13acc7b2020-06-15 10:45:35 +0800131 if not opts.dut:
132 raise errors.ArgumentError('--dut and --branch', 'Neither is specified')
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800133 version = cros_util.query_dut_short_version(opts.dut)
Kuang-che Wudd7f6f02018-06-28 18:19:30 +0800134 if not cros_util.is_cros_short_version(version):
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800135 base = '.'.join(version.split('.')[:-1] + ['0'])
Kuang-che Wu74768d32018-09-07 12:03:24 +0800136 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 Wuacb6efd2018-04-25 18:52:58 +0800140 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 Wud1d45b42018-07-05 00:46:45 +0800148 if int(old) >= int(new):
Kuang-che Wue121fae2018-11-09 16:18:39 +0800149 raise errors.ArgumentError('--old and --new',
150 'bad bisect range (%s, %s)' % (old, new))
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800151
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800152 config = dict(
153 dut=opts.dut,
154 android_root=opts.android_root,
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800155 android_mirror=opts.android_mirror,
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800156 branch=opts.branch,
157 flavor=opts.flavor,
158 old=old,
159 new=new)
160
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800161 spec_manager = android_util.AndroidSpecManager(config)
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800162 cache = repo_util.RepoMirror(opts.android_mirror)
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800163 code_manager = codechange.CodeManager(opts.android_root, spec_manager,
164 cache)
Kuang-che Wu13acc7b2020-06-15 10:45:35 +0800165 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 Wuacb6efd2018-04-25 18:52:58 +0800171
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 Wu13acc7b2020-06-15 10:45:35 +0800183 def fill_candidate_summary(self, summary):
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
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800199
200if __name__ == '__main__':
Kuang-che Wu2526a672019-09-10 16:23:59 +0800201 bisector_cli.BisectorCommandLine(AndroidRepoDomain).main()