Skip to content

Commit

Permalink
Allow use without pre-activation of virtualenv
Browse files Browse the repository at this point in the history
Fixes: #1507
  • Loading branch information
ssbarnea committed Feb 10, 2022
1 parent ae2ba4c commit a4e3303
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@

from enrich.console import should_do_markup

# pylint: disable=unused-import
import ansiblelint._preimport # must be kept before other ansiblelint imports!

# pylint: enable=unused-import
from ansiblelint import cli
from ansiblelint.app import App
from ansiblelint.color import console, console_options, reconfigure, render_yaml
from ansiblelint.config import options
from ansiblelint.constants import ANSIBLE_MISSING_RC, EXIT_CONTROL_C_RC
from ansiblelint.constants import EXIT_CONTROL_C_RC
from ansiblelint.file_utils import abspath, cwd, normpath
from ansiblelint.prerun import check_ansible_presence, prepare_environment
from ansiblelint.skip_utils import normalize_tag
Expand Down Expand Up @@ -81,15 +85,12 @@ def initialize_options(arguments: Optional[List[str]] = None) -> None:
new_options.cwd = pathlib.Path.cwd()

if new_options.version:
ansible_version, err = check_ansible_presence()
ansible_version, _ = check_ansible_presence(exit_on_error=True)
print(
"ansible-lint {ver!s} using ansible {ansible_ver!s}".format(
ver=__version__, ansible_ver=ansible_version
)
)
if err:
_logger.error(err)
sys.exit(ANSIBLE_MISSING_RC)
sys.exit(0)

if new_options.colored is None:
Expand Down
11 changes: 11 additions & 0 deletions src/ansiblelint/_preimport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Code that must run before other imports."""
import os
import sys

# Hack to allow a venv installed ansible-lint be called from outside the venv
paths = [x for x in os.environ.get("PATH", "").split(":") if x]
py_path = os.path.dirname(sys.executable)
if py_path not in paths:
paths.insert(0, py_path)
os.environ["PATH"] = ":".join(paths)
print(f"WARNING: PATH altered to include {py_path}", file=sys.stderr)
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ deps =
commands =
# safety measure to assure we do not accidentally run tests with broken dependencies
{envpython} -m pip check
# Ensure that ansible-lint can be called w/o activated virtualenv
env -i HOME="$HOME" {envdir}/bin/ansible-lint --version
# We add coverage options but not making them mandatory as we do not want to force
# pytest users to run coverage when they just want to run a single test with `pytest -k test`
{envpython} -m pytest \
Expand Down Expand Up @@ -58,6 +60,7 @@ setenv =
PRE_COMMIT_COLOR = always
FORCE_COLOR = 1
allowlist_externals =
env
sh
tox
# both options needed to workaround https://github.com/tox-dev/tox/issues/2197
Expand Down

0 comments on commit a4e3303

Please sign in to comment.