commit_queue.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #!/usr/bin/env python
  2. # Copyright (c) 2011 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. """Access the commit queue from the command line.
  6. """
  7. __version__ = '0.1'
  8. import functools
  9. import json
  10. import logging
  11. import optparse
  12. import os
  13. import sys
  14. import urllib2
  15. import breakpad # pylint: disable=W0611
  16. import auth
  17. import fix_encoding
  18. import rietveld
  19. THIRD_PARTY_DIR = os.path.join(os.path.dirname(__file__), 'third_party')
  20. sys.path.insert(0, THIRD_PARTY_DIR)
  21. from cq_client import cq_pb2
  22. from protobuf26 import text_format
  23. def usage(more):
  24. def hook(fn):
  25. fn.func_usage_more = more
  26. return fn
  27. return hook
  28. def need_issue(fn):
  29. """Post-parse args to create a Rietveld object."""
  30. @functools.wraps(fn)
  31. def hook(parser, args, *extra_args, **kwargs):
  32. old_parse_args = parser.parse_args
  33. def new_parse_args(args=None, values=None):
  34. options, args = old_parse_args(args, values)
  35. auth_config = auth.extract_auth_config_from_options(options)
  36. if not options.issue:
  37. parser.error('Require --issue')
  38. obj = rietveld.Rietveld(options.server, auth_config, options.user)
  39. return options, args, obj
  40. parser.parse_args = new_parse_args
  41. parser.add_option(
  42. '-u', '--user',
  43. metavar='U',
  44. default=os.environ.get('EMAIL_ADDRESS', None),
  45. help='Email address, default: %default')
  46. parser.add_option(
  47. '-i', '--issue',
  48. metavar='I',
  49. type='int',
  50. help='Rietveld issue number')
  51. parser.add_option(
  52. '-s',
  53. '--server',
  54. metavar='S',
  55. default='http://codereview.chromium.org',
  56. help='Rietveld server, default: %default')
  57. auth.add_auth_options(parser)
  58. # Call the original function with the modified parser.
  59. return fn(parser, args, *extra_args, **kwargs)
  60. hook.func_usage_more = '[options]'
  61. return hook
  62. def set_commit(obj, issue, flag):
  63. """Sets the commit bit flag on an issue."""
  64. try:
  65. patchset = obj.get_issue_properties(issue, False)['patchsets'][-1]
  66. print obj.set_flag(issue, patchset, 'commit', flag)
  67. except urllib2.HTTPError, e:
  68. if e.code == 404:
  69. print >> sys.stderr, 'Issue %d doesn\'t exist.' % issue
  70. elif e.code == 403:
  71. print >> sys.stderr, 'Access denied to issue %d.' % issue
  72. else:
  73. raise
  74. return 1
  75. @need_issue
  76. def CMDset(parser, args):
  77. """Sets the commit bit."""
  78. options, args, obj = parser.parse_args(args)
  79. if args:
  80. parser.error('Unrecognized args: %s' % ' '.join(args))
  81. return set_commit(obj, options.issue, '1')
  82. @need_issue
  83. def CMDclear(parser, args):
  84. """Clears the commit bit."""
  85. options, args, obj = parser.parse_args(args)
  86. if args:
  87. parser.error('Unrecognized args: %s' % ' '.join(args))
  88. return set_commit(obj, options.issue, '0')
  89. def CMDbuilders(parser, args):
  90. """Prints json-formatted list of builders given a path to cq.cfg file.
  91. The output is a dictionary in the following format:
  92. {
  93. 'master_name': {
  94. 'builder_name': {
  95. 'custom_property': 'value',
  96. 'testfilter': 'compile'
  97. },
  98. 'another_builder': {}
  99. },
  100. 'another_master': {
  101. 'third_builder': {}
  102. }
  103. }
  104. """
  105. _, args = parser.parse_args(args)
  106. if len(args) != 1:
  107. parser.error('Expected a single path to CQ config. Got: %s' %
  108. ' '.join(args))
  109. with open(args[0]) as config_file:
  110. cq_config = config_file.read()
  111. config = cq_pb2.Config()
  112. text_format.Merge(cq_config, config)
  113. masters = {}
  114. if config.HasField('verifiers') and config.verifiers.HasField('try_job'):
  115. for bucket in config.verifiers.try_job.buckets:
  116. masters.setdefault(bucket.name, {})
  117. for builder in bucket.builders:
  118. if not builder.HasField('experiment_percentage'):
  119. masters[bucket.name].setdefault(builder.name, {})
  120. print json.dumps(masters)
  121. CMDbuilders.func_usage_more = '<path-to-cq-config>'
  122. ###############################################################################
  123. ## Boilerplate code
  124. class OptionParser(optparse.OptionParser):
  125. """An OptionParser instance with default options.
  126. It should be then processed with gen_usage() before being used.
  127. """
  128. def __init__(self, *args, **kwargs):
  129. optparse.OptionParser.__init__(self, *args, **kwargs)
  130. self.add_option(
  131. '-v', '--verbose', action='count', default=0,
  132. help='Use multiple times to increase logging level')
  133. def parse_args(self, args=None, values=None):
  134. options, args = optparse.OptionParser.parse_args(self, args, values)
  135. levels = [logging.WARNING, logging.INFO, logging.DEBUG]
  136. logging.basicConfig(
  137. level=levels[min(len(levels) - 1, options.verbose)],
  138. format='%(levelname)s %(filename)s(%(lineno)d): %(message)s')
  139. return options, args
  140. def format_description(self, _):
  141. """Removes description formatting."""
  142. return self.description.rstrip() + '\n'
  143. def Command(name):
  144. return getattr(sys.modules[__name__], 'CMD' + name, None)
  145. @usage('<command>')
  146. def CMDhelp(parser, args):
  147. """Print list of commands or use 'help <command>'."""
  148. # Strip out the help command description and replace it with the module
  149. # docstring.
  150. parser.description = sys.modules[__name__].__doc__
  151. parser.description += '\nCommands are:\n' + '\n'.join(
  152. ' %-12s %s' % (
  153. fn[3:], Command(fn[3:]).__doc__.split('\n', 1)[0].rstrip('.'))
  154. for fn in dir(sys.modules[__name__]) if fn.startswith('CMD'))
  155. _, args = parser.parse_args(args)
  156. if len(args) == 1 and args[0] != 'help':
  157. return main(args + ['--help'])
  158. parser.print_help()
  159. return 0
  160. def gen_usage(parser, command):
  161. """Modifies an OptionParser object with the command's documentation.
  162. The documentation is taken from the function's docstring.
  163. """
  164. obj = Command(command)
  165. more = getattr(obj, 'func_usage_more')
  166. # OptParser.description prefer nicely non-formatted strings.
  167. parser.description = obj.__doc__ + '\n'
  168. parser.set_usage('usage: %%prog %s %s' % (command, more))
  169. def main(args=None):
  170. # Do it late so all commands are listed.
  171. # pylint: disable=E1101
  172. parser = OptionParser(version=__version__)
  173. if args is None:
  174. args = sys.argv[1:]
  175. if args:
  176. command = Command(args[0])
  177. if command:
  178. # "fix" the usage and the description now that we know the subcommand.
  179. gen_usage(parser, args[0])
  180. return command(parser, args[1:])
  181. # Not a known command. Default to help.
  182. gen_usage(parser, 'help')
  183. return CMDhelp(parser, args)
  184. if __name__ == "__main__":
  185. fix_encoding.fix_encoding()
  186. try:
  187. sys.exit(main())
  188. except KeyboardInterrupt:
  189. sys.stderr.write('interrupted\n')
  190. sys.exit(1)