kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | import argparse |
| 7 | import collections |
| 8 | import logging |
| 9 | import os |
| 10 | import re |
| 11 | import subprocess |
| 12 | import sys |
| 13 | import time |
| 14 | |
geofflang | 527982f9 | 2015-09-15 20:39:24 | [diff] [blame] | 15 | extra_trybots = [ |
kbr | 44b05f8 | 2016-01-07 23:37:01 | [diff] [blame^] | 16 | { |
| 17 | "mastername": "tryserver.chromium.win", |
| 18 | "buildername": "win_clang_dbg", |
| 19 | } |
geofflang | 527982f9 | 2015-09-15 20:39:24 | [diff] [blame] | 20 | ] |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 21 | |
| 22 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 23 | SRC_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir)) |
mcgrathr | b729cfa | 2015-10-26 22:07:51 | [diff] [blame] | 24 | sys.path.insert(0, os.path.join(SRC_DIR, 'build')) |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 25 | import find_depot_tools |
| 26 | find_depot_tools.add_depot_tools_to_path() |
jmadill | 241d77b | 2015-05-25 19:21:54 | [diff] [blame] | 27 | import roll_dep_svn |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 28 | from gclient import GClientKeywords |
| 29 | from third_party import upload |
| 30 | |
| 31 | # Avoid depot_tools/third_party/upload.py print verbose messages. |
| 32 | upload.verbosity = 0 # Errors only. |
| 33 | |
| 34 | CHROMIUM_GIT_URL = 'https://chromium.googlesource.com/chromium/src.git' |
| 35 | CL_ISSUE_RE = re.compile('^Issue number: ([0-9]+) \((.*)\)$') |
| 36 | RIETVELD_URL_RE = re.compile('^https?://(.*)/(.*)') |
| 37 | ROLL_BRANCH_NAME = 'special_angle_roll_branch' |
| 38 | TRYJOB_STATUS_SLEEP_SECONDS = 30 |
| 39 | |
| 40 | # Use a shell for subcommands on Windows to get a PATH search. |
kbr | ebf7b74 | 2015-06-25 02:31:41 | [diff] [blame] | 41 | IS_WIN = sys.platform.startswith('win') |
geofflang | 44a4fb3e | 2015-06-09 21:45:37 | [diff] [blame] | 42 | ANGLE_PATH = os.path.join('third_party', 'angle') |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 43 | |
| 44 | CommitInfo = collections.namedtuple('CommitInfo', ['git_commit', |
| 45 | 'git_repo_url']) |
| 46 | CLInfo = collections.namedtuple('CLInfo', ['issue', 'url', 'rietveld_server']) |
| 47 | |
geofflang | 44a4fb3e | 2015-06-09 21:45:37 | [diff] [blame] | 48 | def _PosixPath(path): |
| 49 | """Convert a possibly-Windows path to a posix-style path.""" |
| 50 | (_, path) = os.path.splitdrive(path) |
| 51 | return path.replace(os.sep, '/') |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 52 | |
| 53 | def _ParseGitCommitHash(description): |
| 54 | for line in description.splitlines(): |
| 55 | if line.startswith('commit '): |
| 56 | return line.split()[1] |
| 57 | logging.error('Failed to parse git commit id from:\n%s\n', description) |
| 58 | sys.exit(-1) |
| 59 | return None |
| 60 | |
| 61 | |
| 62 | def _ParseDepsFile(filename): |
| 63 | with open(filename, 'rb') as f: |
| 64 | deps_content = f.read() |
| 65 | return _ParseDepsDict(deps_content) |
| 66 | |
| 67 | |
| 68 | def _ParseDepsDict(deps_content): |
| 69 | local_scope = {} |
| 70 | var = GClientKeywords.VarImpl({}, local_scope) |
| 71 | global_scope = { |
| 72 | 'File': GClientKeywords.FileImpl, |
| 73 | 'From': GClientKeywords.FromImpl, |
| 74 | 'Var': var.Lookup, |
| 75 | 'deps_os': {}, |
| 76 | } |
| 77 | exec(deps_content, global_scope, local_scope) |
| 78 | return local_scope |
| 79 | |
| 80 | |
geofflang | 44a4fb3e | 2015-06-09 21:45:37 | [diff] [blame] | 81 | def _GenerateCLDescriptionCommand(angle_current, angle_new, bugs): |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 82 | def GetChangeString(current_hash, new_hash): |
| 83 | return '%s..%s' % (current_hash[0:7], new_hash[0:7]); |
| 84 | |
| 85 | def GetChangeLogURL(git_repo_url, change_string): |
| 86 | return '%s/+log/%s' % (git_repo_url, change_string) |
| 87 | |
geofflang | 44a4fb3e | 2015-06-09 21:45:37 | [diff] [blame] | 88 | def GetBugString(bugs): |
| 89 | bug_str = 'BUG=' |
| 90 | for bug in bugs: |
| 91 | bug_str += str(bug) + ',' |
| 92 | return bug_str.rstrip(',') |
| 93 | |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 94 | if angle_current.git_commit != angle_new.git_commit: |
| 95 | change_str = GetChangeString(angle_current.git_commit, |
| 96 | angle_new.git_commit) |
| 97 | changelog_url = GetChangeLogURL(angle_current.git_repo_url, |
| 98 | change_str) |
| 99 | |
geofflang | 44a4fb3e | 2015-06-09 21:45:37 | [diff] [blame] | 100 | return [ |
| 101 | '-m', 'Roll ANGLE ' + change_str, |
| 102 | '-m', '%s' % changelog_url, |
| 103 | '-m', GetBugString(bugs), |
| 104 | '-m', 'TEST=bots', |
| 105 | ] |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 106 | |
| 107 | |
| 108 | class AutoRoller(object): |
| 109 | def __init__(self, chromium_src): |
| 110 | self._chromium_src = chromium_src |
| 111 | |
| 112 | def _RunCommand(self, command, working_dir=None, ignore_exit_code=False, |
| 113 | extra_env=None): |
| 114 | """Runs a command and returns the stdout from that command. |
| 115 | |
| 116 | If the command fails (exit code != 0), the function will exit the process. |
| 117 | """ |
| 118 | working_dir = working_dir or self._chromium_src |
| 119 | logging.debug('cmd: %s cwd: %s', ' '.join(command), working_dir) |
| 120 | env = os.environ.copy() |
| 121 | if extra_env: |
| 122 | logging.debug('extra env: %s', extra_env) |
| 123 | env.update(extra_env) |
| 124 | p = subprocess.Popen(command, stdout=subprocess.PIPE, |
kbr | ebf7b74 | 2015-06-25 02:31:41 | [diff] [blame] | 125 | stderr=subprocess.PIPE, shell=IS_WIN, env=env, |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 126 | cwd=working_dir, universal_newlines=True) |
| 127 | output = p.stdout.read() |
| 128 | p.wait() |
| 129 | p.stdout.close() |
| 130 | p.stderr.close() |
| 131 | |
| 132 | if not ignore_exit_code and p.returncode != 0: |
| 133 | logging.error('Command failed: %s\n%s', str(command), output) |
| 134 | sys.exit(p.returncode) |
| 135 | return output |
| 136 | |
| 137 | def _GetCommitInfo(self, path_below_src, git_hash=None, git_repo_url=None): |
| 138 | working_dir = os.path.join(self._chromium_src, path_below_src) |
| 139 | self._RunCommand(['git', 'fetch', 'origin'], working_dir=working_dir) |
| 140 | revision_range = git_hash or 'origin' |
| 141 | ret = self._RunCommand( |
| 142 | ['git', '--no-pager', 'log', revision_range, '--pretty=full', '-1'], |
| 143 | working_dir=working_dir) |
| 144 | return CommitInfo(_ParseGitCommitHash(ret), git_repo_url) |
| 145 | |
| 146 | def _GetDepsCommitInfo(self, deps_dict, path_below_src): |
geofflang | 44a4fb3e | 2015-06-09 21:45:37 | [diff] [blame] | 147 | entry = deps_dict['deps'][_PosixPath('src/%s' % path_below_src)] |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 148 | at_index = entry.find('@') |
| 149 | git_repo_url = entry[:at_index] |
| 150 | git_hash = entry[at_index + 1:] |
| 151 | return self._GetCommitInfo(path_below_src, git_hash, git_repo_url) |
| 152 | |
| 153 | def _GetCLInfo(self): |
| 154 | cl_output = self._RunCommand(['git', 'cl', 'issue']) |
| 155 | m = CL_ISSUE_RE.match(cl_output.strip()) |
| 156 | if not m: |
| 157 | logging.error('Cannot find any CL info. Output was:\n%s', cl_output) |
| 158 | sys.exit(-1) |
| 159 | issue_number = int(m.group(1)) |
| 160 | url = m.group(2) |
| 161 | |
| 162 | # Parse the Rietveld host from the URL. |
| 163 | m = RIETVELD_URL_RE.match(url) |
| 164 | if not m: |
| 165 | logging.error('Cannot parse Rietveld host from URL: %s', url) |
| 166 | sys.exit(-1) |
| 167 | rietveld_server = m.group(1) |
| 168 | return CLInfo(issue_number, url, rietveld_server) |
| 169 | |
| 170 | def _GetCurrentBranchName(self): |
| 171 | return self._RunCommand( |
| 172 | ['git', 'rev-parse', '--abbrev-ref', 'HEAD']).splitlines()[0] |
| 173 | |
| 174 | def _IsTreeClean(self): |
| 175 | lines = self._RunCommand( |
| 176 | ['git', 'status', '--porcelain', '-uno']).splitlines() |
| 177 | if len(lines) == 0: |
| 178 | return True |
| 179 | |
| 180 | logging.debug('Dirty/unversioned files:\n%s', '\n'.join(lines)) |
| 181 | return False |
| 182 | |
geofflang | 44a4fb3e | 2015-06-09 21:45:37 | [diff] [blame] | 183 | def _GetBugList(self, path_below_src, angle_current, angle_new): |
| 184 | working_dir = os.path.join(self._chromium_src, path_below_src) |
| 185 | lines = self._RunCommand( |
| 186 | ['git','log', |
| 187 | '%s..%s' % (angle_current.git_commit, angle_new.git_commit)], |
| 188 | working_dir=working_dir).split('\n') |
| 189 | bugs = set() |
| 190 | for line in lines: |
| 191 | line = line.strip() |
| 192 | bug_prefix = 'BUG=' |
| 193 | if line.startswith(bug_prefix): |
| 194 | bugs_strings = line[len(bug_prefix):].split(',') |
| 195 | for bug_string in bugs_strings: |
| 196 | try: |
| 197 | bugs.add(int(bug_string)) |
| 198 | except: |
| 199 | # skip this, it may be a project specific bug such as |
| 200 | # "angleproject:X" or an ill-formed BUG= message |
| 201 | pass |
| 202 | return bugs |
| 203 | |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 204 | def _UpdateReadmeFile(self, readme_path, new_revision): |
| 205 | readme = open(os.path.join(self._chromium_src, readme_path), 'r+') |
| 206 | txt = readme.read() |
| 207 | m = re.sub(re.compile('.*^Revision\: ([0-9]*).*', re.MULTILINE), |
| 208 | ('Revision: %s' % new_revision), txt) |
| 209 | readme.seek(0) |
| 210 | readme.write(m) |
| 211 | readme.truncate() |
| 212 | |
| 213 | def PrepareRoll(self, ignore_checks): |
| 214 | # TODO(kjellander): use os.path.normcase, os.path.join etc for all paths for |
| 215 | # cross platform compatibility. |
| 216 | |
| 217 | if not ignore_checks: |
| 218 | if self._GetCurrentBranchName() != 'master': |
| 219 | logging.error('Please checkout the master branch.') |
| 220 | return -1 |
| 221 | if not self._IsTreeClean(): |
| 222 | logging.error('Please make sure you don\'t have any modified files.') |
| 223 | return -1 |
| 224 | |
| 225 | # Always clean up any previous roll. |
| 226 | self.Abort() |
| 227 | |
| 228 | logging.debug('Pulling latest changes') |
| 229 | if not ignore_checks: |
| 230 | self._RunCommand(['git', 'pull']) |
| 231 | |
| 232 | self._RunCommand(['git', 'checkout', '-b', ROLL_BRANCH_NAME]) |
| 233 | |
| 234 | # Modify Chromium's DEPS file. |
| 235 | |
| 236 | # Parse current hashes. |
| 237 | deps_filename = os.path.join(self._chromium_src, 'DEPS') |
| 238 | deps = _ParseDepsFile(deps_filename) |
| 239 | angle_current = self._GetDepsCommitInfo(deps, ANGLE_PATH) |
| 240 | |
| 241 | # Find ToT revisions. |
| 242 | angle_latest = self._GetCommitInfo(ANGLE_PATH) |
| 243 | |
kbr | ebf7b74 | 2015-06-25 02:31:41 | [diff] [blame] | 244 | if IS_WIN: |
| 245 | # Make sure the roll script doesn't use windows line endings |
| 246 | self._RunCommand(['git', 'config', 'core.autocrlf', 'true']) |
geofflang | 44a4fb3e | 2015-06-09 21:45:37 | [diff] [blame] | 247 | |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 248 | self._UpdateDep(deps_filename, ANGLE_PATH, angle_latest) |
| 249 | |
| 250 | if self._IsTreeClean(): |
| 251 | logging.debug('Tree is clean - no changes detected.') |
| 252 | self._DeleteRollBranch() |
| 253 | else: |
geofflang | 44a4fb3e | 2015-06-09 21:45:37 | [diff] [blame] | 254 | bugs = self._GetBugList(ANGLE_PATH, angle_current, angle_latest) |
| 255 | description = _GenerateCLDescriptionCommand( |
| 256 | angle_current, angle_latest, bugs) |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 257 | logging.debug('Committing changes locally.') |
| 258 | self._RunCommand(['git', 'add', '--update', '.']) |
geofflang | 44a4fb3e | 2015-06-09 21:45:37 | [diff] [blame] | 259 | self._RunCommand(['git', 'commit'] + description) |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 260 | logging.debug('Uploading changes...') |
geofflang | 44a4fb3e | 2015-06-09 21:45:37 | [diff] [blame] | 261 | self._RunCommand(['git', 'cl', 'upload'], |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 262 | extra_env={'EDITOR': 'true'}) |
geofflang | 527982f9 | 2015-09-15 20:39:24 | [diff] [blame] | 263 | |
| 264 | # Run the default trybots |
| 265 | base_try_cmd = ['git', 'cl', 'try'] |
| 266 | self._RunCommand(base_try_cmd) |
| 267 | |
| 268 | if extra_trybots: |
| 269 | # Run additional tryjobs |
| 270 | extra_try_args = [] |
| 271 | for extra_trybot in extra_trybots: |
kbr | 44b05f8 | 2016-01-07 23:37:01 | [diff] [blame^] | 272 | extra_try_args += ['-m', extra_trybot["mastername"], |
| 273 | '-b', extra_trybot["buildername"]] |
geofflang | 527982f9 | 2015-09-15 20:39:24 | [diff] [blame] | 274 | self._RunCommand(base_try_cmd + extra_try_args) |
| 275 | |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 276 | cl_info = self._GetCLInfo() |
| 277 | print 'Issue: %d URL: %s' % (cl_info.issue, cl_info.url) |
| 278 | |
| 279 | # Checkout master again. |
| 280 | self._RunCommand(['git', 'checkout', 'master']) |
| 281 | print 'Roll branch left as ' + ROLL_BRANCH_NAME |
| 282 | return 0 |
| 283 | |
| 284 | def _UpdateDep(self, deps_filename, dep_relative_to_src, commit_info): |
geofflang | 44a4fb3e | 2015-06-09 21:45:37 | [diff] [blame] | 285 | dep_name = _PosixPath(os.path.join('src', dep_relative_to_src)) |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 286 | |
jmadill | 241d77b | 2015-05-25 19:21:54 | [diff] [blame] | 287 | # roll_dep_svn.py relies on cwd being the Chromium checkout, so let's |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 288 | # temporarily change the working directory and then change back. |
| 289 | cwd = os.getcwd() |
| 290 | os.chdir(os.path.dirname(deps_filename)) |
jmadill | 241d77b | 2015-05-25 19:21:54 | [diff] [blame] | 291 | roll_dep_svn.update_deps(deps_filename, dep_relative_to_src, dep_name, |
| 292 | commit_info.git_commit, '') |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 293 | os.chdir(cwd) |
| 294 | |
| 295 | def _DeleteRollBranch(self): |
| 296 | self._RunCommand(['git', 'checkout', 'master']) |
| 297 | self._RunCommand(['git', 'branch', '-D', ROLL_BRANCH_NAME]) |
| 298 | logging.debug('Deleted the local roll branch (%s)', ROLL_BRANCH_NAME) |
| 299 | |
| 300 | |
| 301 | def _GetBranches(self): |
| 302 | """Returns a tuple of active,branches. |
| 303 | |
| 304 | The 'active' is the name of the currently active branch and 'branches' is a |
| 305 | list of all branches. |
| 306 | """ |
| 307 | lines = self._RunCommand(['git', 'branch']).split('\n') |
| 308 | branches = [] |
| 309 | active = '' |
| 310 | for l in lines: |
| 311 | if '*' in l: |
| 312 | # The assumption is that the first char will always be the '*'. |
| 313 | active = l[1:].strip() |
| 314 | branches.append(active) |
| 315 | else: |
| 316 | b = l.strip() |
| 317 | if b: |
| 318 | branches.append(b) |
| 319 | return (active, branches) |
| 320 | |
| 321 | def Abort(self): |
| 322 | active_branch, branches = self._GetBranches() |
| 323 | if active_branch == ROLL_BRANCH_NAME: |
| 324 | active_branch = 'master' |
| 325 | if ROLL_BRANCH_NAME in branches: |
| 326 | print 'Aborting pending roll.' |
| 327 | self._RunCommand(['git', 'checkout', ROLL_BRANCH_NAME]) |
| 328 | # Ignore an error here in case an issue wasn't created for some reason. |
| 329 | self._RunCommand(['git', 'cl', 'set_close'], ignore_exit_code=True) |
| 330 | self._RunCommand(['git', 'checkout', active_branch]) |
| 331 | self._RunCommand(['git', 'branch', '-D', ROLL_BRANCH_NAME]) |
| 332 | return 0 |
| 333 | |
| 334 | |
| 335 | def main(): |
kbr | 9fe00f6 | 2015-05-21 21:09:50 | [diff] [blame] | 336 | parser = argparse.ArgumentParser( |
| 337 | description='Auto-generates a CL containing an ANGLE roll.') |
| 338 | parser.add_argument('--abort', |
| 339 | help=('Aborts a previously prepared roll. ' |
| 340 | 'Closes any associated issues and deletes the roll branches'), |
| 341 | action='store_true') |
| 342 | parser.add_argument('--ignore-checks', action='store_true', default=False, |
| 343 | help=('Skips checks for being on the master branch, dirty workspaces and ' |
| 344 | 'the updating of the checkout. Will still delete and create local ' |
| 345 | 'Git branches.')) |
| 346 | parser.add_argument('-v', '--verbose', action='store_true', default=False, |
| 347 | help='Be extra verbose in printing of log messages.') |
| 348 | args = parser.parse_args() |
| 349 | |
| 350 | if args.verbose: |
| 351 | logging.basicConfig(level=logging.DEBUG) |
| 352 | else: |
| 353 | logging.basicConfig(level=logging.ERROR) |
| 354 | |
| 355 | autoroller = AutoRoller(SRC_DIR) |
| 356 | if args.abort: |
| 357 | return autoroller.Abort() |
| 358 | else: |
| 359 | return autoroller.PrepareRoll(args.ignore_checks) |
| 360 | |
| 361 | if __name__ == '__main__': |
| 362 | sys.exit(main()) |