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