dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 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: |
| 9 | ./gerrit_client.py [command] [args]"" |
| 10 | """ |
| 11 | |
| 12 | from __future__ import print_function |
| 13 | |
| 14 | import json |
| 15 | import logging |
| 16 | import optparse |
| 17 | import subcommand |
| 18 | import sys |
| 19 | import urllib |
| 20 | import urlparse |
| 21 | |
| 22 | from third_party import colorama |
| 23 | import fix_encoding |
| 24 | import gerrit_util |
| 25 | import setup_color |
| 26 | |
| 27 | __version__ = '0.1' |
| 28 | # Shortcut since it quickly becomes redundant. |
| 29 | Fore = colorama.Fore |
| 30 | |
| 31 | |
| 32 | def write_result(result, opt): |
| 33 | if opt.json_file: |
| 34 | with open(opt.json_file, 'w') as json_file: |
| 35 | json_file.write(json.dumps(result)) |
| 36 | |
| 37 | |
| 38 | @subcommand.usage('[args ...]') |
| 39 | def CMDbranchinfo(parser, args): |
| 40 | parser.add_option('--branch', dest='branch', help='branch name') |
| 41 | |
| 42 | (opt, args) = parser.parse_args(args) |
| 43 | host = urlparse.urlparse(opt.host).netloc |
| 44 | project = urllib.quote_plus(opt.project) |
| 45 | branch = urllib.quote_plus(opt.branch) |
| 46 | result = gerrit_util.GetGerritBranch(host, project, branch) |
| 47 | logging.info(result) |
| 48 | write_result(result, opt) |
| 49 | |
| 50 | @subcommand.usage('[args ...]') |
| 51 | def CMDbranch(parser, args): |
| 52 | parser.add_option('--branch', dest='branch', help='branch name') |
| 53 | parser.add_option('--commit', dest='commit', help='commit hash') |
| 54 | |
| 55 | (opt, args) = parser.parse_args(args) |
| 56 | |
| 57 | project = urllib.quote_plus(opt.project) |
| 58 | host = urlparse.urlparse(opt.host).netloc |
| 59 | branch = urllib.quote_plus(opt.branch) |
| 60 | commit = urllib.quote_plus(opt.commit) |
| 61 | result = gerrit_util.CreateGerritBranch(host, project, branch, commit) |
| 62 | logging.info(result) |
| 63 | write_result(result, opt) |
| 64 | |
| 65 | |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 66 | @subcommand.usage('[args ...]') |
| 67 | def CMDchanges(parser, args): |
| 68 | parser.add_option('-p', '--param', dest='params', action='append', |
| 69 | help='repeatable query parameter, format: -p key=value') |
| 70 | parser.add_option('--limit', dest='limit', type=int, |
| 71 | help='maximum number of results to return') |
| 72 | parser.add_option('--start', dest='start', type=int, |
| 73 | help='how many changes to skip ' |
| 74 | '(starting with the most recent)') |
| 75 | |
| 76 | (opt, args) = parser.parse_args(args) |
| 77 | |
| 78 | result = gerrit_util.QueryChanges( |
| 79 | urlparse.urlparse(opt.host).netloc, |
| 80 | list(tuple(p.split('=', 1)) for p in opt.params), |
| 81 | start=opt.start, # Default: None |
| 82 | limit=opt.limit, # Default: None |
| 83 | ) |
| 84 | logging.info('Change query returned %d changes.', len(result)) |
| 85 | write_result(result, opt) |
| 86 | |
| 87 | |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 88 | class OptionParser(optparse.OptionParser): |
| 89 | """Creates the option parse and add --verbose support.""" |
| 90 | def __init__(self, *args, **kwargs): |
| 91 | optparse.OptionParser.__init__( |
| 92 | self, *args, prog='git cl', version=__version__, **kwargs) |
| 93 | self.add_option( |
| 94 | '--verbose', action='count', default=0, |
| 95 | help='Use 2 times for more debugging info') |
| 96 | self.add_option('--host', dest='host', help='Url of host.') |
| 97 | self.add_option('--project', dest='project', help='project name') |
| 98 | self.add_option( |
| 99 | '--json_file', dest='json_file', help='output json filepath') |
| 100 | |
| 101 | def parse_args(self, args=None, values=None): |
| 102 | options, args = optparse.OptionParser.parse_args(self, args, values) |
| 103 | levels = [logging.WARNING, logging.INFO, logging.DEBUG] |
| 104 | logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)]) |
| 105 | return options, args |
| 106 | |
| 107 | |
| 108 | def main(argv): |
| 109 | if sys.hexversion < 0x02060000: |
| 110 | print('\nYour python version %s is unsupported, please upgrade.\n' |
| 111 | %(sys.version.split(' ', 1)[0],), |
| 112 | file=sys.stderr) |
| 113 | return 2 |
| 114 | dispatcher = subcommand.CommandDispatcher(__name__) |
| 115 | return dispatcher.execute(OptionParser(), argv) |
| 116 | |
| 117 | |
| 118 | if __name__ == '__main__': |
| 119 | # These affect sys.stdout so do it outside of main() to simplify mocks in |
| 120 | # unit testing. |
| 121 | fix_encoding.fix_encoding() |
| 122 | setup_color.init() |
| 123 | try: |
| 124 | sys.exit(main(sys.argv[1:])) |
| 125 | except KeyboardInterrupt: |
| 126 | sys.stderr.write('interrupted\n') |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 127 | sys.exit(1) |