blob: 3e5503e67f2e2efee021f6653b07103238241973 [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))
67 help = globals()['__doc__']
68
69 @staticmethod
70 def add_init_arguments(parser):
71 parser.add_argument(
72 '--dut',
73 type=cli.argtype_notempty,
74 metavar='DUT',
75 default=configure.get('DUT', ''),
76 help='DUT address')
77 parser.add_argument(
78 '--android_root',
79 metavar='ANDROID_ROOT',
80 type=cli.argtype_dir_path,
81 required=True,
82 default=configure.get('ANDROID_ROOT'),
83 help='Android tree root')
84 parser.add_argument(
Kuang-che Wud1d45b42018-07-05 00:46:45 +080085 '--android_repo_mirror_dir',
86 type=cli.argtype_dir_path,
87 required=True,
88 default=configure.get('ANDROID_REPO_MIRROR_DIR'),
89 help='Android repo mirror path')
90 parser.add_argument(
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080091 '--branch',
92 metavar='ANDROID_BRANCH',
93 help='branch name like "git_mnc-dr-arc-dev"; '
94 'default is auto detect via DUT')
95 parser.add_argument(
96 '--flavor',
97 metavar='ANDROID_FLAVOR',
98 default=configure.get('ANDROID_FLAVOR'),
99 help='example: cheets_x86-user; default is auto detect via DUT')
100 parser.add_argument(
101 '--board',
102 metavar='BOARD',
103 default=configure.get('BOARD'),
104 help='ChromeOS board name, if ARC++')
105
106 @staticmethod
107 def init(opts):
108 if opts.dut:
109 assert cros_util.is_dut(opts.dut)
110
111 if not opts.flavor:
112 assert opts.dut
113 opts.flavor = arc_util.query_flavor(opts.dut)
114
115 if not opts.board:
116 assert opts.dut
117 opts.board = cros_util.query_dut_board(opts.dut)
118 if not opts.branch:
119 version = cros_util.query_dut_short_version(opts.dut)
Kuang-che Wudd7f6f02018-06-28 18:19:30 +0800120 if not cros_util.is_cros_short_version(version):
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800121 base = '.'.join(version.split('.')[:-1] + ['0'])
122 logger.info('ChromeOS version of DUT (%s) is local build, '
123 'use its base version %s to determine Android branch',
124 version, base)
125 version = base
126 opts.branch = cros_util.query_android_branch(opts.board, version)
127 logger.debug('branch=%s', opts.branch)
128 assert opts.branch
129
130 old = determine_android_build_id(opts, opts.old)
131 new = determine_android_build_id(opts, opts.new)
132
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800133 if int(old) >= int(new):
134 raise Exception('bad bisect range (%s, %s)' % (old, new))
135
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800136 config = dict(
137 dut=opts.dut,
138 android_root=opts.android_root,
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800139 android_repo_mirror_dir=opts.android_repo_mirror_dir,
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800140 branch=opts.branch,
141 flavor=opts.flavor,
142 old=old,
143 new=new)
144
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800145 spec_manager = android_util.AndroidSpecManager(config)
146 cache = repo_util.RepoMirror(opts.android_repo_mirror_dir)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800147
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800148 # Make sure all repos in between are cached
149 float_specs = spec_manager.collect_float_spec(old, new)
150 for spec in reversed(float_specs):
151 spec_manager.parse_spec(spec)
152 if cache.are_spec_commits_available(spec):
153 continue
154 spec_manager.sync_disk_state(spec.name)
155
156 code_manager = codechange.CodeManager(opts.android_root, spec_manager,
157 cache)
158 revlist = code_manager.build_revlist(old, new)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800159 return config, revlist
160
161 def __init__(self, config):
162 self.config = config
163
164 def setenv(self, env, rev=None):
165 env['DUT'] = self.config['dut']
166 env['ANDROID_ROOT'] = self.config['android_root']
167 env['ANDROID_FLAVOR'] = self.config['flavor']
168 env['ANDROID_BRANCH'] = self.config['branch']
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800169 env['ANDROID_REPO_MIRROR_DIR'] = self.config['android_repo_mirror_dir']
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800170 env['INTRA_REV'] = rev
171
172 def view(self, old, new):
173 print('old', old)
174 print('new', new)
175
Kuang-che Wud1d45b42018-07-05 00:46:45 +0800176 old_base, old_next, _ = codechange.parse_intra_rev(old)
177 new_base, new_next, _ = codechange.parse_intra_rev(new)
178 # Only print log url if the range is within two releases.
179 if old_next in (new_base, new_next):
180 url_template = (
181 'https://android-build.googleplex.com/'
182 'builds/{new}/branches/{branch}/targets/{flavor}/cls?end={old}')
183 print(url_template.format(
184 old=old_base,
185 new=new_next,
186 branch=self.config['branch'],
187 flavor=self.config['flavor']))
188
189 spec_manager = android_util.AndroidSpecManager(self.config)
190 cache = repo_util.RepoMirror(self.config['android_repo_mirror_dir'])
191 code_manager = codechange.CodeManager(self.config['android_root'],
192 spec_manager, cache)
193 code_manager.view_rev_diff(old, new)
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800194
195
196if __name__ == '__main__':
197 cli.BisectorCommandLine(AndroidRepoDomain).main()