blob: 58447123ff267d0e3ef2fa9ce4593714ad77f38c [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 \\
11 --android_repo_mirror_dir $ANDROID_REPO_MIRROR_DIR
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 Wud1d45b42018-07-05 00:46:45 +080087 '--android_repo_mirror_dir',
88 type=cli.argtype_dir_path,
89 required=True,
90 default=configure.get('ANDROID_REPO_MIRROR_DIR'),
91 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'])
124 logger.info('ChromeOS version of DUT (%s) is local build, '
125 'use its base version %s to determine Android branch',
126 version, base)
127 version = base
128 opts.branch = cros_util.query_android_branch(opts.board, version)
129 logger.debug('branch=%s', opts.branch)
130 assert opts.branch
131
132 old = determine_android_build_id(opts, opts.old)
133 new = determine_android_build_id(opts, opts.new)
134
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800135 if int(old) >= int(new):
136 raise Exception('bad bisect range (%s, %s)' % (old, new))
137
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800138 config = dict(
139 dut=opts.dut,
140 android_root=opts.android_root,
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800141 android_repo_mirror_dir=opts.android_repo_mirror_dir,
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800142 branch=opts.branch,
143 flavor=opts.flavor,
144 old=old,
145 new=new)
146
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800147 spec_manager = android_util.AndroidSpecManager(config)
148 cache = repo_util.RepoMirror(opts.android_repo_mirror_dir)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800149
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800150 # Make sure all repos in between are cached
151 float_specs = spec_manager.collect_float_spec(old, new)
152 for spec in reversed(float_specs):
153 spec_manager.parse_spec(spec)
154 if cache.are_spec_commits_available(spec):
155 continue
156 spec_manager.sync_disk_state(spec.name)
157
158 code_manager = codechange.CodeManager(opts.android_root, spec_manager,
159 cache)
160 revlist = code_manager.build_revlist(old, new)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800161 return config, revlist
162
163 def __init__(self, config):
164 self.config = config
165
166 def setenv(self, env, rev=None):
167 env['DUT'] = self.config['dut']
168 env['ANDROID_ROOT'] = self.config['android_root']
169 env['ANDROID_FLAVOR'] = self.config['flavor']
170 env['ANDROID_BRANCH'] = self.config['branch']
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800171 env['ANDROID_REPO_MIRROR_DIR'] = self.config['android_repo_mirror_dir']
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800172 env['INTRA_REV'] = rev
173
174 def view(self, old, new):
175 print('old', old)
176 print('new', new)
177
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800178 old_base, old_next, _ = codechange.parse_intra_rev(old)
179 new_base, new_next, _ = codechange.parse_intra_rev(new)
180 # Only print log url if the range is within two releases.
181 if old_next in (new_base, new_next):
182 url_template = (
183 'https://android-build.googleplex.com/'
184 'builds/{new}/branches/{branch}/targets/{flavor}/cls?end={old}')
185 print(url_template.format(
186 old=old_base,
187 new=new_next,
188 branch=self.config['branch'],
189 flavor=self.config['flavor']))
190
191 spec_manager = android_util.AndroidSpecManager(self.config)
192 cache = repo_util.RepoMirror(self.config['android_repo_mirror_dir'])
193 code_manager = codechange.CodeManager(self.config['android_root'],
194 spec_manager, cache)
195 code_manager.view_rev_diff(old, new)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800196
197
198if __name__ == '__main__':
199 cli.BisectorCommandLine(AndroidRepoDomain).main()