Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Kuang-che Wu | 6e4beca | 2018-06-27 17:45:02 +0800 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 3 | # 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 | """Chrome bisector to bisect a range of chrome commits. |
| 7 | |
| 8 | This bisector bisects commits between branched chrome and releases. |
| 9 | """ |
| 10 | from __future__ import print_function |
| 11 | import logging |
| 12 | import os |
| 13 | |
| 14 | from bisect_kit import cli |
| 15 | from bisect_kit import codechange |
| 16 | from bisect_kit import configure |
| 17 | from bisect_kit import core |
| 18 | from bisect_kit import cros_util |
| 19 | from bisect_kit import cr_util |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame^] | 20 | from bisect_kit import errors |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 21 | from bisect_kit import gclient_util |
| 22 | from bisect_kit import util |
| 23 | |
| 24 | logger = logging.getLogger(__name__) |
| 25 | |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 26 | |
| 27 | def guess_chrome_version(opts, rev): |
| 28 | if cros_util.is_cros_version(rev): |
| 29 | assert opts.board, 'need to specify BOARD for cros version' |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 30 | chrome_version = cros_util.query_chrome_version(opts.board, rev) |
| 31 | assert cr_util.is_chrome_version(chrome_version) |
| 32 | logger.info('Converted given CrOS version %s to Chrome version %s', rev, |
| 33 | chrome_version) |
| 34 | rev = chrome_version |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 35 | |
| 36 | return rev |
| 37 | |
| 38 | |
| 39 | class ChromeSrcDomain(core.BisectDomain): |
| 40 | """BisectDomain for Chrome branched tree""" |
Kuang-che Wu | 752228c | 2018-09-05 13:54:22 +0800 | [diff] [blame] | 41 | revtype = staticmethod( |
| 42 | cli.argtype_multiplexer(cr_util.argtype_chrome_version, |
| 43 | cros_util.argtype_cros_version)) |
| 44 | intra_revtype = staticmethod( |
| 45 | codechange.argtype_intra_rev(cr_util.argtype_chrome_version)) |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 46 | help = globals()['__doc__'] |
| 47 | |
| 48 | @staticmethod |
| 49 | def add_init_arguments(parser): |
| 50 | parser.add_argument( |
| 51 | '--chrome_root', |
| 52 | required=True, |
| 53 | metavar='CHROME_ROOT', |
| 54 | type=cli.argtype_dir_path, |
| 55 | default=configure.get('CHROME_ROOT'), |
| 56 | help='Root of chrome source tree, like ~/chromium') |
| 57 | parser.add_argument( |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 58 | '--chrome_mirror', |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 59 | required=True, |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 60 | metavar='CHROME_MIRROR', |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 61 | type=cli.argtype_dir_path, |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 62 | default=configure.get('CHROME_MIRROR'), |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 63 | help='gclient cache dir') |
| 64 | |
| 65 | # Only used for Chrome on ChromeOS. |
| 66 | parser.add_argument( |
| 67 | '--dut', |
| 68 | type=cli.argtype_notempty, |
| 69 | metavar='DUT', |
| 70 | default=configure.get('DUT'), |
| 71 | help='For ChromeOS, address of DUT (Device Under Test)') |
| 72 | parser.add_argument( |
| 73 | '--board', |
| 74 | metavar='BOARD', |
| 75 | default=configure.get('BOARD'), |
| 76 | help='For ChromeOS, board name') |
| 77 | |
| 78 | @staticmethod |
| 79 | def init(opts): |
| 80 | chrome_src = os.path.join(opts.chrome_root, 'src') |
| 81 | if not os.path.exists(chrome_src): |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame^] | 82 | raise errors.ArgumentError('--chrome_root', |
| 83 | "chrome src directory doesn't exist") |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 84 | |
| 85 | if opts.dut: |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame^] | 86 | if not cros_util.is_dut(opts.dut): |
| 87 | raise errors.ArgumentError('--dut', 'invalid DUT or unable to connect') |
| 88 | |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 89 | if not opts.board: |
| 90 | opts.board = cros_util.query_dut_board(opts.dut) |
| 91 | |
| 92 | old = guess_chrome_version(opts, opts.old) |
| 93 | new = guess_chrome_version(opts, opts.new) |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame^] | 94 | if old == new: |
| 95 | raise errors.ArgumentError( |
| 96 | '--old and --new', 'start and end of chrome versions are identical') |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 97 | |
| 98 | if not util.is_version_lesseq(old, new): |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame^] | 99 | raise errors.ArgumentError( |
| 100 | '--old and --new', |
| 101 | 'start chrome version (%s) is newer than end version (%s)' % (old, |
| 102 | new)) |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 103 | |
| 104 | config = dict( |
| 105 | chrome_root=opts.chrome_root, |
| 106 | old=old, |
| 107 | new=new, |
| 108 | board=opts.board, |
| 109 | dut=opts.dut, |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 110 | chrome_mirror=opts.chrome_mirror) |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 111 | |
| 112 | spec_manager = cr_util.ChromeSpecManager(config) |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 113 | cache = gclient_util.GclientCache(opts.chrome_mirror) |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 114 | |
| 115 | # Initial sync to get all buildspecs. |
| 116 | release_deps_file = cr_util.ChromeSpecManager.get_release_deps(new) |
| 117 | path = os.path.join(opts.chrome_root, 'buildspec', release_deps_file) |
| 118 | if not os.path.exists(path): |
| 119 | spec_manager.sync_to_release(new) |
| 120 | |
| 121 | for rev in (old, new): |
| 122 | release_deps_file = cr_util.ChromeSpecManager.get_release_deps(rev) |
| 123 | path = os.path.join(opts.chrome_root, 'buildspec', release_deps_file) |
| 124 | if not os.path.exists(path): |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame^] | 125 | raise errors.InternalError("%s doesn't exist" % path) |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 126 | |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 127 | if not util.is_direct_relative_version(old, new): |
| 128 | logger.warning('old=%s is not parent of new=%s', old, new) |
| 129 | old_ancestor = spec_manager.enumerate_ancestor(old) |
| 130 | new_ancestor = spec_manager.enumerate_ancestor(new) |
| 131 | for rev in reversed(old_ancestor): |
| 132 | if rev in new_ancestor: |
| 133 | lowest_common_ancestor = rev |
| 134 | break |
| 135 | else: |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame^] | 136 | raise errors.InternalError( |
| 137 | 'Unable to find their common ancestor: %s, %s' % (old, new)) |
Kuang-che Wu | 74768d3 | 2018-09-07 12:03:24 +0800 | [diff] [blame] | 138 | logger.warning( |
| 139 | 'Assume their lowest common ancestor, %s,' |
| 140 | 'still have expected old behavior as %s', lowest_common_ancestor, old) |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 141 | config['old'] = old = lowest_common_ancestor |
| 142 | |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 143 | # TODO(kcwu): verify full git history is available. |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 144 | code_manager = codechange.CodeManager(opts.chrome_root, spec_manager, cache) |
| 145 | revlist = code_manager.build_revlist(old, new) |
| 146 | |
| 147 | return config, revlist |
| 148 | |
| 149 | def __init__(self, config): |
| 150 | self.config = config |
| 151 | |
| 152 | def setenv(self, env, rev): |
| 153 | env['CHROME_ROOT'] = self.config['chrome_root'] |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 154 | env['CHROME_MIRROR'] = self.config['chrome_mirror'] |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 155 | env['REV'] = rev |
| 156 | |
| 157 | if self.config['board']: |
| 158 | env['BOARD'] = self.config['board'] |
| 159 | if self.config['dut']: |
| 160 | env['DUT'] = self.config['dut'] |
| 161 | |
Kuang-che Wu | 81aecc0 | 2018-10-31 19:37:32 +0800 | [diff] [blame] | 162 | def view(self, revlist, old, new): |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 163 | print('old', old) |
| 164 | print('new', new) |
| 165 | |
| 166 | old_base, old_next, _ = codechange.parse_intra_rev(old) |
| 167 | new_base, new_next, _ = codechange.parse_intra_rev(new) |
| 168 | # Only print log url if the range is within two releases. |
| 169 | if old_next in (new_base, new_next): |
| 170 | log_url = ('https://chromium.googlesource.com/chromium/src/+log/%s..%s' |
| 171 | '?pretty=fuller&n=10000') |
| 172 | print('Be careful the following url only lists chrome src/ commits. For ' |
| 173 | 'example, commits inside v8 and third party repos are not listed.') |
| 174 | if old_base != old_next: |
| 175 | print(log_url % (old_base, old_next)) |
| 176 | if new_base != new_next and old_next != new_next: |
| 177 | print(log_url % (new_base, new_next)) |
| 178 | |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 179 | cache = gclient_util.GclientCache(self.config['chrome_mirror']) |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 180 | spec_manager = cr_util.ChromeSpecManager(self.config) |
| 181 | code_manager = codechange.CodeManager(self.config['chrome_root'], |
| 182 | spec_manager, cache) |
Kuang-che Wu | 81aecc0 | 2018-10-31 19:37:32 +0800 | [diff] [blame] | 183 | code_manager.view_rev_diff(revlist, old, new) |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 184 | |
| 185 | |
| 186 | if __name__ == '__main__': |
| 187 | cli.BisectorCommandLine(ChromeSrcDomain).main() |