mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
2.9 KiB
Python
114 lines
2.9 KiB
Python
8 years ago
|
"""Miscellaneous utility functions and classes specific to ansible cli tools."""
|
||
5 years ago
|
from __future__ import (absolute_import, division, print_function)
|
||
|
__metaclass__ = type
|
||
8 years ago
|
|
||
6 years ago
|
import json
|
||
8 years ago
|
import os
|
||
|
|
||
5 years ago
|
from .constants import (
|
||
6 years ago
|
SOFT_RLIMIT_NOFILE,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .util import (
|
||
7 years ago
|
common_environment,
|
||
6 years ago
|
display,
|
||
|
find_python,
|
||
7 years ago
|
ApplicationError,
|
||
5 years ago
|
ANSIBLE_ROOT,
|
||
5 years ago
|
ANSIBLE_TEST_DATA_ROOT,
|
||
7 years ago
|
)
|
||
|
|
||
5 years ago
|
from .util_common import (
|
||
5 years ago
|
run_command,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .config import (
|
||
7 years ago
|
IntegrationConfig,
|
||
6 years ago
|
EnvironmentConfig,
|
||
7 years ago
|
)
|
||
8 years ago
|
|
||
5 years ago
|
from .data import (
|
||
5 years ago
|
data_context,
|
||
|
)
|
||
8 years ago
|
|
||
6 years ago
|
CHECK_YAML_VERSIONS = {}
|
||
|
|
||
|
|
||
6 years ago
|
def ansible_environment(args, color=True, ansible_config=None):
|
||
8 years ago
|
"""
|
||
|
:type args: CommonConfig
|
||
8 years ago
|
:type color: bool
|
||
6 years ago
|
:type ansible_config: str | None
|
||
8 years ago
|
:rtype: dict[str, str]
|
||
|
"""
|
||
|
env = common_environment()
|
||
|
path = env['PATH']
|
||
|
|
||
5 years ago
|
ansible_path = os.path.join(ANSIBLE_ROOT, 'bin')
|
||
8 years ago
|
|
||
6 years ago
|
if not path.startswith(ansible_path + os.path.pathsep):
|
||
|
path = ansible_path + os.path.pathsep + path
|
||
8 years ago
|
|
||
6 years ago
|
if ansible_config:
|
||
|
pass
|
||
|
elif isinstance(args, IntegrationConfig):
|
||
5 years ago
|
ansible_config = os.path.join(ANSIBLE_ROOT, 'test/integration/%s.cfg' % args.command)
|
||
7 years ago
|
else:
|
||
5 years ago
|
ansible_config = os.path.join(ANSIBLE_TEST_DATA_ROOT, '%s/ansible.cfg' % args.command)
|
||
7 years ago
|
|
||
6 years ago
|
if not args.explain and not os.path.exists(ansible_config):
|
||
7 years ago
|
raise ApplicationError('Configuration not found: %s' % ansible_config)
|
||
|
|
||
8 years ago
|
ansible = dict(
|
||
6 years ago
|
ANSIBLE_PYTHON_MODULE_RLIMIT_NOFILE=str(SOFT_RLIMIT_NOFILE),
|
||
8 years ago
|
ANSIBLE_FORCE_COLOR='%s' % 'true' if args.color and color else 'false',
|
||
8 years ago
|
ANSIBLE_DEPRECATION_WARNINGS='false',
|
||
8 years ago
|
ANSIBLE_HOST_KEY_CHECKING='false',
|
||
6 years ago
|
ANSIBLE_RETRY_FILES_ENABLED='false',
|
||
7 years ago
|
ANSIBLE_CONFIG=os.path.abspath(ansible_config),
|
||
6 years ago
|
ANSIBLE_LIBRARY='/dev/null',
|
||
5 years ago
|
PYTHONPATH=os.path.join(ANSIBLE_ROOT, 'lib'),
|
||
8 years ago
|
PAGER='/bin/cat',
|
||
|
PATH=path,
|
||
|
)
|
||
|
|
||
|
env.update(ansible)
|
||
|
|
||
8 years ago
|
if args.debug:
|
||
7 years ago
|
env.update(dict(
|
||
|
ANSIBLE_DEBUG='true',
|
||
|
ANSIBLE_LOG_PATH=os.path.abspath('test/results/logs/debug.log'),
|
||
|
))
|
||
8 years ago
|
|
||
5 years ago
|
if data_context().content.collection:
|
||
|
env.update(dict(
|
||
|
ANSIBLE_COLLECTIONS_PATHS=data_context().content.collection.root,
|
||
|
))
|
||
|
|
||
8 years ago
|
return env
|
||
6 years ago
|
|
||
|
|
||
|
def check_pyyaml(args, version):
|
||
|
"""
|
||
|
:type args: EnvironmentConfig
|
||
|
:type version: str
|
||
|
"""
|
||
|
if version in CHECK_YAML_VERSIONS:
|
||
|
return
|
||
|
|
||
|
python = find_python(version)
|
||
5 years ago
|
stdout, _dummy = run_command(args, [python, os.path.join(ANSIBLE_TEST_DATA_ROOT, 'yamlcheck.py')], capture=True)
|
||
6 years ago
|
|
||
|
if args.explain:
|
||
|
return
|
||
|
|
||
|
CHECK_YAML_VERSIONS[version] = result = json.loads(stdout)
|
||
|
|
||
|
yaml = result['yaml']
|
||
|
cloader = result['cloader']
|
||
|
|
||
|
if not yaml:
|
||
|
display.warning('PyYAML is not installed for interpreter: %s' % python)
|
||
|
elif not cloader:
|
||
|
display.warning('PyYAML will be slow due to installation without libyaml support for interpreter: %s' % python)
|