Josip Sokcevic | 84434e8 | 2021-06-09 22:48:43 +0000 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 2 | # 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 | |
| 8 | Example usage: |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 9 | ./gerrit_client.py [command] [args] |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 10 | """ |
| 11 | |
| 12 | from __future__ import print_function |
| 13 | |
| 14 | import json |
| 15 | import logging |
| 16 | import optparse |
| 17 | import subcommand |
| 18 | import sys |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 19 | |
| 20 | if sys.version_info.major == 2: |
| 21 | import urlparse |
| 22 | from urllib import quote_plus |
| 23 | else: |
| 24 | from urllib.parse import quote_plus |
| 25 | import urllib.parse as urlparse |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 26 | |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 27 | import fix_encoding |
| 28 | import gerrit_util |
| 29 | import setup_color |
| 30 | |
| 31 | __version__ = '0.1' |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 32 | |
| 33 | |
| 34 | def 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 Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 41 | def CMDmovechanges(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 42 | """Move changes to a different destination branch.""" |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 43 | 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 Frysinger | 8820ab8 | 2020-11-25 00:52:31 +0000 | [diff] [blame] | 50 | for p in opt.params: |
| 51 | assert '=' in p, '--param is key=value, not "%s"' % p |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 52 | host = urlparse.urlparse(opt.host).netloc |
| 53 | |
| 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 ...]') |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 70 | def CMDbranchinfo(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 71 | """Get information on a gerrit branch.""" |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 72 | parser.add_option('--branch', dest='branch', help='branch name') |
| 73 | |
| 74 | (opt, args) = parser.parse_args(args) |
| 75 | host = urlparse.urlparse(opt.host).netloc |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 76 | project = quote_plus(opt.project) |
| 77 | branch = quote_plus(opt.branch) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 78 | result = gerrit_util.GetGerritBranch(host, project, branch) |
| 79 | logging.info(result) |
| 80 | write_result(result, opt) |
| 81 | |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 82 | |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 83 | @subcommand.usage('[args ...]') |
| 84 | def CMDbranch(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 85 | """Create a branch in a gerrit project.""" |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 86 | parser.add_option('--branch', dest='branch', help='branch name') |
| 87 | parser.add_option('--commit', dest='commit', help='commit hash') |
| 88 | |
| 89 | (opt, args) = parser.parse_args(args) |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 90 | assert opt.project, "--project not defined" |
| 91 | assert opt.branch, "--branch not defined" |
| 92 | assert opt.commit, "--commit not defined" |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 93 | |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 94 | project = quote_plus(opt.project) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 95 | host = urlparse.urlparse(opt.host).netloc |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 96 | branch = quote_plus(opt.branch) |
| 97 | commit = quote_plus(opt.commit) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 98 | result = gerrit_util.CreateGerritBranch(host, project, branch, commit) |
| 99 | logging.info(result) |
| 100 | write_result(result, opt) |
| 101 | |
| 102 | |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 103 | @subcommand.usage('[args ...]') |
Josip Sokcevic | df9a802 | 2020-12-08 00:10:19 +0000 | [diff] [blame] | 104 | def CMDhead(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 105 | """Update which branch the project HEAD points to.""" |
Josip Sokcevic | df9a802 | 2020-12-08 00:10:19 +0000 | [diff] [blame] | 106 | parser.add_option('--branch', dest='branch', help='branch name') |
| 107 | |
| 108 | (opt, args) = parser.parse_args(args) |
| 109 | assert opt.project, "--project not defined" |
| 110 | assert opt.branch, "--branch not defined" |
| 111 | |
| 112 | project = quote_plus(opt.project) |
| 113 | host = urlparse.urlparse(opt.host).netloc |
| 114 | branch = quote_plus(opt.branch) |
| 115 | result = gerrit_util.UpdateHead(host, project, branch) |
| 116 | logging.info(result) |
| 117 | write_result(result, opt) |
| 118 | |
| 119 | |
| 120 | @subcommand.usage('[args ...]') |
| 121 | def CMDheadinfo(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 122 | """Retrieves the current HEAD of the project.""" |
Josip Sokcevic | df9a802 | 2020-12-08 00:10:19 +0000 | [diff] [blame] | 123 | |
| 124 | (opt, args) = parser.parse_args(args) |
| 125 | assert opt.project, "--project not defined" |
| 126 | |
| 127 | project = quote_plus(opt.project) |
| 128 | host = urlparse.urlparse(opt.host).netloc |
| 129 | result = gerrit_util.GetHead(host, project) |
| 130 | logging.info(result) |
| 131 | write_result(result, opt) |
| 132 | |
| 133 | |
| 134 | @subcommand.usage('[args ...]') |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 135 | def CMDchanges(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 136 | """Queries gerrit for matching changes.""" |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 137 | parser.add_option('-p', '--param', dest='params', action='append', |
| 138 | help='repeatable query parameter, format: -p key=value') |
Paweł Hajdan, Jr | 24025d3 | 2017-07-11 16:38:21 +0200 | [diff] [blame] | 139 | parser.add_option('-o', '--o-param', dest='o_params', action='append', |
| 140 | help='gerrit output parameters, e.g. ALL_REVISIONS') |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 141 | parser.add_option('--limit', dest='limit', type=int, |
| 142 | help='maximum number of results to return') |
| 143 | parser.add_option('--start', dest='start', type=int, |
| 144 | help='how many changes to skip ' |
| 145 | '(starting with the most recent)') |
| 146 | |
| 147 | (opt, args) = parser.parse_args(args) |
Mike Frysinger | 8820ab8 | 2020-11-25 00:52:31 +0000 | [diff] [blame] | 148 | for p in opt.params: |
| 149 | assert '=' in p, '--param is key=value, not "%s"' % p |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 150 | |
| 151 | result = gerrit_util.QueryChanges( |
| 152 | urlparse.urlparse(opt.host).netloc, |
| 153 | list(tuple(p.split('=', 1)) for p in opt.params), |
Paweł Hajdan, Jr | 24025d3 | 2017-07-11 16:38:21 +0200 | [diff] [blame] | 154 | start=opt.start, # Default: None |
| 155 | limit=opt.limit, # Default: None |
| 156 | o_params=opt.o_params, # Default: None |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 157 | ) |
| 158 | logging.info('Change query returned %d changes.', len(result)) |
| 159 | write_result(result, opt) |
| 160 | |
| 161 | |
LaMont Jones | 9eed423 | 2021-04-02 16:29:49 +0000 | [diff] [blame] | 162 | @subcommand.usage('[args ...]') |
Marco Georgaklis | 85557a0 | 2021-06-03 15:56:54 +0000 | [diff] [blame] | 163 | def CMDrelatedchanges(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 164 | """Gets related changes for a given change and revision.""" |
Marco Georgaklis | 85557a0 | 2021-06-03 15:56:54 +0000 | [diff] [blame] | 165 | parser.add_option('-c', '--change', type=str, help='change id') |
| 166 | parser.add_option('-r', '--revision', type=str, help='revision id') |
| 167 | |
| 168 | (opt, args) = parser.parse_args(args) |
| 169 | |
| 170 | result = gerrit_util.GetRelatedChanges( |
| 171 | urlparse.urlparse(opt.host).netloc, |
| 172 | change=opt.change, |
| 173 | revision=opt.revision, |
| 174 | ) |
| 175 | logging.info(result) |
| 176 | write_result(result, opt) |
| 177 | |
| 178 | |
| 179 | @subcommand.usage('[args ...]') |
LaMont Jones | 9eed423 | 2021-04-02 16:29:49 +0000 | [diff] [blame] | 180 | def CMDcreatechange(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 181 | """Create a new change in gerrit.""" |
LaMont Jones | 9eed423 | 2021-04-02 16:29:49 +0000 | [diff] [blame] | 182 | parser.add_option('-s', '--subject', help='subject for change') |
| 183 | parser.add_option('-b', |
| 184 | '--branch', |
| 185 | default='main', |
| 186 | help='target branch for change') |
| 187 | parser.add_option( |
| 188 | '-p', |
| 189 | '--param', |
| 190 | dest='params', |
| 191 | action='append', |
| 192 | help='repeatable field value parameter, format: -p key=value') |
| 193 | |
| 194 | (opt, args) = parser.parse_args(args) |
| 195 | for p in opt.params: |
| 196 | assert '=' in p, '--param is key=value, not "%s"' % p |
| 197 | |
| 198 | result = gerrit_util.CreateChange( |
| 199 | urlparse.urlparse(opt.host).netloc, |
| 200 | opt.project, |
| 201 | branch=opt.branch, |
| 202 | subject=opt.subject, |
| 203 | params=list(tuple(p.split('=', 1)) for p in opt.params), |
| 204 | ) |
| 205 | logging.info(result) |
| 206 | write_result(result, opt) |
| 207 | |
| 208 | |
| 209 | @subcommand.usage('[args ...]') |
| 210 | def CMDchangeedit(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 211 | """Puts content of a file into a change edit.""" |
LaMont Jones | 9eed423 | 2021-04-02 16:29:49 +0000 | [diff] [blame] | 212 | parser.add_option('-c', '--change', type=int, help='change number') |
| 213 | parser.add_option('--path', help='path for file') |
| 214 | parser.add_option('--file', help='file to place at |path|') |
| 215 | |
| 216 | (opt, args) = parser.parse_args(args) |
| 217 | |
| 218 | with open(opt.file) as f: |
| 219 | data = f.read() |
| 220 | result = gerrit_util.ChangeEdit( |
| 221 | urlparse.urlparse(opt.host).netloc, opt.change, opt.path, data) |
| 222 | logging.info(result) |
| 223 | write_result(result, opt) |
| 224 | |
| 225 | |
| 226 | @subcommand.usage('[args ...]') |
| 227 | def CMDpublishchangeedit(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 228 | """Publish a Gerrit change edit.""" |
LaMont Jones | 9eed423 | 2021-04-02 16:29:49 +0000 | [diff] [blame] | 229 | parser.add_option('-c', '--change', type=int, help='change number') |
| 230 | parser.add_option('--notify', help='whether to notify') |
| 231 | |
| 232 | (opt, args) = parser.parse_args(args) |
| 233 | |
| 234 | result = gerrit_util.PublishChangeEdit( |
| 235 | urlparse.urlparse(opt.host).netloc, opt.change, opt.notify) |
| 236 | logging.info(result) |
| 237 | write_result(result, opt) |
| 238 | |
| 239 | |
Xinan Lin | 6ec7cd8 | 2021-07-21 00:53:42 +0000 | [diff] [blame] | 240 | @subcommand.usage('[args ...]') |
| 241 | def CMDsubmitchange(parser, args): |
| 242 | """Submit a Gerrit change.""" |
| 243 | parser.add_option('-c', '--change', type=int, help='change number') |
| 244 | parser.add_option('--wait-for-merge', |
| 245 | action="store_true", |
| 246 | default=False, |
| 247 | help='whether to wait for the merge') |
| 248 | |
| 249 | (opt, args) = parser.parse_args(args) |
| 250 | result = gerrit_util.SubmitChange(urlparse.urlparse(opt.host).netloc, |
| 251 | opt.change, |
| 252 | wait_for_merge=opt.wait_for_merge) |
| 253 | logging.info(result) |
| 254 | write_result(result, opt) |
| 255 | |
| 256 | |
Xinan Lin | c2fb26a | 2021-07-27 18:01:55 +0000 | [diff] [blame] | 257 | @subcommand.usage('[args ...]') |
| 258 | def CMDgetcommitincludedin(parser, args): |
| 259 | """Retrieves the branches and tags for a given commit.""" |
| 260 | parser.add_option('--commit', dest='commit', help='commit hash') |
| 261 | (opt, args) = parser.parse_args(args) |
| 262 | result = gerrit_util.GetCommitIncludedIn( |
| 263 | urlparse.urlparse(opt.host).netloc, opt.project, opt.commit) |
| 264 | logging.info(result) |
| 265 | write_result(result, opt) |
| 266 | |
| 267 | |
Xinan Lin | 0b0738d | 2021-07-27 19:13:49 +0000 | [diff] [blame] | 268 | @subcommand.usage('[args ...]') |
| 269 | def CMDsetbotcommit(parser, args): |
| 270 | """Sets bot-commit+1 to a bot generated change.""" |
| 271 | parser.add_option('-c', '--change', type=int, help='change number') |
| 272 | (opt, args) = parser.parse_args(args) |
| 273 | result = gerrit_util.SetReview( |
| 274 | urlparse.urlparse(opt.host).netloc, |
| 275 | opt.change, |
| 276 | labels={'Bot-Commit': 1}, |
| 277 | ready=True) |
| 278 | logging.info(result) |
| 279 | write_result(result, opt) |
| 280 | |
| 281 | |
Sergiy Belozorov | fe34723 | 2019-02-27 15:07:33 +0000 | [diff] [blame] | 282 | @subcommand.usage('') |
| 283 | def CMDabandon(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 284 | """Abandons a Gerrit change.""" |
Sergiy Belozorov | fe34723 | 2019-02-27 15:07:33 +0000 | [diff] [blame] | 285 | parser.add_option('-c', '--change', type=int, help='change number') |
| 286 | parser.add_option('-m', '--message', default='', help='reason for abandoning') |
| 287 | |
| 288 | (opt, args) = parser.parse_args(args) |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 289 | assert opt.change, "-c not defined" |
Sergiy Belozorov | fe34723 | 2019-02-27 15:07:33 +0000 | [diff] [blame] | 290 | result = gerrit_util.AbandonChange( |
| 291 | urlparse.urlparse(opt.host).netloc, |
| 292 | opt.change, opt.message) |
| 293 | logging.info(result) |
| 294 | write_result(result, opt) |
| 295 | |
| 296 | |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 297 | @subcommand.usage('') |
Josip Sokcevic | e1a9894 | 2021-04-07 21:35:29 +0000 | [diff] [blame] | 298 | def CMDmass_abandon(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 299 | """Mass abandon changes |
| 300 | |
| 301 | Abandons CLs that match search criteria provided by user. Before any change is |
| 302 | actually abandoned, user is presented with a list of CLs that will be affected |
| 303 | if user confirms. User can skip confirmation by passing --force parameter. |
| 304 | |
| 305 | The script can abandon up to 100 CLs per invocation. |
| 306 | |
| 307 | Examples: |
| 308 | gerrit_client.py mass-abandon --host https://HOST -p 'project=repo2' |
| 309 | gerrit_client.py mass-abandon --host https://HOST -p 'message=testing' |
| 310 | gerrit_client.py mass-abandon --host https://HOST -p 'is=wip' -p 'age=1y' |
| 311 | """ |
Josip Sokcevic | e1a9894 | 2021-04-07 21:35:29 +0000 | [diff] [blame] | 312 | parser.add_option('-p', |
| 313 | '--param', |
| 314 | dest='params', |
| 315 | action='append', |
| 316 | default=[], |
| 317 | help='repeatable query parameter, format: -p key=value') |
| 318 | parser.add_option('-m', '--message', default='', help='reason for abandoning') |
| 319 | parser.add_option('-f', |
| 320 | '--force', |
| 321 | action='store_true', |
| 322 | help='Don\'t prompt for confirmation') |
| 323 | |
| 324 | opt, args = parser.parse_args(args) |
| 325 | |
| 326 | for p in opt.params: |
| 327 | assert '=' in p, '--param is key=value, not "%s"' % p |
| 328 | search_query = list(tuple(p.split('=', 1)) for p in opt.params) |
| 329 | if not any(t for t in search_query if t[0] == 'owner'): |
| 330 | # owner should always be present when abandoning changes |
| 331 | search_query.append(('owner', 'me')) |
| 332 | search_query.append(('status', 'open')) |
| 333 | logging.info("Searching for: %s" % search_query) |
| 334 | |
| 335 | host = urlparse.urlparse(opt.host).netloc |
| 336 | |
| 337 | result = gerrit_util.QueryChanges( |
| 338 | host, |
| 339 | search_query, |
| 340 | # abandon at most 100 changes as not all Gerrit instances support |
| 341 | # unlimited results. |
| 342 | limit=100, |
| 343 | ) |
| 344 | if len(result) == 0: |
| 345 | logging.warn("Nothing to abandon") |
| 346 | return |
| 347 | |
| 348 | logging.warn("%s CLs match search query: " % len(result)) |
| 349 | for change in result: |
| 350 | logging.warn("[ID: %d] %s" % (change['_number'], change['subject'])) |
| 351 | |
| 352 | if not opt.force: |
| 353 | q = raw_input( |
| 354 | 'Do you want to move forward with abandoning? [y to confirm] ').strip() |
| 355 | if q not in ['y', 'Y']: |
| 356 | logging.warn("Aborting...") |
| 357 | return |
| 358 | |
| 359 | for change in result: |
| 360 | logging.warning("Abandoning: %s" % change['subject']) |
| 361 | gerrit_util.AbandonChange(host, change['id'], opt.message) |
| 362 | |
| 363 | logging.warning("Done") |
| 364 | |
| 365 | |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 366 | class OptionParser(optparse.OptionParser): |
| 367 | """Creates the option parse and add --verbose support.""" |
| 368 | def __init__(self, *args, **kwargs): |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 369 | optparse.OptionParser.__init__(self, *args, version=__version__, **kwargs) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 370 | self.add_option( |
| 371 | '--verbose', action='count', default=0, |
| 372 | help='Use 2 times for more debugging info') |
| 373 | self.add_option('--host', dest='host', help='Url of host.') |
| 374 | self.add_option('--project', dest='project', help='project name') |
| 375 | self.add_option( |
| 376 | '--json_file', dest='json_file', help='output json filepath') |
| 377 | |
| 378 | def parse_args(self, args=None, values=None): |
| 379 | options, args = optparse.OptionParser.parse_args(self, args, values) |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 380 | # Host is always required |
| 381 | assert options.host, "--host not defined." |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 382 | levels = [logging.WARNING, logging.INFO, logging.DEBUG] |
| 383 | logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)]) |
| 384 | return options, args |
| 385 | |
| 386 | |
| 387 | def main(argv): |
| 388 | if sys.hexversion < 0x02060000: |
| 389 | print('\nYour python version %s is unsupported, please upgrade.\n' |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 390 | % (sys.version.split(' ', 1)[0],), |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 391 | file=sys.stderr) |
| 392 | return 2 |
| 393 | dispatcher = subcommand.CommandDispatcher(__name__) |
| 394 | return dispatcher.execute(OptionParser(), argv) |
| 395 | |
| 396 | |
| 397 | if __name__ == '__main__': |
| 398 | # These affect sys.stdout so do it outside of main() to simplify mocks in |
| 399 | # unit testing. |
| 400 | fix_encoding.fix_encoding() |
| 401 | setup_color.init() |
| 402 | try: |
| 403 | sys.exit(main(sys.argv[1:])) |
| 404 | except KeyboardInterrupt: |
| 405 | sys.stderr.write('interrupted\n') |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 406 | sys.exit(1) |