Jon Hall | e1b478d | 2015-01-12 11:31:56 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Jon Hall | d3963eb | 2017-05-24 10:51:28 -0700 | [diff] [blame] | 3 | # 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 |
| 5 | P8INDENT=E126,E127 # Ignoring overindenting of continuing lines to allow for readability |
| 6 | P8IGN=E251,E201,E302,E202,E126,E127,E203,E226,W391 # Turned off on second pass, corrected by unpep8 script |
| 7 | EXCLUDE=$P8INDENT,E501,E265 # Always exclude these |
Jon Hall | e1b478d | 2015-01-12 11:31:56 -0800 | [diff] [blame] | 8 | |
| 9 | # help text function |
| 10 | help () |
| 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 Hall | d3963eb | 2017-05-24 10:51:28 -0700 | [diff] [blame] | 18 | echo -e " --relaxed\tIgnore some rules such as line length" |
Jon Hall | e1b478d | 2015-01-12 11:31:56 -0800 | [diff] [blame] | 19 | echo -e " --help\tDisplays this help text and exits" |
| 20 | } |
| 21 | |
| 22 | |
| 23 | if [ -z "$1" ] || [[ "$1" =~ --* ]]; then |
| 24 | help |
| 25 | elif [ "$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 Hall | d3963eb | 2017-05-24 10:51:28 -0700 | [diff] [blame] | 29 | autopep8 -a -a $1 --ignore=$EXCLUDE | $(dirname "${BASH_SOURCE}")/unpep8 > $file |
| 30 | autopep8 --in-place -a -a $file --ignore=$EXCLUDE,$P8IGN |
| 31 | elif [ -z "$2" ] || [ "$2" = "--reason" ] || [ "$2" = "--relaxed" ]; then |
Jon Hall | e1b478d | 2015-01-12 11:31:56 -0800 | [diff] [blame] | 32 | echo "Running pyflakes..." |
Jon Hall | 88f5a8c | 2018-05-11 13:27:24 -0700 | [diff] [blame] | 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'" | grep -v "unable to detect undefined names" |
Jon Hall | e1b478d | 2015-01-12 11:31:56 -0800 | [diff] [blame] | 36 | echo "Running pylint..." |
Jon Hall | 88f5a8c | 2018-05-11 13:27:24 -0700 | [diff] [blame] | 37 | # Pylint is a static code checker |
| 38 | # ignoring some errors due to TestON |
Jon Hall | e1b478d | 2015-01-12 11:31:56 -0800 | [diff] [blame] | 39 | pylint -E --rcfile=$(dirname "${BASH_SOURCE}")/.pylint $1 | grep -v "Undefined variable 'main'" | grep -v "Undefined variable 'utilities'" |
| 40 | |
| 41 | echo "Running PEP8..." |
Jon Hall | 88f5a8c | 2018-05-11 13:27:24 -0700 | [diff] [blame] | 42 | # PEP8 is a the most common python coding style standard |
Jon Hall | d3963eb | 2017-05-24 10:51:28 -0700 | [diff] [blame] | 43 | if [ "$2" = "--reason" ]; then |
Jon Hall | e1b478d | 2015-01-12 11:31:56 -0800 | [diff] [blame] | 44 | pep8 --repeat --show-source --show-pep8 --ignore=$P8IGN $1 |
Jon Hall | d3963eb | 2017-05-24 10:51:28 -0700 | [diff] [blame] | 45 | elif [ "$2" = "--relaxed" ]; then |
| 46 | pep8 --repeat --show-source --ignore=$P8IGN,$EXCLUDE $1 |
| 47 | else |
| 48 | pep8 --repeat --show-source --ignore=$P8IGN $1 |
Jon Hall | e1b478d | 2015-01-12 11:31:56 -0800 | [diff] [blame] | 49 | fi |
| 50 | else |
| 51 | help |
| 52 | fi |
| 53 | |
| 54 | |
| 55 | |