Skip to content

Instantly share code, notes, and snippets.

@imankulov
Created March 21, 2012 10:08
Show Gist options
  • Save imankulov/2145938 to your computer and use it in GitHub Desktop.
Save imankulov/2145938 to your computer and use it in GitHub Desktop.
Python hook skeleton for gitolite
#!/usr/bin/env python
"""
Python hook skeleton for gitolite.
- Place this file as ~/.gitolite/hooks/common/<hook-name> (i.e. as
~/.gitolite/hooks/common/post-receive)
- Run "gl-setup" in console
- Define a number of function with @hook() decorators inside
Providing such configuration, every function is executed as a hook for a
repository which is provided either in @hook('argument') or in the function
name itself.
For example, following hook will be executed for the repository named 'repo'
(don't forget about the parentheses, please):
@hook()
def repo():
...
This is the hook for the repository 'another-repo'.
@hook('another-repo')
def another_repo_hook():
...
For more details about gitolite hooks see
http://sitaramc.github.com/gitolite/hooks.html
Hint: try to use it with OpenSSH wrapper (https://github.com/NetAngels/openssh-wrapper).
It can make it quite handy to execute commands on a remote host.
"""
#-----------------------------------------------------------------------------
# Define some imports, decorator and the hook storage
#-----------------------------------------------------------------------------
import os
import subprocess
hook_storage = {}
class hook(object):
def __init__(self, reponame=None):
self.reponame = reponame
def __call__(self, func):
reponame = self.reponame or func.__name__
hook_storage[reponame] = func
return func
#-----------------------------------------------------------------------------
# Sample hooks
#-----------------------------------------------------------------------------
@hook()
def repo():
subprocess.call(['ls', '-l'])
print 'testing done'
@hook('another-repo')
def another_repo_hook():
subprocess.call(['ls', '-l'])
print 'testing 2 done'
#-----------------------------------------------------------------------------
# Run hooks
#-----------------------------------------------------------------------------
if __name__ == '__main__':
repo = os.environ.get('GL_REPO')
hook_callable = hook_storage.get(repo)
if hook_callable:
hook_callable()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment