blob: 15b1208dd76fbac3a52448b56714e3184643e025 [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
34from bisect_kit import repo_util
35
36logger = logging.getLogger(__name__)
37
38
39def determine_android_build_id(opts, rev):
40 """Determine android build id.
41
42 If `rev` is ChromeOS version, query its corresponding Android build id.
43
44 Args:
45 opts: parse result of argparse
46 rev: Android build id or ChromeOS version
47
48 Returns:
49 Android build id
50 """
51 if cros_util.is_cros_version(rev):
52 assert opts.board, 'need to specify BOARD for cros version'
53 android_build_id = cros_util.query_android_build_id(opts.board, rev)
54 assert android_util.is_android_build_id(android_build_id)
55 logger.info('Converted given CrOS version %s to Android build id %s', rev,
56 android_build_id)
57 rev = android_build_id
58 return rev
59
60
61class AndroidRepoDomain(core.BisectDomain):
62 """BisectDomain for Android code changes."""
63 # Accepts Android build id or ChromeOS version
64 revtype = staticmethod(
65 cli.argtype_multiplexer(cros_util.argtype_cros_version,
66 android_util.argtype_android_build_id))
Kuang-che Wu752228c2018-09-05 13:54:22 +080067 intra_revtype = staticmethod(
68 codechange.argtype_intra_rev(android_util.argtype_android_build_id))
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080069 help = globals()['__doc__']
70
71 @staticmethod
72 def add_init_arguments(parser):
73 parser.add_argument(
74 '--dut',
75 type=cli.argtype_notempty,
76 metavar='DUT',
77 default=configure.get('DUT', ''),
78 help='DUT address')
79 parser.add_argument(
80 '--android_root',
81 metavar='ANDROID_ROOT',
82 type=cli.argtype_dir_path,
83 required=True,
84 default=configure.get('ANDROID_ROOT'),
85 help='Android tree root')
86 parser.add_argument(
Kuang-che Wud8fc9572018-10-03 21:00:41 +080087 '--android_mirror',
Kuang-che Wud1d45b42018-07-05 00:46:45 +080088 type=cli.argtype_dir_path,
89 required=True,
Kuang-che Wud8fc9572018-10-03 21:00:41 +080090 default=configure.get('ANDROID_MIRROR'),
Kuang-che Wud1d45b42018-07-05 00:46:45 +080091 help='Android repo mirror path')
92 parser.add_argument(
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080093 '--branch',
94 metavar='ANDROID_BRANCH',
95 help='branch name like "git_mnc-dr-arc-dev"; '
96 'default is auto detect via DUT')
97 parser.add_argument(
98 '--flavor',
99 metavar='ANDROID_FLAVOR',
100 default=configure.get('ANDROID_FLAVOR'),
101 help='example: cheets_x86-user; default is auto detect via DUT')
102 parser.add_argument(
103 '--board',
104 metavar='BOARD',
105 default=configure.get('BOARD'),
106 help='ChromeOS board name, if ARC++')
107
108 @staticmethod
109 def init(opts):
110 if opts.dut:
111 assert cros_util.is_dut(opts.dut)
112
113 if not opts.flavor:
114 assert opts.dut
115 opts.flavor = arc_util.query_flavor(opts.dut)
116
117 if not opts.board:
118 assert opts.dut
119 opts.board = cros_util.query_dut_board(opts.dut)
120 if not opts.branch:
121 version = cros_util.query_dut_short_version(opts.dut)
Kuang-che Wudd7f6f02018-06-28 18:19:30 +0800122 if not cros_util.is_cros_short_version(version):
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800123 base = '.'.join(version.split('.')[:-1] + ['0'])
Kuang-che Wu74768d32018-09-07 12:03:24 +0800124 logger.info(
125 'ChromeOS version of DUT (%s) is local build, '
126 'use its base version %s to determine Android branch', version,
127 base)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800128 version = base
129 opts.branch = cros_util.query_android_branch(opts.board, version)
130 logger.debug('branch=%s', opts.branch)
131 assert opts.branch
132
133 old = determine_android_build_id(opts, opts.old)
134 new = determine_android_build_id(opts, opts.new)
135
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800136 if int(old) >= int(new):
137 raise Exception('bad bisect range (%s, %s)' % (old, new))
138
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800139 config = dict(
140 dut=opts.dut,
141 android_root=opts.android_root,
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800142 android_mirror=opts.android_mirror,
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800143 branch=opts.branch,
144 flavor=opts.flavor,
145 old=old,
146 new=new)
147
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800148 spec_manager = android_util.AndroidSpecManager(config)
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800149 cache = repo_util.RepoMirror(opts.android_mirror)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800150
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800151 # Make sure all repos in between are cached
152 float_specs = spec_manager.collect_float_spec(old, new)
153 for spec in reversed(float_specs):
154 spec_manager.parse_spec(spec)
155 if cache.are_spec_commits_available(spec):
156 continue
157 spec_manager.sync_disk_state(spec.name)
158
159 code_manager = codechange.CodeManager(opts.android_root, spec_manager,
160 cache)
161 revlist = code_manager.build_revlist(old, new)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800162 return config, revlist
163
164 def __init__(self, config):
165 self.config = config
166
167 def setenv(self, env, rev=None):
168 env['DUT'] = self.config['dut']
169 env['ANDROID_ROOT'] = self.config['android_root']
170 env['ANDROID_FLAVOR'] = self.config['flavor']
171 env['ANDROID_BRANCH'] = self.config['branch']
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800172 env['ANDROID_MIRROR'] = self.config['android_mirror']
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800173 env['INTRA_REV'] = rev
174
Kuang-che Wu81aecc02018-10-31 19:37:32 +0800175 def view(self, revlist, old, new):
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800176 print('old', old)
177 print('new', new)
178
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800179 old_base, old_next, _ = codechange.parse_intra_rev(old)
180 new_base, new_next, _ = codechange.parse_intra_rev(new)
181 # Only print log url if the range is within two releases.
182 if old_next in (new_base, new_next):
183 url_template = (
184 'https://android-build.googleplex.com/'
185 'builds/{new}/branches/{branch}/targets/{flavor}/cls?end={old}')
186 print(url_template.format(
187 old=old_base,
188 new=new_next,
189 branch=self.config['branch'],
190 flavor=self.config['flavor']))
191
192 spec_manager = android_util.AndroidSpecManager(self.config)
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800193 cache = repo_util.RepoMirror(self.config['android_mirror'])
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800194 code_manager = codechange.CodeManager(self.config['android_root'],
195 spec_manager, cache)
Kuang-che Wu81aecc02018-10-31 19:37:32 +0800196 code_manager.view_rev_diff(revlist, old, new)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800197
198
199if __name__ == '__main__':
200 cli.BisectorCommandLine(AndroidRepoDomain).main()