1#!/bin/sh 2# Run flake8 (Python linter) on the source directory or on the given files/directories 3 4# Run on the base directory if no argument has been given 5if [ $# -eq 0 ] ; then 6 cd "$(dirname -- "$0")/.." || exit $? 7 8 # Run on both files ending with .py and Python files without extension 9 # shellcheck disable=SC2046 10 set -- $( (find . -name '*.py' ; grep --exclude-dir=.git -l -e '^#!\s*/usr/bin/python' -e '^#!/usr/bin/env python' -r .) | sort -u ) 11 echo "Analyzing $# Python scripts" 12fi 13 14# Assign each ignore warning on a line, in order to ease testing enabling the warning again 15IGNORE_LIST='' 16 17# Important warnings that should be fixed 18# (Comment one line and run this script in order to see where the warning occurs) 19IGNORE_LIST="$IGNORE_LIST,W191" # indentation contains tabs 20 21IGNORE_LIST="$IGNORE_LIST,E101" # indentation contains mixed spaces and tabs 22IGNORE_LIST="$IGNORE_LIST,E711" # comparison to None should be 'if cond is not None:' 23IGNORE_LIST="$IGNORE_LIST,E712" # comparison to False should be 'if cond is False:' or 'if not cond:' 24IGNORE_LIST="$IGNORE_LIST,E722" # do not use bare 'except' 25 26IGNORE_LIST="$IGNORE_LIST,F401" # module imported but unused 27IGNORE_LIST="$IGNORE_LIST,F841" # local variable '...' is assigned to but never used 28 29 30# Less-important warnings 31IGNORE_LIST="$IGNORE_LIST,W291" # trailing whitespace 32IGNORE_LIST="$IGNORE_LIST,W293" # blank line contains whitespace 33IGNORE_LIST="$IGNORE_LIST,W391" # blank line at end of file 34IGNORE_LIST="$IGNORE_LIST,W503" # line break before binary operator 35IGNORE_LIST="$IGNORE_LIST,W504" # line break after binary operator 36 37IGNORE_LIST="$IGNORE_LIST,E111" # indentation is not a multiple of four 38IGNORE_LIST="$IGNORE_LIST,E114" # indentation is not a multiple of four (comment) 39IGNORE_LIST="$IGNORE_LIST,E115" # expected an indented block (comment) 40IGNORE_LIST="$IGNORE_LIST,E116" # unexpected indentation (comment) 41IGNORE_LIST="$IGNORE_LIST,E122" # continuation line missing indentation or outdented 42IGNORE_LIST="$IGNORE_LIST,E123" # closing bracket does not match indentation of opening bracket's line 43IGNORE_LIST="$IGNORE_LIST,E126" # continuation line over-indented for hanging indent 44IGNORE_LIST="$IGNORE_LIST,E127" # continuation line over-indented for visual indent 45IGNORE_LIST="$IGNORE_LIST,E128" # continuation line under-indented for visual indent 46IGNORE_LIST="$IGNORE_LIST,E201" # whitespace after '[' 47IGNORE_LIST="$IGNORE_LIST,E202" # whitespace before '}' 48IGNORE_LIST="$IGNORE_LIST,E203" # whitespace before ':' 49IGNORE_LIST="$IGNORE_LIST,E211" # whitespace before '(' 50IGNORE_LIST="$IGNORE_LIST,E221" # multiple spaces before operator 51IGNORE_LIST="$IGNORE_LIST,E222" # multiple spaces after operator 52IGNORE_LIST="$IGNORE_LIST,E225" # missing whitespace around operator 53IGNORE_LIST="$IGNORE_LIST,E226" # missing whitespace around arithmetic operator 54IGNORE_LIST="$IGNORE_LIST,E231" # missing whitespace after ',' 55IGNORE_LIST="$IGNORE_LIST,E241" # multiple spaces after ':' 56IGNORE_LIST="$IGNORE_LIST,E251" # unexpected spaces around keyword / parameter equals 57IGNORE_LIST="$IGNORE_LIST,E261" # at least two spaces before inline comment 58IGNORE_LIST="$IGNORE_LIST,E265" # block comment should start with '# ' 59IGNORE_LIST="$IGNORE_LIST,E266" # too many leading '#' for block comment 60IGNORE_LIST="$IGNORE_LIST,E272" # multiple spaces before keyword 61IGNORE_LIST="$IGNORE_LIST,E301" # expected 1 blank line, found 0 62IGNORE_LIST="$IGNORE_LIST,E302" # expected 2 blank lines, found 1 63IGNORE_LIST="$IGNORE_LIST,E303" # too many blank lines 64IGNORE_LIST="$IGNORE_LIST,E305" # expected 2 blank lines after class or function definition, found 0 65IGNORE_LIST="$IGNORE_LIST,E306" # expected 1 blank line before a nested definition, found 0 66IGNORE_LIST="$IGNORE_LIST,E401" # multiple imports on one line 67IGNORE_LIST="$IGNORE_LIST,E402" # module level import not at top of file 68IGNORE_LIST="$IGNORE_LIST,E501" # line too long 69IGNORE_LIST="$IGNORE_LIST,E701" # multiple statements on one line (colon) 70IGNORE_LIST="$IGNORE_LIST,E704" # multiple statements on one line (def) 71IGNORE_LIST="$IGNORE_LIST,E731" # do not assign a lambda expression, use a def 72IGNORE_LIST="$IGNORE_LIST,E741" # ambiguous variable name 'l' / 'I' 73 74IGNORE_LIST="$IGNORE_LIST,F403" # 'from ... import *' used; unable to detect undefined names 75IGNORE_LIST="$IGNORE_LIST,F405" # '...' may be undefined, or defined from star imports 76 77# Ignore errors from files generated from SWIG 78IGNORE_LIST="$IGNORE_LIST,F811" # redefinition of unused ... 79 80 81exec flake8 --max-line-length=120 --builtins='_,basestring,unicode' --ignore=",$IGNORE_LIST" "$@" 82