pyflakes.sh 2.72 KB
Newer Older
1
#!/bin/sh -u
2
. "$(dirname $0)/common.sh"
3

4
err=0
5

6
cd $(dirname $0)/..
7

8 9
PY3MODS=$(grep -v '^#' ./tools/py3-compatible.modules|tr '\n' '|'|sed 's/|$//')

10 11
MODULE_FILES=$(git ls-files modules|grep '\.py$')
MODULE_FILES3=$(printf "%s\n" $MODULE_FILES|grep -E -w "^modules/(${PY3MODS})")
12

13
PYFILES=$(git ls-files | grep '^scripts\|\.py$'|grep -v boilerplate_data|grep -v stable_backport_data|grep -v '^modules'|grep -v '^contrib')
14
PYFILES3="$PYFILES $MODULE_FILES3"
15
PYFILES="$PYFILES $MODULE_FILES"
16
grep -n 'class [^( ]\+:$' ${PYFILES} && echo 'Error: old class style found, always inherit object' && err=3
17
grep -n '[[:space:]]$' ${PYFILES} && echo 'Error: tabs or trailing whitespace found, remove them' && err=4
18 19 20 21 22 23 24
grep -Fn '.setlocale' ${PYFILES} && echo 'Error: do not use setlocale' && err=5
grep -Fn '__future__ import with_statement' ${PYFILES} && echo 'Error: with_statement useless as we do not support Python 2.5' && err=6
grep -nE '^[[:space:]]+except [[:alnum:] ]+,[[:alnum:] ]+' ${PYFILES} && echo 'Error: use new "as" way of naming exceptions' && err=7
grep -nE "^ *print " ${PYFILES} && echo 'Error: Use the print function' && err=8
grep -Fn ".has_key" ${PYFILES} && echo 'Error: Deprecated, use operator "in"' && err=9
grep -Fn "os.isatty" ${PYFILES} && echo 'Error: Use stream.isatty() instead of os.isatty(stream.fileno())' && err=10
grep -Fn "raise StopIteration" ${PYFILES} && echo 'Error: PEP 479' && err=11
25

26
grep -nE "\.iter(keys|values|items)\(\)" ${PYFILES3} | grep -Fv "six.iter" && echo 'Error: iterkeys/itervalues/iteritems is forbidden' && err=12
27

28 29 30 31
grep -nE "^ *print(\(| )" ${MODULE_FILES} && echo 'Error: Use of print in modules is forbidden, use logger instead' && err=20
grep -n xrange ${MODULE_FILES3} && echo 'Error: xrange is forbidden' && err=21
grep -nE "from (urllib|urlparse) import" ${MODULE_FILES3} && echo 'Error: python2 urllib is forbidden' && err=22
grep -nE "^import (urllib|urlparse)$" ${MODULE_FILES3} && echo 'Error: python2 urllib is forbidden' && err=22
32

33
if [ ${VER} -eq 2 ]
34 35 36 37 38 39 40 41 42
then
  if ${PYTHON2} -c "import flake8" 2>/dev/null; then
      FLAKER2=flake8
      OPT2="--select=E9,F"
  elif ${PYTHON2} -c "import pyflakes" 2>/dev/null; then
      FLAKER2=pyflakes
      OPT2=
  else
      echo "flake8 or pyflakes for python2 not found"
43 44 45 46
      err=1
  fi
  if [ ${err} -ne 1 ]; then
    $PYTHON2 -m ${FLAKER2} ${OPT2} ${PYFILES} || err=32
47
  fi
48 49
fi

50
if [ ${VER} -eq 3 ]
51 52 53 54 55 56 57 58 59
then
  if ${PYTHON3} -c "import flake8" 2>/dev/null; then
      FLAKER3=flake8
      OPT3="--select=E9,F"
  elif ${PYTHON3} -c "import pyflakes" 2>/dev/null; then
      FLAKER3=pyflakes
      OPT3=
  else
      echo "flake8 or pyflakes for python3 not found"
60 61 62 63
      err=1
  fi
  if [ ${err} -ne 1 ]; then
    $PYTHON3 -m ${FLAKER3} ${OPT3} ${PYFILES3} || exit 33
64
  fi
65
fi
66

67
exit $err