Created
September 26, 2011 12:29
-
-
Save dopuskh3/1242120 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import fnmatch | |
import logging | |
import subprocess | |
def Logger(): | |
return logging.getLogger("post-recieve-hook") | |
class GitPostRevieveHookHelper(object): | |
def __init__(self): | |
self._dCallbacks = {} | |
def runCommand(self, psCommand): | |
lsCommand = psCommand | |
if isinstance(psCommand, list): | |
lsCommand = ' '.join(psCommand) | |
loCommand = subprocess.Popen(lsCommand, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
ltOut = loCommand.communicate() | |
if loCommand.returncode != 0: | |
Logger().error("Command %s failed: %s, %s, %d" % (psCommand, ltOut[0], ltOut[1], loCommand.returncode)) | |
else: | |
Logger().debug("Run %s with the following result %s, %s , %d" % (psCommand, | |
ltOut[0], | |
ltOut[1], | |
loCommand.returncode)) | |
return dict(stdout=ltOut[0], | |
stderr=ltOut[1], | |
code=loCommand.returncode) | |
def getChangedFiles(self, psOldRev, psNewRev): | |
ldOut = self.runCommand("git diff --name-only %s %s" % (psOldRev, psNewRev)) | |
if ldOut.get('code') == 0: | |
return ldOut.get('stdout').splitlines() | |
Logger().error("Command git diff failed.") | |
def registerCallBack(self, psFileGlob, pcCallback): | |
self._dCallbacks[psFileGlob] = pcCallback | |
def _isMatchCallback(self, psGlob, psName): | |
return fnmatch.fnmatch(psName, psGlob) | |
def _runFile(self, psFilename, psOldRev, psNewRev): | |
for lsGlob, lcCallback in self._dCallbacks.items(): | |
if self._isMatchCallback(lsGlob, psFilename): | |
lcCallback(psFilename, psOldRev, psNewRev) | |
def run(self, psOldRev, psNewRev): | |
for lsFile in self.getChangedFiles(psOldRev, psNewRev): | |
self._runFile(lsFile, psOldRev, psNewRev) | |
def printFile(psFile, psOldRev, psNewRev): | |
print psFile, psOldRev, psNewRev | |
if __name__ == "__main__": | |
lsParams = sys.stdin.readline() | |
(lsOldRev, lsNewRev, lsRefName) = lsParams.split(' ') | |
loHook = GitPostRevieveHookHelper() | |
loHook.registerCallBack("config/*", printFile) | |
loHook.run(lsOldRev, lsNewRev) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment