Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on ansible library #2805

Merged
merged 1 commit into from
Aug 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion molecule/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
from uuid import uuid4

import pkg_resources
from ansible.module_utils.parsing.convert_bool import boolean

from molecule import api, interpolation, logger, platforms, scenario, state, util
from molecule.dependency import ansible_galaxy, gilt, shell
from molecule.model import schema_v3
from molecule.provisioner import ansible
from molecule.util import boolean

LOG = logger.get_logger(__name__)
MOLECULE_DEBUG = boolean(os.environ.get("MOLECULE_DEBUG", "False"))
Expand Down
16 changes: 14 additions & 2 deletions molecule/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,28 @@
import logging
import os
import sys
from typing import Any

import colorama
from ansible.module_utils.parsing.convert_bool import boolean as to_bool


# Based on Ansible implementation
def to_bool(a: Any) -> bool:
"""Return a bool for the arg."""
if a is None or isinstance(a, bool):
return bool(a)
if isinstance(a, str):
a = a.lower()
if a in ("yes", "on", "1", "true", 1):
return True
return False


def should_do_markup() -> bool:
"""Decide about use of ANSI colors."""
py_colors = os.environ.get("PY_COLORS", None)
if py_colors is not None:
return to_bool(py_colors, strict=False)
return to_bool(py_colors)

return sys.stdout.isatty() and os.environ.get("TERM") != "dumb"

Expand Down
19 changes: 14 additions & 5 deletions molecule/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
# DEALINGS IN THE SOFTWARE.
"""Molecule Shell Module."""

import subprocess
import sys
from functools import lru_cache

import click
import click_completion
Expand All @@ -40,20 +42,27 @@
LOCAL_CONFIG_SEARCH = ".config/molecule/config.yml"
LOCAL_CONFIG = lookup_config_file(LOCAL_CONFIG_SEARCH)


ENV_FILE = ".env.yml"


@lru_cache()
def _version_string() -> str:

v = pkg_resources.parse_version(molecule.__version__)
color = "bright_yellow" if v.is_prerelease else "green" # type: ignore
msg = "molecule %s\n" % _colorize(molecule.__version__, color)

try:
ansible_version = pkg_resources.get_distribution("ansible-base").version
except Exception:
ansible_version = pkg_resources.get_distribution("ansible").version
# extract ansible from the command line
ansible_version = (
subprocess.run(
["ansible", "--version"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
.stdout.splitlines()[0]
.split()[1]
)

msg += _colorize(
" ansible==%s python==%s.%s"
Expand Down
3 changes: 1 addition & 2 deletions molecule/test/scenarios/plugins/library/project_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
"""testplugin role plugin library project_library."""
from ansible.module_utils.basic import AnsibleModule


def main():
Expand All @@ -35,6 +36,4 @@ def main():
module.exit_json(**ansible_facts_dict)


from ansible.module_utils.basic import * # noqa

main()
27 changes: 27 additions & 0 deletions molecule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import sys
from collections.abc import Mapping
from functools import lru_cache # noqa
from typing import Any

import colorama
import jinja2
Expand Down Expand Up @@ -360,3 +361,29 @@ def lookup_config_file(filename: str) -> str:
LOG.info("Found config file %s", f)
return f
return f


def boolean(value: Any, strict=True) -> bool:
"""Evaluate any object as boolean matching ansible behavior."""
# Based on https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/parsing/convert_bool.py

BOOLEANS_TRUE = frozenset(("y", "yes", "on", "1", "true", "t", 1, 1.0, True))
BOOLEANS_FALSE = frozenset(("n", "no", "off", "0", "false", "f", 0, 0.0, False))
BOOLEANS = BOOLEANS_TRUE.union(BOOLEANS_FALSE)

if isinstance(value, bool):
return value

normalized_value = value
if isinstance(value, (str, bytes)):
normalized_value = str(value).lower().strip()

if normalized_value in BOOLEANS_TRUE:
return True
elif normalized_value in BOOLEANS_FALSE or not strict:
return False

raise TypeError(
"The value '%s' is not a valid boolean. Valid booleans include: %s"
% (str(value), ", ".join(repr(i) for i in BOOLEANS))
)
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ setup_requires =

# These are required in actual runtime:
install_requires =
ansible >= 2.8 # keep it N/N-1

cerberus >= 1.3.1
click >= 7.0
click-completion >= 0.5.1
Expand Down Expand Up @@ -98,6 +96,8 @@ docker =
windows =
pywinrm
test =
ansible >= 2.8 # keep it N/N-1

ansi2html
coverage < 5 # https://github.com/pytest-dev/pytest-cov/issues/250

Expand Down