Split YAML & JSON parsing

It turns out that invalid JSON can be valid YAML ... thus if you mess
up a pkg-map file that still works as a YAML file dib-lint will let it
pass, but when pkg-map later tries to open it as a JSON file, it
fails.

Parse each type separately to catch these problems.

Change-Id: Ib3985e7d1599ed6bf3b7a73b786a53177b71fae0
This commit is contained in:
Ian Wienand 2016-04-22 11:20:57 +10:00
parent b388b20f99
commit 8b4a5e9919

View File

@ -176,27 +176,35 @@ if ! excluded mddocs; then
fi fi
fi fi
echo "Checking YAML & JSON parsing" echo "Checking YAML parsing..."
for i in $(find elements -type f -name '*.yaml'); do
for i in $(find elements -name '*.yaml' \ echo "Parsing $i"
-o \( -name pkg-map -type f -a \! -executable \)); do
py_check=" py_check="
import json
import yaml import yaml
import sys import sys
try:
objs = json.load(open('$i'))
sys.exit(0)
except ValueError:
pass
try: try:
objs = yaml.load(open('$i')) objs = yaml.load(open('$i'))
sys.exit(0)
except yaml.parser.ParserError: except yaml.parser.ParserError:
pass sys.exit(1)
sys.exit(1)" "
if ! python -c "$py_check"; then if ! python -c "$py_check"; then
error "$i is not a valid yaml/json file" error "$i is not a valid YAML file"
fi
done
echo "Checking pkg-map files..."
for i in $(find elements -type f \
-name 'pkg-map' -a \! -executable); do
echo "Parsing $i"
py_check="
import json
import sys
try:
objs = json.load(open('$i'))
except ValueError:
sys.exit(1)
"
if ! python -c "$py_check"; then
error "$i is not a valid JSON file"
fi fi
done done