blob: ab572a070006dc1c8c095b6e6265abd5338a4c49 [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:
9 $ ./bisect_android_repo.py init --old rev1 --new rev2 --android_root ~/android
10 $ ./bisect_android_repo.py config switch ./switch_arc_localbuild.py
11 $ ./bisect_android_repo.py config eval ./eval-manually.sh
12 $ ./bisect_android_repo.py run
13
14When running switcher and evaluator, following environment variables
15will be set:
16 ANDROID_BRANCH (e.g. git_mnc-dr-arc-dev),
17 ANDROID_FLAVOR (e.g. cheets_x86-user),
18 ANDROID_ROOT,
19 DUT (e.g. samus-dut, if available).
20"""
21
22from __future__ import print_function
23import logging
24
25from bisect_kit import android_util
26from bisect_kit import arc_util
27from bisect_kit import cli
Kuang-che Wud1d45b42018-07-05 00:46:45 +080028from bisect_kit import codechange
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080029from bisect_kit import configure
30from bisect_kit import core
31from bisect_kit import cros_util
32from bisect_kit import repo_util
33
34logger = logging.getLogger(__name__)
35
36
37def determine_android_build_id(opts, rev):
38 """Determine android build id.
39
40 If `rev` is ChromeOS version, query its corresponding Android build id.
41
42 Args:
43 opts: parse result of argparse
44 rev: Android build id or ChromeOS version
45
46 Returns:
47 Android build id
48 """
49 if cros_util.is_cros_version(rev):
50 assert opts.board, 'need to specify BOARD for cros version'
51 android_build_id = cros_util.query_android_build_id(opts.board, rev)
52 assert android_util.is_android_build_id(android_build_id)
53 logger.info('Converted given CrOS version %s to Android build id %s', rev,
54 android_build_id)
55 rev = android_build_id
56 return rev
57
58
59class AndroidRepoDomain(core.BisectDomain):
60 """BisectDomain for Android code changes."""
61 # Accepts Android build id or ChromeOS version
62 revtype = staticmethod(
63 cli.argtype_multiplexer(cros_util.argtype_cros_version,
64 android_util.argtype_android_build_id))
65 help = globals()['__doc__']
66
67 @staticmethod
68 def add_init_arguments(parser):
69 parser.add_argument(
70 '--dut',
71 type=cli.argtype_notempty,
72 metavar='DUT',
73 default=configure.get('DUT', ''),
74 help='DUT address')
75 parser.add_argument(
76 '--android_root',
77 metavar='ANDROID_ROOT',
78 type=cli.argtype_dir_path,
79 required=True,
80 default=configure.get('ANDROID_ROOT'),
81 help='Android tree root')
82 parser.add_argument(
Kuang-che Wud1d45b42018-07-05 00:46:45 +080083 '--android_repo_mirror_dir',
84 type=cli.argtype_dir_path,
85 required=True,
86 default=configure.get('ANDROID_REPO_MIRROR_DIR'),
87 help='Android repo mirror path')
88 parser.add_argument(
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080089 '--branch',
90 metavar='ANDROID_BRANCH',
91 help='branch name like "git_mnc-dr-arc-dev"; '
92 'default is auto detect via DUT')
93 parser.add_argument(
94 '--flavor',
95 metavar='ANDROID_FLAVOR',
96 default=configure.get('ANDROID_FLAVOR'),
97 help='example: cheets_x86-user; default is auto detect via DUT')
98 parser.add_argument(
99 '--board',
100 metavar='BOARD',
101 default=configure.get('BOARD'),
102 help='ChromeOS board name, if ARC++')
103
104 @staticmethod
105 def init(opts):
106 if opts.dut:
107 assert cros_util.is_dut(opts.dut)
108
109 if not opts.flavor:
110 assert opts.dut
111 opts.flavor = arc_util.query_flavor(opts.dut)
112
113 if not opts.board:
114 assert opts.dut
115 opts.board = cros_util.query_dut_board(opts.dut)
116 if not opts.branch:
117 version = cros_util.query_dut_short_version(opts.dut)
Kuang-che Wudd7f6f02018-06-28 18:19:30 +0800118 if not cros_util.is_cros_short_version(version):
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800119 base = '.'.join(version.split('.')[:-1] + ['0'])
120 logger.info('ChromeOS version of DUT (%s) is local build, '
121 'use its base version %s to determine Android branch',
122 version, base)
123 version = base
124 opts.branch = cros_util.query_android_branch(opts.board, version)
125 logger.debug('branch=%s', opts.branch)
126 assert opts.branch
127
128 old = determine_android_build_id(opts, opts.old)
129 new = determine_android_build_id(opts, opts.new)
130
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800131 if int(old) >= int(new):
132 raise Exception('bad bisect range (%s, %s)' % (old, new))
133
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800134 config = dict(
135 dut=opts.dut,
136 android_root=opts.android_root,
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800137 android_repo_mirror_dir=opts.android_repo_mirror_dir,
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800138 branch=opts.branch,
139 flavor=opts.flavor,
140 old=old,
141 new=new)
142
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800143 spec_manager = android_util.AndroidSpecManager(config)
144 cache = repo_util.RepoMirror(opts.android_repo_mirror_dir)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800145
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800146 # Make sure all repos in between are cached
147 float_specs = spec_manager.collect_float_spec(old, new)
148 for spec in reversed(float_specs):
149 spec_manager.parse_spec(spec)
150 if cache.are_spec_commits_available(spec):
151 continue
152 spec_manager.sync_disk_state(spec.name)
153
154 code_manager = codechange.CodeManager(opts.android_root, spec_manager,
155 cache)
156 revlist = code_manager.build_revlist(old, new)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800157 return config, revlist
158
159 def __init__(self, config):
160 self.config = config
161
162 def setenv(self, env, rev=None):
163 env['DUT'] = self.config['dut']
164 env['ANDROID_ROOT'] = self.config['android_root']
165 env['ANDROID_FLAVOR'] = self.config['flavor']
166 env['ANDROID_BRANCH'] = self.config['branch']
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800167 env['ANDROID_REPO_MIRROR_DIR'] = self.config['android_repo_mirror_dir']
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800168 env['INTRA_REV'] = rev
169
170 def view(self, old, new):
171 print('old', old)
172 print('new', new)
173
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800174 old_base, old_next, _ = codechange.parse_intra_rev(old)
175 new_base, new_next, _ = codechange.parse_intra_rev(new)
176 # Only print log url if the range is within two releases.
177 if old_next in (new_base, new_next):
178 url_template = (
179 'https://android-build.googleplex.com/'
180 'builds/{new}/branches/{branch}/targets/{flavor}/cls?end={old}')
181 print(url_template.format(
182 old=old_base,
183 new=new_next,
184 branch=self.config['branch'],
185 flavor=self.config['flavor']))
186
187 spec_manager = android_util.AndroidSpecManager(self.config)
188 cache = repo_util.RepoMirror(self.config['android_repo_mirror_dir'])
189 code_manager = codechange.CodeManager(self.config['android_root'],
190 spec_manager, cache)
191 code_manager.view_rev_diff(old, new)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800192
193
194if __name__ == '__main__':
195 cli.BisectorCommandLine(AndroidRepoDomain).main()