commit_queue.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 logging
  10. import optparse
  11. import os
  12. import sys
  13. import urllib2
  14. import breakpad # pylint: disable=W0611
  15. import fix_encoding
  16. import rietveld
  17. def usage(more):
  18. def hook(fn):
  19. fn.func_usage_more = more
  20. return fn
  21. return hook
  22. def need_issue(fn):
  23. """Post-parse args to create a Rietveld object."""
  24. @functools.wraps(fn)
  25. def hook(parser, args, *extra_args, **kwargs):
  26. old_parse_args = parser.parse_args
  27. def new_parse_args(args):
  28. options, args = old_parse_args(args)
  29. if not options.issue:
  30. parser.error('Require --issue')
  31. obj = rietveld.Rietveld(options.server, options.user, None)
  32. return options, args, obj
  33. parser.parse_args = new_parse_args
  34. parser.add_option(
  35. '-u', '--user',
  36. metavar='U',
  37. default=os.environ.get('EMAIL_ADDRESS', None),
  38. help='Email address, default: %default')
  39. parser.add_option(
  40. '-i', '--issue',
  41. metavar='I',
  42. type='int',
  43. help='Rietveld issue number')
  44. parser.add_option(
  45. '-s',
  46. '--server',
  47. metavar='S',
  48. default='http://codereview.chromium.org',
  49. help='Rietveld server, default: %default')
  50. # Call the original function with the modified parser.
  51. return fn(parser, args, *extra_args, **kwargs)
  52. hook.func_usage_more = '[options]'
  53. return hook
  54. def set_commit(obj, issue, flag):
  55. """Sets the commit bit flag on an issue."""
  56. try:
  57. patchset = obj.get_issue_properties(issue, False)['patchsets'][-1]
  58. print obj.set_flag(issue, patchset, 'commit', flag)
  59. except urllib2.HTTPError, e:
  60. if e.code == 404:
  61. print >> sys.stderr, 'Issue %d doesn\'t exist.' % issue
  62. elif e.code == 403:
  63. print >> sys.stderr, 'Access denied to issue %d.' % issue
  64. else:
  65. raise
  66. return 1
  67. @need_issue
  68. def CMDset(parser, args):
  69. """Sets the commit bit."""
  70. options, args, obj = parser.parse_args(args)
  71. if args:
  72. parser.error('Unrecognized args: %s' % ' '.join(args))
  73. return set_commit(obj, options.issue, '1')
  74. @need_issue
  75. def CMDclear(parser, args):
  76. """Clears the commit bit."""
  77. options, args, obj = parser.parse_args(args)
  78. if args:
  79. parser.error('Unrecognized args: %s' % ' '.join(args))
  80. return set_commit(obj, options.issue, '0')
  81. ###############################################################################
  82. ## Boilerplate code
  83. def gen_parser():
  84. """Returns an OptionParser instance with default options.
  85. It should be then processed with gen_usage() before being used.
  86. """
  87. parser = optparse.OptionParser(version=__version__)
  88. # Remove description formatting
  89. parser.format_description = (
  90. lambda _: parser.description) # pylint: disable=E1101
  91. # Add common parsing.
  92. old_parser_args = parser.parse_args
  93. def Parse(*args, **kwargs):
  94. options, args = old_parser_args(*args, **kwargs)
  95. logging.basicConfig(
  96. level=[logging.WARNING, logging.INFO, logging.DEBUG][
  97. min(2, options.verbose)],
  98. format='%(levelname)s %(filename)s(%(lineno)d): %(message)s')
  99. return options, args
  100. parser.parse_args = Parse
  101. parser.add_option(
  102. '-v', '--verbose', action='count', default=0,
  103. help='Use multiple times to increase logging level')
  104. return parser
  105. def Command(name):
  106. return getattr(sys.modules[__name__], 'CMD' + name, None)
  107. @usage('<command>')
  108. def CMDhelp(parser, args):
  109. """Print list of commands or use 'help <command>'."""
  110. # Strip out the help command description and replace it with the module
  111. # docstring.
  112. parser.description = sys.modules[__name__].__doc__
  113. parser.description += '\nCommands are:\n' + '\n'.join(
  114. ' %-12s %s' % (
  115. fn[3:], Command(fn[3:]).__doc__.split('\n', 1)[0].rstrip('.'))
  116. for fn in dir(sys.modules[__name__]) if fn.startswith('CMD'))
  117. _, args = parser.parse_args(args)
  118. if len(args) == 1 and args[0] != 'help':
  119. return main(args + ['--help'])
  120. parser.print_help()
  121. return 0
  122. def gen_usage(parser, command):
  123. """Modifies an OptionParser object with the command's documentation.
  124. The documentation is taken from the function's docstring.
  125. """
  126. obj = Command(command)
  127. more = getattr(obj, 'func_usage_more')
  128. # OptParser.description prefer nicely non-formatted strings.
  129. parser.description = obj.__doc__ + '\n'
  130. parser.set_usage('usage: %%prog %s %s' % (command, more))
  131. def main(args=None):
  132. # Do it late so all commands are listed.
  133. # pylint: disable=E1101
  134. parser = gen_parser()
  135. if args is None:
  136. args = sys.argv[1:]
  137. if args:
  138. command = Command(args[0])
  139. if command:
  140. # "fix" the usage and the description now that we know the subcommand.
  141. gen_usage(parser, args[0])
  142. return command(parser, args[1:])
  143. # Not a known command. Default to help.
  144. gen_usage(parser, 'help')
  145. return CMDhelp(parser, args)
  146. if __name__ == "__main__":
  147. fix_encoding.fix_encoding()
  148. sys.exit(main())