diff --git a/check-docstyle.sh b/check-docstyle.sh index 26c30a6..448dc8e 100755 --- a/check-docstyle.sh +++ b/check-docstyle.sh @@ -1,6 +1,6 @@ #!/bin/bash -directories="fabric8a_auth tests" +directories="fabric8a_auth tests tools" separate_files="setup.py" pass=0 fail=0 diff --git a/cico_run_pydoc.sh b/cico_run_pydoc.sh index c88e810..3b2aa35 100755 --- a/cico_run_pydoc.sh +++ b/cico_run_pydoc.sh @@ -8,5 +8,10 @@ prep() { yum -y install python36 python36-virtualenv which } +check_python_version() { + python3 tools/check_python_version.py 3 6 +} + prep +check_python_version ./check-docstyle.sh diff --git a/cico_run_pylint.sh b/cico_run_pylint.sh index 79bbeaa..5d595d5 100755 --- a/cico_run_pylint.sh +++ b/cico_run_pylint.sh @@ -8,10 +8,15 @@ prep() { yum -y install python36 python36-virtualenv which } +check_python_version() { + python3 tools/check_python_version.py 3 6 +} + # this script is copied by CI, we don't need it rm -f env-toolkit prep +check_python_version ./detect-common-errors.sh ./detect-dead-code.sh ./measure-cyclomatic-complexity.sh --fail-on-error diff --git a/detect-common-errors.sh b/detect-common-errors.sh index 643ac96..d8c2f00 100755 --- a/detect-common-errors.sh +++ b/detect-common-errors.sh @@ -1,6 +1,6 @@ #!/bin/bash -directories="fabric8a_auth tests" +directories="fabric8a_auth tests tools" separate_files="setup.py" pass=0 fail=0 diff --git a/detect-dead-code.sh b/detect-dead-code.sh index 0be0691..50bf8eb 100755 --- a/detect-dead-code.sh +++ b/detect-dead-code.sh @@ -1,6 +1,6 @@ #!/bin/bash -directories="fabric8a_auth tests" +directories="fabric8a_auth tests tools" separate_files="setup.py" pass=0 fail=0 diff --git a/run-linter.sh b/run-linter.sh index 7895b30..fdcd08c 100755 --- a/run-linter.sh +++ b/run-linter.sh @@ -1,6 +1,6 @@ #!/bin/bash -directories="fabric8a_auth tests" +directories="fabric8a_auth tests tools" separate_files="setup.py" pass=0 fail=0 diff --git a/runtests.sh b/runtests.sh index 17dee24..824d2df 100755 --- a/runtests.sh +++ b/runtests.sh @@ -20,6 +20,10 @@ export PYTHONPATH printf "%sCreate Virtualenv for Python deps ..." "${NORMAL}" +check_python_version() { + python3 tools/check_python_version.py 3 6 +} + function prepare_venv() { VIRTUALENV=$(which virtualenv) if [ $? -eq 1 ] @@ -37,6 +41,8 @@ function prepare_venv() { printf "%sPython virtual environment initialized%s\n" "${YELLOW}" "${NORMAL}" } +check_python_version + [ "$NOVENV" == "1" ] || prepare_venv || exit 1 # this script is copied by CI, we don't need it diff --git a/tools/check_python_version.py b/tools/check_python_version.py new file mode 100644 index 0000000..94cae0d --- /dev/null +++ b/tools/check_python_version.py @@ -0,0 +1,71 @@ +"""Check if the installed Python interpreter has correct version. + +This script has to be called with two command line arguments: +expected_major_version expected_minor_version + +The script then check if actual Python version (major+minor) is +the same or newer than expected version. + +Usage: +python check_python_version.py 2.7 +python3 check_python_version.py 3.6 +python3 check_python_version.py 3.7 +etc. +""" + +import sys + + +def get_expected_version(arguments): + """Get the expected version from arguments provided on command line.""" + if len(arguments) <= 2: + print("Usage python check_python_version.py major minor\n" + " python3 check_python_version.py major minor") + raise Exception("CLI arguments missing") + + # try to read major version + try: + major = int(arguments[1]) + except Exception as e: + print("Can not parse major version '{}'".format(arguments[1])) + raise e + + # try to read minor version + try: + minor = int(arguments[2]) + except Exception as e: + print("Can not parse minor version '{}'".format(arguments[2])) + raise e + + return (major, minor) + + +def get_actual_version(): + """Get the actual version of Python interpreter.""" + return (sys.version_info.major, sys.version_info.minor) + + +def compare_versions(actual, expected): + """Compare Python versions, return the exit code.""" + if actual < expected: + print("Unsupported version {}.{}".format(actual[0], actual[1])) + return 1 + else: + m = "OK: actual Python version {}.{} conforms to expected version {}.{}" + print(m.format(actual[0], actual[1], expected[0], expected[1])) + return 0 + + +def main(): + """Entry to the Python version comparator.""" + try: + actual = get_actual_version() + expected = get_expected_version(sys.argv) + exit_code = compare_versions(actual, expected) + sys.exit(exit_code) + except Exception: + sys.exit(2) + + +if __name__ == "__main__": + main()