OLD | NEW |
| (Empty) |
1 #!/usr/bin/python2 | |
2 | |
3 # Copyright 2014 Google Inc. | |
4 # | |
5 # Use of this source code is governed by a BSD-style license that can be | |
6 # found in the LICENSE file. | |
7 | |
8 """Skia's Chromium DEPS roll script. | |
9 | |
10 This script: | |
11 - searches through the last N Skia git commits to find out the hash that is | |
12 associated with the SVN revision number. | |
13 - creates a new branch in the Chromium tree, modifies the DEPS file to | |
14 point at the given Skia commit, commits, uploads to Rietveld, and | |
15 deletes the local copy of the branch. | |
16 - creates a whitespace-only commit and uploads that to to Rietveld. | |
17 - returns the Chromium tree to its previous state. | |
18 | |
19 To specify the location of the git executable, set the GIT_EXECUTABLE | |
20 environment variable. | |
21 | |
22 Usage: | |
23 %prog -c CHROMIUM_PATH -r REVISION [OPTIONAL_OPTIONS] | |
24 """ | |
25 | |
26 | |
27 import optparse | |
28 import os | |
29 import re | |
30 import shutil | |
31 import sys | |
32 import tempfile | |
33 | |
34 import fix_pythonpath # pylint: disable=W0611 | |
35 from common.py.utils import git_utils | |
36 from common.py.utils import misc | |
37 from common.py.utils import shell_utils | |
38 | |
39 | |
40 DEFAULT_BOTS_LIST = [ | |
41 'android_clang_dbg', | |
42 'android_dbg', | |
43 'android_rel', | |
44 'cros_daisy', | |
45 'linux', | |
46 'linux_asan', | |
47 'linux_chromeos', | |
48 'linux_chromeos_asan', | |
49 'linux_chromium_gn_dbg', | |
50 'linux_gpu', | |
51 'linux_layout', | |
52 'linux_layout_rel', | |
53 'mac', | |
54 'mac_asan', | |
55 'mac_layout', | |
56 'mac_layout_rel', | |
57 'win', | |
58 'win_gpu', | |
59 'win_layout', | |
60 'win_layout_rel', | |
61 ] | |
62 | |
63 REGEXP_SKIA_REVISION = ( | |
64 r'^ "skia_revision": "(?P<revision>[0-9a-fA-F]{2,40})",$') | |
65 | |
66 | |
67 class DepsRollConfig(object): | |
68 """Contains configuration options for this module. | |
69 | |
70 Attributes: | |
71 chromium_path: (string) path to a local chromium git repository. | |
72 save_branches: (boolean) iff false, delete temporary branches. | |
73 verbose: (boolean) iff false, suppress the output from git-cl. | |
74 skip_cl_upload: (boolean) | |
75 cl_bot_list: (list of strings) | |
76 """ | |
77 | |
78 # pylint: disable=I0011,R0903,R0902 | |
79 def __init__(self, options=None): | |
80 if not options: | |
81 options = DepsRollConfig.GetOptionParser() | |
82 # pylint: disable=I0011,E1103 | |
83 self.verbose = options.verbose | |
84 self.save_branches = not options.delete_branches | |
85 self.chromium_path = options.chromium_path | |
86 self.skip_cl_upload = options.skip_cl_upload | |
87 # Split and remove empty strigns from the bot list. | |
88 self.cl_bot_list = [bot for bot in options.bots.split(',') if bot] | |
89 self.default_branch_name = 'autogenerated_deps_roll_branch' | |
90 self.reviewers_list = ','.join([ | |
91 # 'rmistry@google.com', | |
92 # 'reed@google.com', | |
93 # 'bsalomon@google.com', | |
94 # 'robertphillips@google.com', | |
95 ]) | |
96 self.cc_list = ','.join([ | |
97 # 'skia-team@google.com', | |
98 ]) | |
99 | |
100 @staticmethod | |
101 def GetOptionParser(): | |
102 # pylint: disable=I0011,C0103 | |
103 """Returns an optparse.OptionParser object. | |
104 | |
105 Returns: | |
106 An optparse.OptionParser object. | |
107 | |
108 Called by the main() function. | |
109 """ | |
110 option_parser = optparse.OptionParser(usage=__doc__) | |
111 # Anyone using this script on a regular basis should set the | |
112 # CHROMIUM_CHECKOUT_PATH environment variable. | |
113 option_parser.add_option( | |
114 '-c', '--chromium_path', help='Path to local Chromium Git' | |
115 ' repository checkout, defaults to CHROMIUM_CHECKOUT_PATH' | |
116 ' if that environment variable is set.', | |
117 default=os.environ.get('CHROMIUM_CHECKOUT_PATH')) | |
118 option_parser.add_option( | |
119 '-r', '--revision', default=None, | |
120 help='The Skia Git commit hash.') | |
121 | |
122 option_parser.add_option( | |
123 '', '--delete_branches', help='Delete the temporary branches', | |
124 action='store_true', dest='delete_branches', default=False) | |
125 option_parser.add_option( | |
126 '', '--verbose', help='Do not suppress the output from `git cl`.', | |
127 action='store_true', dest='verbose', default=False) | |
128 option_parser.add_option( | |
129 '', '--skip_cl_upload', help='Skip the cl upload step; useful' | |
130 ' for testing.', | |
131 action='store_true', default=False) | |
132 | |
133 default_bots_help = ( | |
134 'Comma-separated list of bots, defaults to a list of %d bots.' | |
135 ' To skip `git cl try`, set this to an empty string.' | |
136 % len(DEFAULT_BOTS_LIST)) | |
137 default_bots = ','.join(DEFAULT_BOTS_LIST) | |
138 option_parser.add_option( | |
139 '', '--bots', help=default_bots_help, default=default_bots) | |
140 | |
141 return option_parser | |
142 | |
143 | |
144 class DepsRollError(Exception): | |
145 """Exceptions specific to this module.""" | |
146 pass | |
147 | |
148 | |
149 def change_skia_deps(revision, depspath): | |
150 """Update the DEPS file. | |
151 | |
152 Modify the skia_revision entry in the given DEPS file. | |
153 | |
154 Args: | |
155 revision: (string) Skia commit hash. | |
156 depspath: (string) path to DEPS file. | |
157 """ | |
158 temp_file = tempfile.NamedTemporaryFile(delete=False, | |
159 prefix='skia_DEPS_ROLL_tmp_') | |
160 try: | |
161 deps_regex_rev = re.compile(REGEXP_SKIA_REVISION) | |
162 deps_regex_rev_repl = ' "skia_revision": "%s",' % revision | |
163 | |
164 with open(depspath, 'r') as input_stream: | |
165 for line in input_stream: | |
166 line = deps_regex_rev.sub(deps_regex_rev_repl, line) | |
167 temp_file.write(line) | |
168 finally: | |
169 temp_file.close() | |
170 shutil.move(temp_file.name, depspath) | |
171 | |
172 | |
173 def submit_tries(bots_to_run, dry_run=False): | |
174 """Submit try requests for the current branch on the given bots. | |
175 | |
176 Args: | |
177 bots_to_run: (list of strings) bots to request. | |
178 dry_run: (bool) whether to actually submit the try request. | |
179 """ | |
180 git_try = [ | |
181 git_utils.GIT, 'cl', 'try', '-m', 'tryserver.chromium'] | |
182 git_try.extend([arg for bot in bots_to_run for arg in ('-b', bot)]) | |
183 | |
184 if dry_run: | |
185 space = ' ' | |
186 print 'You should call:' | |
187 print space, git_try | |
188 print | |
189 else: | |
190 shell_utils.run(git_try) | |
191 | |
192 | |
193 def roll_deps(config, revision): | |
194 """Upload changed DEPS and a whitespace change. | |
195 | |
196 Given the correct git_hash, create two Reitveld issues. | |
197 | |
198 Args: | |
199 config: (roll_deps.DepsRollConfig) object containing options. | |
200 revision: (string) Skia Git hash. | |
201 | |
202 Returns: | |
203 a tuple containing textual description of the two issues. | |
204 | |
205 Raises: | |
206 OSError: failed to execute git or git-cl. | |
207 subprocess.CalledProcessError: git returned unexpected status. | |
208 """ | |
209 with misc.ChDir(config.chromium_path, verbose=config.verbose): | |
210 git_utils.Fetch() | |
211 output = shell_utils.run([git_utils.GIT, 'show', 'origin/master:DEPS'], | |
212 log_in_real_time=False).rstrip() | |
213 match = re.search(REGEXP_SKIA_REVISION, output, flags=re.MULTILINE) | |
214 old_revision = None | |
215 if match: | |
216 old_revision = match.group('revision') | |
217 assert old_revision | |
218 | |
219 master_hash = git_utils.FullHash('origin/master').rstrip() | |
220 | |
221 # master_hash[8] gives each whitespace CL a unique name. | |
222 branch = 'control_%s' % master_hash[:8] | |
223 message = ('whitespace change %s\n\n' | |
224 'Chromium base revision: %s\n\n' | |
225 'This CL was created by Skia\'s roll_deps.py script.\n' | |
226 ) % (master_hash[:8], master_hash[:8]) | |
227 with git_utils.GitBranch(branch, message, | |
228 delete_when_finished=not config.save_branches, | |
229 upload=not config.skip_cl_upload | |
230 ) as whitespace_branch: | |
231 branch = git_utils.GetCurrentBranch() | |
232 with open(os.path.join('build', 'whitespace_file.txt'), 'a') as f: | |
233 f.write('\nCONTROL\n') | |
234 | |
235 control_url = whitespace_branch.commit_and_upload() | |
236 if config.cl_bot_list: | |
237 submit_tries(config.cl_bot_list, dry_run=config.skip_cl_upload) | |
238 whitespace_cl = control_url | |
239 if config.save_branches: | |
240 whitespace_cl += '\n branch: %s' % branch | |
241 | |
242 branch = 'roll_%s_%s' % (revision, master_hash[:8]) | |
243 message = ( | |
244 'roll skia DEPS to %s\n\n' | |
245 'Chromium base revision: %s\n' | |
246 'Old Skia revision: %s\n' | |
247 'New Skia revision: %s\n' | |
248 'Control CL: %s\n\n' | |
249 'This CL was created by Skia\'s roll_deps.py script.\n\n' | |
250 'Bypassing commit queue trybots:\n' | |
251 'NOTRY=true\n' | |
252 % (revision, master_hash[:8], | |
253 old_revision[:8], revision[:8], control_url)) | |
254 with git_utils.GitBranch(branch, message, | |
255 delete_when_finished=not config.save_branches, | |
256 upload=not config.skip_cl_upload | |
257 ) as roll_branch: | |
258 change_skia_deps(revision, 'DEPS') | |
259 deps_url = roll_branch.commit_and_upload() | |
260 if config.cl_bot_list: | |
261 submit_tries(config.cl_bot_list, dry_run=config.skip_cl_upload) | |
262 deps_cl = deps_url | |
263 if config.save_branches: | |
264 deps_cl += '\n branch: %s' % branch | |
265 | |
266 return deps_cl, whitespace_cl | |
267 | |
268 | |
269 def main(args): | |
270 """main function; see module-level docstring and GetOptionParser help. | |
271 | |
272 Args: | |
273 args: sys.argv[1:]-type argument list. | |
274 """ | |
275 option_parser = DepsRollConfig.GetOptionParser() | |
276 options = option_parser.parse_args(args)[0] | |
277 | |
278 if not options.revision: | |
279 option_parser.error('Must specify a revision.') | |
280 if not options.chromium_path: | |
281 option_parser.error('Must specify chromium_path.') | |
282 if not os.path.isdir(options.chromium_path): | |
283 option_parser.error('chromium_path must be a directory.') | |
284 | |
285 config = DepsRollConfig(options) | |
286 shell_utils.VERBOSE = options.verbose | |
287 deps_issue, whitespace_issue = roll_deps(config, options.revision) | |
288 | |
289 if deps_issue and whitespace_issue: | |
290 print 'DEPS roll:\n %s\n' % deps_issue | |
291 print 'Whitespace change:\n %s\n' % whitespace_issue | |
292 else: | |
293 print >> sys.stderr, 'No issues created.' | |
294 | |
295 | |
296 if __name__ == '__main__': | |
297 main(sys.argv[1:]) | |
OLD | NEW |