Commits

Markus Zapke-Gründemann committed 1f30c46

Switched to flake8 and added README.

  • Participants
  • Parent commits 58449c5

Comments (0)

Files changed (2)

+*********
+pyanalyze
+*********
+
+pyanalyze is a small Python code analyzer. It is a shell wrapper around SLOCCount_ and flake8_.
+
+Usage::
+
+    $ pyanalyze directory
+
+pyanalyze will generate a short report with all PEP8 violations and a rating of the code.
+
+.. _SLOCCount: http://www.dwheeler.com/sloccount/
+.. _flake8: https://pypi.python.org/pypi/flake8
 #
 # Python Code Analyzer
 #
+# pyanalyze is a small wrapper around SLOCCount and flake8.
+#
 # Copyright Markus Zapke-Gründemann <markus@keimlink.de>, All Rights Reserved
 #
 
     exit 1
 fi
 
+# Names of directories and files to be excluded.
+# ack accepts regular expressions, flake8 does not.
 EXCLUDE_ACK='(?!.*migrations|.*docs|.*run(\-)*tests\.py)'
-EXCLUDE_PEP8='migrations,docs,run-tests.py,runtests.py'
+EXCLUDE_FLAKE8='migrations,docs,run-tests.py,runtests.py'
+echo "Excluding: $EXCLUDE_FLAKE8"
 
-SLOC=$(sloccount --details $1 | ack "^[1-9]\d*(?=.*py|)$EXCLUDE_ACK" -o | paste -sd+ - | bc -l)
-echo "Python SLOC"
-echo "\t$SLOC"
-
+# Collect and display all PEP8 violations.
+VIOLATIONS=$(flake8 --exclude=$EXCLUDE_FLAKE8 $1)
 echo
 echo "PEP8 violations"
-pep8 --exclude=$EXCLUDE_PEP8 --statistics $1
+echo "$VIOLATIONS"
 
-VIOLATIONS=$(pep8 --exclude=$EXCLUDE_PEP8 $1 | ack -oc 'E\d+')
+# Display PEP8 violation statistics.
 echo
-echo "Total PEP8 violations"
-echo "\t$VIOLATIONS"
+echo "$VIOLATIONS" | ack -o "[EW]\d{1,3}.*" | sort | uniq -c
 
-RATING=$(echo "$SLOC/$VIOLATIONS" | bc)
-if [ "$RATING" -ge "10" ]; then
+# Compute and display number of errors, warnings and total PEP8 violations.
+ERRORS=$(echo "$VIOLATIONS" | ack -co 'E\d+')
+WARNINGS=$(echo "$VIOLATIONS" | ack -co 'W\d+')
+TOTAL=$(echo "$ERRORS+$WARNINGS" | bc)
+echo
+echo "PEP8 errors: $ERRORS"
+echo "PEP8 warnings: $WARNINGS"
+echo "Total PEP8 violations: $TOTAL"
+
+# Collect SLOC for all Python files which are not excluded.
+SLOC=$(sloccount --details $1 | ack "^[1-9]\d*(?=.*py|)$EXCLUDE_ACK" -o | paste -sd+ - | bc -l)
+echo "Python SLOC: $SLOC"
+
+# Compute and display rating (maximum is 10 points) and smilie.
+# Warnings inflict double damage.
+RATING=$(echo "10-10*($ERRORS+($WARNINGS*2))/$SLOC" | bc -l)
+if [ "${RATING/\.*}" -ge "9" ]; then
     SMILIE=':)'
+elif [ "${RATING/\.*}" -ge "7" ]; then
+    SMILIE=':|'
 else
     SMILIE=':('
 fi
-
-printf "\nThis means there is a PEP8 violation on every %dth SLOC. %s\n" $RATING $SMILIE
+printf "\nPoints: %.2f %s\n" $RATING $SMILIE