andrej@1786: #!/bin/sh andrej@1786: # -*- coding: utf-8 -*- andrej@1786: andrej@1786: # This file is part of Beremiz, a Integrated Development Environment for andrej@1786: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. andrej@1786: # andrej@1786: # Copyright (C) 2017: Andrey Skvortsov andrej@1786: # andrej@1786: # See COPYING file for copyrights details. andrej@1786: # andrej@1786: # This program is free software; you can redistribute it and/or andrej@1786: # modify it under the terms of the GNU General Public License andrej@1786: # as published by the Free Software Foundation; either version 2 andrej@1786: # of the License, or (at your option) any later version. andrej@1786: # andrej@1786: # This program is distributed in the hope that it will be useful, andrej@1786: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1786: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1786: # GNU General Public License for more details. andrej@1786: # andrej@1786: # You should have received a copy of the GNU General Public License andrej@1786: # along with this program; if not, write to the Free Software andrej@1786: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. andrej@1786: andrej@1786: andrej@1786: exit_code=0 andrej@1786: set_exit_error() andrej@1786: { andrej@1786: if [ $exit_code -eq 0 ]; then andrej@1786: exit_code=1 andrej@1786: fi andrej@1786: } andrej@1786: andrej@1786: andrej@1786: compile_checks() andrej@1786: { andrej@2016: echo "Syntax checking using python ..." andrej@2016: python --version andrej@2016: andrej@1786: # remove compiled Python files andrej@1786: find . -name '*.pyc' -exec rm -f {} \; andrej@1786: andrej@1786: for i in $py_files; do andrej@1786: # echo $i andrej@1786: python -m py_compile $i andrej@1786: if [ $? -ne 0 ]; then andrej@1786: echo "Syntax error in $i" andrej@1786: set_exit_error andrej@1786: fi andrej@1786: done andrej@2016: echo "DONE" andrej@2016: echo "" andrej@1786: } andrej@1786: andrej@1786: # pep8 was renamed to pycodestyle andrej@1786: # detect existed version andrej@1786: pep8_detect() andrej@1786: { andrej@1786: test -n $pep8 && (which pep8 > /dev/null) && pep8="pep8" andrej@1786: test -n $pep8 && (which pycodestyle > /dev/null) && pep8="pycodestyle" andrej@1786: if [ -z $pep8 ]; then andrej@1786: echo "pep8/pycodestyle is not found" andrej@1786: set_exit_error andrej@1786: fi andrej@2016: echo -n "pep8 version: " andrej@2016: $pep8 --version andrej@1786: } andrej@1786: andrej@1786: pep8_checks_default() andrej@1786: { andrej@1786: echo "Check basic code-style problems for PEP-8" andrej@1786: andrej@1786: test -n $pep8 && pep8_detect andrej@1786: test -z $pep8 && return andrej@1786: andrej@1786: user_ignore= andrej@1786: # user_ignore=$user_ignore,E265 # E265 block comment should start with '# ' andrej@1786: user_ignore=$user_ignore,E501 # E501 line too long (80 > 79 characters) andrej@1786: andrej@1786: # ignored by default, andrej@1786: default_ignore= andrej@1786: default_ignore=$default_ignore,E121 # E121 continuation line under-indented for hanging indent andrej@1786: default_ignore=$default_ignore,E123 # E123 closing bracket does not match indentation of opening bracket’s line andrej@1786: default_ignore=$default_ignore,E126 # E126 continuation line over-indented for hanging indent andrej@1786: default_ignore=$default_ignore,E133 # E133 closing bracket is missing indentation andrej@1786: default_ignore=$default_ignore,E226 # E226 missing whitespace around arithmetic operator andrej@1786: default_ignore=$default_ignore,E241 # E241 multiple spaces after ':' andrej@1786: default_ignore=$default_ignore,E242 # E242 tab after ‘,’ andrej@1786: default_ignore=$default_ignore,E704 # E704 multiple statements on one line (def) andrej@1786: default_ignore=$default_ignore,W503 # W503 line break occurred before a binary operator andrej@1786: ignore=$user_ignore,$default_ignore andrej@1786: andrej@1786: # $pep8 --ignore $ignore --exclude build ./ andrej@2181: $pep8 --max-line-length 300 --exclude build $py_files andrej@1786: if [ $? -ne 0 ]; then andrej@1786: set_exit_error andrej@1786: fi andrej@2016: andrej@2016: echo "DONE" andrej@2016: echo "" andrej@1786: } andrej@1786: andrej@1786: andrej@1786: pep8_checks_selected() andrej@1786: { andrej@1786: echo "Check basic code-style problems for PEP-8 (selective)" andrej@1786: andrej@1786: test -n $pep8 && pep8_detect andrej@1786: test -z $pep8 && return andrej@1786: andrej@1786: # select checks: andrej@1786: user_select= andrej@1786: user_select=$user_select,W291 # W291 trailing whitespace andrej@1786: user_select=$user_select,E401 # E401 multiple imports on one line andrej@1786: user_select=$user_select,E265 # E265 block comment should start with '# ' andrej@1786: user_select=$user_select,E228 # E228 missing whitespace around modulo operator andrej@1786: user_select=$user_select,W293 # W293 blank line contains whitespace andrej@1786: user_select=$user_select,E302 # E302 expected 2 blank lines, found 1 andrej@1786: user_select=$user_select,E261 # E261 at least two spaces before inline comment andrej@1786: user_select=$user_select,E271 # E271 multiple spaces after keyword andrej@1786: user_select=$user_select,E231 # E231 missing whitespace after ',' andrej@1786: user_select=$user_select,E303 # E303 too many blank lines (2) andrej@1786: user_select=$user_select,E225 # E225 missing whitespace around operator andrej@1786: user_select=$user_select,E711 # E711 comparison to None should be 'if cond is not None:' andrej@1786: user_select=$user_select,E251 # E251 unexpected spaces around keyword / parameter equals andrej@1786: user_select=$user_select,E227 # E227 missing whitespace around bitwise or shift operator andrej@1786: user_select=$user_select,E202 # E202 whitespace before ')' andrej@1786: user_select=$user_select,E201 # E201 whitespace after '{' andrej@1786: user_select=$user_select,W391 # W391 blank line at end of file andrej@1786: user_select=$user_select,E305 # E305 expected 2 blank lines after class or function definition, found X andrej@1786: user_select=$user_select,E306 # E306 expected 1 blank line before a nested definition, found X andrej@1786: user_select=$user_select,E703 # E703 statement ends with a semicolon andrej@1786: user_select=$user_select,E701 # E701 multiple statements on one line (colon) andrej@1786: user_select=$user_select,E221 # E221 multiple spaces before operator andrej@1786: user_select=$user_select,E741 # E741 ambiguous variable name 'l' andrej@1786: user_select=$user_select,E111 # E111 indentation is not a multiple of four andrej@1786: user_select=$user_select,E222 # E222 multiple spaces after operator andrej@1786: user_select=$user_select,E712 # E712 comparison to True should be 'if cond is True:' or 'if cond:' andrej@1786: user_select=$user_select,E262 # E262 inline comment should start with '# ' andrej@1786: user_select=$user_select,E203 # E203 whitespace before ',' andrej@1786: user_select=$user_select,E731 # E731 do not assign a lambda expression, use a def andrej@1786: user_select=$user_select,W601 # W601 .has_key() is deprecated, use 'in' andrej@1786: user_select=$user_select,E502 # E502 the backslash is redundant between brackets andrej@1786: user_select=$user_select,W602 # W602 deprecated form of raising exception andrej@1786: user_select=$user_select,E129 # E129 visually indented line with same indent as next logical line andrej@1786: user_select=$user_select,E127 # E127 continuation line over-indented for visual indent andrej@1786: user_select=$user_select,E128 # E128 continuation line under-indented for visual indent andrej@1786: user_select=$user_select,E125 # E125 continuation line with same indent as next logical line andrej@1786: user_select=$user_select,E114 # E114 indentation is not a multiple of four (comment) andrej@1786: user_select=$user_select,E211 # E211 whitespace before '[' andrej@1786: user_select=$user_select,W191 # W191 indentation contains tabs andrej@1786: user_select=$user_select,E101 # E101 indentation contains mixed spaces and tabs andrej@1786: user_select=$user_select,E124 # E124 closing bracket does not match visual indentation andrej@1786: user_select=$user_select,E272 # E272 multiple spaces before keyword andrej@1786: user_select=$user_select,E713 # E713 test for membership should be 'not in' andrej@1786: user_select=$user_select,E122 # E122 continuation line missing indentation or outdented andrej@1786: user_select=$user_select,E131 # E131 continuation line unaligned for hanging indent andrej@1786: user_select=$user_select,E721 # E721 do not compare types, use 'isinstance()' andrej@1786: user_select=$user_select,E115 # E115 expected an indented block (comment) andrej@1786: user_select=$user_select,E722 # E722 do not use bare except' andrej@1786: user_select=$user_select,E266 # E266 too many leading '#' for block comment andrej@1786: user_select=$user_select,E402 # E402 module level import not at top of file andrej@1786: user_select=$user_select,W503 # W503 line break before binary operator andrej@1786: andrej@2181: $pep8 --select $user_select --exclude=build $py_files andrej@1786: if [ $? -ne 0 ]; then andrej@1786: set_exit_error andrej@1786: fi andrej@2016: andrej@2016: echo "DONE" andrej@2016: echo "" andrej@1786: } andrej@1786: andrej@1786: flake8_checks() andrej@1786: { andrej@1786: echo "Check for problems using flake8 ..." andrej@1786: andrej@1786: which flake8 > /dev/null andrej@1786: if [ $? -ne 0 ]; then andrej@1786: echo "flake8 is not found" andrej@1786: set_exit_error andrej@1786: return andrej@1786: fi andrej@1786: andrej@2016: echo -n "flake8 version: " andrej@2016: flake8 --version andrej@2016: andrej@2181: flake8 --max-line-length=300 --exclude=build --builtins="_" $py_files andrej@1786: if [ $? -ne 0 ]; then andrej@1786: set_exit_error andrej@1786: fi andrej@2016: andrej@2016: echo "DONE" andrej@2016: echo "" andrej@1786: } andrej@1786: andrej@1786: pylint_checks() andrej@1786: { andrej@1786: echo "Check for problems using pylint ..." andrej@1786: andrej@1786: which pylint > /dev/null andrej@1786: if [ $? -ne 0 ]; then andrej@1786: echo "pylint is not found" andrej@1786: set_exit_error andrej@1786: return andrej@1786: fi andrej@2016: pylint --version andrej@1786: andrej@1786: export PYTHONPATH="$PWD/../CanFestival-3/objdictgen":$PYTHONPATH andrej@1786: andrej@1786: disable= andrej@1854: # These warnings most likely will not be fixed andrej@1854: andrej@1833: disable=$disable,C0103 # invalid-name andrej@1833: disable=$disable,C0326 # bad whitespace andrej@1833: disable=$disable,W0110 # (deprecated-lambda) map/filter on lambda could be replaced by comprehension andrej@1846: disable=$disable,W1401 # (anomalous-backslash-in-string) Anomalous backslash in string: '\.'. String constant might be missing an r prefix. andrej@1851: disable=$disable,W0613 # (unused-argument) Unused argument 'X' andrej@1851: disable=$disable,W0622 # (redefined-builtin) Redefining built-in andrej@2182: disable=$disable,W0621 # (redefined-outer-name) Redefining name 'Y' from outer scope (line X) andrej@1855: disable=$disable,W0122 # (exec-used) Use of exec andrej@1855: disable=$disable,W0123 # (eval-used) Use of eval andrej@1873: disable=$disable,I0011 # (locally-disabled) Locally disabling ungrouped-imports (C0412) andrej@1876: disable=$disable,R0204 # (redefined-variable-type) Redefinition of current type from X to Y andrej@1878: disable=$disable,R0201 # (no-self-use) Method could be a function andrej@1879: disable=$disable,W0221 # (arguments-differ) Arguments number differs from overridden 'X' method andrej@1879: disable=$disable,C0201 # (consider-iterating-dictionary) Consider iterating the dictionary directly instead of calling .keys() andrej@1854: andrej@1854: # It'd be nice to fix warnings below some day andrej@1854: disable=$disable,C0111 # missing-docstring andrej@1854: disable=$disable,W0703 # broad-except andrej@1854: disable=$disable,C0301 # Line too long andrej@1854: disable=$disable,C0302 # Too many lines in module andrej@2182: disable=$disable,W0511 # fixme andrej@1868: disable=$disable,R0901 # (too-many-ancestors) Too many ancestors (9/7) andrej@1868: disable=$disable,R0902 # (too-many-instance-attributes) Too many instance attributes (10/7) andrej@1868: disable=$disable,R0903 # (too-few-public-methods) Too few public methods (0/2) andrej@1868: disable=$disable,R0904 # (too-many-public-methods) Too many public methods (41/20) andrej@1868: disable=$disable,R0911 # (too-many-return-statements) Too many return statements (7/6) andrej@1868: disable=$disable,R0912 # (too-many-branches) Too many branches (61/12) andrej@1868: disable=$disable,R0913 # (too-many-arguments) Too many arguments (6/5) andrej@1868: disable=$disable,R0914 # (too-many-locals) Too many local variables (18/15) andrej@1868: disable=$disable,R0915 # (too-many-statements) Too many statements (57/50) andrej@1868: disable=$disable,R0916 # (too-many-boolean-expressions) Too many boolean expressions in if statement (6/5) andrej@1872: disable=$disable,R0101 # (too-many-nested-blocks) Too many nested blocks (7/5) andrej@1876: disable=$disable,R0801 # (duplicate-code) Similar lines in N files andrej@1872: andrej@1879: andrej@1786: enable= andrej@1831: enable=$enable,E1601 # print statement used andrej@2182: enable=$enable,C0325 # (superfluous-parens) Unnecessary parens after keyword andrej@2182: enable=$enable,W0404 # reimported module andrej@1834: enable=$enable,C0411 # (wrong-import-order) standard import "import x" comes before "import y" andrej@1834: enable=$enable,W0108 # (unnecessary-lambda) Lambda may not be necessary andrej@1834: enable=$enable,C0412 # (ungrouped-imports) Imports from package X are not grouped andrej@1835: enable=$enable,C0321 # (multiple-statements) More than one statement on a single line andrej@1836: enable=$enable,W0231 # (super-init-not-called) __init__ method from base class is not called andrej@2182: enable=$enable,W0105 # (pointless-string-statement) String statement has no effect andrej@1838: enable=$enable,W0311 # (bad-indentation) Bad indentation. Found 16 spaces, expected 12 andrej@1839: enable=$enable,W0101 # (unreachable) Unreachable code andrej@1840: enable=$enable,E0102 # (function-redefined) method already defined andrej@1841: enable=$enable,W0602 # (global-variable-not-assigned) Using global for 'X' but no assignment is done andrej@1846: enable=$enable,W0612 # (unused-variable) Unused variable 'X' andrej@1851: enable=$enable,W0611 # (unused-import) Unused import X andrej@1851: enable=$enable,C1001 # (old-style-class) Old-style class defined. Problem with PyJS andrej@1852: enable=$enable,W0102 # (dangerous-default-value) Dangerous default value {} as argument andrej@1853: enable=$enable,W0403 # (relative-import) Relative import 'Y', should be 'X.Y' andrej@1854: enable=$enable,C0112 # (empty-docstring) andrej@1855: enable=$enable,W0631 # (undefined-loop-variable) Using possibly undefined loop variable 'X' andrej@1856: enable=$enable,W0104 # (pointless-statement) Statement seems to have no effect andrej@1857: enable=$enable,W0107 # (unnecessary-pass) Unnecessary pass statement andrej@1858: enable=$enable,W0406 # (import-self) Module import itself andrej@1859: enable=$enable,C0413 # (wrong-import-position) Import "import X" should be placed at the top of the module andrej@1860: enable=$enable,E1305 # (too-many-format-args) Too many arguments for format string andrej@1861: enable=$enable,E0704 # (misplaced-bare-raise) The raise statement is not inside an except clause andrej@1862: enable=$enable,C0123 # (unidiomatic-typecheck) Using type() instead of isinstance() for a typecheck. andrej@1865: enable=$enable,E0601 # (used-before-assignment) Using variable 'X' before assignment andrej@1865: enable=$enable,E1120 # (no-value-for-parameter) No value for argument 'X' in function call andrej@1866: enable=$enable,E0701 # (bad-except-order) Bad except clauses order (X is an ancestor class of Y) andrej@1867: enable=$enable,E0611 # (no-name-in-module) No name 'X' in module 'Y' andrej@1868: enable=$enable,E0213 # (no-self-argument) Method should have "self" as first argument andrej@1869: enable=$enable,E0401 # (import-error) Unable to import 'X' andrej@1870: enable=$enable,E1121 # (too-many-function-args) Too many positional arguments for function call andrej@1872: enable=$enable,E0602 # (undefined-variable) Undefined variable 'X' andrej@1873: enable=$enable,W0232 # (no-init) Class has no __init__ method andrej@1875: enable=$enable,W0233 # (non-parent-init-called) __init__ method from a non direct base class 'X' is called andrej@1875: enable=$enable,W0601 # (global-variable-undefined) Global variable 'X' undefined at the module level andrej@1875: enable=$enable,W0623 # (redefine-in-handler) Redefining name 'X' from outer scope (line Y) in exception handler andrej@1878: enable=$enable,W0106 # (expression-not-assigned) Expression "X" is assigned to nothing andrej@1878: enable=$enable,C0330 # (bad-continuation) Wrong hanging indentation before block andrej@1879: enable=$enable,E1136 # (unsubscriptable-object) Value 'X' is unsubscriptable andrej@1881: enable=$enable,W1618 # (no-absolute-import) import missing `from __future__ import absolute_import` andrej@1828: # enable= andrej@1786: andrej@1786: options= andrej@1827: options="$options --rcfile=.pylint" andrej@1786: # options="$options --py3k" # report errors for Python 3 porting andrej@1786: andrej@1786: if [ -n "$enable" ]; then andrej@1786: options="$options --disable=all" andrej@1786: options="$options --enable=$enable" andrej@1786: else andrej@1786: options="$options --disable=$disable" andrej@1786: fi andrej@1827: # echo $options andrej@1827: andrej@2182: echo $py_files | xargs pylint $options andrej@2181: if [ $? -ne 0 ]; then andrej@2181: set_exit_error andrej@2181: fi andrej@2181: andrej@2181: echo "DONE" andrej@2181: echo "" andrej@2181: } andrej@2181: andrej@2181: andrej@2181: get_files_to_check() andrej@2181: { Edouard@2251: py_files=$(find . -name '*.py' -not -path '*/build/*' -not -path './etherlab/*') andrej@2241: if [ -e .hg/skiphook ]; then andrej@2241: echo "Skipping checks in the hook ..." andrej@2241: exit 0 andrej@2241: fi andrej@2181: if [ "$1" = "--only-changes" ]; then andrej@2181: if which hg > /dev/null; then andrej@2184: if [ ! -z "$HG_NODE" ]; then andrej@2184: hg_change="--change $HG_NODE" andrej@2184: msg="for commit $HG_NODE" andrej@2184: else andrej@2184: hg_change="" andrej@2184: msg="in local repository" andrej@2184: fi andrej@2184: echo "Only changes ${msg} will be checked" andrej@2181: echo "" andrej@2184: py_files=$(hg status -m -a -n -I '**.py' $hg_change) andrej@2184: if [ $? -ne 0 ]; then andrej@2184: exit 1; andrej@2183: fi andrej@2183: fi andrej@2181: fi andrej@2184: if [ "$1" = "--files-to-check" ]; then andrej@2184: list="$2" andrej@2184: if [ -z "$list" ]; then andrej@2184: echo "--files-to-check requires filename as argument" andrej@2184: print_help andrej@2184: fi andrej@2184: if [ -e "$list" ]; then andrej@2184: py_files=$(cat $2 | grep '\.py$') andrej@2184: fi andrej@2184: fi andrej@2184: if [ -z "$py_files" ]; then andrej@2184: echo "No files to check" andrej@2184: exit 0; andrej@2184: fi andrej@2184: } andrej@2184: andrej@2184: andrej@2184: print_help() andrej@2184: { andrej@2184: echo "Usage: check_source.sh [--only-changes | --files-to-check ]" andrej@2184: echo "" andrej@2184: echo "By default without arguments script checks all python source files" andrej@2184: echo "" andrej@2184: echo "--only-changes" andrej@2184: echo " only files with local changes are checked. " andrej@2184: echo " If script is called from mercurial pretxncommit hook," andrej@2184: echo " then only commited files are checked" andrej@2184: echo "" andrej@2184: echo "--files-to-check " andrej@2184: echo " script read list of files to check from file.lst" andrej@2184: andrej@2184: exit 1 andrej@1786: } andrej@1786: andrej@1786: main() andrej@1786: { andrej@2184: get_files_to_check $@ andrej@1786: compile_checks andrej@1786: pep8_checks_default andrej@1786: # pep8_checks_selected andrej@2184: andrej@1786: # flake8_checks andrej@1827: pylint_checks andrej@1786: exit $exit_code andrej@1786: } andrej@1786: andrej@2184: [ "$1" = "--help" -o "$1" = "-h" ] && print_help andrej@2184: main $@