Add command line argument to run linter only on changed files
authorAndrey Skvortsov <andrej.skvortzov@gmail.com>
Sat, 09 Jun 2018 17:13:16 +0300
changeset 2181 52630996e51b
parent 2180 017ad9cc20b9
child 2182 eeca1aff0691
Add command line argument to run linter only on changed files

./tests/tools/check_source.sh --only-changes

It's recommended to run check_source.sh automatically on each commit,
so the rules are always enforced.
Even better is to use docker for that, so the versions of pep8 and
pylint are the same as on pipeline server.

To do this couple of lines should be added into repository's hgrc file.

[----------- cut from .hg/hgrc------------------]

[hooks]
precommit.linter = ./tests/tools/check_source.sh --only-changes
# precommit.linter = docker run -it --volume=$PWD:/beremiz --workdir="/beremiz" --volume=$PWD/../CanFestival-3:/CanFestival-3 --memory=1g --entrypoint=/beremiz/tests/tools/check_source.sh skvorl/beremiz-requirements --only-changes

[-----------------------------------------------]
tests/tools/check_source.sh
--- a/tests/tools/check_source.sh	Fri Jun 08 15:10:27 2018 +0300
+++ b/tests/tools/check_source.sh	Sat Jun 09 17:13:16 2018 +0300
@@ -35,8 +35,6 @@
 compile_checks()
 {
     echo "Syntax checking using python ..."
-    py_files=$(find . -name '*.py')
-
     python --version
 
     # remove compiled Python files
@@ -93,7 +91,7 @@
     ignore=$user_ignore,$default_ignore
 
     # $pep8 --ignore $ignore --exclude build ./
-    $pep8 --max-line-length 300 --exclude build ./
+    $pep8 --max-line-length 300 --exclude build $py_files
     if [ $? -ne 0 ]; then
         set_exit_error
     fi
@@ -164,7 +162,7 @@
     user_select=$user_select,E402   # E402 module level import not at top of file
     user_select=$user_select,W503   # W503 line break before binary operator
 
-    $pep8 --select $user_select --exclude=build .
+    $pep8 --select $user_select --exclude=build $py_files
     if [ $? -ne 0 ]; then
         set_exit_error
     fi
@@ -187,7 +185,7 @@
     echo -n "flake8 version: "
     flake8 --version
 
-    flake8 --max-line-length=300  --exclude=build --builtins="_" ./
+    flake8 --max-line-length=300  --exclude=build --builtins="_" $py_files
     if [ $? -ne 0 ]; then
         set_exit_error
     fi
@@ -306,17 +304,31 @@
     fi
     # echo $options
 
-    find ./ -name '*.py' | grep -v '/build/' | xargs pylint $options 
-    if [ $? -ne 0 ]; then
-        set_exit_error
-    fi
-
-    echo "DONE"
-    echo ""
+    echo $py_files | xargs pylint $options 
+    if [ $? -ne 0 ]; then
+        set_exit_error
+    fi
+
+    echo "DONE"
+    echo ""
+}
+
+
+get_files_to_check()
+{
+    py_files=$(find . -name '*.py' -not -path '*/build/*')    
+    if [ "$1" = "--only-changes" ]; then
+        if which hg > /dev/null; then
+            echo "Only changes will be checked"
+            echo ""
+            py_files=$(hg status -m -a -n -I '**.py')
+        fi
+    fi
 }
 
 main()
 {
+    get_files_to_check $1
     compile_checks
     pep8_checks_default
     # pep8_checks_selected
@@ -325,4 +337,4 @@
     exit $exit_code
 }
 
-main
+main $1