A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. 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.reply(data.chan, data.nick, "you must be a bot admin to use this command.")
  11. return
  12. if not data.args:
  13. actions.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. actions.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 out of 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. actions.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. actions.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. actions.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. actions.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. actions.reply(data.chan, data.nick, "already on \x0302%s\x0301!" % branch)
  78. else:
  79. actions.reply(data.chan, data.nick, "switched to branch \x0302%s\x0301." % branch)
  80. except subprocess.CalledProcessError: # git couldn't switch branches
  81. actions.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. actions.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. actions.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. actions.reply(data.chan, data.nick, "branch \x0302%s\x0301 has been deleted locally." % delete_branch)
  97. except subprocess.CalledProcessError: # git couldn't delete
  98. actions.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. actions.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. actions.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. actions.reply(data.chan, data.nick, "done; %s." % changes)
  110. def do_status():
  111. """check whether we have anything to pull"""
  112. result = exec_shell("git fetch --dry-run")
  113. if not result:
  114. actions.reply(data.chan, data.nick, "local copy is up-to-date with remote.")
  115. else:
  116. actions.reply(data.chan, data.nick, "remote is ahead of local copy.")