tests/tools/check_source.sh
changeset 1786 b6d47158d68a
child 1827 b8b47f9b5e56
equal deleted inserted replaced
1785:0ff2a45dcefa 1786:b6d47158d68a
       
     1 #!/bin/sh
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 # This file is part of Beremiz, a Integrated Development Environment for
       
     5 # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
       
     6 #
       
     7 # Copyright (C) 2017: Andrey Skvortsov
       
     8 #
       
     9 # See COPYING file for copyrights details.
       
    10 #
       
    11 # This program is free software; you can redistribute it and/or
       
    12 # modify it under the terms of the GNU General Public License
       
    13 # as published by the Free Software Foundation; either version 2
       
    14 # of the License, or (at your option) any later version.
       
    15 #
       
    16 # This program is distributed in the hope that it will be useful,
       
    17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    19 # GNU General Public License for more details.
       
    20 #
       
    21 # You should have received a copy of the GNU General Public License
       
    22 # along with this program; if not, write to the Free Software
       
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    24 
       
    25 
       
    26 exit_code=0
       
    27 set_exit_error()
       
    28 {
       
    29     if [ $exit_code -eq 0 ]; then
       
    30        exit_code=1
       
    31     fi
       
    32 }
       
    33 
       
    34 
       
    35 compile_checks()
       
    36 {
       
    37     echo "Syntax checking..."
       
    38     py_files=$(find . -name '*.py')
       
    39 
       
    40     # remove compiled Python files
       
    41     find . -name '*.pyc' -exec rm -f {} \;
       
    42 
       
    43     for i in $py_files; do
       
    44         # echo $i
       
    45         python -m py_compile $i
       
    46         if [ $? -ne 0 ]; then
       
    47             echo "Syntax error in $i"
       
    48             set_exit_error
       
    49         fi
       
    50     done
       
    51 }
       
    52 
       
    53 # pep8 was renamed to pycodestyle
       
    54 # detect existed version
       
    55 pep8_detect()
       
    56 {
       
    57     test -n $pep8 && (which pep8 > /dev/null) && pep8="pep8"
       
    58     test -n $pep8 && (which pycodestyle > /dev/null) && pep8="pycodestyle"
       
    59     if [ -z $pep8 ]; then
       
    60         echo "pep8/pycodestyle is not found"
       
    61         set_exit_error
       
    62     fi
       
    63 }
       
    64 
       
    65 pep8_checks_default()
       
    66 {
       
    67     echo "Check basic code-style problems for PEP-8"
       
    68 
       
    69     test -n $pep8 && pep8_detect
       
    70     test -z $pep8 && return
       
    71 
       
    72     user_ignore=
       
    73     # user_ignore=$user_ignore,E265  # E265 block comment should start with '# '
       
    74     user_ignore=$user_ignore,E501  # E501 line too long (80 > 79 characters)
       
    75 
       
    76     # ignored by default,
       
    77     default_ignore=
       
    78     default_ignore=$default_ignore,E121  # E121 continuation line under-indented for hanging indent
       
    79     default_ignore=$default_ignore,E123  # E123 closing bracket does not match indentation of opening bracket’s line
       
    80     default_ignore=$default_ignore,E126  # E126 continuation line over-indented for hanging indent
       
    81     default_ignore=$default_ignore,E133  # E133 closing bracket is missing indentation
       
    82     default_ignore=$default_ignore,E226  # E226 missing whitespace around arithmetic operator
       
    83     default_ignore=$default_ignore,E241  # E241 multiple spaces after ':'
       
    84     default_ignore=$default_ignore,E242  # E242 tab after ‘,’
       
    85     default_ignore=$default_ignore,E704  # E704 multiple statements on one line (def)
       
    86     default_ignore=$default_ignore,W503  # W503 line break occurred before a binary operator
       
    87     ignore=$user_ignore,$default_ignore
       
    88 
       
    89     # $pep8 --ignore $ignore --exclude build ./
       
    90     $pep8 --max-line-length 300 --exclude build ./
       
    91     if [ $? -ne 0 ]; then
       
    92         set_exit_error
       
    93     fi
       
    94 }
       
    95 
       
    96 
       
    97 pep8_checks_selected()
       
    98 {
       
    99     echo "Check basic code-style problems for PEP-8 (selective)"
       
   100 
       
   101     test -n $pep8 && pep8_detect
       
   102     test -z $pep8 && return
       
   103 
       
   104     # select checks:
       
   105     user_select=
       
   106     user_select=$user_select,W291   # W291 trailing whitespace
       
   107     user_select=$user_select,E401   # E401 multiple imports on one line
       
   108     user_select=$user_select,E265   # E265 block comment should start with '# '
       
   109     user_select=$user_select,E228   # E228 missing whitespace around modulo operator
       
   110     user_select=$user_select,W293   # W293 blank line contains whitespace
       
   111     user_select=$user_select,E302   # E302 expected 2 blank lines, found 1
       
   112     user_select=$user_select,E261   # E261 at least two spaces before inline comment
       
   113     user_select=$user_select,E271   # E271 multiple spaces after keyword
       
   114     user_select=$user_select,E231   # E231 missing whitespace after ','
       
   115     user_select=$user_select,E303   # E303 too many blank lines (2)
       
   116     user_select=$user_select,E225   # E225 missing whitespace around operator
       
   117     user_select=$user_select,E711   # E711 comparison to None should be 'if cond is not None:'
       
   118     user_select=$user_select,E251   # E251 unexpected spaces around keyword / parameter equals
       
   119     user_select=$user_select,E227   # E227 missing whitespace around bitwise or shift operator
       
   120     user_select=$user_select,E202   # E202 whitespace before ')'
       
   121     user_select=$user_select,E201   # E201 whitespace after '{'
       
   122     user_select=$user_select,W391   # W391 blank line at end of file
       
   123     user_select=$user_select,E305   # E305 expected 2 blank lines after class or function definition, found X
       
   124     user_select=$user_select,E306   # E306 expected 1 blank line before a nested definition, found X
       
   125     user_select=$user_select,E703   # E703 statement ends with a semicolon
       
   126     user_select=$user_select,E701   # E701 multiple statements on one line (colon)
       
   127     user_select=$user_select,E221   # E221 multiple spaces before operator
       
   128     user_select=$user_select,E741   # E741 ambiguous variable name 'l'
       
   129     user_select=$user_select,E111   # E111 indentation is not a multiple of four
       
   130     user_select=$user_select,E222   # E222 multiple spaces after operator
       
   131     user_select=$user_select,E712   # E712 comparison to True should be 'if cond is True:' or 'if cond:'
       
   132     user_select=$user_select,E262   # E262 inline comment should start with '# '
       
   133     user_select=$user_select,E203   # E203 whitespace before ','
       
   134     user_select=$user_select,E731   # E731 do not assign a lambda expression, use a def
       
   135     user_select=$user_select,W601   # W601 .has_key() is deprecated, use 'in'
       
   136     user_select=$user_select,E502   # E502 the backslash is redundant between brackets
       
   137     user_select=$user_select,W602   # W602 deprecated form of raising exception
       
   138     user_select=$user_select,E129   # E129 visually indented line with same indent as next logical line
       
   139     user_select=$user_select,E127   # E127 continuation line over-indented for visual indent
       
   140     user_select=$user_select,E128   # E128 continuation line under-indented for visual indent
       
   141     user_select=$user_select,E125   # E125 continuation line with same indent as next logical line
       
   142     user_select=$user_select,E114   # E114 indentation is not a multiple of four (comment)
       
   143     user_select=$user_select,E211   # E211 whitespace before '['
       
   144     user_select=$user_select,W191   # W191 indentation contains tabs
       
   145     user_select=$user_select,E101   # E101 indentation contains mixed spaces and tabs
       
   146     user_select=$user_select,E124   # E124 closing bracket does not match visual indentation
       
   147     user_select=$user_select,E272   # E272 multiple spaces before keyword
       
   148     user_select=$user_select,E713   # E713 test for membership should be 'not in'
       
   149     user_select=$user_select,E122   # E122 continuation line missing indentation or outdented
       
   150     user_select=$user_select,E131   # E131 continuation line unaligned for hanging indent
       
   151     user_select=$user_select,E721   # E721 do not compare types, use 'isinstance()'
       
   152     user_select=$user_select,E115   # E115 expected an indented block (comment)
       
   153     user_select=$user_select,E722   # E722 do not use bare except'
       
   154     user_select=$user_select,E266   # E266 too many leading '#' for block comment
       
   155     user_select=$user_select,E402   # E402 module level import not at top of file
       
   156     user_select=$user_select,W503   # W503 line break before binary operator
       
   157 
       
   158     $pep8 --select $user_select --exclude=build .
       
   159     if [ $? -ne 0 ]; then
       
   160         set_exit_error
       
   161     fi
       
   162 }
       
   163 
       
   164 flake8_checks()
       
   165 {
       
   166     echo "Check for problems using flake8 ..."
       
   167 
       
   168     which flake8 > /dev/null
       
   169     if [ $? -ne 0 ]; then
       
   170         echo "flake8 is not found"
       
   171         set_exit_error
       
   172         return
       
   173     fi
       
   174 
       
   175     flake8 --max-line-length=300  --exclude=build --builtins="_" ./
       
   176     if [ $? -ne 0 ]; then
       
   177         set_exit_error
       
   178     fi
       
   179 }
       
   180 
       
   181 pylint_checks()
       
   182 {
       
   183     echo "Check for problems using pylint ..."
       
   184 
       
   185     which pylint > /dev/null
       
   186     if [ $? -ne 0 ]; then
       
   187         echo "pylint is not found"
       
   188         set_exit_error
       
   189         return
       
   190     fi
       
   191 
       
   192     export PYTHONPATH="$PWD/../CanFestival-3/objdictgen":$PYTHONPATH
       
   193 
       
   194     disable=
       
   195     disable=$disable,C0103 # invalid-name
       
   196     disable=$disable,C0111 # missing-docstring
       
   197     disable=$disable,W0703 # broad-except
       
   198     disable=$disable,C0326 # bad whitespace
       
   199     disable=$disable,C0301 # Line too long
       
   200     disable=$disable,C0302 # Too many lines in module
       
   201     disable=$disable,W0511 # fixme
       
   202 
       
   203     enable=
       
   204     # enable=$enable,W0403   # relative import
       
   205 
       
   206     options=
       
   207     options="$options --additional-builtins=_"
       
   208     options="$options -r no"
       
   209     # options="$options --py3k"   # report errors for Python 3 porting
       
   210     # options="$options -E"       # report only major errors
       
   211     options="$options --output-format=parseable"
       
   212 
       
   213     if [ -n "$enable" ]; then
       
   214         options="$options --disable=all"
       
   215         options="$options --enable=$enable"
       
   216     else
       
   217         options="$options --disable=$disable"
       
   218     fi
       
   219     echo $options
       
   220 
       
   221     find ./ -name '*.py' | grep -v '/build/' | xargs pylint $options
       
   222     if [ $? -ne 0 ]; then
       
   223         set_exit_error
       
   224     fi
       
   225 }
       
   226 
       
   227 main()
       
   228 {
       
   229     compile_checks
       
   230     pep8_checks_default
       
   231     # pep8_checks_selected
       
   232     # flake8_checks
       
   233     # pylint_checks
       
   234     exit $exit_code
       
   235 }
       
   236 
       
   237 main