2019-04-02 14:34:03 +00:00
|
|
|
"""Base methods."""
|
|
|
|
|
2019-04-09 08:58:15 +00:00
|
|
|
import importlib
|
2019-04-02 14:34:03 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2019-04-09 08:58:15 +00:00
|
|
|
from distutils.version import LooseVersion
|
2019-04-02 14:34:03 +00:00
|
|
|
|
|
|
|
import ansible
|
2019-04-12 11:45:35 +00:00
|
|
|
import toolz
|
2019-04-01 13:07:22 +00:00
|
|
|
|
2019-04-10 14:09:37 +00:00
|
|
|
from ansiblelater import settings
|
|
|
|
from ansiblelater import utils
|
2019-04-02 14:34:03 +00:00
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
|
|
|
|
def get_settings(args):
|
2019-04-02 14:34:03 +00:00
|
|
|
"""
|
|
|
|
Get new settings object.
|
|
|
|
|
|
|
|
:param args: cli args from argparse
|
|
|
|
:returns: Settings object
|
|
|
|
|
|
|
|
"""
|
2020-04-05 12:33:43 +00:00
|
|
|
config = settings.Settings(args=args)
|
2019-04-01 13:07:22 +00:00
|
|
|
|
|
|
|
return config
|
2019-04-02 14:34:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_standards(filepath):
|
2019-04-11 09:57:30 +00:00
|
|
|
sys.path.append(os.path.abspath(os.path.expanduser(filepath)))
|
|
|
|
try:
|
|
|
|
standards = importlib.import_module("standards")
|
|
|
|
except ImportError as e:
|
|
|
|
utils.sysexit_with_message(
|
2020-04-11 14:20:41 +00:00
|
|
|
"Could not import standards from directory {path}: {msg}".format(
|
|
|
|
path=filepath, msg=str(e)
|
|
|
|
)
|
2020-04-05 12:33:43 +00:00
|
|
|
)
|
2019-04-11 09:57:30 +00:00
|
|
|
|
|
|
|
if getattr(standards, "ansible_min_version", None) and \
|
|
|
|
LooseVersion(standards.ansible_min_version) > LooseVersion(ansible.__version__):
|
2020-04-05 12:33:43 +00:00
|
|
|
utils.sysexit_with_message(
|
2020-04-11 14:20:41 +00:00
|
|
|
"Standards require ansible version {min_version} (current version {version}). "
|
|
|
|
"Please upgrade ansible.".format(
|
|
|
|
min_version=standards.ansible_min_version, version=ansible.__version__
|
|
|
|
)
|
2020-04-05 12:33:43 +00:00
|
|
|
)
|
2019-04-11 09:57:30 +00:00
|
|
|
|
2019-04-15 10:26:58 +00:00
|
|
|
if getattr(standards, "ansible_later_min_version", None) and \
|
2021-01-19 21:31:53 +00:00
|
|
|
utils.get_property("__version__") != "0.0.0" and \
|
2019-04-15 10:26:58 +00:00
|
|
|
LooseVersion(standards.ansible_later_min_version) > LooseVersion(
|
2019-04-11 09:57:30 +00:00
|
|
|
utils.get_property("__version__")):
|
|
|
|
utils.sysexit_with_message(
|
2020-04-11 14:20:41 +00:00
|
|
|
"Standards require ansible-later version {min_version} (current version {version}). "
|
|
|
|
"Please upgrade ansible-later.".format(
|
|
|
|
min_version=standards.ansible_later_min_version,
|
|
|
|
version=utils.get_property("__version__")
|
|
|
|
)
|
2020-04-05 12:33:43 +00:00
|
|
|
)
|
2019-04-11 09:57:30 +00:00
|
|
|
|
2019-04-12 11:45:35 +00:00
|
|
|
normalized_std = (list(toolz.remove(lambda x: x.id == "", standards.standards)))
|
|
|
|
unique_std = len(list(toolz.unique(normalized_std, key=lambda x: x.id)))
|
|
|
|
all_std = len(normalized_std)
|
|
|
|
if not all_std == unique_std:
|
|
|
|
utils.sysexit_with_message(
|
2020-04-05 12:33:43 +00:00
|
|
|
"Detect duplicate ID's in standards definition. Please use unique ID's only."
|
|
|
|
)
|
2019-04-12 11:45:35 +00:00
|
|
|
|
2019-04-11 09:57:30 +00:00
|
|
|
return standards.standards
|