From 2e35b9ac4334038128b45e4642412863ccf0b1a7 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Thu, 5 Nov 2020 14:57:50 +0200 Subject: [PATCH] Initial implementation of validation script --- .travis.yml | 11 ++++------- ci_scripts/.gitignore | 2 ++ ci_scripts/README.md | 7 +++++++ ci_scripts/flake8_linter_check.cfg | 15 +++++++++++++++ ci_scripts/flake8_syntax_check.cfg | 9 +++++++++ ci_scripts/install_requirements.ps1 | 5 +++++ ci_scripts/isort.cfg | 5 +++++ ci_scripts/requirements.txt | 6 ++++++ ci_scripts/validate.ps1 | 20 ++++++++++++++++++++ 9 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 ci_scripts/.gitignore create mode 100644 ci_scripts/README.md create mode 100644 ci_scripts/flake8_linter_check.cfg create mode 100644 ci_scripts/flake8_syntax_check.cfg create mode 100644 ci_scripts/install_requirements.ps1 create mode 100644 ci_scripts/isort.cfg create mode 100644 ci_scripts/requirements.txt create mode 100644 ci_scripts/validate.ps1 diff --git a/.travis.yml b/.travis.yml index 887b7cc67..35f6210cb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -60,20 +60,17 @@ before_script: script: # Check Python code -## Check syntax errors and fail the build if any are found. -- flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics +- flake8 ./monkey --config ./ci_scripts/flake8_syntax_check.config ## Warn about linter issues. ### --exit-zero forces Flake8 to use the exit status code 0 even if there are errors, which means this will NOT fail the build. -### --count will print the total number of errors. -### --statistics Count the number of occurrences of each error/warning code and print a report. ### The output is redirected to a file. -- flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics > flake8_warnings.txt +- flake8 ./monkey --exit-zero --config ./ci_scripts/flake8_linter_check.config > ./ci_scripts/flake8_warnings.txt ## Display the linter issues -- cat flake8_warnings.txt +- cat ./ci_scripts/flake8_warnings.txt ## Make sure that we haven't increased the amount of warnings. - PYTHON_WARNINGS_AMOUNT_UPPER_LIMIT=80 -- if [ $(tail -n 1 flake8_warnings.txt) -gt $PYTHON_WARNINGS_AMOUNT_UPPER_LIMIT ]; then echo "Too many python linter warnings! Failing this build. Lower the amount of linter errors in this and try again. " && exit 1; fi +- if [ $(tail -n 1 ./ci_scripts/flake8_warnings.txt) -gt $PYTHON_WARNINGS_AMOUNT_UPPER_LIMIT ]; then echo "Too many python linter warnings! Failing this build. Lower the amount of linter errors in this and try again. " && exit 1; fi ## Check import order - python -m isort . -c -p common -p infection_monkey -p monkey_island diff --git a/ci_scripts/.gitignore b/ci_scripts/.gitignore new file mode 100644 index 000000000..67f93fcdc --- /dev/null +++ b/ci_scripts/.gitignore @@ -0,0 +1,2 @@ +./validation-env +./flake8_warnings.txt diff --git a/ci_scripts/README.md b/ci_scripts/README.md new file mode 100644 index 000000000..af555a7dd --- /dev/null +++ b/ci_scripts/README.md @@ -0,0 +1,7 @@ +# About + +Run this script to validate your code locally and auto fix/format the problems before pushing. + +# Usage + +run from `infection_monkey` directory: `powershell .\ci_scripts\validate.ps1` diff --git a/ci_scripts/flake8_linter_check.cfg b/ci_scripts/flake8_linter_check.cfg new file mode 100644 index 000000000..b8daeaf70 --- /dev/null +++ b/ci_scripts/flake8_linter_check.cfg @@ -0,0 +1,15 @@ +[flake8] +## Warn about linter issues. + +exclude = ../monkey/monkey_island/cc/ui, + ../monkey/common/cloud +show-source = True +max-complexity = 10 +max-line-length = 127 + +### --statistics Count the number of occurrences of each error/warning code and print a report. +statistics = True + +### --count will print the total number of errors. +count = True + diff --git a/ci_scripts/flake8_syntax_check.cfg b/ci_scripts/flake8_syntax_check.cfg new file mode 100644 index 000000000..7171e3db6 --- /dev/null +++ b/ci_scripts/flake8_syntax_check.cfg @@ -0,0 +1,9 @@ +[flake8] + +## Check syntax errors and fail the build if any are found. +exclude = ../monkey/monkey_island/cc/ui, + ../monkey/common/cloud +select = E901,E999,F821,F822,F823 +count = True +show-source = True +statistics = True diff --git a/ci_scripts/install_requirements.ps1 b/ci_scripts/install_requirements.ps1 new file mode 100644 index 000000000..de42d8599 --- /dev/null +++ b/ci_scripts/install_requirements.ps1 @@ -0,0 +1,5 @@ +python -m venv validation-env +.\validation-env\Scripts\activate.ps1 +python -m pip install -r .\requirements.txt +npm i -g eslint +deactivate diff --git a/ci_scripts/isort.cfg b/ci_scripts/isort.cfg new file mode 100644 index 000000000..f9a62b0ed --- /dev/null +++ b/ci_scripts/isort.cfg @@ -0,0 +1,5 @@ +[settings] +project=common,infection_monkey,monkey_island +skip=./monkey/common/cloud/scoutsuite,./monkey/monkey_island/cc/services/zero_trust/scoutsuite/data_parsing/rule_path_building/rule_path_creators_list.py,./monkey/monkey_island/cc/ui +line-length=120 +wrap-length=120 diff --git a/ci_scripts/requirements.txt b/ci_scripts/requirements.txt new file mode 100644 index 000000000..2b7db1909 --- /dev/null +++ b/ci_scripts/requirements.txt @@ -0,0 +1,6 @@ +flake8 +pytest +dlint +isort +coverage +black diff --git a/ci_scripts/validate.ps1 b/ci_scripts/validate.ps1 new file mode 100644 index 000000000..98a0ab4dd --- /dev/null +++ b/ci_scripts/validate.ps1 @@ -0,0 +1,20 @@ +.\ci_scripts\validation-env\Scripts\activate.ps1 +flake8 ./monkey --config ./ci_scripts/flake8_syntax_check.cfg +flake8 ./monkey --exit-zero --config ./ci_scripts/flake8_linter_check.cfg | Out-File -FilePath .\ci_scripts\flake8_warnings.txt +Get-Content -Path .\ci_scripts\flake8_warnings.txt +$PYTHON_WARNINGS_AMOUNT_UPPER_LIMIT = 80 +if ((Get-Item -Path .\ci_scripts\flake8_warnings.txt | Get-Content -Tail 1) -gt $PYTHON_WARNINGS_AMOUNT_UPPER_LIMIT){ + "Too many python linter warnings! Failing this build. Lower the amount of linter errors in this and try again. " + exit +} +python -m isort ./monkey -c --settings-file ./ci_scripts/isort.cfg +# python -m isort ./monkey --settings-file ./ci_scripts/isort.cfg +python monkey_island/cc/environment/set_server_config.py testing +python -m pytest +Push-Location -Path .\monkey_island\cc\ui +eslint ./src -c ./.eslintrc + +Pop-Location +Pop-Location +$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'); +deactivate