From 66b30d19cf524bbae55a178445fbb6079ba07765 Mon Sep 17 00:00:00 2001 From: Stephen Gran Date: Thu, 5 Apr 2012 07:59:22 +0100 Subject: [PATCH] add tools directory Signed-off-by: Stephen Gran --- tools/git-hooks/pre-commit | 151 ++++++++++++++++++++++++++++++ tools/vim/after/syntax/puppet.vim | 18 ++++ tools/vim/ftdetect/puppet.vim | 2 + tools/vim/ftplugin/puppet.vim | 94 +++++++++++++++++++ tools/vim/indent/puppet.vim | 76 +++++++++++++++ tools/vim/syntax/puppet.vim | 115 +++++++++++++++++++++++ 6 files changed, 456 insertions(+) create mode 100755 tools/git-hooks/pre-commit create mode 100644 tools/vim/after/syntax/puppet.vim create mode 100644 tools/vim/ftdetect/puppet.vim create mode 100644 tools/vim/ftplugin/puppet.vim create mode 100644 tools/vim/indent/puppet.vim create mode 100644 tools/vim/syntax/puppet.vim diff --git a/tools/git-hooks/pre-commit b/tools/git-hooks/pre-commit new file mode 100755 index 00000000..23e6e6aa --- /dev/null +++ b/tools/git-hooks/pre-commit @@ -0,0 +1,151 @@ +#!/bin/bash + +if git rev-parse --verify HEAD &>/dev/null; then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 +fi + +check_puppet_manifest () { + local file=$1 + + local pp=${workdir}/${file} + mkdir -p $(dirname ${pp}) + git cat-file blob :0:${file} | sed 's/^import .*/#&/' >${pp} + trap "rm -f ${pp}" RETURN + + local output=$(puppet apply --noop --ignoreimport ${pp} 2>&1) + if [ $? -ne 0 ] || [ -n "${output}" ]; then + echo '** Syntax check failed:' >&2 + echo "${output}" >&2 + return 1 + fi + + # Check manifests for exactly one class or define per file. + case "${file}" in + modules/*/manifests/*) + if [[ "$(egrep -c '^(class|define) ' ${file})" != "1" ]]; then + echo "** Multiple class/defines:" >&2 + egrep -n '^(class|define) ' ${file} >&2 + return 1 + fi + ;; + esac + + case "${file}" in + manifests/site.pp|modules/*/manifests/*) ;; + *) return + esac + + if [ -x /usr/bin/pcregrep ]; then + local wsfail=$({ + # grep -nPT would be nice. grep -P seems broken on gudeploy01. + # Patterns checked for are (in this order): + # - leading SPACE + # - SPACE followed by TAB + # - TAB followed by SPACE + # - non-leading TAB (but allow around '#' comment leader) + # - trailing whitespace + pcregrep -n -e '^ ' -e ' \t' -e '\t ' -e '[^\t#]\t+(?!#)' -e '\s+$' ${pp} + }) + if [[ $(wc -l <${pp}) > $(cat -s ${pp}|wc -l) ]]; then + wsfail+="Excess blank line(s). 'cat -s' is your friend." + fi + if [[ -n "${wsfail}" ]]; then + echo '** Bad whitespace (see Manifest Standards):' >&2 + echo "${wsfail}" >&2 + return 1 + fi + else + echo "Missing pcregrep: apt-get install pcregrep" >&2 + fi + + case "${file}" in + manifests/site.pp|modules/*/manifests/*) + if [[ -n "$(which puppet-lint)" ]]; then + puppet-lint --no-hard_tabs-check --no-80chars-check ${pp} >&2 | uniq + else + echo "Please install puppet-lint (gem install puppet-lint)" >&2 + fi + ;; + *) + return + ;; + esac + + return 0 +} + +# Check a Puppet template. +# +check_puppet_template () { + local file=$1 + + # Beware of 'Syntax OK' message on success. + local output=$((git cat-file blob :0:${file}|erb -x -P -T -|ruby -c >/dev/null) 2>&1) + if [[ $? -ne 0 || -n "${output}" ]]; then + echo '** Syntax check failed:' >&2 + echo "${output}" >&2 + return 1 + fi + + return 0 +} + +readonly failed=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX) +readonly missing=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX) +readonly parse=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX) +readonly retest=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX) +readonly workdir=$(mktemp -d ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX) +trap "rm -f ${failed} ${missing} ${parse} ${retest}; rm -rf ${workdir}" EXIT + +error=0 +warning=0 +for file in $(git diff-index --name-only --diff-filter=AM --cached ${against}); do + output="" + rc=0 + + case "${file}" in + manifests/site.pp) + output=$(check_puppet_manifest ${file} 2>&1); rc=$?;; + modules/*) + + case "${file}" in + modules/*/manifests/*) + case "${file}" in + # FIXME: doing this so the whitespace check happens + *.pp) output=$(check_puppet_manifest ${file} 2>&1); rc=$?;; + esac + ;; + modules/*/templates/*) + output=$(check_puppet_template ${file} 2>&1); rc=$?;; + esac + ;; + + esac + + if [[ ${rc} -ne 0 || -n "${output}" ]]; then + echo "** ${file}" >&2 + echo "${output}" >&2 + if [[ ${file} != *.pp ]]; then + error=1 + # puppet-lint warnings are acceptable. + elif [[ -n "$(egrep -v '^WARNING: ' <<<${output})" ]]; then + error=1 + else + warning=1 + fi + fi +done + +if [ "$error" -ne 0 ]; then + echo "** Please correct the above errors." >&2 + exit 1 +fi + +if [ "$warning" -ne 0 ]; then + sleep 5 +fi + +exit 0 diff --git a/tools/vim/after/syntax/puppet.vim b/tools/vim/after/syntax/puppet.vim new file mode 100644 index 00000000..20e60367 --- /dev/null +++ b/tools/vim/after/syntax/puppet.vim @@ -0,0 +1,18 @@ +setlocal noexpandtab +setlocal shiftwidth=2 +setlocal tabstop=2 + +syn match puppetBadWhitespace /^ \+/ " leading spaces +syn match puppetBadWhitespace /\s\+$/ " trailing whitespace +syn match puppetBadWhitespace /[^\t]\zs\t\+/ " embedded tab +syn match puppetBadWhitespace /\t\zs \+/ " space(s) following tab +highlight puppetBadWhitespace ctermbg=red guibg=red + +" Spellcheck in comments. +setlocal spell +syn match puppetComment "\s*#.*$" contains=puppetTodo,@Spell +syn region puppetComment start="/\*" end="\*/" contains=puppetTodo,@Spell extend + +" Play nicely with ctags. +setlocal iskeyword=@,48-57,_,:,- " not strictly valid but required +setlocal tags=~/Projects/puppet/tags " you probably need to change this diff --git a/tools/vim/ftdetect/puppet.vim b/tools/vim/ftdetect/puppet.vim new file mode 100644 index 00000000..c9d15ea5 --- /dev/null +++ b/tools/vim/ftdetect/puppet.vim @@ -0,0 +1,2 @@ +" detect puppet filetype +au BufRead,BufNewFile *.pp set filetype=puppet diff --git a/tools/vim/ftplugin/puppet.vim b/tools/vim/ftplugin/puppet.vim new file mode 100644 index 00000000..b6491554 --- /dev/null +++ b/tools/vim/ftplugin/puppet.vim @@ -0,0 +1,94 @@ +" Vim filetype plugin +" Language: Puppet +" Maintainer: Todd Zullinger +" Last Change: 2009 Aug 19 +" vim: set sw=4 sts=4: + +if exists("b:did_ftplugin") + finish +endif +let b:did_ftplugin = 1 + +if !exists("no_plugin_maps") && !exists("no_puppet_maps") + if !hasmapto("AlignRange") + map = AlignRange + endif +endif + +noremap