A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

189 行
8.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 by Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import logging
  23. from threading import Lock, Thread, enumerate as enumerate_threads
  24. from time import sleep, time
  25. from earwigbot.config import BotConfig
  26. from earwigbot.irc import Frontend, Watcher
  27. from earwigbot.managers import CommandManager, TaskManager
  28. from earwigbot.wiki import SitesDB
  29. __all__ = ["Bot"]
  30. class Bot(object):
  31. """
  32. The Bot class is the core of EarwigBot, essentially responsible for
  33. starting the various bot components and making sure they are all happy.
  34. EarwigBot has three components that can run independently of each other: an
  35. IRC front-end, an IRC watcher, and a wiki scheduler.
  36. * The IRC front-end runs on a normal IRC server and expects users to
  37. interact with it/give it commands.
  38. * The IRC watcher runs on a wiki recent-changes server and listens for
  39. edits. Users cannot interact with this part of the bot.
  40. * The wiki scheduler runs wiki-editing bot tasks in separate threads at
  41. user-defined times through a cron-like interface.
  42. The Bot() object is accessable from within commands and tasks as self.bot.
  43. This is the primary way to access data from other components of the bot.
  44. For example, our BotConfig object is accessable from bot.config, tasks
  45. can be started with bot.tasks.start(), and sites can be loaded from the
  46. wiki toolset with bot.wiki.get_site().
  47. """
  48. def __init__(self, root_dir, level=logging.INFO):
  49. self.config = BotConfig(root_dir, level)
  50. self.logger = logging.getLogger("earwigbot")
  51. self.commands = CommandManager(self)
  52. self.tasks = TaskManager(self)
  53. self.wiki = SitesDB(self.config)
  54. self.frontend = None
  55. self.watcher = None
  56. self.component_lock = Lock()
  57. self._keep_looping = True
  58. self.config.load()
  59. self.commands.load()
  60. self.tasks.load()
  61. def _start_irc_components(self):
  62. """Start the IRC frontend/watcher in separate threads if enabled."""
  63. if self.config.components.get("irc_frontend"):
  64. self.logger.info("Starting IRC frontend")
  65. self.frontend = Frontend(self)
  66. Thread(name="irc_frontend", target=self.frontend.loop).start()
  67. if self.config.components.get("irc_watcher"):
  68. self.logger.info("Starting IRC watcher")
  69. self.watcher = Watcher(self)
  70. Thread(name="irc_watcher", target=self.watcher.loop).start()
  71. def _start_wiki_scheduler(self):
  72. """Start the wiki scheduler in a separate thread if enabled."""
  73. def wiki_scheduler():
  74. while self._keep_looping:
  75. time_start = time()
  76. self.tasks.schedule()
  77. time_end = time()
  78. time_diff = time_start - time_end
  79. if time_diff < 60: # Sleep until the next minute
  80. sleep(60 - time_diff)
  81. if self.config.components.get("wiki_scheduler"):
  82. self.logger.info("Starting wiki scheduler")
  83. thread = Thread(name="wiki_scheduler", target=wiki_scheduler)
  84. thread.daemon = True # Stop if other threads stop
  85. thread.start()
  86. def _stop_irc_components(self, msg):
  87. """Request the IRC frontend and watcher to stop if enabled."""
  88. if self.frontend:
  89. self.frontend.stop(msg)
  90. if self.watcher:
  91. self.watcher.stop(msg)
  92. def _stop_task_threads(self):
  93. """Notify the user of which task threads are going to be killed.
  94. Unfortunately, there is no method right now of stopping task threads
  95. safely. This is because there is no way to tell them to stop like the
  96. IRC components can be told; furthermore, they are run as daemons, and
  97. daemon threads automatically stop without calling any __exit__ or
  98. try/finally code when all non-daemon threads stop. They were originally
  99. implemented as regular non-daemon threads, but this meant there was no
  100. way to completely stop the bot if tasks were running, because all other
  101. threads would exit and threading would absorb KeyboardInterrupts.
  102. The advantage of this is that stopping the bot is truly guarenteed to
  103. *stop* the bot, while the disadvantage is that the tasks are given no
  104. advance warning of their forced shutdown.
  105. """
  106. tasks = []
  107. non_tasks = self.config.components.keys() + ["MainThread", "reminder"]
  108. for thread in enumerate_threads():
  109. if thread.name not in non_tasks and thread.is_alive():
  110. tasks.append(thread.name)
  111. if tasks:
  112. log = "The following tasks will be killed: {0}"
  113. self.logger.warn(log.format(" ".join(tasks)))
  114. def run(self):
  115. """Main entry point into running the bot.
  116. Starts all config-enabled components and then enters an idle loop,
  117. ensuring that all components remain online and restarting components
  118. that get disconnected from their servers.
  119. """
  120. self.logger.info("Starting bot")
  121. self._start_irc_components()
  122. self._start_wiki_scheduler()
  123. while self._keep_looping:
  124. with self.component_lock:
  125. if self.frontend and self.frontend.is_stopped():
  126. self.logger.warn("IRC frontend has stopped; restarting")
  127. self.frontend = Frontend(self)
  128. Thread(name=name, target=self.frontend.loop).start()
  129. if self.watcher and self.watcher.is_stopped():
  130. self.logger.warn("IRC watcher has stopped; restarting")
  131. self.watcher = Watcher(self)
  132. Thread(name=name, target=self.watcher.loop).start()
  133. sleep(2)
  134. def restart(self, msg=None):
  135. """Reload config, commands, tasks, and safely restart IRC components.
  136. This is thread-safe, and it will gracefully stop IRC components before
  137. reloading anything. Note that you can safely reload commands or tasks
  138. without restarting the bot with bot.commands.load() or
  139. bot.tasks.load(). These should not interfere with running components
  140. or tasks.
  141. If given, 'msg' will be used as our quit message.
  142. """
  143. if msg:
  144. self.logger.info('Restarting bot ("{0}")'.format(msg))
  145. else:
  146. self.logger.info("Restarting bot")
  147. with self.component_lock:
  148. self._stop_irc_components(msg)
  149. self.config.load()
  150. self.commands.load()
  151. self.tasks.load()
  152. self._start_irc_components()
  153. def stop(self, msg=None):
  154. """Gracefully stop all bot components.
  155. If given, 'msg' will be used as our quit message.
  156. """
  157. if msg:
  158. self.logger.info('Stopping bot ("{0}")'.format(msg))
  159. else:
  160. self.logger.info("Stopping bot")
  161. with self.component_lock:
  162. self._stop_irc_components(msg)
  163. self._keep_looping = False
  164. self._stop_task_threads()