public
Last active

git hooks

  • Download Gist
Makefile
Makefile
1 2 3 4 5 6 7 8 9 10 11 12 13
all: install
install:
chmod +x ./pre-commit
chmod +x ./post-commit
 
sudo rm -fR /usr/share/git-core/templates/hooks
sudo mkdir /usr/share/git-core/templates/hooks
sudo cp $(shell pwd)/_autopep8.py /usr/share/git-core/templates/hooks/_autopep8.py
sudo cp $(shell pwd)/_delete.pyc.py /usr/share/git-core/templates/hooks/_delete.pyc.py
sudo cp $(shell pwd)/_pylint.py /usr/share/git-core/templates/hooks/_pylint.py
sudo cp $(shell pwd)/post-commit /usr/share/git-core/templates/hooks/post-commit
sudo cp $(shell pwd)/pre-commit /usr/share/git-core/templates/hooks/pre-commit
_autopep8.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#!/usr/bin/env python
from os import getcwd, walk
from os.path import join, relpath,splitext
from sys import stdout
from autopep8 import FixPEP8
from shebang import python
from growlnotify import growlnotify
 
 
def files(directory):
result = []
for root, dirs, files in walk(directory):
try:
dirs.remove('.git')
except ValueError:
pass
for name in files:
f = join(root, name)
result.append(f)
return result
 
# autopep8
 
 
class options:
ignore = "E501"
select = []
verbose = False
max_line_length = 79
aggressive = True
 
autofixed=[]
 
def autofix(f):
source = open(f).read() # original
contents = source
fixed = False
while not fixed:
pep = FixPEP8(None, options(), contents=contents)
fix = pep.fix()
fixed = fix == contents
contents = fix
if fix[-1] != "\n":
fix = fix + "\n"
if source != fix:
autofixed.append(relpath(f,getcwd()))
open(f, "w").write(fix)
 
for f in files(getcwd()):
if splitext(f)[1] == ".py" or python(f):
autofix(f)
 
if len(autofixed)>0:
print "\nautopep8:"+"\n".join(autofixed)+"\n"
if not stdout.isatty():
growlnotify("autopep8","\n".join(autofixed),app="Python")
_delete.pyc.py
Python
1 2 3 4 5
#!/usr/bin/env python
from glob import glob
from os import unlink
 
map(unlink, glob('*.pyc'))
_pylint.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#!/usr/bin/env python
from cStringIO import StringIO
from os.path import join, relpath, splitext
from os import getcwd, walk
from sys import exit, stdout
from pylint.lint import PyLinter
from pylint.reporters.text import ParseableTextReporter
from growlnotify import growlnotify
from shebang import python
 
 
def files(directory):
result = []
for root, dirs, files in walk(directory):
try:
dirs.remove('.git')
except ValueError:
pass
for name in files:
f = join(root, name)
result.append(f)
return result
 
l = PyLinter()
reporter = ParseableTextReporter()
reporter.set_output(StringIO())
l.set_reporter(reporter)
 
errors = []
for f in files(getcwd()):
if splitext(f)[1] == ".py" or python(f):
l.check("fixtures/errors.py")
if l.stats["error"] > 0:
errors.append(relpath(f, getcwd()))
 
if len(errors) > 0:
print "\npylint errors:" + "\n".join(errors) + "\n"
if not stdout.isatty(): # not terminal
growlnotify("pylint errors", "\n".join(errors), app="Python")
exit(1) # raise Exception
post-commit
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#!/usr/bin/env python
# post-commit
from growlnotify import growlnotify as notify
try:
from os.path import exists
from sys import stdout
from urlparse import urlparse
from githubhook import create, edit
from osascript import sudo
from pypixmlrpc import default
from setuppy.api import error, fullname, name, version
if exists("setup.py"):
sudo("python -m setuppy.install")
notify(fullname(), "python setup.py install", app="Python")
releases = default.package_releases(name())
if releases == [] or releases != [version()]:
result = sudo("python -m setuppy.upload")
notify(fullname(), "python setup.py sdist upload", app="Python")
if error(result):
notify(fullname(), error(result), app="Python")
else:
notify(
fullname(),
"Already exists in %s" % urlparse(default.url).netloc,
app="Python"
)
create() # git remote add github url if not exists and create remote repo
if exists("description"):
edit(description=open("description").read())
notify("post-commit OK", app="Terminal")
except Exception, e:
if stdout.isatty(): # terminal
print type(e), str(e)
else: # IDE
notify(str(e))
 
# Star my repositories and follow me to increase my open source power :)
pre-commit
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#!/usr/bin/env python
# pre-commit
 
try: # recreate hooks
from clonegithooks import clonehooks
clonehooks()
except Exception, e:
print e
from growlnotify import growlnotify
growlnotify(str(e), sticky=True)
 
# python hooks
execfile("/usr/share/git-core/templates/hooks/_autopep8.py")
execfile("/usr/share/git-core/templates/hooks/_delete.pyc.py")
execfile("/usr/share/git-core/templates/hooks/_pylint.py")
 
try:
from growlnotify import growlnotify
from os.path import exists
from envoy import run
from githubhook import https2git
from setuppy.api import check
 
https2git() # rename https://gist.github to git@gist.github
 
if exists("setup.py"): # python package
check() # python setup.py check
l = [
"/usr/local/bin/pandoc -o README.rst README.md",
"/usr/local/bin/coverage run /usr/local/bin/nosetests",
"/usr/local/bin/coverage html"
]
for cmd in l:
r = run(cmd)
if r.status_code != 0:
raise Exception(r.std_err)
growlnotify("pre-commit OK", app="Terminal")
except SystemExit: # sys.exit(1)
pass
except Exception, e:
from sys import stdout
print type(e), str(e)
if not stdout.isatty(): # not terminal
growlnotify(str(e))
 
# Star my repositories and follow me :)
# https://github.com/cancerhermit

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.