blob: ed81d75d280f58af6108b7c37ad9edcab03e95a5 [file] [log] [blame]
dimu833c94c2017-01-18 17:36:15 -08001#!/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
8Example usage:
9 ./gerrit_client.py [command] [args]""
10"""
11
12from __future__ import print_function
13
14import json
15import logging
16import optparse
17import subcommand
18import sys
19import urllib
20import urlparse
21
22from third_party import colorama
23import fix_encoding
24import gerrit_util
25import setup_color
26
27__version__ = '0.1'
28# Shortcut since it quickly becomes redundant.
29Fore = colorama.Fore
30
31
32def 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 ...]')
39def 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 ...]')
51def 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
66class OptionParser(optparse.OptionParser):
67 """Creates the option parse and add --verbose support."""
68 def __init__(self, *args, **kwargs):
69 optparse.OptionParser.__init__(
70 self, *args, prog='git cl', version=__version__, **kwargs)
71 self.add_option(
72 '--verbose', action='count', default=0,
73 help='Use 2 times for more debugging info')
74 self.add_option('--host', dest='host', help='Url of host.')
75 self.add_option('--project', dest='project', help='project name')
76 self.add_option(
77 '--json_file', dest='json_file', help='output json filepath')
78
79 def parse_args(self, args=None, values=None):
80 options, args = optparse.OptionParser.parse_args(self, args, values)
81 levels = [logging.WARNING, logging.INFO, logging.DEBUG]
82 logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)])
83 return options, args
84
85
86def main(argv):
87 if sys.hexversion < 0x02060000:
88 print('\nYour python version %s is unsupported, please upgrade.\n'
89 %(sys.version.split(' ', 1)[0],),
90 file=sys.stderr)
91 return 2
92 dispatcher = subcommand.CommandDispatcher(__name__)
93 return dispatcher.execute(OptionParser(), argv)
94
95
96if __name__ == '__main__':
97 # These affect sys.stdout so do it outside of main() to simplify mocks in
98 # unit testing.
99 fix_encoding.fix_encoding()
100 setup_color.init()
101 try:
102 sys.exit(main(sys.argv[1:]))
103 except KeyboardInterrupt:
104 sys.stderr.write('interrupted\n')
105 sys.exit(1)