Merge "add better validation checks (part 1)"

This commit is contained in:
Jenkins 2014-10-14 19:44:51 +00:00 committed by Gerrit Code Review
commit ec5a16dcec
6 changed files with 131 additions and 33 deletions

45
tools/pre-commit-hook Executable file
View File

@ -0,0 +1,45 @@
#!/bin/sh
TOPLEVEL=$(git rev-parse --show-toplevel)
RES=0
cd $TOPLEVEL
if [ "$1" == "--install" ]; then
ln -sf ../../tools/pre-commit-hook .git/hooks/pre-commit
exit
fi
tmpdir=$(mktemp -d precommit.XXXXXX) || exit 1
trap "rm -rf $TOPLEVEL/$tmpdir" 0
git diff --cached --name-only --diff-filter=ACMR |
xargs git checkout-index --prefix=$tmpdir/ --
cd $tmpdir
echo "=== starting pre-commit checks ==="
echo "Checking the following files:"
find . -type f
echo "=== bashate checks ==="
find . -type f -print0 |
xargs -0 --no-run-if-empty egrep -lZ '^#!/bin/(ba)?sh' |
xargs -0 bashate || RES=1
echo "=== yaml checks ==="
find . -name '*.yaml' -print0 |
xargs -0 --no-run-if-empty python ${TOPLEVEL}/tools/try-parse-yaml.py
echo "=== json checks ==="
find . -name '*.json' -print0 |
xargs -0 -n1 --no-run-if-empty python -mjson.tool \
|| RES=1
exit $RES

9
tools/validate-all-json.sh Executable file
View File

@ -0,0 +1,9 @@
#!/bin/sh
TOPLEVEL=$(git rev-parse --show-toplevel)
cd $TOPLEVEL
git ls-files -z '*.json' |
xargs -0 python tools/validate-json.py || exit 1

9
tools/validate-all-yaml.sh Executable file
View File

@ -0,0 +1,9 @@
#!/bin/sh
TOPLEVEL=$(git rev-parse --show-toplevel)
cd $TOPLEVEL
git ls-files -z '*.yaml' |
xargs -0 python tools/validate-yaml.py || exit 1

33
tools/validate-json.py Executable file
View File

@ -0,0 +1,33 @@
#!/usr/bin/python
import os
import sys
import argparse
import json
import logging
def parse_args():
p = argparse.ArgumentParser()
p.add_argument('input', nargs='*')
return p.parse_args()
def main():
args = parse_args()
logging.basicConfig()
res = 0
for filename in args.input:
with open(filename) as fd:
try:
data = json.load(fd)
except ValueError as error:
res = 1
logging.error('%s failed validation: %s',
filename, error)
sys.exit(res)
if __name__ == '__main__':
main()

View File

@ -1,34 +1,3 @@
#!/bin/bash #!/bin/sh
set -e
ret=0
# For MacOSX users the freebsd's mktemp by default behave diferently, true
# installing the coreutils from brew would give you this but that would be
# renamed to gmktemp to don't conflict with the stand mktemp, so let just use
# that if available.
if type -p gmktemp &>/dev/null; then
TMPFILE=$(gmktemp)
else
TMPFILE=$(mktemp)
fi
function clean {
rm -f ${TMPFILE}
}
trap clean EXIT
linter=jsonlint
type -p jsonlint &>/dev/null || linter=python
for f in $(find docker -type f -name '*.json');do
if [[ ${linter} == jsonlint ]];then
jsonlint -s ${f} >${TMPFILE}
egrep -q 'has errors$' ${TMPFILE} && { cat ${TMPFILE}; ret=1 ;}
else
python -m json.tool ${f} &>/dev/null || { echo "$f: has json errors"; ret=1; }
fi
done
cat ${TMPFILE}
exit ${ret}

33
tools/validate-yaml.py Executable file
View File

@ -0,0 +1,33 @@
#!/usr/bin/python
import os
import sys
import argparse
import yaml
import logging
def parse_args():
p = argparse.ArgumentParser()
p.add_argument('input', nargs='*')
return p.parse_args()
def main():
args = parse_args()
logging.basicConfig()
res = 0
for filename in args.input:
with open(filename) as fd:
try:
data = yaml.load(fd)
except yaml.error.YAMLError as error:
res = 1
logging.error('%s failed validation: %s',
filename, error)
sys.exit(res)
if __name__ == '__main__':
main()