A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

153 řádky
5.0 KiB

  1. # -*- coding: utf-8 -*-
  2. """Commands to interface with the bot's git repository; use '!git help' for sub-command list."""
  3. import shlex, subprocess, re
  4. from config.irc_config import *
  5. connection, data = None, None
  6. def call(c, d):
  7. global connection, data
  8. connection, data = c, d
  9. if data.host not in OWNERS:
  10. connection.reply(data.chan, data.nick, "you must be a bot owner to use this command.")
  11. return
  12. if not data.args:
  13. connection.reply(data.chan, data.nick, "no arguments provided.")
  14. return
  15. if data.args[0] == "help":
  16. do_help()
  17. elif data.args[0] == "branch":
  18. do_branch()
  19. elif data.args[0] == "branches":
  20. do_branches()
  21. elif data.args[0] == "checkout":
  22. do_checkout()
  23. elif data.args[0] == "delete":
  24. do_delete()
  25. elif data.args[0] == "pull":
  26. do_pull()
  27. elif data.args[0] == "status":
  28. do_status()
  29. else: # they asked us to do something we don't know
  30. connection.reply(data.chan, data.nick, "unknown argument: \x0303%s\x0301." % data.args[0])
  31. def exec_shell(command):
  32. """execute a shell command and get the output"""
  33. command = shlex.split(command)
  34. result = subprocess.check_output(command, stderr=subprocess.STDOUT)
  35. return result
  36. def do_help():
  37. """display all commands"""
  38. help = ""
  39. help_dict = {
  40. "branch": "get current branch",
  41. "branches": "get all branches",
  42. "checkout": "switch branches",
  43. "delete": "delete an old branch",
  44. "pull": "update everything from the remote server",
  45. "status": "check if we are up-to-date",
  46. }
  47. keys = help_dict.keys()
  48. keys.sort()
  49. for key in keys:
  50. help += "\x0303%s\x0301 (%s), " % (key, help_dict[key])
  51. help = help[:-2] # trim last comma
  52. connection.reply(data.chan, data.nick, "sub-commands are: %s." % help)
  53. def do_branch():
  54. """get our current branch"""
  55. branch = exec_shell("git name-rev --name-only HEAD")
  56. branch = branch[:-1] # strip newline
  57. connection.reply(data.chan, data.nick, "currently on branch \x0302%s\x0301." % branch)
  58. def do_branches():
  59. """get list of branches"""
  60. branches = exec_shell("git branch")
  61. branches = branches[:-1] # strip newline
  62. branches = branches.replace('\n* ', ', ') # cleanup extraneous characters
  63. branches = branches.replace('* ', ' ')
  64. branches = branches.replace('\n ', ', ')
  65. branches = branches.strip()
  66. connection.reply(data.chan, data.nick, "branches: \x0302%s\x0301." % branches)
  67. def do_checkout():
  68. """switch branches"""
  69. try:
  70. branch = data.args[1]
  71. except IndexError: # no branch name provided
  72. connection.reply(data.chan, data.nick, "switch to which branch?")
  73. return
  74. try:
  75. result = exec_shell("git checkout %s" % branch)
  76. if "Already on" in result:
  77. connection.reply(data.chan, data.nick, "already on \x0302%s\x0301!" % branch)
  78. else:
  79. connection.reply(data.chan, data.nick, "switched to branch \x0302%s\x0301." % branch)
  80. except subprocess.CalledProcessError: # git couldn't switch branches
  81. connection.reply(data.chan, data.nick, "branch \x0302%s\x0301 doesn't exist!" % branch)
  82. def do_delete():
  83. """delete a branch, while making sure that we are not on it"""
  84. try:
  85. delete_branch = data.args[1]
  86. except IndexError: # no branch name provided
  87. connection.reply(data.chan, data.nick, "delete which branch?")
  88. return
  89. current_branch = exec_shell("git name-rev --name-only HEAD")
  90. current_branch = current_branch[:-1] # strip newline
  91. if current_branch == delete_branch:
  92. connection.reply(data.chan, data.nick, "you're currently on this branch; please checkout to a different branch before deleting.")
  93. return
  94. try:
  95. exec_shell("git branch -d %s" % delete_branch)
  96. connection.reply(data.chan, data.nick, "branch \x0302%s\x0301 has been deleted locally." % delete_branch)
  97. except subprocess.CalledProcessError: # git couldn't delete
  98. connection.reply(data.chan, data.nick, "branch \x0302%s\x0301 doesn't exist!" % delete_branch)
  99. def do_pull():
  100. """pull from remote repository"""
  101. branch = exec_shell("git name-rev --name-only HEAD")
  102. branch = branch[:-1] # strip newline
  103. connection.reply(data.chan, data.nick, "pulling from remote (currently on \x0302%s\x0301)..." % branch)
  104. result = exec_shell("git pull")
  105. if "Already up-to-date." in result:
  106. connection.reply(data.chan, data.nick, "done; no new changes.")
  107. else:
  108. changes = re.findall("\s*((.*?)\sfile(.*?)tions?\(-\))", result)[0][0] # find the changes
  109. connection.reply(data.chan, data.nick, "done; %s." % changes)
  110. def do_status():
  111. """check whether we have anything to pull"""
  112. connection.reply(data.chan, data.nick, "checking remote for updates...")
  113. result = exec_shell("git fetch --dry-run")
  114. if not result:
  115. connection.reply(data.chan, data.nick, "local copy is up-to-date with remote.")
  116. else:
  117. connection.reply(data.chan, data.nick, "remote is ahead of local copy.")