blob: 9bda02556124244e47b8c2da4b1049b018e665fa [file] [log] [blame]
Josip Sokcevic84434e82021-06-09 22:48:43 +00001#!/usr/bin/env vpython3
dimu833c94c2017-01-18 17:36:15 -08002# Copyright 2017 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"""Simple client for the Gerrit REST API.
7
8Example usage:
Michael Moss5eebf6f2021-07-16 13:18:34 +00009 ./gerrit_client.py [command] [args]
dimu833c94c2017-01-18 17:36:15 -080010"""
11
Gavin Mak42674f52023-08-24 20:39:59 +000012from __future__ import print_function
13
dimu833c94c2017-01-18 17:36:15 -080014import json
15import logging
16import optparse
17import subcommand
18import sys
Gavin Mak42674f52023-08-24 20:39:59 +000019
20if sys.version_info.major == 2:
21 import urlparse
22 from urllib import quote_plus
23else:
24 from urllib.parse import quote_plus
25 import urllib.parse as urlparse
dimu833c94c2017-01-18 17:36:15 -080026
dimu833c94c2017-01-18 17:36:15 -080027import fix_encoding
28import gerrit_util
29import setup_color
30
31__version__ = '0.1'
dimu833c94c2017-01-18 17:36:15 -080032
33
34def write_result(result, opt):
35 if opt.json_file:
36 with open(opt.json_file, 'w') as json_file:
37 json_file.write(json.dumps(result))
38
39
40@subcommand.usage('[args ...]')
Josip Sokcevicc39ab992020-09-24 20:09:15 +000041def CMDmovechanges(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +000042 """Move changes to a different destination branch."""
Josip Sokcevicc39ab992020-09-24 20:09:15 +000043 parser.add_option('-p', '--param', dest='params', action='append',
44 help='repeatable query parameter, format: -p key=value')
45 parser.add_option('--destination_branch', dest='destination_branch',
46 help='where to move changes to')
47
48 (opt, args) = parser.parse_args(args)
49 assert opt.destination_branch, "--destination_branch not defined"
Mike Frysinger8820ab82020-11-25 00:52:31 +000050 for p in opt.params:
51 assert '=' in p, '--param is key=value, not "%s"' % p
Gavin Mak42674f52023-08-24 20:39:59 +000052 host = urlparse.urlparse(opt.host).netloc
Josip Sokcevicc39ab992020-09-24 20:09:15 +000053
54 limit = 100
55 while True:
56 result = gerrit_util.QueryChanges(
57 host,
58 list(tuple(p.split('=', 1)) for p in opt.params),
59 limit=limit,
60 )
61 for change in result:
62 gerrit_util.MoveChange(host, change['id'], opt.destination_branch)
63
64 if len(result) < limit:
65 break
66 logging.info("Done")
67
68
69@subcommand.usage('[args ...]')
dimu833c94c2017-01-18 17:36:15 -080070def CMDbranchinfo(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +000071 """Get information on a gerrit branch."""
dimu833c94c2017-01-18 17:36:15 -080072 parser.add_option('--branch', dest='branch', help='branch name')
73
74 (opt, args) = parser.parse_args(args)
Gavin Mak42674f52023-08-24 20:39:59 +000075 host = urlparse.urlparse(opt.host).netloc
76 project = quote_plus(opt.project)
77 branch = quote_plus(opt.branch)
dimu833c94c2017-01-18 17:36:15 -080078 result = gerrit_util.GetGerritBranch(host, project, branch)
79 logging.info(result)
80 write_result(result, opt)
81
Quinten Yearsleyd9cbe7a2019-09-03 16:49:11 +000082
dimu833c94c2017-01-18 17:36:15 -080083@subcommand.usage('[args ...]')
Michael Moss9c28af42021-10-25 16:59:05 +000084def CMDrawapi(parser, args):
85 """Call an arbitrary Gerrit REST API endpoint."""
86 parser.add_option('--path', dest='path', help='HTTP path of the API endpoint')
87 parser.add_option('--method', dest='method',
88 help='HTTP method for the API (default: GET)')
89 parser.add_option('--body', dest='body', help='API JSON body contents')
90 parser.add_option('--accept_status',
91 dest='accept_status',
92 help='Comma-delimited list of status codes for success.')
93
94 (opt, args) = parser.parse_args(args)
95 assert opt.path, "--path not defined"
96
Gavin Mak42674f52023-08-24 20:39:59 +000097 host = urlparse.urlparse(opt.host).netloc
Michael Moss9c28af42021-10-25 16:59:05 +000098 kwargs = {}
99 if opt.method:
100 kwargs['reqtype'] = opt.method.upper()
101 if opt.body:
102 kwargs['body'] = json.loads(opt.body)
103 if opt.accept_status:
104 kwargs['accept_statuses'] = [int(x) for x in opt.accept_status.split(',')]
105 result = gerrit_util.CallGerritApi(host, opt.path, **kwargs)
106 logging.info(result)
107 write_result(result, opt)
108
109
110@subcommand.usage('[args ...]')
dimu833c94c2017-01-18 17:36:15 -0800111def CMDbranch(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000112 """Create a branch in a gerrit project."""
dimu833c94c2017-01-18 17:36:15 -0800113 parser.add_option('--branch', dest='branch', help='branch name')
114 parser.add_option('--commit', dest='commit', help='commit hash')
Xinan Lindbcecc92023-05-03 18:29:00 +0000115 parser.add_option('--allow-existent-branch',
116 action='store_true',
117 help=('Accept that the branch alread exists as long as the'
118 ' branch head points the given commit'))
dimu833c94c2017-01-18 17:36:15 -0800119
120 (opt, args) = parser.parse_args(args)
Josip Sokcevicc99efb22020-03-17 00:35:34 +0000121 assert opt.project, "--project not defined"
122 assert opt.branch, "--branch not defined"
123 assert opt.commit, "--commit not defined"
dimu833c94c2017-01-18 17:36:15 -0800124
Gavin Mak42674f52023-08-24 20:39:59 +0000125 project = quote_plus(opt.project)
126 host = urlparse.urlparse(opt.host).netloc
127 branch = quote_plus(opt.branch)
Xinan Lindbcecc92023-05-03 18:29:00 +0000128 result = gerrit_util.GetGerritBranch(host, project, branch)
129 if result:
130 if not opt.allow_existent_branch:
131 raise gerrit_util.GerritError(200, 'Branch already exists')
132 if result.get('revision') != opt.commit:
133 raise gerrit_util.GerritError(
134 200, ('Branch already exists but '
135 'the branch head is not at the given commit'))
136 else:
137 try:
138 result = gerrit_util.CreateGerritBranch(host, project, branch, opt.commit)
139 except gerrit_util.GerritError as e:
140 result = gerrit_util.GetGerritBranch(host, project, branch)
141 if not result:
142 raise e
143 # If reached here, we hit a real conflict error, because the
144 # branch just created is pointing a different commit.
145 if result.get('revision') != opt.commit:
146 raise gerrit_util.GerritError(
147 200, ('Conflict: branch was created but '
148 'the branch head is not at the given commit'))
dimu833c94c2017-01-18 17:36:15 -0800149 logging.info(result)
150 write_result(result, opt)
151
152
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200153@subcommand.usage('[args ...]')
Michael Mossb6ce2442021-10-20 04:36:24 +0000154def CMDtag(parser, args):
155 """Create a tag in a gerrit project."""
156 parser.add_option('--tag', dest='tag', help='tag name')
157 parser.add_option('--commit', dest='commit', help='commit hash')
158
159 (opt, args) = parser.parse_args(args)
160 assert opt.project, "--project not defined"
161 assert opt.tag, "--tag not defined"
162 assert opt.commit, "--commit not defined"
163
Gavin Mak42674f52023-08-24 20:39:59 +0000164 project = quote_plus(opt.project)
165 host = urlparse.urlparse(opt.host).netloc
166 tag = quote_plus(opt.tag)
Michael Moss5c9e8b42021-10-26 16:54:16 +0000167 result = gerrit_util.CreateGerritTag(host, project, tag, opt.commit)
Michael Mossb6ce2442021-10-20 04:36:24 +0000168 logging.info(result)
169 write_result(result, opt)
170
171
172@subcommand.usage('[args ...]')
Josip Sokcevicdf9a8022020-12-08 00:10:19 +0000173def CMDhead(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000174 """Update which branch the project HEAD points to."""
Josip Sokcevicdf9a8022020-12-08 00:10:19 +0000175 parser.add_option('--branch', dest='branch', help='branch name')
176
177 (opt, args) = parser.parse_args(args)
178 assert opt.project, "--project not defined"
179 assert opt.branch, "--branch not defined"
180
Gavin Mak42674f52023-08-24 20:39:59 +0000181 project = quote_plus(opt.project)
182 host = urlparse.urlparse(opt.host).netloc
183 branch = quote_plus(opt.branch)
Josip Sokcevicdf9a8022020-12-08 00:10:19 +0000184 result = gerrit_util.UpdateHead(host, project, branch)
185 logging.info(result)
186 write_result(result, opt)
187
188
189@subcommand.usage('[args ...]')
190def CMDheadinfo(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000191 """Retrieves the current HEAD of the project."""
Josip Sokcevicdf9a8022020-12-08 00:10:19 +0000192
193 (opt, args) = parser.parse_args(args)
194 assert opt.project, "--project not defined"
195
Gavin Mak42674f52023-08-24 20:39:59 +0000196 project = quote_plus(opt.project)
197 host = urlparse.urlparse(opt.host).netloc
Josip Sokcevicdf9a8022020-12-08 00:10:19 +0000198 result = gerrit_util.GetHead(host, project)
199 logging.info(result)
200 write_result(result, opt)
201
202
203@subcommand.usage('[args ...]')
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200204def CMDchanges(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000205 """Queries gerrit for matching changes."""
Mike Frysinger98d3bb22023-06-22 21:35:59 +0000206 parser.add_option('-p',
207 '--param',
208 dest='params',
209 action='append',
210 default=[],
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200211 help='repeatable query parameter, format: -p key=value')
Mike Frysinger98d3bb22023-06-22 21:35:59 +0000212 parser.add_option('--query', help='raw gerrit search query string')
Paweł Hajdan, Jr24025d32017-07-11 16:38:21 +0200213 parser.add_option('-o', '--o-param', dest='o_params', action='append',
214 help='gerrit output parameters, e.g. ALL_REVISIONS')
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200215 parser.add_option('--limit', dest='limit', type=int,
216 help='maximum number of results to return')
217 parser.add_option('--start', dest='start', type=int,
218 help='how many changes to skip '
219 '(starting with the most recent)')
220
221 (opt, args) = parser.parse_args(args)
Mike Frysinger98d3bb22023-06-22 21:35:59 +0000222 assert opt.params or opt.query, '--param or --query required'
Mike Frysinger8820ab82020-11-25 00:52:31 +0000223 for p in opt.params:
224 assert '=' in p, '--param is key=value, not "%s"' % p
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200225
226 result = gerrit_util.QueryChanges(
Gavin Mak42674f52023-08-24 20:39:59 +0000227 urlparse.urlparse(opt.host).netloc,
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200228 list(tuple(p.split('=', 1)) for p in opt.params),
Mike Frysinger98d3bb22023-06-22 21:35:59 +0000229 first_param=opt.query,
230 start=opt.start, # Default: None
231 limit=opt.limit, # Default: None
Paweł Hajdan, Jr24025d32017-07-11 16:38:21 +0200232 o_params=opt.o_params, # Default: None
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200233 )
234 logging.info('Change query returned %d changes.', len(result))
235 write_result(result, opt)
236
237
LaMont Jones9eed4232021-04-02 16:29:49 +0000238@subcommand.usage('[args ...]')
Marco Georgaklis85557a02021-06-03 15:56:54 +0000239def CMDrelatedchanges(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000240 """Gets related changes for a given change and revision."""
Marco Georgaklis85557a02021-06-03 15:56:54 +0000241 parser.add_option('-c', '--change', type=str, help='change id')
242 parser.add_option('-r', '--revision', type=str, help='revision id')
243
244 (opt, args) = parser.parse_args(args)
245
246 result = gerrit_util.GetRelatedChanges(
Gavin Mak42674f52023-08-24 20:39:59 +0000247 urlparse.urlparse(opt.host).netloc,
Marco Georgaklis85557a02021-06-03 15:56:54 +0000248 change=opt.change,
249 revision=opt.revision,
250 )
251 logging.info(result)
252 write_result(result, opt)
253
254
255@subcommand.usage('[args ...]')
LaMont Jones9eed4232021-04-02 16:29:49 +0000256def CMDcreatechange(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000257 """Create a new change in gerrit."""
LaMont Jones9eed4232021-04-02 16:29:49 +0000258 parser.add_option('-s', '--subject', help='subject for change')
259 parser.add_option('-b',
260 '--branch',
261 default='main',
262 help='target branch for change')
263 parser.add_option(
264 '-p',
265 '--param',
266 dest='params',
267 action='append',
268 help='repeatable field value parameter, format: -p key=value')
269
Xinan Lin91d2a5d2022-02-04 21:18:32 +0000270 parser.add_option('--cc',
271 dest='cc_list',
272 action='append',
273 help='CC address to notify, format: --cc foo@example.com')
274
LaMont Jones9eed4232021-04-02 16:29:49 +0000275 (opt, args) = parser.parse_args(args)
276 for p in opt.params:
277 assert '=' in p, '--param is key=value, not "%s"' % p
278
Xinan Lin91d2a5d2022-02-04 21:18:32 +0000279 params = list(tuple(p.split('=', 1)) for p in opt.params)
280
281 if opt.cc_list:
282 params.append(('notify_details', {'CC': {'accounts': opt.cc_list}}))
283
LaMont Jones9eed4232021-04-02 16:29:49 +0000284 result = gerrit_util.CreateChange(
Gavin Mak42674f52023-08-24 20:39:59 +0000285 urlparse.urlparse(opt.host).netloc,
LaMont Jones9eed4232021-04-02 16:29:49 +0000286 opt.project,
287 branch=opt.branch,
288 subject=opt.subject,
Xinan Lin91d2a5d2022-02-04 21:18:32 +0000289 params=params,
LaMont Jones9eed4232021-04-02 16:29:49 +0000290 )
291 logging.info(result)
292 write_result(result, opt)
293
294
295@subcommand.usage('[args ...]')
296def CMDchangeedit(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000297 """Puts content of a file into a change edit."""
LaMont Jones9eed4232021-04-02 16:29:49 +0000298 parser.add_option('-c', '--change', type=int, help='change number')
299 parser.add_option('--path', help='path for file')
300 parser.add_option('--file', help='file to place at |path|')
301
302 (opt, args) = parser.parse_args(args)
303
304 with open(opt.file) as f:
305 data = f.read()
306 result = gerrit_util.ChangeEdit(
Gavin Mak42674f52023-08-24 20:39:59 +0000307 urlparse.urlparse(opt.host).netloc, opt.change, opt.path, data)
LaMont Jones9eed4232021-04-02 16:29:49 +0000308 logging.info(result)
309 write_result(result, opt)
310
311
312@subcommand.usage('[args ...]')
313def CMDpublishchangeedit(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000314 """Publish a Gerrit change edit."""
LaMont Jones9eed4232021-04-02 16:29:49 +0000315 parser.add_option('-c', '--change', type=int, help='change number')
316 parser.add_option('--notify', help='whether to notify')
317
318 (opt, args) = parser.parse_args(args)
319
320 result = gerrit_util.PublishChangeEdit(
Gavin Mak42674f52023-08-24 20:39:59 +0000321 urlparse.urlparse(opt.host).netloc, opt.change, opt.notify)
LaMont Jones9eed4232021-04-02 16:29:49 +0000322 logging.info(result)
323 write_result(result, opt)
324
325
Xinan Lin6ec7cd82021-07-21 00:53:42 +0000326@subcommand.usage('[args ...]')
327def CMDsubmitchange(parser, args):
328 """Submit a Gerrit change."""
329 parser.add_option('-c', '--change', type=int, help='change number')
Xinan Lin6ec7cd82021-07-21 00:53:42 +0000330 (opt, args) = parser.parse_args(args)
Xinan Lin1bd4ffa2021-07-28 00:54:22 +0000331 result = gerrit_util.SubmitChange(
Gavin Mak42674f52023-08-24 20:39:59 +0000332 urlparse.urlparse(opt.host).netloc, opt.change)
Xinan Lin6ec7cd82021-07-21 00:53:42 +0000333 logging.info(result)
334 write_result(result, opt)
335
336
Xinan Linc2fb26a2021-07-27 18:01:55 +0000337@subcommand.usage('[args ...]')
Xinan Lin2b4ec952021-08-20 17:35:29 +0000338def CMDchangesubmittedtogether(parser, args):
339 """Get all changes submitted with the given one."""
340 parser.add_option('-c', '--change', type=int, help='change number')
341 (opt, args) = parser.parse_args(args)
342 result = gerrit_util.GetChangesSubmittedTogether(
Gavin Mak42674f52023-08-24 20:39:59 +0000343 urlparse.urlparse(opt.host).netloc, opt.change)
Xinan Lin2b4ec952021-08-20 17:35:29 +0000344 logging.info(result)
345 write_result(result, opt)
346
347
348@subcommand.usage('[args ...]')
Xinan Linc2fb26a2021-07-27 18:01:55 +0000349def CMDgetcommitincludedin(parser, args):
350 """Retrieves the branches and tags for a given commit."""
351 parser.add_option('--commit', dest='commit', help='commit hash')
352 (opt, args) = parser.parse_args(args)
353 result = gerrit_util.GetCommitIncludedIn(
Gavin Mak42674f52023-08-24 20:39:59 +0000354 urlparse.urlparse(opt.host).netloc, opt.project, opt.commit)
Xinan Linc2fb26a2021-07-27 18:01:55 +0000355 logging.info(result)
356 write_result(result, opt)
357
358
Xinan Lin0b0738d2021-07-27 19:13:49 +0000359@subcommand.usage('[args ...]')
360def CMDsetbotcommit(parser, args):
361 """Sets bot-commit+1 to a bot generated change."""
362 parser.add_option('-c', '--change', type=int, help='change number')
363 (opt, args) = parser.parse_args(args)
Gavin Mak42674f52023-08-24 20:39:59 +0000364 result = gerrit_util.SetReview(
365 urlparse.urlparse(opt.host).netloc,
366 opt.change,
367 labels={'Bot-Commit': 1},
368 ready=True)
Xinan Lin0b0738d2021-07-27 19:13:49 +0000369 logging.info(result)
370 write_result(result, opt)
371
372
Ben Pastene281edf72021-10-06 01:23:24 +0000373@subcommand.usage('[args ...]')
374def CMDsetlabel(parser, args):
375 """Sets a label to a specific value on a given change."""
376 parser.add_option('-c', '--change', type=int, help='change number')
377 parser.add_option('-l',
378 '--label',
379 nargs=2,
380 metavar=('label_name', 'label_value'))
381 (opt, args) = parser.parse_args(args)
Gavin Mak42674f52023-08-24 20:39:59 +0000382 result = gerrit_util.SetReview(urlparse.urlparse(opt.host).netloc,
Ben Pastene281edf72021-10-06 01:23:24 +0000383 opt.change,
384 labels={opt.label[0]: opt.label[1]})
385 logging.info(result)
386 write_result(result, opt)
387
388
Sergiy Belozorovfe347232019-02-27 15:07:33 +0000389@subcommand.usage('')
390def CMDabandon(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000391 """Abandons a Gerrit change."""
Sergiy Belozorovfe347232019-02-27 15:07:33 +0000392 parser.add_option('-c', '--change', type=int, help='change number')
393 parser.add_option('-m', '--message', default='', help='reason for abandoning')
394
395 (opt, args) = parser.parse_args(args)
Josip Sokcevicc99efb22020-03-17 00:35:34 +0000396 assert opt.change, "-c not defined"
Sergiy Belozorovfe347232019-02-27 15:07:33 +0000397 result = gerrit_util.AbandonChange(
Gavin Mak42674f52023-08-24 20:39:59 +0000398 urlparse.urlparse(opt.host).netloc,
399 opt.change, opt.message)
Sergiy Belozorovfe347232019-02-27 15:07:33 +0000400 logging.info(result)
401 write_result(result, opt)
402
403
Michael Moss5eebf6f2021-07-16 13:18:34 +0000404@subcommand.usage('')
Josip Sokcevice1a98942021-04-07 21:35:29 +0000405def CMDmass_abandon(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000406 """Mass abandon changes
407
408 Abandons CLs that match search criteria provided by user. Before any change is
409 actually abandoned, user is presented with a list of CLs that will be affected
410 if user confirms. User can skip confirmation by passing --force parameter.
411
412 The script can abandon up to 100 CLs per invocation.
413
414 Examples:
415 gerrit_client.py mass-abandon --host https://HOST -p 'project=repo2'
416 gerrit_client.py mass-abandon --host https://HOST -p 'message=testing'
417 gerrit_client.py mass-abandon --host https://HOST -p 'is=wip' -p 'age=1y'
418 """
Josip Sokcevice1a98942021-04-07 21:35:29 +0000419 parser.add_option('-p',
420 '--param',
421 dest='params',
422 action='append',
423 default=[],
424 help='repeatable query parameter, format: -p key=value')
425 parser.add_option('-m', '--message', default='', help='reason for abandoning')
426 parser.add_option('-f',
427 '--force',
428 action='store_true',
429 help='Don\'t prompt for confirmation')
430
431 opt, args = parser.parse_args(args)
432
433 for p in opt.params:
434 assert '=' in p, '--param is key=value, not "%s"' % p
435 search_query = list(tuple(p.split('=', 1)) for p in opt.params)
436 if not any(t for t in search_query if t[0] == 'owner'):
437 # owner should always be present when abandoning changes
438 search_query.append(('owner', 'me'))
439 search_query.append(('status', 'open'))
440 logging.info("Searching for: %s" % search_query)
441
Gavin Mak42674f52023-08-24 20:39:59 +0000442 host = urlparse.urlparse(opt.host).netloc
Josip Sokcevice1a98942021-04-07 21:35:29 +0000443
444 result = gerrit_util.QueryChanges(
445 host,
446 search_query,
447 # abandon at most 100 changes as not all Gerrit instances support
448 # unlimited results.
449 limit=100,
450 )
451 if len(result) == 0:
Nico Weber6b33f852023-06-21 17:14:24 +0000452 logging.warning("Nothing to abandon")
Josip Sokcevice1a98942021-04-07 21:35:29 +0000453 return
454
Nico Weber6b33f852023-06-21 17:14:24 +0000455 logging.warning("%s CLs match search query: " % len(result))
Josip Sokcevice1a98942021-04-07 21:35:29 +0000456 for change in result:
Nico Weber6b33f852023-06-21 17:14:24 +0000457 logging.warning("[ID: %d] %s" % (change['_number'], change['subject']))
Josip Sokcevice1a98942021-04-07 21:35:29 +0000458
459 if not opt.force:
Josip Sokcevic284fbdd2021-10-08 18:26:30 +0000460 q = input(
Josip Sokcevice1a98942021-04-07 21:35:29 +0000461 'Do you want to move forward with abandoning? [y to confirm] ').strip()
462 if q not in ['y', 'Y']:
Nico Weber6b33f852023-06-21 17:14:24 +0000463 logging.warning("Aborting...")
Josip Sokcevice1a98942021-04-07 21:35:29 +0000464 return
465
466 for change in result:
467 logging.warning("Abandoning: %s" % change['subject'])
468 gerrit_util.AbandonChange(host, change['id'], opt.message)
469
470 logging.warning("Done")
471
472
dimu833c94c2017-01-18 17:36:15 -0800473class OptionParser(optparse.OptionParser):
474 """Creates the option parse and add --verbose support."""
475 def __init__(self, *args, **kwargs):
Josip Sokcevicc99efb22020-03-17 00:35:34 +0000476 optparse.OptionParser.__init__(self, *args, version=__version__, **kwargs)
dimu833c94c2017-01-18 17:36:15 -0800477 self.add_option(
478 '--verbose', action='count', default=0,
479 help='Use 2 times for more debugging info')
480 self.add_option('--host', dest='host', help='Url of host.')
481 self.add_option('--project', dest='project', help='project name')
482 self.add_option(
483 '--json_file', dest='json_file', help='output json filepath')
484
485 def parse_args(self, args=None, values=None):
486 options, args = optparse.OptionParser.parse_args(self, args, values)
Josip Sokcevicc99efb22020-03-17 00:35:34 +0000487 # Host is always required
488 assert options.host, "--host not defined."
dimu833c94c2017-01-18 17:36:15 -0800489 levels = [logging.WARNING, logging.INFO, logging.DEBUG]
490 logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)])
491 return options, args
492
493
494def main(argv):
495 if sys.hexversion < 0x02060000:
496 print('\nYour python version %s is unsupported, please upgrade.\n'
Quinten Yearsleyd9cbe7a2019-09-03 16:49:11 +0000497 % (sys.version.split(' ', 1)[0],),
dimu833c94c2017-01-18 17:36:15 -0800498 file=sys.stderr)
499 return 2
500 dispatcher = subcommand.CommandDispatcher(__name__)
501 return dispatcher.execute(OptionParser(), argv)
502
503
504if __name__ == '__main__':
505 # These affect sys.stdout so do it outside of main() to simplify mocks in
506 # unit testing.
507 fix_encoding.fix_encoding()
508 setup_color.init()
509 try:
510 sys.exit(main(sys.argv[1:]))
511 except KeyboardInterrupt:
512 sys.stderr.write('interrupted\n')
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200513 sys.exit(1)