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

Enable prepare_environment to be used without cli #1488

Merged
merged 1 commit into from Mar 22, 2021
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
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