Add argument to set list of source files to check
authorAndrey Skvortsov <andrej.skvortzov@gmail.com>
Fri, 15 Jun 2018 16:12:50 +0300
changeset 2184 bbd2364fbf71
parent 2183 63591802d3e2
child 2185 439fc5d13c41
Add argument to set list of source files to check

This is necessary, in case you want to use docker run in pretxncommit
Mercurial hook. Unfortunately for some reason if hg running in
container can't find just created commit defined by $HG_NODE. And it
throws error 'unknown revision'.

Here is example for hgrc.

[hooks]
pretxncommit.linter = hg status -m -n -a -n -I '**.py' --change $HG_NODE > files.lst && docker run --volume=$PWD:/beremiz --workdir="/beremiz" --volume=$PWD/../CanFestival-3:/CanFestival-3 --memory=1g --entrypoint=/beremiz/tests/tools/check_source.sh skvorl/beremiz-requirements --files-to-check files.lst
tests/tools/check_source.sh
--- a/tests/tools/check_source.sh	Sat Jun 09 17:59:46 2018 +0300
+++ b/tests/tools/check_source.sh	Fri Jun 15 16:12:50 2018 +0300
@@ -319,26 +319,66 @@
     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"
+            if [ ! -z "$HG_NODE" ]; then
+                hg_change="--change $HG_NODE"
+                msg="for commit $HG_NODE"
+            else
+                hg_change=""
+                msg="in local repository"
+            fi
+            echo "Only changes ${msg} will be checked"
             echo ""
-            py_files=$(hg status -m -a -n -I '**.py')
-            if [ -z "$py_files" ]; then
-                echo "No files to check"
-                exit 0;
+            py_files=$(hg status -m -a -n -I '**.py' $hg_change)
+            if [ $? -ne 0 ]; then
+                exit 1;
             fi
        fi
     fi
+    if [ "$1" = "--files-to-check" ]; then
+        list="$2"
+        if [ -z "$list" ]; then
+            echo "--files-to-check requires filename as argument"
+            print_help
+        fi
+        if [ -e "$list" ]; then
+            py_files=$(cat $2 | grep '\.py$')
+        fi
+    fi
+    if [ -z "$py_files" ]; then
+        echo "No files to check"
+        exit 0;
+    fi
+}
+
+
+print_help()
+{
+    echo "Usage: check_source.sh [--only-changes | --files-to-check <filename> ]"
+    echo ""
+    echo "By default without arguments script checks all python source files"
+    echo ""
+    echo "--only-changes"
+    echo "                only files with local changes are checked. "
+    echo "                If script is called from mercurial pretxncommit hook,"
+    echo "                then only commited files are checked"
+    echo ""
+    echo "--files-to-check <file.lst>"
+    echo "                script read list of files to check from file.lst"
+
+    exit 1
 }
 
 main()
 {
-    get_files_to_check $1
+    get_files_to_check $@
     compile_checks
     pep8_checks_default
     # pep8_checks_selected
+
     # flake8_checks
     pylint_checks
     exit $exit_code
 }
 
-main $1
+[ "$1" = "--help" -o "$1" = "-h" ] && print_help
+main $@