blob: 8e2c61cbe72d10bb3d30663bdfd7da5d65192080 [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
62class AndroidRepoDomain(core.BisectDomain):
63 """BisectDomain for Android code changes."""
64 # Accepts Android build id or ChromeOS version
65 revtype = staticmethod(
66 cli.argtype_multiplexer(cros_util.argtype_cros_version,
67 android_util.argtype_android_build_id))
Kuang-che Wu752228c2018-09-05 13:54:22 +080068 intra_revtype = staticmethod(
69 codechange.argtype_intra_rev(android_util.argtype_android_build_id))
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080070 help = globals()['__doc__']
71
72 @staticmethod
73 def add_init_arguments(parser):
74 parser.add_argument(
75 '--dut',
76 type=cli.argtype_notempty,
77 metavar='DUT',
78 default=configure.get('DUT', ''),
79 help='DUT address')
80 parser.add_argument(
81 '--android_root',
82 metavar='ANDROID_ROOT',
83 type=cli.argtype_dir_path,
84 required=True,
85 default=configure.get('ANDROID_ROOT'),
86 help='Android tree root')
87 parser.add_argument(
Kuang-che Wud8fc9572018-10-03 21:00:41 +080088 '--android_mirror',
Kuang-che Wud1d45b42018-07-05 00:46:45 +080089 type=cli.argtype_dir_path,
90 required=True,
Kuang-che Wud8fc9572018-10-03 21:00:41 +080091 default=configure.get('ANDROID_MIRROR'),
Kuang-che Wud1d45b42018-07-05 00:46:45 +080092 help='Android repo mirror path')
93 parser.add_argument(
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080094 '--branch',
95 metavar='ANDROID_BRANCH',
96 help='branch name like "git_mnc-dr-arc-dev"; '
97 'default is auto detect via DUT')
98 parser.add_argument(
99 '--flavor',
100 metavar='ANDROID_FLAVOR',
101 default=configure.get('ANDROID_FLAVOR'),
102 help='example: cheets_x86-user; default is auto detect via DUT')
103 parser.add_argument(
104 '--board',
105 metavar='BOARD',
106 default=configure.get('BOARD'),
107 help='ChromeOS board name, if ARC++')
108
109 @staticmethod
110 def init(opts):
111 if opts.dut:
112 assert cros_util.is_dut(opts.dut)
113
114 if not opts.flavor:
115 assert opts.dut
116 opts.flavor = arc_util.query_flavor(opts.dut)
117
118 if not opts.board:
119 assert opts.dut
120 opts.board = cros_util.query_dut_board(opts.dut)
121 if not opts.branch:
122 version = cros_util.query_dut_short_version(opts.dut)
Kuang-che Wudd7f6f02018-06-28 18:19:30 +0800123 if not cros_util.is_cros_short_version(version):
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800124 base = '.'.join(version.split('.')[:-1] + ['0'])
Kuang-che Wu74768d32018-09-07 12:03:24 +0800125 logger.info(
126 'ChromeOS version of DUT (%s) is local build, '
127 'use its base version %s to determine Android branch', version,
128 base)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800129 version = base
130 opts.branch = cros_util.query_android_branch(opts.board, version)
131 logger.debug('branch=%s', opts.branch)
132 assert opts.branch
133
134 old = determine_android_build_id(opts, opts.old)
135 new = determine_android_build_id(opts, opts.new)
136
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800137 if int(old) >= int(new):
Kuang-che Wue121fae2018-11-09 16:18:39 +0800138 raise errors.ArgumentError('--old and --new',
139 'bad bisect range (%s, %s)' % (old, new))
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800140
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800141 config = dict(
142 dut=opts.dut,
143 android_root=opts.android_root,
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800144 android_mirror=opts.android_mirror,
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800145 branch=opts.branch,
146 flavor=opts.flavor,
147 old=old,
148 new=new)
149
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800150 spec_manager = android_util.AndroidSpecManager(config)
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800151 cache = repo_util.RepoMirror(opts.android_mirror)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800152
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800153 # Make sure all repos in between are cached
154 float_specs = spec_manager.collect_float_spec(old, new)
155 for spec in reversed(float_specs):
156 spec_manager.parse_spec(spec)
157 if cache.are_spec_commits_available(spec):
158 continue
159 spec_manager.sync_disk_state(spec.name)
160
161 code_manager = codechange.CodeManager(opts.android_root, spec_manager,
162 cache)
163 revlist = code_manager.build_revlist(old, new)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800164 return config, revlist
165
166 def __init__(self, config):
167 self.config = config
168
169 def setenv(self, env, rev=None):
170 env['DUT'] = self.config['dut']
171 env['ANDROID_ROOT'] = self.config['android_root']
172 env['ANDROID_FLAVOR'] = self.config['flavor']
173 env['ANDROID_BRANCH'] = self.config['branch']
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800174 env['ANDROID_MIRROR'] = self.config['android_mirror']
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800175 env['INTRA_REV'] = rev
176
Kuang-che Wu81aecc02018-10-31 19:37:32 +0800177 def view(self, revlist, old, new):
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800178 print('old', old)
179 print('new', new)
180
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800181 old_base, old_next, _ = codechange.parse_intra_rev(old)
182 new_base, new_next, _ = codechange.parse_intra_rev(new)
183 # Only print log url if the range is within two releases.
184 if old_next in (new_base, new_next):
185 url_template = (
186 'https://android-build.googleplex.com/'
187 'builds/{new}/branches/{branch}/targets/{flavor}/cls?end={old}')
188 print(url_template.format(
189 old=old_base,
190 new=new_next,
191 branch=self.config['branch'],
192 flavor=self.config['flavor']))
193
194 spec_manager = android_util.AndroidSpecManager(self.config)
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800195 cache = repo_util.RepoMirror(self.config['android_mirror'])
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800196 code_manager = codechange.CodeManager(self.config['android_root'],
197 spec_manager, cache)
Kuang-che Wu81aecc02018-10-31 19:37:32 +0800198 code_manager.view_rev_diff(revlist, old, new)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800199
200
201if __name__ == '__main__':
202 cli.BisectorCommandLine(AndroidRepoDomain).main()