From 2be11fd227e5a438815cc8252c53eed20532eae6 Mon Sep 17 00:00:00 2001 From: Vincent Ardisson Date: Mon, 26 Jun 2017 15:40:16 +0200 Subject: [PATCH] pyflakes: check python2 and python3 (selectively) Since not all modules are Python3-compatible, don't emit warnings for all. They should be declared in a dedicated file. All modules are checked in Python2 though. --- tools/py3-compatible.modules | 5 +++ tools/pyflakes.sh | 83 +++++++++++++++++++----------------- 2 files changed, 48 insertions(+), 40 deletions(-) create mode 100644 tools/py3-compatible.modules diff --git a/tools/py3-compatible.modules b/tools/py3-compatible.modules new file mode 100644 index 0000000000..f9acfc3c36 --- /dev/null +++ b/tools/py3-compatible.modules @@ -0,0 +1,5 @@ +## +# this file declares which modules are compatible with python3 and should be checked with pyflakes3 +## +caissedepargne/ +cragr/ diff --git a/tools/pyflakes.sh b/tools/pyflakes.sh index c6509b7421..b3cdd8fee0 100755 --- a/tools/pyflakes.sh +++ b/tools/pyflakes.sh @@ -1,14 +1,26 @@ #!/bin/bash -u -VER=2 +find_cmd () { + for cmd in $@; do + which $cmd 2>/dev/null && return + done + return 1 +} + +PY3MODS=./tools/py3-compatible.modules if [ "${1-}" = -3 ]; then - VER=3 shift fi cd $(dirname $0) cd .. -PYFILES=$(git ls-files|grep '^scripts\|\.py$'|grep -v boilerplate_data|tr '\n' ' ') + +MODULE_FILES=$(git ls-files|grep '^modules/.*\.py$') +MODULE_FILES3=$(printf "%s\n" $MODULE_FILES|grep -F -f $PY3MODS) + +PYFILES=$(git ls-files '^scripts\|\.py$'|grep -v boilerplate_data|grep -v '^modules'|grep -v '^contrib') +PYFILES3="$PYFILES $MODULE_FILES3" +PYFILES="$PYFILES $MODULE_FILES" grep -n 'class [^( ]\+:$' ${PYFILES} && echo 'Error: old class style found, always inherit object' && exit 3 grep -n $'\t\|\s$' $PYFILES && echo 'Error: tabs or trailing whitespace found, remove them' && exit 4 grep -Fn '.setlocale' ${PYFILES} && echo 'Error: do not use setlocale' && exit 5 @@ -18,48 +30,39 @@ grep -nE "^ *print " ${PYFILES} && echo 'Error: Use the print function' && exit grep -Fn ".has_key" ${PYFILES} && echo 'Error: Deprecated, use operator "in"' && exit 9 grep -Fn "os.isatty" ${PYFILES} && echo 'Error: Use stream.isatty() instead of os.isatty(stream.fileno())' && exit 10 grep -Fn "raise StopIteration" ${PYFILES} && echo 'Error: PEP 479' && exit 11 -if [ "$VER" -eq 3 ]; then - grep -nE "\.iter(keys|values|items)\(\)" ${PYFILES} | grep -Fv "six.iter" && echo 'Error: iterkeys/itervalues/iteritems is forbidden' && exit 12 -fi -MODULE_FILES=$(git ls-files|grep '^modules/.*\.py$'|tr '\n' ' ') +grep -nE "\.iter(keys|values|items)\(\)" ${PYFILES3} | grep -Fv "six.iter" && echo 'Error: iterkeys/itervalues/iteritems is forbidden' && exit 12 + grep -nE "^ *print(\(| )" ${MODULE_FILES} && echo 'Error: Use of print in modules is forbidden, use logger instead' && exit 20 -if [ "$VER" -eq 3 ]; then - grep -n xrange ${MODULE_FILES} && echo 'Error: xrange is forbidden' && exit 21 - grep -nE "from (urllib|urlparse) import" ${MODULE_FILES} && echo 'Error: python2 urllib is forbidden' && exit 22 - grep -nE "import (urllib|urlparse)$" ${MODULE_FILES} && echo 'Error: python2 urllib is forbidden' && exit 22 +grep -n xrange ${MODULE_FILES3} && echo 'Error: xrange is forbidden' && exit 21 +grep -nE "from (urllib|urlparse) import" ${MODULE_FILES3} && echo 'Error: python2 urllib is forbidden' && exit 22 +grep -nE "import (urllib|urlparse)$" ${MODULE_FILES3} && echo 'Error: python2 urllib is forbidden' && exit 22 + +FLAKE8=$(find_cmd flake8-python2 flake8) +if [ -n "$FLAKE8" ]; then + python2 ${FLAKE8} --select=E9,F *.py $PYFILES || exit 30 +fi +FLAKE83=$(find_cmd flake8-python3 flake8) +if [ -n "$FLAKE83" ]; then + python3 ${FLAKE8} --select=E9,F *.py $PYFILES3 || exit 31 fi -FLAKE8="" -if which flake8 >/dev/null 2>&1; then - FLAKE8=$(which flake8) +if [ -n "$FLAKE8$FLAKE83" ]; then + exit 0 fi -if which flake8-python3 >/dev/null 2>&1; then - FLAKE8=$(which flake8-python$VER) + +PYFLAKES=$(find_cmd pyflakes-python2 pyflakes) +if [ -n "$PYFLAKES" ]; then + python2 ${PYFLAKES} $PYFILES || exit 32 +else + echo "pyflakes not found" + exit 1 fi -if [ -n "${FLAKE8}" ]; then - exec env python$VER ${FLAKE8} --select=E9,F *.py $PYFILES +PYFLAKES3=$(find_cmd pyflakes-python3 pyflakes3 pyflakes) +if [ -n "$PYFLAKES3" ]; then + python3 ${PYFLAKES3} $PYFILES3 || exit 33 else - PYFLAKES="" - if [ "$VER" -eq 3 ] && which pyflakes3 >/dev/null 2>&1; then - PYFLAKES=pyflakes3 - fi - if which pyflakes >/dev/null 2>&1; then - PYFLAKES=pyflakes - fi - if [ -z "${PYFLAKES}" ] - then - echo "pyflakes not found" - exit 1 - fi - # check for modern pyflakes - if ${PYFLAKES} --version >/dev/null 2>&1; then - exec ${PYFLAKES} $PYFILES - else - # hide error reported by mistake. - # grep will return 0 only if it founds something, but our script - # wants to return 0 when it founds nothing! - ${PYFLAKES} $PYFILES | grep -v redefinition && exit 1 || exit 0 - fi - fi + echo "pyflakes3 not found" + exit 1 +fi -- GitLab