A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

103 行
3.4 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
  4. from config.irc_config import *
  5. actions, data = None, None
  6. def call(a, d):
  7. global actions, data
  8. actions, data = a, d
  9. if data.host not in ADMINS:
  10. actions.say(data.chan, "\x02%s\x0F: you must be a bot admin to use this command." % data.nick)
  11. return
  12. if not data.args:
  13. actions.say(data.chan, "\x02%s\x0F: no arguments provided." % data.nick)
  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] == "pull":
  24. do_pull()
  25. else: # they asked us to do something we don't know
  26. actions.say(data.chan, "\x02%s\x0F: unknown argument: \x0303%s\x0301." % (data.nick, data.args[0]))
  27. def exec_shell(command):
  28. """execute a shell command and get the output"""
  29. command = shlex.split(command)
  30. result = subprocess.check_output(command, stderr=subprocess.STDOUT)
  31. return result
  32. def do_help():
  33. """display all commands"""
  34. help = ["\x0303branch\x0301 (show current branch)", "\x0303branches\x0301 (show all branches)",
  35. "\x0303checkout\x0301 (switch branches)", "\x0303pull\x0301 (update current branch)"]
  36. help = ', '.join(help)
  37. actions.say(data.chan, "\x02%s\x0F: sub-commands are: %s" % (data.nick, help))
  38. def do_branch():
  39. """get our current branch"""
  40. branch = exec_shell("git name-rev --name-only HEAD")
  41. branch = branch[:-1] # strip newline
  42. actions.say(data.chan, "\x02%s\x0F: currently on branch \x0302%s\x0301." % (data.nick, branch))
  43. def do_branches():
  44. """get list of branches"""
  45. branches = exec_shell("git branch")
  46. branches = branches[:-1] # strip newline
  47. branches = branches.replace('\n* ', ', ') # cleanup extraneous characters
  48. branches = branches.replace('* ', ' ')
  49. branches = branches.replace('\n ', ', ')
  50. branches = branches.strip()
  51. actions.say(data.chan, "\x02%s\x0F: branches: \x0302%s\x0301." % (data.nick, branches))
  52. def do_checkout():
  53. """switch branches"""
  54. try:
  55. branch = data.args[1]
  56. except IndexError: # no branch name provided
  57. actions.say(data.chan, "\x02%s\x0F: switch to which branch?" % data.nick)
  58. return
  59. try:
  60. result = exec_shell("git checkout %s" % branch)
  61. if "Already on" in result:
  62. actions.say(data.chan, "\x02%s\x0F: already on \x0302%s\x0301!" % (data.nick, branch))
  63. else:
  64. actions.say(data.chan, "\x02%s\x0F: switched to branch \x0302%s\x0301." % (data.nick, branch))
  65. except subprocess.CalledProcessError: # git couldn't switch branches
  66. actions.say(data.chan, "\x02%s\x0F: branch \x0302%s\x0301 does not exist!" % (data.nick, branch))
  67. def do_pull():
  68. """pull from remote repository"""
  69. branch = exec_shell("git name-rev --name-only HEAD")
  70. branch = branch[:-1] # strip newline
  71. actions.say(data.chan, "\x02%s\x0F: pulling from remote (currently on \x0302%s\x0301)..." % (data.nick, branch))
  72. result = exec_shell("git pull")
  73. if "Already up-to-date." in result:
  74. actions.say(data.chan, "\x02%s\x0F: done; no new changes." % data.nick)
  75. else:
  76. actions.say(data.chan, "\x02%s\x0F: done; new changes merged." % data.nick)