blob: 51b9649e48c6bc5e62adfe442498e0cb1b96abfe [file] [log] [blame]
#!/bin/bash
# See http://pep8.readthedocs.io/en/release-1.7.x/intro.html#error-codes
# These are from the Mininet coding style and modified slightly
P8INDENT=E126,E127 # Ignoring overindenting of continuing lines to allow for readability
P8IGN=E251,E201,E302,E202,E126,E127,E203,E226,W391 # Turned off on second pass, corrected by unpep8 script
EXCLUDE=$P8INDENT,E501,E265 # Always exclude these
# help text function
help ()
{
echo "Usage: codecheck [FILE]... [OPTION]..."
echo "Simple script that runs some python code check tools on a file or folder"
echo "Note that for now, options must come after the file/folder path"
echo -e "\nOptions include:"
echo -e " --fix\tRun the automatic style scripts on a file and save the output to FILE.fixed"
echo -e " --reason\tShow a short snippit for each PEP8 violation"
echo -e " --relaxed\tIgnore some rules such as line length"
echo -e " --help\tDisplays this help text and exits"
}
if [ -z "$1" ] || [[ "$1" =~ --* ]]; then
help
elif [ "$2" = "--fix" ]; then
file=$1.fixed
echo "Fixing PEP8 errors. WARNING: This may be harmful to your code."
echo "For safety, the new code is written to $file"
autopep8 -a -a $1 --ignore=$EXCLUDE | $(dirname "${BASH_SOURCE}")/unpep8 > $file
autopep8 --in-place -a -a $file --ignore=$EXCLUDE,$P8IGN
elif [ -z "$2" ] || [ "$2" = "--reason" ] || [ "$2" = "--relaxed" ]; then
echo "Running pyflakes..."
# Pyflakes is a source checker. It doesn't run the code so it is safer than other programs
# ignoring some errors due to TestON
pyflakes $1 | grep -v "undefined name 'main'" | grep -v "undefined name 'utilities'" | grep -v "unable to detect undefined names"
echo "Running pylint..."
# Pylint is a static code checker
# ignoring some errors due to TestON
pylint -E --rcfile=$(dirname "${BASH_SOURCE}")/.pylint $1 | grep -v "Undefined variable 'main'" | grep -v "Undefined variable 'utilities'"
echo "Running PEP8..."
# PEP8 is a the most common python coding style standard
if [ "$2" = "--reason" ]; then
pep8 --repeat --show-source --show-pep8 --ignore=$P8IGN $1
elif [ "$2" = "--relaxed" ]; then
pep8 --repeat --show-source --ignore=$P8IGN,$EXCLUDE $1
else
pep8 --repeat --show-source --ignore=$P8IGN $1
fi
else
help
fi