Add unit test for the Settings API Bubble.
[chromium-blink-merge.git] / tools / safely-roll-deps.py
blob7ea202aaa92f8e77a209eb44ed43b03563ececdb
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.
6 """Generate a CL to roll a DEPS entry to the specified revision number and post
7 it to Rietveld so that the CL will land automatically if it passes the
8 commit-queue's checks.
9 """
11 import logging
12 import optparse
13 import os
14 import re
15 import sys
17 import find_depot_tools
18 import scm
19 import subprocess2
22 def die_with_error(msg):
23 print >> sys.stderr, msg
24 sys.exit(1)
27 def process_deps(path, project, new_rev, is_dry_run):
28 """Update project_revision to |new_issue|.
30 A bit hacky, could it be made better?
31 """
32 content = open(path).read()
33 # Hack for Blink to get the AutoRollBot running again.
34 if project == "blink":
35 project = "webkit"
36 old_line = r'(\s+)"%s_revision": "(\d+)",' % project
37 new_line = r'\1"%s_revision": "%d",' % (project, new_rev)
38 new_content = re.sub(old_line, new_line, content, 1)
39 old_rev = re.search(old_line, content).group(2)
40 if not old_rev or new_content == content:
41 die_with_error('Failed to update the DEPS file')
43 if not is_dry_run:
44 open(path, 'w').write(new_content)
45 return old_rev
48 def main():
49 tool_dir = os.path.dirname(os.path.abspath(__file__))
50 parser = optparse.OptionParser(usage='%prog [options] <project> <new rev>',
51 description=sys.modules[__name__].__doc__)
52 parser.add_option('-v', '--verbose', action='count', default=0)
53 parser.add_option('--dry-run', action='store_true')
54 parser.add_option('--commit', action='store_true', default=True,
55 help='(default) Put change in commit queue on upload.')
56 parser.add_option('--no-commit', action='store_false', dest='commit',
57 help='Don\'t put change in commit queue on upload.')
58 parser.add_option('-r', '--reviewers', default='',
59 help='Add given users as either reviewers or TBR as'
60 ' appropriate.')
61 parser.add_option('--upstream', default='origin/master',
62 help='(default "%default") Use given start point for change'
63 ' to upload. For instance, if you use the old git workflow,'
64 ' you might set it to "origin/trunk".')
65 parser.add_option('--cc', help='CC email addresses for issue.')
66 parser.add_option('-m', '--message', help='Custom commit message.')
68 options, args = parser.parse_args()
69 logging.basicConfig(
70 level=
71 [logging.WARNING, logging.INFO, logging.DEBUG][
72 min(2, options.verbose)])
73 if len(args) != 2:
74 parser.print_help()
75 exit(0)
77 root_dir = os.path.dirname(tool_dir)
78 os.chdir(root_dir)
80 project = args[0]
81 new_rev = int(args[1])
83 # Silence the editor.
84 os.environ['EDITOR'] = 'true'
86 old_branch = scm.GIT.GetBranch(root_dir)
87 new_branch = '%s_roll' % project
88 if old_branch == new_branch:
89 parser.error('Please delete the branch %s and move to a different branch'
90 % new_branch)
92 if not options.dry_run:
93 subprocess2.check_output(
94 ['git', 'checkout', '-b', new_branch, options.upstream])
96 try:
97 old_rev = int(process_deps(os.path.join(root_dir, 'DEPS'), project, new_rev,
98 options.dry_run))
99 print '%s roll %s:%s' % (project.title(), old_rev, new_rev)
101 review_field = 'TBR' if options.commit else 'R'
102 commit_msg = options.message or (
103 '%s roll %s:%s\n'
104 '\n'
105 '%s=%s\n' % (project.title(), old_rev, new_rev,
106 review_field, options.reviewers))
108 if options.dry_run:
109 print 'Commit message: ' + commit_msg
110 return 0
112 subprocess2.check_output(['git', 'commit', '-m', commit_msg, 'DEPS'])
113 subprocess2.check_call(['git', 'diff', '--no-ext-diff', options.upstream])
114 upload_cmd = ['git', 'cl', 'upload']
115 if options.commit:
116 upload_cmd.append('--use-commit-queue')
117 if options.reviewers:
118 upload_cmd.append('--send-mail')
119 if options.cc:
120 upload_cmd.extend(['--cc', options.cc])
121 subprocess2.check_call(upload_cmd)
122 finally:
123 if not options.dry_run:
124 subprocess2.check_output(['git', 'checkout', old_branch])
125 subprocess2.check_output(['git', 'branch', '-D', new_branch])
126 return 0
129 if __name__ == '__main__':
130 sys.exit(main())