diff --git a/changelogs/fragments/version-libyaml-git.yml b/changelogs/fragments/version-libyaml-git.yml new file mode 100644 index 00000000000..51e843bc45c --- /dev/null +++ b/changelogs/fragments/version-libyaml-git.yml @@ -0,0 +1,4 @@ +minor_changes: +- CLI - Specify whether PyYAML includes libyaml support in version output +bugfixes: +- CLI - Restore git information in version output when running from source diff --git a/lib/ansible/cli/arguments/option_helpers.py b/lib/ansible/cli/arguments/option_helpers.py index 021fcb872e6..2e39fd6536a 100644 --- a/lib/ansible/cli/arguments/option_helpers.py +++ b/lib/ansible/cli/arguments/option_helpers.py @@ -13,6 +13,12 @@ import sys import time import yaml +try: + import _yaml + HAS_LIBYAML = True +except ImportError: + HAS_LIBYAML = False + import ansible from ansible import constants as C from ansible.module_utils._text import to_native @@ -141,47 +147,33 @@ def _git_repo_info(repo_path): def _gitinfo(): - basedir = os.path.join(os.path.dirname(__file__), '..', '..', '..') + basedir = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..')) repo_path = os.path.join(basedir, '.git') - result = _git_repo_info(repo_path) - submodules = os.path.join(basedir, '.gitmodules') - - if not os.path.exists(submodules): - return result - - with open(submodules) as f: - for line in f: - tokens = line.strip().split(' ') - if tokens[0] == 'path': - submodule_path = tokens[2] - submodule_info = _git_repo_info(os.path.join(basedir, submodule_path, '.git')) - if not submodule_info: - submodule_info = ' not found - use git submodule update --init ' + submodule_path - result += "\n {0}: {1}".format(submodule_path, submodule_info) - return result + return _git_repo_info(repo_path) def version(prog=None): """ return ansible version """ if prog: - result = " ".join((prog, __version__)) + result = [" ".join((prog, __version__))] else: - result = __version__ + result = [__version__] gitinfo = _gitinfo() if gitinfo: - result = result + " {0}".format(gitinfo) - result += "\n config file = %s" % C.CONFIG_FILE + result[0] = "{0} {1}".format(result[0], gitinfo) + result.append(" config file = %s" % C.CONFIG_FILE) if C.DEFAULT_MODULE_PATH is None: cpath = "Default w/o overrides" else: cpath = C.DEFAULT_MODULE_PATH - result = result + "\n configured module search path = %s" % cpath - result = result + "\n ansible python module location = %s" % ':'.join(ansible.__path__) - result = result + "\n ansible collection location = %s" % ':'.join(C.COLLECTIONS_PATHS) - result = result + "\n executable location = %s" % sys.argv[0] - result = result + "\n python version = %s" % ''.join(sys.version.splitlines()) - return result + result.append(" configured module search path = %s" % cpath) + result.append(" ansible python module location = %s" % ':'.join(ansible.__path__)) + result.append(" ansible collection location = %s" % ':'.join(C.COLLECTIONS_PATHS)) + result.append(" executable location = %s" % sys.argv[0]) + result.append(" python version = %s" % ''.join(sys.version.splitlines())) + result.append(" libyaml = %s" % HAS_LIBYAML) + return "\n".join(result) # diff --git a/test/units/cli/test_adhoc.py b/test/units/cli/test_adhoc.py index b660993bff1..2697e8a41f6 100644 --- a/test/units/cli/test_adhoc.py +++ b/test/units/cli/test_adhoc.py @@ -104,7 +104,7 @@ def test_ansible_version(capsys, mocker): # Python 2.6 does return a named tuple, so get the first item version_lines = version[0].splitlines() - assert len(version_lines) == 7, 'Incorrect number of lines in "ansible --version" output' + assert len(version_lines) == 8, 'Incorrect number of lines in "ansible --version" output' assert re.match('ansible [0-9.a-z]+$', version_lines[0]), 'Incorrect ansible version line in "ansible --version" output' assert re.match(' config file = .*$', version_lines[1]), 'Incorrect config file line in "ansible --version" output' assert re.match(' configured module search path = .*$', version_lines[2]), 'Incorrect module search path in "ansible --version" output' @@ -112,3 +112,4 @@ def test_ansible_version(capsys, mocker): assert re.match(' ansible collection location = .*$', version_lines[4]), 'Incorrect collection location in "ansible --version" output' assert re.match(' executable location = .*$', version_lines[5]), 'Incorrect executable locaction in "ansible --version" output' assert re.match(' python version = .*$', version_lines[6]), 'Incorrect python version in "ansible --version" output' + assert re.match(' libyaml = .*$', version_lines[7]), 'Missing libyaml in "ansible --version" output'