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.

git.py 5.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. from irc.base_command import BaseCommand
  6. class Git(BaseCommand):
  7. def get_hook(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. try:
  83. result = self.exec_shell("git checkout %s" % branch)
  84. if "Already on" in result:
  85. self.connection.reply(self.data, "already on \x0302%s\x0301!" % branch)
  86. else:
  87. self.connection.reply(self.data, "switched to branch \x0302%s\x0301." % branch)
  88. except subprocess.CalledProcessError: # git couldn't switch branches
  89. self.connection.reply(self.data, "branch \x0302%s\x0301 doesn't exist!" % branch)
  90. def do_delete(self):
  91. """delete a branch, while making sure that we are not on it"""
  92. try:
  93. delete_branch = self.data.args[1]
  94. except IndexError: # no branch name provided
  95. self.connection.reply(self.data, "delete which branch?")
  96. return
  97. current_branch = self.exec_shell("git name-rev --name-only HEAD")
  98. if current_branch == delete_branch:
  99. self.connection.reply(self.data, "you're currently on this branch; please checkout to a different branch before deleting.")
  100. return
  101. try:
  102. self.exec_shell("git branch -d %s" % delete_branch)
  103. self.connection.reply(self.data, "branch \x0302%s\x0301 has been deleted locally." % delete_branch)
  104. except subprocess.CalledProcessError: # git couldn't delete
  105. self.connection.reply(self.data, "branch \x0302%s\x0301 doesn't exist!" % delete_branch)
  106. def do_pull(self):
  107. """pull from remote repository"""
  108. branch = self.exec_shell("git name-rev --name-only HEAD")
  109. self.connection.reply(self.data, "pulling from remote (currently on \x0302%s\x0301)..." % branch)
  110. result = self.exec_shell("git pull")
  111. if "Already up-to-date." in result:
  112. self.connection.reply(self.data, "done; no new changes.")
  113. else:
  114. changes = re.findall("\s*((.*?)\sfile(.*?)tions?\(-\))", result)[0][0] # find the changes
  115. self.connection.reply(self.data, "done; %s." % changes)
  116. def do_status(self):
  117. """check whether we have anything to pull"""
  118. self.connection.reply(self.data, "checking remote for updates...")
  119. result = self.exec_shell("git fetch --dry-run")
  120. if not result:
  121. self.connection.reply(self.data, "local copy is up-to-date with remote.")
  122. else:
  123. self.connection.reply(self.data, "remote is ahead of local copy.")