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

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