Home | History | Annotate | Download | only in commands
      1 # Copyright (c) 2013 Google Inc. All rights reserved.
      2 #
      3 # Redistribution and use in source and binary forms, with or without
      4 # modification, are permitted provided that the following conditions are
      5 # met:
      6 #
      7 #     * Redistributions of source code must retain the above copyright
      8 # notice, this list of conditions and the following disclaimer.
      9 #     * Redistributions in binary form must reproduce the above
     10 # copyright notice, this list of conditions and the following disclaimer
     11 # in the documentation and/or other materials provided with the
     12 # distribution.
     13 #
     14 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     15 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     16 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     17 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     18 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     19 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     20 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25 
     26 import logging
     27 from optparse import make_option
     28 import time
     29 import traceback
     30 
     31 from webkitpy.common.config.irc import update_wait_seconds
     32 from webkitpy.tool.bot.commitannouncer import CommitAnnouncer, CommitAnnouncerThread
     33 from webkitpy.tool.multicommandtool import AbstractDeclarativeCommand
     34 
     35 _log = logging.getLogger(__name__)
     36 
     37 
     38 class CommitAnnouncerCommand(AbstractDeclarativeCommand):
     39     name = "commit-announcer"
     40     help_text = "Start an IRC bot for announcing new git commits."
     41     show_in_main_help = True
     42 
     43     def __init__(self):
     44         options = [
     45             make_option("--irc-password", default=None, help="Specify IRC password to use."),
     46         ]
     47         AbstractDeclarativeCommand.__init__(self, options)
     48 
     49     def execute(self, options, args, tool):
     50         bot_thread = CommitAnnouncerThread(tool, options.irc_password)
     51         bot_thread.start()
     52         _log.info("Bot started")
     53         try:
     54             while bot_thread.is_alive():
     55                 bot_thread.bot.post_new_commits()
     56                 time.sleep(update_wait_seconds)
     57         except KeyboardInterrupt:
     58             _log.error("Terminated by keyboard interrupt")
     59         except Exception, e:
     60             _log.error("Unexpected error:")
     61             _log.error(traceback.format_exc())
     62 
     63         if bot_thread.is_alive():
     64             _log.info("Disconnecting bot")
     65             bot_thread.stop()
     66         else:
     67             _log.info("Bot offline")
     68         _log.info("Done")
     69