commit_queue.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 auth
  16. import fix_encoding
  17. import rietveld
  18. THIRD_PARTY_DIR = os.path.join(os.path.dirname(__file__), 'third_party')
  19. sys.path.insert(0, THIRD_PARTY_DIR)
  20. from cq_client import cq_pb2
  21. from protobuf26 import text_format
  22. def usage(more):
  23. def hook(fn):
  24. fn.func_usage_more = more
  25. return fn
  26. return hook
  27. def need_issue(fn):
  28. """Post-parse args to create a Rietveld object."""
  29. @functools.wraps(fn)
  30. def hook(parser, args, *extra_args, **kwargs):
  31. old_parse_args = parser.parse_args
  32. def new_parse_args(args=None, values=None):
  33. options, args = old_parse_args(args, values)
  34. auth_config = auth.extract_auth_config_from_options(options)
  35. if not options.issue:
  36. parser.error('Require --issue')
  37. obj = rietveld.Rietveld(options.server, auth_config, options.user)
  38. return options, args, obj
  39. parser.parse_args = new_parse_args
  40. parser.add_option(
  41. '-u', '--user',
  42. metavar='U',
  43. default=os.environ.get('EMAIL_ADDRESS', None),
  44. help='Email address, default: %default')
  45. parser.add_option(
  46. '-i', '--issue',
  47. metavar='I',
  48. type='int',
  49. help='Rietveld issue number')
  50. parser.add_option(
  51. '-s',
  52. '--server',
  53. metavar='S',
  54. default='http://codereview.chromium.org',
  55. help='Rietveld server, default: %default')
  56. auth.add_auth_options(parser)
  57. # Call the original function with the modified parser.
  58. return fn(parser, args, *extra_args, **kwargs)
  59. hook.func_usage_more = '[options]'
  60. return hook
  61. def _apply_on_issue(fun, obj, issue):
  62. """Applies function 'fun' on an issue."""
  63. try:
  64. return fun(obj.get_issue_properties(issue, False))
  65. except urllib2.HTTPError, e:
  66. if e.code == 404:
  67. print >> sys.stderr, 'Issue %d doesn\'t exist.' % issue
  68. elif e.code == 403:
  69. print >> sys.stderr, 'Access denied to issue %d.' % issue
  70. else:
  71. raise
  72. return 1
  73. def get_commit(obj, issue):
  74. """Gets the commit bit flag of an issue."""
  75. def _get_commit(properties):
  76. print int(properties['commit'])
  77. return 0
  78. _apply_on_issue(_get_commit, obj, issue)
  79. def set_commit(obj, issue, flag):
  80. """Sets the commit bit flag on an issue."""
  81. def _set_commit(properties):
  82. print obj.set_flag(issue, properties['patchsets'][-1], 'commit', flag)
  83. return 0
  84. _apply_on_issue(_set_commit, obj, issue)
  85. def get_master_builder_map(
  86. config_path, include_experimental=True, include_triggered=True):
  87. """Returns a map of master -> [builders] from cq config."""
  88. with open(config_path) as config_file:
  89. cq_config = config_file.read()
  90. config = cq_pb2.Config()
  91. text_format.Merge(cq_config, config)
  92. masters = {}
  93. if config.HasField('verifiers') and config.verifiers.HasField('try_job'):
  94. for bucket in config.verifiers.try_job.buckets:
  95. masters.setdefault(bucket.name, [])
  96. for builder in bucket.builders:
  97. if (not include_experimental and
  98. builder.HasField('experiment_percentage')):
  99. continue
  100. if (not include_triggered and
  101. builder.HasField('triggered_by')):
  102. continue
  103. masters[bucket.name].append(builder.name)
  104. return masters
  105. @need_issue
  106. def CMDset(parser, args):
  107. """Sets the commit bit."""
  108. options, args, obj = parser.parse_args(args)
  109. if args:
  110. parser.error('Unrecognized args: %s' % ' '.join(args))
  111. return set_commit(obj, options.issue, '1')
  112. @need_issue
  113. def CMDget(parser, args):
  114. """Gets the commit bit."""
  115. options, args, obj = parser.parse_args(args)
  116. if args:
  117. parser.error('Unrecognized args: %s' % ' '.join(args))
  118. return get_commit(obj, options.issue)
  119. @need_issue
  120. def CMDclear(parser, args):
  121. """Clears the commit bit."""
  122. options, args, obj = parser.parse_args(args)
  123. if args:
  124. parser.error('Unrecognized args: %s' % ' '.join(args))
  125. return set_commit(obj, options.issue, '0')
  126. def CMDbuilders(parser, args):
  127. """Prints json-formatted list of builders given a path to cq.cfg file.
  128. The output is a dictionary in the following format:
  129. {
  130. 'master_name': [
  131. 'builder_name',
  132. 'another_builder'
  133. ],
  134. 'another_master': [
  135. 'third_builder'
  136. ]
  137. }
  138. """
  139. parser.add_option('--include-experimental', action='store_true')
  140. parser.add_option('--exclude-experimental', action='store_false',
  141. dest='include_experimental')
  142. parser.add_option('--include-triggered', action='store_true')
  143. parser.add_option('--exclude-triggered', action='store_false',
  144. dest='include_triggered')
  145. # The defaults have been chosen because of backward compatbility.
  146. parser.set_defaults(include_experimental=True, include_triggered=True)
  147. options, args = parser.parse_args(args)
  148. if len(args) != 1:
  149. parser.error('Expected a single path to CQ config. Got: %s' %
  150. ' '.join(args))
  151. print json.dumps(get_master_builder_map(
  152. args[0],
  153. include_experimental=options.include_experimental,
  154. include_triggered=options.include_triggered))
  155. CMDbuilders.func_usage_more = '<path-to-cq-config>'
  156. def CMDvalidate(parser, args):
  157. """Validates a CQ config, returns 0 on valid config.
  158. BUGS: this doesn't do semantic validation, only verifies validity of protobuf.
  159. But don't worry - bad cq.cfg won't cause outages, luci-config service will
  160. not accept them, will send warning email, and continue using previous
  161. version.
  162. """
  163. _, args = parser.parse_args(args)
  164. if len(args) != 1:
  165. parser.error('Expected a single path to CQ config. Got: %s' %
  166. ' '.join(args))
  167. config = cq_pb2.Config()
  168. try:
  169. with open(args[0]) as config_file:
  170. text_config = config_file.read()
  171. text_format.Merge(text_config, config)
  172. # TODO(tandrii): provide an option to actually validate semantics of CQ
  173. # config.
  174. return 0
  175. except text_format.ParseError as e:
  176. print 'failed to parse cq.cfg: %s' % e
  177. return 1
  178. CMDvalidate.func_usage_more = '<path-to-cq-config>'
  179. ###############################################################################
  180. ## Boilerplate code
  181. class OptionParser(optparse.OptionParser):
  182. """An OptionParser instance with default options.
  183. It should be then processed with gen_usage() before being used.
  184. """
  185. def __init__(self, *args, **kwargs):
  186. optparse.OptionParser.__init__(self, *args, **kwargs)
  187. self.add_option(
  188. '-v', '--verbose', action='count', default=0,
  189. help='Use multiple times to increase logging level')
  190. def parse_args(self, args=None, values=None):
  191. options, args = optparse.OptionParser.parse_args(self, args, values)
  192. levels = [logging.WARNING, logging.INFO, logging.DEBUG]
  193. logging.basicConfig(
  194. level=levels[min(len(levels) - 1, options.verbose)],
  195. format='%(levelname)s %(filename)s(%(lineno)d): %(message)s')
  196. return options, args
  197. def format_description(self, _):
  198. """Removes description formatting."""
  199. return self.description.rstrip() + '\n'
  200. def Command(name):
  201. return getattr(sys.modules[__name__], 'CMD' + name, None)
  202. @usage('<command>')
  203. def CMDhelp(parser, args):
  204. """Print list of commands or use 'help <command>'."""
  205. # Strip out the help command description and replace it with the module
  206. # docstring.
  207. parser.description = sys.modules[__name__].__doc__
  208. parser.description += '\nCommands are:\n' + '\n'.join(
  209. ' %-12s %s' % (
  210. fn[3:], Command(fn[3:]).__doc__.split('\n', 1)[0].rstrip('.'))
  211. for fn in dir(sys.modules[__name__]) if fn.startswith('CMD'))
  212. _, args = parser.parse_args(args)
  213. if len(args) == 1 and args[0] != 'help':
  214. return main(args + ['--help'])
  215. parser.print_help()
  216. return 0
  217. def gen_usage(parser, command):
  218. """Modifies an OptionParser object with the command's documentation.
  219. The documentation is taken from the function's docstring.
  220. """
  221. obj = Command(command)
  222. more = getattr(obj, 'func_usage_more')
  223. # OptParser.description prefer nicely non-formatted strings.
  224. parser.description = obj.__doc__ + '\n'
  225. parser.set_usage('usage: %%prog %s %s' % (command, more))
  226. def main(args=None):
  227. # Do it late so all commands are listed.
  228. # pylint: disable=no-member
  229. parser = OptionParser(version=__version__)
  230. if args is None:
  231. args = sys.argv[1:]
  232. if args:
  233. command = Command(args[0])
  234. if command:
  235. # "fix" the usage and the description now that we know the subcommand.
  236. gen_usage(parser, args[0])
  237. return command(parser, args[1:])
  238. # Not a known command. Default to help.
  239. gen_usage(parser, 'help')
  240. return CMDhelp(parser, args)
  241. if __name__ == "__main__":
  242. fix_encoding.fix_encoding()
  243. try:
  244. sys.exit(main())
  245. except KeyboardInterrupt:
  246. sys.stderr.write('interrupted\n')
  247. sys.exit(1)