Skip to content

Commit

Permalink
Enable prepare_environment to be used without cli (#1488)
Browse files Browse the repository at this point in the history
Allows reuse of prepare_environment() outside the ansible-lint, so
other tools like molecule can reuse its logic.
  • Loading branch information
ssbarnea committed Mar 22, 2021
1 parent 49d48c6 commit a956ebc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/ansiblelint/__main__.py
Expand Up @@ -27,7 +27,7 @@
import subprocess
import sys
from contextlib import contextmanager
from typing import TYPE_CHECKING, List
from typing import TYPE_CHECKING, List, Optional

from enrich.console import should_do_markup

Expand Down Expand Up @@ -76,9 +76,9 @@ def initialize_logger(level: int = 0) -> None:
_logger.debug("Logging initialized to level %s", logging_level)


def initialize_options(arguments: List[str]):
def initialize_options(arguments: Optional[List[str]] = None) -> None:
"""Load config options and store them inside options module."""
new_options = cli.get_config(arguments)
new_options = cli.get_config(arguments or [])
new_options.cwd = pathlib.Path.cwd()

if new_options.version:
Expand All @@ -105,6 +105,8 @@ def initialize_options(arguments: List[str]):
options.skip_list = [normalize_tag(tag) for tag in options.skip_list]
options.warn_list = [normalize_tag(tag) for tag in options.warn_list]

options.configured = True


def report_outcome(result: "LintResult", options, mark_as_success=False) -> int:
"""Display information about how to skip found rules.
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/config.py
Expand Up @@ -53,6 +53,7 @@

options = Namespace(
colored=True,
configured=False,
cwd=".",
display_relative_path=True,
exclude_paths=[],
Expand Down
8 changes: 8 additions & 0 deletions src/ansiblelint/prerun.py
Expand Up @@ -96,6 +96,14 @@ def _get_ver_err() -> Tuple[str, str]:

def prepare_environment() -> None:
"""Make dependencies available if needed."""
if not options.configured:
# Allow method to be used without calling the command line, so we can
# reuse it in other tools, like molecule.
# pylint: disable=import-outside-toplevel,cyclic-import
from ansiblelint.__main__ import initialize_options

initialize_options()

if not options.offline and os.path.exists("requirements.yml"):

cmd = [
Expand Down

0 comments on commit a956ebc

Please sign in to comment.