Monthly Archives: June 2018

use flake8 to check recursively a python dir/project

For all those lazy yet finicky python coders. Code to be read while listening to David Byrne’s Lazy


#!/bin/bash

TOSCAN=$1
FLAKE8="flake8 --max-line-length=100"

if [ "X$TOSCAN" == "X" ]
then
        TOSCAN="."
fi

TMPDIR=/tmp/$$
mkdir $TMPDIR

echo "0" > $TMPDIR/flakeerrors.txt
flakeerrors=0
find $TOSCAN -type f -name "*.py" |\
while IFS= read -r file
do
        echo ">>>>>>>> flake8 checking file:$file "
        res=0
        $FLAKE8 "$file" >&  $TMPDIR/flake8out.txt
        res=$?
        cat $TMPDIR/flake8out.txt
        if (( $res > 0 ))
        then
                echo "======== flake8 Return:$res"
                (( flakeerrors++ ))
        fi
        # subshells do not propagate vars
        echo $flakeerrors > $TMPDIR/flakeerrors.txt
done
# back from the while subshell
flakeerrors=$(cat $TMPDIR/thisbuild/flakeerrors.txt)
if (( $flakeerrors > 0 ))
then
        echo " **********************
                flake8 error
                count: ${flakeerrors}
                *********************" | /usr/games/cowsay -e \*\*
fi