|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2014 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 | +"""Generate a CL to roll a DEPS entry to the specified revision number and post |
| 7 | +it for review so that the CL will land automatically if it passes the |
| 8 | +commit-queue's checks. |
| 9 | +""" |
| 10 | + |
| 11 | +import logging |
| 12 | +import optparse |
| 13 | +import os |
| 14 | +import re |
| 15 | +import sys |
| 16 | + |
| 17 | +SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 18 | +SRC_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir, os.pardir)) |
| 19 | +sys.path.insert(0, os.path.join(SRC_DIR, 'build')) |
| 20 | +import find_depot_tools |
| 21 | +import scm |
| 22 | +import subprocess |
| 23 | + |
| 24 | +IS_WINDOWS = sys.platform.startswith('win') |
| 25 | + |
| 26 | +def die_with_error(msg): |
| 27 | + print >> sys.stderr, msg |
| 28 | + sys.exit(1) |
| 29 | + |
| 30 | + |
| 31 | +def process_deps(path, project, new_rev, is_dry_run): |
| 32 | + """Update project_revision to |new_issue|. |
| 33 | +
|
| 34 | + A bit hacky, could it be made better? |
| 35 | + """ |
| 36 | + content = open(path).read() |
| 37 | + # Hack for Blink to get the AutoRollBot running again. |
| 38 | + if project == "blink": |
| 39 | + project = "webkit" |
| 40 | + old_line = r"(\s+)'%s_revision': '([0-9a-f]{2,40})'," % project |
| 41 | + new_line = r"\1'%s_revision': '%s'," % (project, new_rev) |
| 42 | + new_content = re.sub(old_line, new_line, content, 1) |
| 43 | + old_rev = re.search(old_line, content).group(2) |
| 44 | + if not old_rev: |
| 45 | + die_with_error('Failed to update the DEPS file') |
| 46 | + |
| 47 | + if not is_dry_run and new_content != content: |
| 48 | + open(path, 'w').write(new_content) |
| 49 | + return old_rev |
| 50 | + |
| 51 | + |
| 52 | +class PrintSubprocess(object): |
| 53 | + """Wrapper for subprocess2 which prints out every command.""" |
| 54 | + def __getattr__(self, attr): |
| 55 | + def _run_subprocess2(cmd, *args, **kwargs): |
| 56 | + print cmd |
| 57 | + sys.stdout.flush() |
| 58 | + return getattr(subprocess2, attr)(cmd, *args, **kwargs) |
| 59 | + return _run_subprocess2 |
| 60 | + |
| 61 | +prnt_subprocess = PrintSubprocess() |
| 62 | + |
| 63 | + |
| 64 | +def main(): |
| 65 | + tool_dir = os.path.dirname(os.path.abspath(__file__)) |
| 66 | + parser = optparse.OptionParser(usage='%prog [options] <project> <new rev>', |
| 67 | + description=sys.modules[__name__].__doc__) |
| 68 | + parser.add_option('-v', '--verbose', action='count', default=0) |
| 69 | + parser.add_option('--dry-run', action='store_true') |
| 70 | + parser.add_option('-f', '--force', action='store_true', |
| 71 | + help='Make destructive changes to the local checkout if ' |
| 72 | + 'necessary.') |
| 73 | + parser.add_option('--commit', action='store_true', default=True, |
| 74 | + help='(default) Put change in commit queue on upload.') |
| 75 | + parser.add_option('--no-commit', action='store_false', dest='commit', |
| 76 | + help='Don\'t put change in commit queue on upload.') |
| 77 | + parser.add_option('-r', '--reviewers', default='', |
| 78 | + help='Add given users as either reviewers or TBR as' |
| 79 | + ' appropriate.') |
| 80 | + parser.add_option('--upstream', default='origin/master', |
| 81 | + help='(default "%default") Use given start point for change' |
| 82 | + ' to upload. For instance, if you use the old git workflow,' |
| 83 | + ' you might set it to "origin/trunk".') |
| 84 | + parser.add_option('--cc', help='CC email addresses for issue.') |
| 85 | + parser.add_option('-m', '--message', help='Custom commit message.') |
| 86 | + |
| 87 | + options, args = parser.parse_args() |
| 88 | + logging.basicConfig( |
| 89 | + level= |
| 90 | + [logging.WARNING, logging.INFO, logging.DEBUG][ |
| 91 | + min(2, options.verbose)]) |
| 92 | + #if len(args) != 2: |
| 93 | + # parser.print_help() |
| 94 | + # exit(0) |
| 95 | + |
| 96 | + root_dir = os.path.dirname(tool_dir) |
| 97 | + os.chdir(root_dir) |
| 98 | + |
| 99 | + projects = [('src', '.'), ('v8', 'v8'), ('node', 'third_party/node-nw')] |
| 100 | + for project, path in projects: |
| 101 | + os.chdir(os.path.join(SRC_DIR, path)) |
| 102 | + new_rev = subprocess.check_output(['git', 'rev-parse', 'HEAD'], shell=IS_WINDOWS).rstrip() |
| 103 | + |
| 104 | + # Silence the editor. |
| 105 | + os.environ['EDITOR'] = 'true' |
| 106 | + |
| 107 | + old_rev = process_deps(os.path.join(root_dir, 'DEPS'), 'nw_' + project, new_rev, |
| 108 | + options.dry_run) |
| 109 | + if old_rev == new_rev: |
| 110 | + continue |
| 111 | + print '%s roll %s:%s' % (project, old_rev, new_rev) |
| 112 | + commit_msg = subprocess.check_output(['git', 'log', '-1', '--format=%s'], shell=IS_WINDOWS).rstrip() |
| 113 | + commit_msg = project + ": " + commit_msg |
| 114 | + os.chdir(root_dir) |
| 115 | + subprocess.check_output(['git', 'add', 'DEPS'], shell=IS_WINDOWS) |
| 116 | + subprocess.check_output(['git', 'commit', '--quiet', '-m', commit_msg], shell=IS_WINDOWS) |
| 117 | + break |
| 118 | + return 0 |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + sys.exit(main()) |
0 commit comments