Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
bzr hooks (py, rb, php syntax check)
#!/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)
if ext == 'rb':
(rc,mess) = commands.getstatusoutput("ruby -c %s | grep 'syntax error'"%file)
if mess != '': messages.append('[rb] %s'%mess)
if ext == 'php':
(rc,mess) = commands.getstatusoutput("php -l %s | grep 'Parse error'"%file)
if mess != '': messages.append('[php] %s'%mess)
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)')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment