A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

git.py 6.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 import *
  5. from irc.base_command import BaseCommand
  6. class Git(BaseCommand):
  7. def get_hooks(self):
  8. return ["msg"]
  9. def get_help(self, command):
  10. return "Commands to interface with the bot's git repository; use '!git help' for sub-command list."
  11. def check(self, data):
  12. if data.is_command and data.command == "git":
  13. return True
  14. return False
  15. def process(self, data):
  16. self.data = data
  17. if data.host not in OWNERS:
  18. self.connection.reply(data, "you must be a bot owner to use this command.")
  19. return
  20. if not data.args:
  21. self.connection.reply(data, "no arguments provided. Maybe you wanted '!git help'?")
  22. return
  23. if data.args[0] == "help":
  24. self.do_help()
  25. elif data.args[0] == "branch":
  26. self.do_branch()
  27. elif data.args[0] == "branches":
  28. self.do_branches()
  29. elif data.args[0] == "checkout":
  30. self.do_checkout()
  31. elif data.args[0] == "delete":
  32. self.do_delete()
  33. elif data.args[0] == "pull":
  34. self.do_pull()
  35. elif data.args[0] == "status":
  36. self.do_status()
  37. else: # they asked us to do something we don't know
  38. self.connection.reply(data, "unknown argument: \x0303%s\x0301." % data.args[0])
  39. def exec_shell(self, command):
  40. """execute a shell command and get the output"""
  41. command = shlex.split(command)
  42. result = subprocess.check_output(command, stderr=subprocess.STDOUT)
  43. if result:
  44. result = result[:-1] # strip newline
  45. return result
  46. def do_help(self):
  47. """display all commands"""
  48. help_dict = {
  49. "branch": "get current branch",
  50. "branches": "get all branches",
  51. "checkout": "switch branches",
  52. "delete": "delete an old branch",
  53. "pull": "update everything from the remote server",
  54. "status": "check if we are up-to-date",
  55. }
  56. keys = help_dict.keys()
  57. keys.sort()
  58. help = ""
  59. for key in keys:
  60. help += "\x0303%s\x0301 (%s), " % (key, help_dict[key])
  61. help = help[:-2] # trim last comma and space
  62. self.connection.reply(self.data, "sub-commands are: %s." % help)
  63. def do_branch(self):
  64. """get our current branch"""
  65. branch = self.exec_shell("git name-rev --name-only HEAD")
  66. self.connection.reply(self.data, "currently on branch \x0302%s\x0301." % branch)
  67. def do_branches(self):
  68. """get list of branches"""
  69. branches = self.exec_shell("git branch")
  70. branches = branches.replace('\n* ', ', ') # cleanup extraneous characters
  71. branches = branches.replace('* ', ' ')
  72. branches = branches.replace('\n ', ', ')
  73. branches = branches.strip()
  74. self.connection.reply(self.data, "branches: \x0302%s\x0301." % branches)
  75. def do_checkout(self):
  76. """switch branches"""
  77. try:
  78. branch = self.data.args[1]
  79. except IndexError: # no branch name provided
  80. self.connection.reply(self.data, "switch to which branch?")
  81. return
  82. current_branch = self.exec_shell("git name-rev --name-only HEAD")
  83. try:
  84. result = self.exec_shell("git checkout %s" % branch)
  85. if "Already on" in result:
  86. self.connection.reply(self.data, "already on \x0302%s\x0301!" % branch)
  87. else:
  88. self.connection.reply(self.data, "switched from branch \x0302%s\x0301 to \x0302%s\x0301." % (current_branch, branch))
  89. except subprocess.CalledProcessError: # git couldn't switch branches
  90. self.connection.reply(self.data, "branch \x0302%s\x0301 doesn't exist!" % branch)
  91. def do_delete(self):
  92. """delete a branch, while making sure that we are not on it"""
  93. try:
  94. delete_branch = self.data.args[1]
  95. except IndexError: # no branch name provided
  96. self.connection.reply(self.data, "delete which branch?")
  97. return
  98. current_branch = self.exec_shell("git name-rev --name-only HEAD")
  99. if current_branch == delete_branch:
  100. self.connection.reply(self.data, "you're currently on this branch; please checkout to a different branch before deleting.")
  101. return
  102. try:
  103. self.exec_shell("git branch -d %s" % delete_branch)
  104. self.connection.reply(self.data, "branch \x0302%s\x0301 has been deleted locally." % delete_branch)
  105. except subprocess.CalledProcessError: # git couldn't delete
  106. self.connection.reply(self.data, "branch \x0302%s\x0301 doesn't exist!" % delete_branch)
  107. def do_pull(self):
  108. """pull from remote repository"""
  109. branch = self.exec_shell("git name-rev --name-only HEAD")
  110. self.connection.reply(self.data, "pulling from remote (currently on \x0302%s\x0301)..." % branch)
  111. result = self.exec_shell("git pull")
  112. if "Already up-to-date." in result:
  113. self.connection.reply(self.data, "done; no new changes.")
  114. else:
  115. changes = re.findall("\s*((.*?)\sfile(.*?)tions?\(-\))", result)[0][0] # find the changes
  116. try:
  117. remote = self.exec_shell("git config --get branch.%s.remote" % branch)
  118. url = self.exec_shell("git config --get remote.%s.url" % remote)
  119. self.connection.reply(self.data, "done; %s [from %s]." % (changes, url))
  120. except subprocess.CalledProcessError: # something in .git/config is not specified correctly, so we cannot get the remote's url
  121. self.connection.reply(self.data, "done; %s." % changes)
  122. def do_status(self):
  123. """check whether we have anything to pull"""
  124. last = self.exec_shell("git log -n 1 --pretty=\"%ar\"")
  125. result = self.exec_shell("git fetch --dry-run")
  126. if not result: # nothing was fetched, so remote and local are equal
  127. self.connection.reply(self.data, "last commit was %s. Local copy is \x02up-to-date\x0F with remote." % last)
  128. else:
  129. self.connection.reply(self.data, "last local commit was %s. Remote is \x02ahead\x0F of local copy." % last)