blob: c06bb4187ab2d7a91bc10dbe39674ecbf88f9a1b [file] [log] [blame]
[email protected]61e0b692011-04-12 21:01:011#!/usr/bin/env python
[email protected]34f68552012-05-09 19:18:362# Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]61e0b692011-04-12 21:01:013# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Applies an issue from Rietveld.
7"""
[email protected]a3d7c4b2013-11-20 02:14:088import json
[email protected]61e0b692011-04-12 21:01:019import logging
10import optparse
[email protected]3cdb7f32011-05-05 16:37:2411import os
[email protected]1833e572012-09-17 13:21:1312import subprocess
[email protected]61e0b692011-04-12 21:01:0113import sys
[email protected]9dbbe542012-08-31 15:33:2314import urllib2
[email protected]61e0b692011-04-12 21:01:0115
[email protected]3cdb7f32011-05-05 16:37:2416
[email protected]a3d7c4b2013-11-20 02:14:0817import annotated_gclient
[email protected]cf6a5d22015-04-09 22:02:0018import auth
[email protected]3cdb7f32011-05-05 16:37:2419import checkout
[email protected]61e0b692011-04-12 21:01:0120import fix_encoding
[email protected]1833e572012-09-17 13:21:1321import gclient_utils
[email protected]61e0b692011-04-12 21:01:0122import rietveld
[email protected]3cdb7f32011-05-05 16:37:2423import scm
[email protected]61e0b692011-04-12 21:01:0124
[email protected]6afd0c32012-09-18 10:34:2525BASE_DIR = os.path.dirname(os.path.abspath(__file__))
26
[email protected]61e0b692011-04-12 21:01:0127
[email protected]69c68c32015-10-05 21:54:2928RETURN_CODE_OK = 0
[email protected]cd958052015-10-05 19:17:3729RETURN_CODE_OTHER_FAILURE = 1 # any other failure, likely patch apply one.
30RETURN_CODE_ARGPARSE_FAILURE = 2 # default in python.
31RETURN_CODE_INFRA_FAILURE = 3 # considered as infra failure.
[email protected]15af77d2015-10-05 15:59:3232
33
[email protected]fc490ff2012-11-07 20:02:0134class Unbuffered(object):
35 """Disable buffering on a file object."""
36 def __init__(self, stream):
37 self.stream = stream
38
39 def write(self, data):
40 self.stream.write(data)
41 self.stream.flush()
42
43 def __getattr__(self, attr):
44 return getattr(self.stream, attr)
45
46
[email protected]15af77d2015-10-05 15:59:3247def _get_arg_parser():
[email protected]61e0b692011-04-12 21:01:0148 parser = optparse.OptionParser(description=sys.modules[__name__].__doc__)
49 parser.add_option(
[email protected]58fe6622011-06-03 20:59:2750 '-v', '--verbose', action='count', default=0,
51 help='Prints debugging infos')
[email protected]61e0b692011-04-12 21:01:0152 parser.add_option(
[email protected]9d5f4ad2012-10-01 15:03:2753 '-e', '--email',
[email protected]33578fe2012-09-18 14:49:4354 help='Email address to access rietveld. If not specified, anonymous '
55 'access will be used.')
56 parser.add_option(
[email protected]99798242014-03-26 18:44:4357 '-E', '--email-file',
58 help='File containing the email address to access rietveld. '
59 'If not specified, anonymous access will be used.')
60 parser.add_option(
[email protected]99798242014-03-26 18:44:4361 '-k', '--private-key-file',
62 help='Path to file containing a private key in p12 format for OAuth2 '
[email protected]cf6a5d22015-04-09 22:02:0063 'authentication with "notasecret" password (as generated by Google '
64 'Cloud Console).')
[email protected]ed233252012-07-06 17:25:1165 parser.add_option(
[email protected]61e0b692011-04-12 21:01:0166 '-i', '--issue', type='int', help='Rietveld issue number')
67 parser.add_option(
68 '-p', '--patchset', type='int', help='Rietveld issue\'s patchset number')
69 parser.add_option(
70 '-r',
71 '--root_dir',
[email protected]3cdb7f32011-05-05 16:37:2472 default=os.getcwd(),
[email protected]61e0b692011-04-12 21:01:0173 help='Root directory to apply the patch')
74 parser.add_option(
75 '-s',
76 '--server',
[email protected]61e0b692011-04-12 21:01:0177 default='http://codereview.chromium.org',
78 help='Rietveld server')
[email protected]94145992013-07-27 02:32:1179 parser.add_option('--no-auth', action='store_true',
80 help='Do not attempt authenticated requests.')
[email protected]a3d7c4b2013-11-20 02:14:0881 parser.add_option('--revision-mapping', default='{}',
82 help='When running gclient, annotate the got_revisions '
83 'using the revision-mapping.')
[email protected]d067d812014-02-21 02:23:1484 parser.add_option('-f', '--force', action='store_true',
85 help='Really run apply_issue, even if .update.flag '
86 'is detected.')
[email protected]c4396a12014-05-10 02:19:2787 parser.add_option('-b', '--base_ref', help='DEPRECATED do not use.')
[email protected]b1d0cc12014-05-06 19:46:1488 parser.add_option('--whitelist', action='append', default=[],
89 help='Patch only specified file(s).')
90 parser.add_option('--blacklist', action='append', default=[],
91 help='Don\'t patch specified file(s).')
[email protected]98e82e12014-04-21 18:47:3292 parser.add_option('-d', '--ignore_deps', action='store_true',
93 help='Don\'t run gclient sync on DEPS changes.')
[email protected]d461faa2016-05-17 07:15:4194 parser.add_option('--extra_patchlevel', type='int',
95 help='Number of directories the patch level number should '
96 'be incremented (useful for patches from repos with '
97 'different directory hierarchies).')
[email protected]cf6a5d22015-04-09 22:02:0098
99 auth.add_auth_options(parser)
[email protected]15af77d2015-10-05 15:59:32100 return parser
101
102
103def main():
104 # TODO(pgervais,tandrii): split this func, it's still too long.
105 sys.stdout = Unbuffered(sys.stdout)
106 parser = _get_arg_parser()
[email protected]61e0b692011-04-12 21:01:01107 options, args = parser.parse_args()
[email protected]cf6a5d22015-04-09 22:02:00108 auth_config = auth.extract_auth_config_from_options(options)
[email protected]99798242014-03-26 18:44:43109
[email protected]b1d0cc12014-05-06 19:46:14110 if options.whitelist and options.blacklist:
111 parser.error('Cannot specify both --whitelist and --blacklist')
112
[email protected]99798242014-03-26 18:44:43113 if options.email and options.email_file:
114 parser.error('-e and -E options are incompatible')
115
[email protected]d067d812014-02-21 02:23:14116 if (os.path.isfile(os.path.join(os.getcwd(), 'update.flag'))
117 and not options.force):
118 print 'update.flag file found: bot_update has run and checkout is already '
119 print 'in a consistent state. No actions will be performed in this step.'
120 return 0
[email protected]15af77d2015-10-05 15:59:32121
[email protected]58fe6622011-06-03 20:59:27122 logging.basicConfig(
[email protected]5e975632011-09-29 18:07:06123 format='%(levelname)5s %(module)11s(%(lineno)4d): %(message)s',
[email protected]58fe6622011-06-03 20:59:27124 level=[logging.WARNING, logging.INFO, logging.DEBUG][
125 min(2, options.verbose)])
[email protected]61e0b692011-04-12 21:01:01126 if args:
127 parser.error('Extra argument(s) "%s" not understood' % ' '.join(args))
128 if not options.issue:
129 parser.error('Require --issue')
[email protected]9dbbe542012-08-31 15:33:23130 options.server = options.server.rstrip('/')
131 if not options.server:
132 parser.error('Require a valid server')
[email protected]61e0b692011-04-12 21:01:01133
[email protected]a3d7c4b2013-11-20 02:14:08134 options.revision_mapping = json.loads(options.revision_mapping)
135
[email protected]99798242014-03-26 18:44:43136 # read email if needed
137 if options.email_file:
138 if not os.path.exists(options.email_file):
139 parser.error('file does not exist: %s' % options.email_file)
140 with open(options.email_file, 'rb') as f:
141 options.email = f.read().strip()
142
[email protected]370a6232012-10-22 19:41:31143 print('Connecting to %s' % options.server)
[email protected]99798242014-03-26 18:44:43144 # Always try un-authenticated first, except for OAuth2
145 if options.private_key_file:
146 # OAuth2 authentication
[email protected]15af77d2015-10-05 15:59:32147 rietveld_obj = rietveld.JwtOAuth2Rietveld(options.server,
[email protected]99798242014-03-26 18:44:43148 options.email,
[email protected]cf6a5d22015-04-09 22:02:00149 options.private_key_file)
[email protected]15af77d2015-10-05 15:59:32150 try:
151 properties = rietveld_obj.get_issue_properties(options.issue, False)
152 except urllib2.URLError:
153 logging.exception('failed to fetch issue properties')
154 sys.exit(RETURN_CODE_INFRA_FAILURE)
[email protected]99798242014-03-26 18:44:43155 else:
[email protected]cf6a5d22015-04-09 22:02:00156 # Passing None as auth_config disables authentication.
[email protected]15af77d2015-10-05 15:59:32157 rietveld_obj = rietveld.Rietveld(options.server, None)
[email protected]99798242014-03-26 18:44:43158 properties = None
159 # Bad except clauses order (HTTPError is an ancestor class of
160 # ClientLoginError)
Quinten Yearsleyb2cc4a92016-12-15 21:53:26161 # pylint: disable=bad-except-order
[email protected]99798242014-03-26 18:44:43162 try:
[email protected]15af77d2015-10-05 15:59:32163 properties = rietveld_obj.get_issue_properties(options.issue, False)
[email protected]99798242014-03-26 18:44:43164 except urllib2.HTTPError as e:
165 if e.getcode() != 302:
166 raise
167 if options.no_auth:
168 exit('FAIL: Login detected -- is issue private?')
169 # TODO(maruel): A few 'Invalid username or password.' are printed first,
170 # we should get rid of those.
[email protected]15af77d2015-10-05 15:59:32171 except urllib2.URLError:
172 logging.exception('failed to fetch issue properties')
173 return RETURN_CODE_INFRA_FAILURE
[email protected]cf6a5d22015-04-09 22:02:00174 except rietveld.upload.ClientLoginError as e:
[email protected]99798242014-03-26 18:44:43175 # Fine, we'll do proper authentication.
176 pass
177 if properties is None:
[email protected]15af77d2015-10-05 15:59:32178 rietveld_obj = rietveld.Rietveld(options.server, auth_config,
179 options.email)
[email protected]cf6a5d22015-04-09 22:02:00180 try:
[email protected]15af77d2015-10-05 15:59:32181 properties = rietveld_obj.get_issue_properties(options.issue, False)
[email protected]cf6a5d22015-04-09 22:02:00182 except rietveld.upload.ClientLoginError as e:
183 print('Accessing the issue requires proper credentials.')
[email protected]cd958052015-10-05 19:17:37184 return RETURN_CODE_OTHER_FAILURE
[email protected]15af77d2015-10-05 15:59:32185 except urllib2.URLError:
186 logging.exception('failed to fetch issue properties')
187 return RETURN_CODE_INFRA_FAILURE
[email protected]61e0b692011-04-12 21:01:01188
189 if not options.patchset:
[email protected]3bf4b3c2012-09-03 15:53:02190 options.patchset = properties['patchsets'][-1]
[email protected]38ab82d2012-08-29 19:34:45191 print('No patchset specified. Using patchset %d' % options.patchset)
192
[email protected]d91b7e32015-06-23 11:24:07193 issues_patchsets_to_apply = [(options.issue, options.patchset)]
[email protected]15af77d2015-10-05 15:59:32194 try:
195 depends_on_info = rietveld_obj.get_depends_on_patchset(
196 options.issue, options.patchset)
197 except urllib2.URLError:
198 logging.exception('failed to fetch depends_on_patchset')
199 return RETURN_CODE_INFRA_FAILURE
200
[email protected]56445e82015-07-15 12:04:42201 while depends_on_info:
202 depends_on_issue = int(depends_on_info['issue'])
203 depends_on_patchset = int(depends_on_info['patchset'])
204 try:
[email protected]15af77d2015-10-05 15:59:32205 depends_on_info = rietveld_obj.get_depends_on_patchset(depends_on_issue,
[email protected]56445e82015-07-15 12:04:42206 depends_on_patchset)
207 issues_patchsets_to_apply.insert(0, (depends_on_issue,
208 depends_on_patchset))
209 except urllib2.HTTPError:
210 print ('The patchset that was marked as a dependency no longer '
211 'exists: %s/%d/#ps%d' % (
212 options.server, depends_on_issue, depends_on_patchset))
213 print 'Therefore it is likely that this patch will not apply cleanly.'
214 print
215 depends_on_info = None
[email protected]15af77d2015-10-05 15:59:32216 except urllib2.URLError:
217 logging.exception('failed to fetch dependency issue')
218 return RETURN_CODE_INFRA_FAILURE
[email protected]3cdb7f32011-05-05 16:37:24219
[email protected]d91b7e32015-06-23 11:24:07220 num_issues_patchsets_to_apply = len(issues_patchsets_to_apply)
221 if num_issues_patchsets_to_apply > 1:
222 print
223 print 'apply_issue.py found %d dependent CLs.' % (
224 num_issues_patchsets_to_apply - 1)
225 print 'They will be applied in the following order:'
226 num = 1
227 for issue_to_apply, patchset_to_apply in issues_patchsets_to_apply:
228 print ' #%d %s/%d/#ps%d' % (
229 num, options.server, issue_to_apply, patchset_to_apply)
230 num += 1
231 print
[email protected]9d72d2b2012-09-07 22:23:21232
[email protected]d91b7e32015-06-23 11:24:07233 for issue_to_apply, patchset_to_apply in issues_patchsets_to_apply:
234 issue_url = '%s/%d/#ps%d' % (options.server, issue_to_apply,
235 patchset_to_apply)
236 print('Downloading patch from %s' % issue_url)
237 try:
[email protected]15af77d2015-10-05 15:59:32238 patchset = rietveld_obj.get_patch(issue_to_apply, patchset_to_apply)
[email protected]95a38492015-10-02 17:21:52239 except urllib2.HTTPError:
[email protected]d91b7e32015-06-23 11:24:07240 print(
241 'Failed to fetch the patch for issue %d, patchset %d.\n'
242 'Try visiting %s/%d') % (
243 issue_to_apply, patchset_to_apply,
244 options.server, issue_to_apply)
[email protected]15af77d2015-10-05 15:59:32245 # If we got this far, then this is likely missing patchset.
246 # Thus, it's not infra failure.
[email protected]cd958052015-10-05 19:17:37247 return RETURN_CODE_OTHER_FAILURE
[email protected]15af77d2015-10-05 15:59:32248 except urllib2.URLError:
249 logging.exception(
250 'Failed to fetch the patch for issue %d, patchset %d',
251 issue_to_apply, patchset_to_apply)
252 return RETURN_CODE_INFRA_FAILURE
[email protected]d91b7e32015-06-23 11:24:07253 if options.whitelist:
254 patchset.patches = [patch for patch in patchset.patches
255 if patch.filename in options.whitelist]
256 if options.blacklist:
257 patchset.patches = [patch for patch in patchset.patches
258 if patch.filename not in options.blacklist]
259 for patch in patchset.patches:
260 print(patch)
[email protected]d461faa2016-05-17 07:15:41261 if options.extra_patchlevel:
262 patch.patchlevel += options.extra_patchlevel
[email protected]d91b7e32015-06-23 11:24:07263 full_dir = os.path.abspath(options.root_dir)
264 scm_type = scm.determine_scm(full_dir)
agable037edb82016-10-24 21:35:12265 if scm_type == 'git':
[email protected]d91b7e32015-06-23 11:24:07266 scm_obj = checkout.GitCheckout(full_dir, None, None, None, None)
[email protected]d91b7e32015-06-23 11:24:07267 else:
268 parser.error('Couldn\'t determine the scm')
269
[email protected]d91b7e32015-06-23 11:24:07270 print('\nApplying the patch from %s' % issue_url)
271 try:
272 scm_obj.apply_patch(patchset, verbose=True)
273 except checkout.PatchApplicationFailed as e:
274 print(str(e))
275 print('CWD=%s' % os.getcwd())
276 print('Checkout path=%s' % scm_obj.project_path)
[email protected]cd958052015-10-05 19:17:37277 return RETURN_CODE_OTHER_FAILURE
[email protected]1833e572012-09-17 13:21:13278
[email protected]98e82e12014-04-21 18:47:32279 if ('DEPS' in map(os.path.basename, patchset.filenames)
280 and not options.ignore_deps):
[email protected]0aca0f92012-10-01 16:39:45281 gclient_root = gclient_utils.FindGclientRoot(full_dir)
[email protected]1833e572012-09-17 13:21:13282 if gclient_root and scm_type:
283 print(
284 'A DEPS file was updated inside a gclient checkout, running gclient '
285 'sync.')
[email protected]6afd0c32012-09-18 10:34:25286 gclient_path = os.path.join(BASE_DIR, 'gclient')
287 if sys.platform == 'win32':
288 gclient_path += '.bat'
[email protected]a3d7c4b2013-11-20 02:14:08289 with annotated_gclient.temp_filename(suffix='gclient') as f:
290 cmd = [
[email protected]7e79d372013-04-29 18:01:49291 gclient_path, 'sync',
[email protected]7e79d372013-04-29 18:01:49292 '--nohooks',
293 '--delete_unversioned_trees',
[email protected]a3d7c4b2013-11-20 02:14:08294 ]
295 if options.revision_mapping:
296 cmd.extend(['--output-json', f])
297
298 retcode = subprocess.call(cmd, cwd=gclient_root)
299
300 if retcode == 0 and options.revision_mapping:
301 revisions = annotated_gclient.parse_got_revision(
302 f, options.revision_mapping)
303 annotated_gclient.emit_buildprops(revisions)
304
305 return retcode
[email protected]cd958052015-10-05 19:17:37306 return RETURN_CODE_OK
[email protected]61e0b692011-04-12 21:01:01307
308
309if __name__ == "__main__":
310 fix_encoding.fix_encoding()
[email protected]013731e2015-02-26 18:28:43311 try:
312 sys.exit(main())
313 except KeyboardInterrupt:
314 sys.stderr.write('interrupted\n')
[email protected]cd958052015-10-05 19:17:37315 sys.exit(RETURN_CODE_OTHER_FAILURE)