From 2525d0a136c8b38735c8976ffa385bde04c213d8 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Wed, 8 Feb 2023 15:19:40 -0800 Subject: [PATCH] basic.py - Perform Python version check earlier (#79954) --- changelogs/fragments/basic-version-check.yml | 2 ++ lib/ansible/module_utils/basic.py | 28 +++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 changelogs/fragments/basic-version-check.yml diff --git a/changelogs/fragments/basic-version-check.yml b/changelogs/fragments/basic-version-check.yml new file mode 100644 index 00000000000..dade86365d8 --- /dev/null +++ b/changelogs/fragments/basic-version-check.yml @@ -0,0 +1,2 @@ +bugfixes: + - basic.py module_utils - Perform Python version check much earlier to ensure it runs before other errors occur. diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 42df052a03c..67be7e489c5 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -5,6 +5,21 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type +import sys + +# Used for determining if the system is running a new enough python version +# and should only restrict on our documented minimum versions +_PY3_MIN = sys.version_info >= (3, 5) +_PY2_MIN = (2, 7) <= sys.version_info < (3,) +_PY_MIN = _PY3_MIN or _PY2_MIN + +if not _PY_MIN: + print( + '\n{"failed": true, ' + '"msg": "ansible-core requires a minimum of Python2 version 2.7 or Python3 version 3.5. Current version: %s"}' % ''.join(sys.version.splitlines()) + ) + sys.exit(1) + FILE_ATTRIBUTES = { 'A': 'noatime', 'a': 'append', @@ -49,7 +64,6 @@ import shutil import signal import stat import subprocess -import sys import tempfile import time import traceback @@ -254,18 +268,6 @@ MODE_OPERATOR_RE = re.compile(r'[+=-]') USERS_RE = re.compile(r'[^ugo]') PERMS_RE = re.compile(r'[^rwxXstugo]') -# Used for determining if the system is running a new enough python version -# and should only restrict on our documented minimum versions -_PY3_MIN = sys.version_info >= (3, 5) -_PY2_MIN = (2, 7) <= sys.version_info < (3,) -_PY_MIN = _PY3_MIN or _PY2_MIN -if not _PY_MIN: - print( - '\n{"failed": true, ' - '"msg": "ansible-core requires a minimum of Python2 version 2.7 or Python3 version 3.5. Current version: %s"}' % ''.join(sys.version.splitlines()) - ) - sys.exit(1) - # # Deprecated functions