gerrit_client.py 4.4 KB

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