Skip to content

Commit

Permalink
Remove dependency on ansible library
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Aug 30, 2020
1 parent db6cc31 commit 8fe3178
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion molecule/config.py
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
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
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
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
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
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

0 comments on commit 8fe3178

Please sign in to comment.