blob: e3769dad864367c3f6b362ea35b05d4d73ba8c5c [file] [log] [blame]
Jon Halle1b478d2015-01-12 11:31:56 -08001#!/bin/bash
2
Jon Halld3963eb2017-05-24 10:51:28 -07003# See http://pep8.readthedocs.io/en/release-1.7.x/intro.html#error-codes
4# These are from the Mininet coding style and modified slightly
5P8INDENT=E126,E127 # Ignoring overindenting of continuing lines to allow for readability
6P8IGN=E251,E201,E302,E202,E126,E127,E203,E226,W391 # Turned off on second pass, corrected by unpep8 script
7EXCLUDE=$P8INDENT,E501,E265 # Always exclude these
Jon Halle1b478d2015-01-12 11:31:56 -08008
9# help text function
10help ()
11{
12 echo "Usage: codecheck [FILE]... [OPTION]..."
13 echo "Simple script that runs some python code check tools on a file or folder"
14 echo "Note that for now, options must come after the file/folder path"
15 echo -e "\nOptions include:"
16 echo -e " --fix\tRun the automatic style scripts on a file and save the output to FILE.fixed"
17 echo -e " --reason\tShow a short snippit for each PEP8 violation"
Jon Halld3963eb2017-05-24 10:51:28 -070018 echo -e " --relaxed\tIgnore some rules such as line length"
Jon Halle1b478d2015-01-12 11:31:56 -080019 echo -e " --help\tDisplays this help text and exits"
20}
21
22
23if [ -z "$1" ] || [[ "$1" =~ --* ]]; then
24 help
25elif [ "$2" = "--fix" ]; then
26 file=$1.fixed
27 echo "Fixing PEP8 errors. WARNING: This may be harmful to your code."
28 echo "For safety, the new code is written to $file"
Jon Halld3963eb2017-05-24 10:51:28 -070029 autopep8 -a -a $1 --ignore=$EXCLUDE | $(dirname "${BASH_SOURCE}")/unpep8 > $file
30 autopep8 --in-place -a -a $file --ignore=$EXCLUDE,$P8IGN
31elif [ -z "$2" ] || [ "$2" = "--reason" ] || [ "$2" = "--relaxed" ]; then
Jon Halle1b478d2015-01-12 11:31:56 -080032 echo "Running pyflakes..."
33 #Pyflakes is a source checker. It doesn't run the code so it is safer than other programs
34 #ignoring some errors due to TestON
35 pyflakes $1 | grep -v "undefined name 'main'" | grep -v "undefined name 'utilities'"
36 echo "Running pylint..."
37 #Pylint is a static code checker
38 #ignoring some errors due to TestON
39 pylint -E --rcfile=$(dirname "${BASH_SOURCE}")/.pylint $1 | grep -v "Undefined variable 'main'" | grep -v "Undefined variable 'utilities'"
40
41 echo "Running PEP8..."
42 #PEP8 is a the most common python coding style standard
Jon Halld3963eb2017-05-24 10:51:28 -070043 if [ "$2" = "--reason" ]; then
Jon Halle1b478d2015-01-12 11:31:56 -080044 pep8 --repeat --show-source --show-pep8 --ignore=$P8IGN $1
Jon Halld3963eb2017-05-24 10:51:28 -070045 elif [ "$2" = "--relaxed" ]; then
46 pep8 --repeat --show-source --ignore=$P8IGN,$EXCLUDE $1
47 else
48 pep8 --repeat --show-source --ignore=$P8IGN $1
Jon Halle1b478d2015-01-12 11:31:56 -080049 fi
50else
51 help
52fi
53
54
55