blob: 739412387963acf50d84c38c236b7dca402c8993 [file] [log] [blame]
[email protected]3b54b072014-05-23 14:11:411#!/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
7it to Rietveld so that the CL will land automatically if it passes the
8commit-queue's checks.
9"""
10
11import logging
12import optparse
13import os
14import re
15import sys
16
mcgrathrb729cfa2015-10-26 22:07:5117SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
18SRC_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir))
19sys.path.insert(0, os.path.join(SRC_DIR, 'build'))
[email protected]3b54b072014-05-23 14:11:4120import find_depot_tools
21import scm
22import subprocess2
23
24
25def die_with_error(msg):
26 print >> sys.stderr, msg
27 sys.exit(1)
28
29
30def process_deps(path, project, new_rev, is_dry_run):
31 """Update project_revision to |new_issue|.
32
33 A bit hacky, could it be made better?
34 """
35 content = open(path).read()
36 # Hack for Blink to get the AutoRollBot running again.
37 if project == "blink":
38 project = "webkit"
mlamouri59de5c912014-08-29 14:05:5039 old_line = r"(\s+)'%s_revision': '([0-9a-f]{2,40})'," % project
40 new_line = r"\1'%s_revision': '%s'," % (project, new_rev)
[email protected]3b54b072014-05-23 14:11:4141 new_content = re.sub(old_line, new_line, content, 1)
42 old_rev = re.search(old_line, content).group(2)
43 if not old_rev or new_content == content:
44 die_with_error('Failed to update the DEPS file')
45
46 if not is_dry_run:
47 open(path, 'w').write(new_content)
48 return old_rev
49
50
51class PrintSubprocess(object):
52 """Wrapper for subprocess2 which prints out every command."""
53 def __getattr__(self, attr):
54 def _run_subprocess2(cmd, *args, **kwargs):
55 print cmd
56 sys.stdout.flush()
57 return getattr(subprocess2, attr)(cmd, *args, **kwargs)
58 return _run_subprocess2
59
60prnt_subprocess = PrintSubprocess()
61
62
63def main():
64 tool_dir = os.path.dirname(os.path.abspath(__file__))
65 parser = optparse.OptionParser(usage='%prog [options] <project> <new rev>',
66 description=sys.modules[__name__].__doc__)
67 parser.add_option('-v', '--verbose', action='count', default=0)
68 parser.add_option('--dry-run', action='store_true')
69 parser.add_option('-f', '--force', action='store_true',
70 help='Make destructive changes to the local checkout if '
71 'necessary.')
72 parser.add_option('--commit', action='store_true', default=True,
73 help='(default) Put change in commit queue on upload.')
74 parser.add_option('--no-commit', action='store_false', dest='commit',
75 help='Don\'t put change in commit queue on upload.')
76 parser.add_option('-r', '--reviewers', default='',
77 help='Add given users as either reviewers or TBR as'
78 ' appropriate.')
79 parser.add_option('--upstream', default='origin/master',
80 help='(default "%default") Use given start point for change'
81 ' to upload. For instance, if you use the old git workflow,'
82 ' you might set it to "origin/trunk".')
83 parser.add_option('--cc', help='CC email addresses for issue.')
84 parser.add_option('-m', '--message', help='Custom commit message.')
85
86 options, args = parser.parse_args()
87 logging.basicConfig(
88 level=
89 [logging.WARNING, logging.INFO, logging.DEBUG][
90 min(2, options.verbose)])
91 if len(args) != 2:
92 parser.print_help()
93 exit(0)
94
95 root_dir = os.path.dirname(tool_dir)
96 os.chdir(root_dir)
97
98 project = args[0]
99 new_rev = args[1]
100
101 # Silence the editor.
102 os.environ['EDITOR'] = 'true'
103
104 if options.force and not options.dry_run:
105 prnt_subprocess.check_call(['git', 'clean', '-d', '-f'])
106 prnt_subprocess.call(['git', 'rebase', '--abort'])
107
108 old_branch = scm.GIT.GetBranch(root_dir)
109 new_branch = '%s_roll' % project
110
111 if options.upstream == new_branch:
112 parser.error('Cannot set %s as its own upstream.' % new_branch)
113
114 if old_branch == new_branch:
115 if options.force:
116 if not options.dry_run:
117 prnt_subprocess.check_call(['git', 'checkout', options.upstream, '-f'])
118 prnt_subprocess.call(['git', 'branch', '-D', old_branch])
119 else:
120 parser.error('Please delete the branch %s and move to a different branch'
121 % new_branch)
122
123 if not options.dry_run:
124 prnt_subprocess.check_call(['git', 'fetch', 'origin'])
125 prnt_subprocess.call(['git', 'svn', 'fetch'])
126 branch_cmd = ['git', 'checkout', '-b', new_branch, options.upstream]
127 if options.force:
128 branch_cmd.append('-f')
129 prnt_subprocess.check_output(branch_cmd)
130
131 try:
132 old_rev = process_deps(os.path.join(root_dir, 'DEPS'), project, new_rev,
133 options.dry_run)
134 print '%s roll %s:%s' % (project.title(), old_rev, new_rev)
135
136 review_field = 'TBR' if options.commit else 'R'
137 commit_msg = options.message or '%s roll %s:%s\n' % (project.title(),
138 old_rev, new_rev)
139 commit_msg += '\n%s=%s\n' % (review_field, options.reviewers)
140
141 if options.dry_run:
142 print 'Commit message: ' + commit_msg
143 return 0
144
145 prnt_subprocess.check_output(['git', 'commit', '-m', commit_msg, 'DEPS'])
146 prnt_subprocess.check_call(['git', 'diff', '--no-ext-diff',
147 options.upstream])
[email protected]b19e9e572014-06-02 18:22:02148 upload_cmd = ['git', 'cl', 'upload', '--bypass-hooks']
[email protected]3b54b072014-05-23 14:11:41149 if options.commit:
150 upload_cmd.append('--use-commit-queue')
151 if options.reviewers:
152 upload_cmd.append('--send-mail')
153 if options.cc:
154 upload_cmd.extend(['--cc', options.cc])
155 prnt_subprocess.check_call(upload_cmd)
156 finally:
157 if not options.dry_run:
158 prnt_subprocess.check_output(['git', 'checkout', old_branch])
159 prnt_subprocess.check_output(['git', 'branch', '-D', new_branch])
160 return 0
161
162
163if __name__ == '__main__':
164 sys.exit(main())