forked from gaoyaqiu/depot_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gerrit_client.py
executable file
·143 lines (115 loc) · 4.53 KB
/
gerrit_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Simple client for the Gerrit REST API.
Example usage:
./gerrit_client.py [command] [args]""
"""
from __future__ import print_function
import json
import logging
import optparse
import subcommand
import sys
import urllib
import urlparse
from third_party import colorama
import fix_encoding
import gerrit_util
import setup_color
__version__ = '0.1'
# Shortcut since it quickly becomes redundant.
Fore = colorama.Fore
def write_result(result, opt):
if opt.json_file:
with open(opt.json_file, 'w') as json_file:
json_file.write(json.dumps(result))
@subcommand.usage('[args ...]')
def CMDbranchinfo(parser, args):
parser.add_option('--branch', dest='branch', help='branch name')
(opt, args) = parser.parse_args(args)
host = urlparse.urlparse(opt.host).netloc
project = urllib.quote_plus(opt.project)
branch = urllib.quote_plus(opt.branch)
result = gerrit_util.GetGerritBranch(host, project, branch)
logging.info(result)
write_result(result, opt)
@subcommand.usage('[args ...]')
def CMDbranch(parser, args):
parser.add_option('--branch', dest='branch', help='branch name')
parser.add_option('--commit', dest='commit', help='commit hash')
(opt, args) = parser.parse_args(args)
project = urllib.quote_plus(opt.project)
host = urlparse.urlparse(opt.host).netloc
branch = urllib.quote_plus(opt.branch)
commit = urllib.quote_plus(opt.commit)
result = gerrit_util.CreateGerritBranch(host, project, branch, commit)
logging.info(result)
write_result(result, opt)
@subcommand.usage('[args ...]')
def CMDchanges(parser, args):
parser.add_option('-p', '--param', dest='params', action='append',
help='repeatable query parameter, format: -p key=value')
parser.add_option('-o', '--o-param', dest='o_params', action='append',
help='gerrit output parameters, e.g. ALL_REVISIONS')
parser.add_option('--limit', dest='limit', type=int,
help='maximum number of results to return')
parser.add_option('--start', dest='start', type=int,
help='how many changes to skip '
'(starting with the most recent)')
(opt, args) = parser.parse_args(args)
result = gerrit_util.QueryChanges(
urlparse.urlparse(opt.host).netloc,
list(tuple(p.split('=', 1)) for p in opt.params),
start=opt.start, # Default: None
limit=opt.limit, # Default: None
o_params=opt.o_params, # Default: None
)
logging.info('Change query returned %d changes.', len(result))
write_result(result, opt)
@subcommand.usage('')
def CMDabandon(parser, args):
parser.add_option('-c', '--change', type=int, help='change number')
parser.add_option('-m', '--message', default='', help='reason for abandoning')
(opt, args) = parser.parse_args(args)
result = gerrit_util.AbandonChange(
urlparse.urlparse(opt.host).netloc,
opt.change, opt.message)
logging.info(result)
write_result(result, opt)
class OptionParser(optparse.OptionParser):
"""Creates the option parse and add --verbose support."""
def __init__(self, *args, **kwargs):
optparse.OptionParser.__init__(
self, *args, prog='git cl', version=__version__, **kwargs)
self.add_option(
'--verbose', action='count', default=0,
help='Use 2 times for more debugging info')
self.add_option('--host', dest='host', help='Url of host.')
self.add_option('--project', dest='project', help='project name')
self.add_option(
'--json_file', dest='json_file', help='output json filepath')
def parse_args(self, args=None, values=None):
options, args = optparse.OptionParser.parse_args(self, args, values)
levels = [logging.WARNING, logging.INFO, logging.DEBUG]
logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)])
return options, args
def main(argv):
if sys.hexversion < 0x02060000:
print('\nYour python version %s is unsupported, please upgrade.\n'
%(sys.version.split(' ', 1)[0],),
file=sys.stderr)
return 2
dispatcher = subcommand.CommandDispatcher(__name__)
return dispatcher.execute(OptionParser(), argv)
if __name__ == '__main__':
# These affect sys.stdout so do it outside of main() to simplify mocks in
# unit testing.
fix_encoding.fix_encoding()
setup_color.init()
try:
sys.exit(main(sys.argv[1:]))
except KeyboardInterrupt:
sys.stderr.write('interrupted\n')
sys.exit(1)