A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

92 рядки
3.9 KiB

  1. # -*- coding: utf-8 -*-
  2. import string, re, subprocess
  3. from config import *
  4. s, send, say, action, notice, join = None, None, None, None, None, None
  5. def check_triggers(cmds, act, nick, ident, host, chan, msg = None):
  6. global s, send, say, action, notice, join # set commands as globals so we can use them in other functions
  7. s, send, say, action, notice, join = cmds # unpack commands
  8. if act == "join":
  9. pass
  10. if act == "msg_private":
  11. pass
  12. if act == "msg_public":
  13. pass
  14. if act == "msg":
  15. if msg == "!test":
  16. cmd_test(nick, chan)
  17. if msg.startswith("!git"):
  18. cmd_git(nick, host, chan, msg)
  19. def get_args(msg): # get command arguments
  20. args = msg.strip().split(' ') # strip out extra whitespace and split the message into a list
  21. while '' in args: # remove any empty arguments
  22. args.remove('')
  23. return args[1:] # remove the command itself
  24. def cmd_test(nick, chan): # bot test
  25. say(chan, "Hey \x02%s\x0F!" % nick)
  26. def cmd_git(nick, host, chan, msg): # commands to interface with the bot's git repository
  27. if host not in ADMINS:
  28. say(chan, "\x02%s\x0F: you must be a bot admin to use this command." % nick)
  29. return
  30. args = get_args(msg)
  31. if not args:
  32. say(chan, "\x02%s\x0F: no arguments provided." % nick)
  33. return
  34. if args[0] == "help": # display all commands
  35. cmds = ["branch (show current branch)", "branches (show all branches)", "checkout (switch branches)", "pull (update current branch)"]
  36. cmds = ', '.join(cmds)
  37. say(chan, "\x02%s\x0F: sub-commands are: %s" % (nick, cmds))
  38. elif args[0] == "branch": # get our current branch
  39. branch = subprocess.check_output(['git', 'name-rev', '--name-only', 'HEAD'], stderr=subprocess.STDOUT) # git name-rev --name-only HEAD
  40. branch = branch[:-1] # strip newline
  41. say(chan, "\x02%s\x0F: currently on branch '\x0302%s\x0301'." % (nick, branch))
  42. elif args[0] == "branches": # get list of branches
  43. branches = subprocess.check_output(['git', 'branch'], stderr=subprocess.STDOUT) # git branch
  44. branches = branches[:-1] # strip newline
  45. branches = branches.replace('\n* ', ', ') # cleanup extraneous characters
  46. branches = branches.replace('* ', ' ')
  47. branches = branches.replace('\n ', ', ')
  48. branches = branches.strip()
  49. say(chan, "\x02%s\x0F: branches: \x0302%s\x0301." % (nick, branches))
  50. elif args[0] == "checkout": # switch branches
  51. try:
  52. branch = args[1]
  53. except IndexError: # no branch name provided
  54. say(chan, "\x02%s\x0F: switch to which branch?" % nick)
  55. return
  56. try:
  57. result = subprocess.check_output(['git', 'checkout', branch], stderr=subprocess.STDOUT) # git checkout our_branch
  58. if "Already on" in result:
  59. say(chan, "\x02%s\x0F: already on '\x0302%s\x0301'!" % (nick, branch))
  60. else:
  61. say(chan, "\x02%s\x0F: switched to branch '\x0302%s\x0301'." % (nick, branch))
  62. except subprocess.CalledProcessError: # git couldn't switch branches
  63. say(chan, "\x02%s\x0F: branch '\x0302%s\x0301' does not exist!" % (nick, branch))
  64. elif args[0] == "pull": # pull from remote repository
  65. branch = subprocess.check_output(['git', 'name-rev', '--name-only', 'HEAD'], stderr=subprocess.STDOUT) # git name-rev --name-only HEAD
  66. branch = branch[:-1] # strip newline
  67. say(chan, "\x02%s\x0F: pulling from remote (currently on '\x0302%s\x0301')..." % (nick, branch))
  68. result = subprocess.check_output(['git', 'pull']) # pull from remote
  69. if "Already up-to-date." in result:
  70. say(chan, "\x02%s\x0F: done; no new changes." % nick)
  71. else:
  72. say(chan, "\x02%s\x0F: done; new changes merged." % nick)
  73. else:
  74. say(chan, "\x02%s\x0F: unknown argument: '\x0302%s\x0301'." % (nick, arg[0]))