A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Commands to interface with the bot's git repository.
  3. import shlex, subprocess
  4. from config.irc_config import *
  5. actions, data = None, None
  6. args = None
  7. def call(a, d):
  8. global actions, data
  9. actions, data = a, d
  10. if not check_user_is_admin():
  11. return
  12. get_args()
  13. if not check_has_args():
  14. return
  15. if args[0] == "help":
  16. do_help()
  17. elif args[0] == "branch":
  18. do_branch()
  19. elif args[0] == "branches":
  20. do_branches()
  21. elif args[0] == "checkout":
  22. do_checkout()
  23. elif args[0] == "pull":
  24. do_pull()
  25. else:
  26. unknown_arg() # they asked us to do something we don't know
  27. def get_args():
  28. """get command arguments"""
  29. global args
  30. args = data.msg.strip().split(' ') # strip out extra whitespace and split the message into a list
  31. while '' in args: # remove any empty arguments
  32. args.remove('')
  33. args = args[1:] # remove the command itself
  34. def check_user_is_admin():
  35. """check if the user is a bot admin (and can use this command, as a result)"""
  36. if data.host not in ADMINS:
  37. actions.say(data.chan, "\x02%s\x0F: you must be a bot admin to use this command." % data.nick)
  38. return False
  39. return True
  40. def check_has_args():
  41. """check if they provided arguments along with the !git command"""
  42. if not args:
  43. actions.say(data.chan, "\x02%s\x0F: no arguments provided." % data.nick)
  44. return False
  45. return True
  46. def exec_shell(command):
  47. """execute a shell command and get the output"""
  48. command = shlex.split(command)
  49. result = subprocess.check_output(command, stderr=subprocess.STDOUT)
  50. return result
  51. def do_help():
  52. """display all commands"""
  53. help = ["\x0303branch\x0301 (show current branch)", "\x0303branches\x0301 (show all branches)",
  54. "\x0303checkout\x0301 (switch branches)", "\x0303pull\x0301 (update current branch)"]
  55. help = ', '.join(help)
  56. actions.say(data.chan, "\x02%s\x0F: sub-commands are: %s" % (data.nick, help))
  57. def do_branch():
  58. """get our current branch"""
  59. branch = exec_shell("git name-rev --name-only HEAD")
  60. branch = branch[:-1] # strip newline
  61. actions.say(data.chan, "\x02%s\x0F: currently on branch \x0302%s\x0301." % (data.nick, branch))
  62. def do_branches():
  63. """get list of branches"""
  64. branches = exec_shell("git branch")
  65. branches = branches[:-1] # strip newline
  66. branches = branches.replace('\n* ', ', ') # cleanup extraneous characters
  67. branches = branches.replace('* ', ' ')
  68. branches = branches.replace('\n ', ', ')
  69. branches = branches.strip()
  70. actions.say(data.chan, "\x02%s\x0F: branches: \x0302%s\x0301." % (data.nick, branches))
  71. def do_checkout():
  72. """switch branches"""
  73. try:
  74. branch = args[1]
  75. except IndexError: # no branch name provided
  76. actions.say(data.chan, "\x02%s\x0F: switch to which branch?" % data.nick)
  77. return
  78. try:
  79. result = exec_shell("git checkout %s" % branch)
  80. if "Already on" in result:
  81. actions.say(data.chan, "\x02%s\x0F: already on \x0302%s\x0301!" % (data.nick, branch))
  82. else:
  83. actions.say(data.chan, "\x02%s\x0F: switched to branch \x0302%s\x0301." % (data.nick, branch))
  84. except subprocess.CalledProcessError: # git couldn't switch branches
  85. actions.say(data.chan, "\x02%s\x0F: branch \x0302%s\x0301 does not exist!" % (data.nick, branch))
  86. def do_pull():
  87. """pull from remote repository"""
  88. branch = exec_shell("git name-rev --name-only HEAD")
  89. branch = branch[:-1] # strip newline
  90. actions.say(data.chan, "\x02%s\x0F: pulling from remote (currently on \x0302%s\x0301)..." % (data.nick, branch))
  91. result = exec_shell("git pull")
  92. if "Already up-to-date." in result:
  93. actions.say(data.chan, "\x02%s\x0F: done; no new changes." % data.nick)
  94. else:
  95. actions.say(data.chan, "\x02%s\x0F: done; new changes merged." % data.nick)
  96. def unknown_arg():
  97. actions.say(data.chan, "\x02%s\x0F: unknown argument: \x0303%s\x0301." % (data.nick, args[0]))