public
Last active

bzr hooks (py, rb, php syntax check)

  • Download Gist
bzr_precommit_lint.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# this is a plugin/hook for bazaar. just add this file to ~/.bazaar/plugins/precommit"""
#
# @author: starenka
# @email: starenka0[at]gmail[dot]com
# @version: 1.1
# @since Feb 28, 2010
 
import os
from bzrlib.branch import Branch
 
RED = '\033[31m'
RESET = '\033[0;0m'
BOLD = '\033[1m'
 
def check_syntax(local, master, old_revno, old_revid, future_revno, future_revid, tree_delta, future_tree):
import commands,py_compile
from bzrlib import errors
from bzrlib import urlutils
BASE_PATH = urlutils.local_path_from_url(master.base)
 
messages = []
for i in tree_delta.added + tree_delta.modified:
if i[0].find('.') == -1: continue
ext = i[0].split('.')[-1:][0]
file = os.path.join(BASE_PATH,i[0])
for ext in [ext]:
if ext == 'py':
try: py_compile.compile(file,'/tmp/x','/tmp%s'%file,True)
except py_compile.PyCompileError,e: messages.append('[py] %s'%e)
break
if ext == 'rb':
(rc,mess) = commands.getstatusoutput("ruby -c %s | grep 'syntax error'"%file)
if mess != '': messages.append('[rb] %s'%mess)
break
if ext == 'php':
(rc,mess) = commands.getstatusoutput("php -l %s | grep 'Parse error'"%file)
if mess != '': messages.append('[php] %s'%mess)
break
if messages:
print '\n%s%s%s%s'%(BOLD,RED,'\n'.join(messages),RESET)
raise errors.BzrError("Precommit hook failed. Try harder :p")
Branch.hooks.install_named_hook('pre_commit', check_syntax,'runnig lint (php,py,rb)')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.