Skip to content

Commit

Permalink
Change install_collection to avoid use of -p (#233)
Browse files Browse the repository at this point in the history
As passing -p (target collection path destination) break ansible-galaxy
ability to find existing collection in other location, we avoid it
and use the environment variable approach instead.

Related: ansible/ansible-lint#3251
  • Loading branch information
ssbarnea authored Apr 13, 2023
1 parent 18ddf94 commit 02fc200
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,19 @@ def install_collection(
if matches and Version(matches[1]).is_prerelease:
cmd.append("--pre")

if destination:
cmd.extend(["-p", str(destination)])
cpaths: List[str] = self.config.collections_paths
if destination and str(destination) not in cpaths:
# we cannot use '-p' because it breaks galaxy ability to ignore already installed collections, so
# we hack ansible_collections_path instead and inject our own path there.
# pylint: disable=no-member
cpaths.insert(0, str(destination))
cmd.append(f"{collection}")

_logger.info("Running from %s : %s", os.getcwd(), " ".join(cmd))
run = self.exec(
cmd,
retry=True,
env={**self.environ, ansible_collections_path(): ":".join(cpaths)},
)
if run.returncode != 0:
msg = f"Command returned {run.returncode} code:\n{run.stdout}\n{run.stderr}"
Expand Down Expand Up @@ -351,15 +356,14 @@ def install_requirements(
)
else:
cmd.extend(["-r", requirement])
cpaths = ansible_collections_path().split(":")
cpaths = self.config.collections_paths
if self.cache_dir:
# we cannot use '-p' because it breaks galaxy ability to ignore already installed collections, so
# we hack ansible_collections_path instead and inject our own path there.
dest_path = f"{self.cache_dir}/collections"
cpaths = ansible_collections_path().split(":")
if dest_path not in cpaths:
# pylint: disable=no-member
cpaths.insert(0, dest_path)
# cmd.extend(["-p", f"{self.cache_dir}/collections"])
_logger.info("Running %s", " ".join(cmd))
result = self.exec(
cmd,
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ commands =
# pytest users to run coverage when they just want to run a single test with `pytest -k test`
coverage run -m pytest {posargs:}
sh -c "coverage xml || true && coverage report"
# We fail if files are modified at the end
git diff --exit-code
commands_pre =
# safety measure to assure we do not accidentally run tests with broken dependencies
{envpython} -m pip check
Expand All @@ -63,6 +65,7 @@ setenv =
FORCE_COLOR = 1
allowlist_externals =
ansible
git
sh

[testenv:lint]
Expand Down

0 comments on commit 02fc200

Please sign in to comment.