Skip to content

Commit

Permalink
More pathlib conversion (#3397)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ssbarnea and pre-commit-ci[bot] authored May 4, 2023
1 parent 3b7bcd8 commit e0d4e7d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ known-first-party = ["ansiblelint"]
"src/ansiblelint/rules/*.py" = ["S"]
"src/ansiblelint/testing/*.py" = ["S"]
# Temporary disabled until we fix them:
"src/ansiblelint/{utils,file_utils,runner,loaders,constants,config,cli,_mockings,__main__}.py" = [
"src/ansiblelint/{utils,file_utils,runner,loaders,constants,config,cli,_mockings}.py" = [
"PTH",
]

Expand Down
21 changes: 12 additions & 9 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ def _previous_revision() -> Iterator[None]:
)
yield
finally:
options.exclude_paths = [abspath(p, os.getcwd()) for p in rel_exclude_paths]
options.exclude_paths = [
str(Path.cwd().resolve() / p) for p in rel_exclude_paths
]


def _run_cli_entrypoint() -> None:
Expand Down Expand Up @@ -356,7 +358,7 @@ def path_inject() -> None: # noqa: C901
expanded = False
for idx, path in enumerate(paths):
if path.startswith("~"): # pragma: no cover
paths[idx] = os.path.expanduser(path)
paths[idx] = str(Path(path).expanduser())
expanded = True
if expanded: # pragma: no cover
print( # noqa: T201
Expand All @@ -366,15 +368,16 @@ def path_inject() -> None: # noqa: C901

inject_paths = []

userbase_bin_path = f"{site.getuserbase()}/bin"
if userbase_bin_path not in paths and os.path.exists(
f"{userbase_bin_path}/bin/ansible",
userbase_bin_path = Path(site.getuserbase()) / "bin"
if (
str(userbase_bin_path) not in paths
and (userbase_bin_path / "bin" / "ansible").exists()
):
inject_paths.append(userbase_bin_path)
inject_paths.append(str(userbase_bin_path))

py_path = os.path.dirname(sys.executable)
if py_path not in paths and os.path.exists(f"{py_path}/ansible"):
inject_paths.append(py_path)
py_path = Path(sys.executable).parent
if str(py_path) not in paths and (py_path / "ansible").exists():
inject_paths.append(str(py_path))

if not os.environ.get("PYENV_VIRTUAL_ENV", None):
if inject_paths:
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import sys
from collections import namedtuple
from pathlib import Path
from typing import TYPE_CHECKING, Any

import black
Expand Down Expand Up @@ -88,7 +89,7 @@ def matchtask( # noqa: C901
if isinstance(v, str):
try:
template(
basedir=file.dir if file else ".",
basedir=file.path.parent if file else Path("."),
value=v,
variables=deannotate(task.get("vars", {})),
fail_on_error=True, # we later decide which ones to ignore or not
Expand Down
36 changes: 20 additions & 16 deletions src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ def path_dwim(basedir: str, given: str) -> str:
return str(dataloader.path_dwim(given))


def ansible_templar(basedir: str, templatevars: Any) -> Templar:
def ansible_templar(basedir: Path, templatevars: Any) -> Templar:
"""Create an Ansible Templar using templatevars."""
# `basedir` is the directory containing the lintable file.
# Therefore, for tasks in a role, `basedir` has the form
# `roles/some_role/tasks`. On the other hand, the search path
# is `roles/some_role/{files,templates}`. As a result, the
# `tasks` part in the basedir should be stripped stripped.
if os.path.basename(basedir) == "tasks":
basedir = os.path.dirname(basedir)
if basedir.name == "tasks":
basedir = basedir.parent

dataloader = DataLoader()
dataloader.set_basedir(basedir)
Expand All @@ -129,7 +129,7 @@ def mock_filter(left: Any, *args: Any, **kwargs: Any) -> Any: # noqa: ARG001


def ansible_template(
basedir: str,
basedir: Path,
varname: Any,
templatevars: Any,
**kwargs: Any,
Expand Down Expand Up @@ -245,19 +245,19 @@ def _playbook_items(pb_data: AnsibleBaseYAMLObject) -> ItemsView: # type: ignor
return [item for play in pb_data if play for item in play.items()] # type: ignore[return-value]


def _set_collections_basedir(basedir: str) -> None:
def _set_collections_basedir(basedir: Path) -> None:
# Sets the playbook directory as playbook_paths for the collection loader
# Ansible expects only absolute paths inside `playbook_paths` and will
# produce weird errors if we use a relative one.
AnsibleCollectionConfig.playbook_paths = os.path.abspath(basedir)
AnsibleCollectionConfig.playbook_paths = str(basedir.resolve())


def find_children(lintable: Lintable) -> list[Lintable]: # noqa: C901
"""Traverse children of a single file or folder."""
if not lintable.path.exists():
return []
playbook_dir = str(lintable.path.parent)
_set_collections_basedir(playbook_dir or os.path.abspath("."))
_set_collections_basedir(lintable.path.parent)
add_all_plugin_dirs(playbook_dir or ".")
if lintable.kind == "role":
playbook_ds = AnsibleMapping({"roles": [{"role": str(lintable.path)}]})
Expand All @@ -269,13 +269,17 @@ def find_children(lintable: Lintable) -> list[Lintable]: # noqa: C901
except AnsibleError as exc:
raise SystemExit(exc) from exc
results = []
basedir = os.path.dirname(str(lintable.path))
# playbook_ds can be an AnsibleUnicode string, which we consider invalid
if isinstance(playbook_ds, str):
raise MatchError(lintable=lintable, rule=LoadingFailureRule())
for item in _playbook_items(playbook_ds):
# if lintable.kind not in ["playbook"]:
for child in play_children(basedir, item, lintable.kind, playbook_dir):
for child in play_children(
lintable.path.parent,
item,
lintable.kind,
playbook_dir,
):
# We avoid processing parametrized children
path_str = str(child.path)
if "$" in path_str or "{{" in path_str:
Expand All @@ -298,7 +302,7 @@ def find_children(lintable: Lintable) -> list[Lintable]: # noqa: C901


def template(
basedir: str,
basedir: Path,
value: Any,
variables: Any,
fail_on_error: bool = False,
Expand All @@ -308,7 +312,7 @@ def template(
"""Attempt rendering a value with known vars."""
try:
value = ansible_template(
os.path.abspath(basedir),
basedir.resolve(),
value,
variables,
**dict(kwargs, fail_on_undefined=fail_on_undefined),
Expand All @@ -323,7 +327,7 @@ def template(


def play_children(
basedir: str,
basedir: Path,
item: tuple[str, Any],
parent_type: FileType,
playbook_dir: str, # noqa: ARG001
Expand All @@ -348,16 +352,16 @@ def play_children(
"ansible.builtin.import_tasks": _include_children,
}
(k, v) = item
add_all_plugin_dirs(os.path.abspath(basedir))
add_all_plugin_dirs(str(basedir.resolve()))

if k in delegate_map and v:
v = template(
os.path.abspath(basedir),
basedir,
v,
{"playbook_dir": PLAYBOOK_DIR or os.path.abspath(basedir)},
{"playbook_dir": PLAYBOOK_DIR or str(basedir.resolve())},
fail_on_undefined=False,
)
return delegate_map[k](basedir, k, v, parent_type)
return delegate_map[k](str(basedir), k, v, parent_type)
return []


Expand Down
2 changes: 1 addition & 1 deletion test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_call_from_outside_venv(expected_warning: bool) -> None:
# environment variables from the current process, so we emulate being
# called from outside the venv.
proc = subprocess.run(
[py_path / "ansible-lint", "--version"],
[str(py_path / "ansible-lint"), "--version"],
check=False,
capture_output=True,
text=True,
Expand Down
2 changes: 1 addition & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def test_extract_from_list_recursive() -> None:
def test_template(template: str, output: str) -> None:
"""Verify that resolvable template vars and filters get rendered."""
result = utils.template(
basedir="/base/dir",
basedir=Path("/base/dir"),
value=template,
variables={"playbook_dir": "/a/b/c"},
fail_on_error=False,
Expand Down

0 comments on commit e0d4e7d

Please sign in to comment.