Skip to content

Commit

Permalink
Switch cache to user cache folder
Browse files Browse the repository at this point in the history
- instead of in-repo .cache folder make use of ~/.cache
- use a hash based on project_dir path to avoid cross-contamination
  between various projects

Fixes: #1566
  • Loading branch information
ssbarnea committed May 21, 2021
1 parent 127d248 commit 85c02f4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
11 changes: 10 additions & 1 deletion src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"""Command line implementation."""

import errno
import hashlib
import logging
import os
import pathlib
Expand Down Expand Up @@ -107,6 +108,14 @@ def initialize_options(arguments: Optional[List[str]] = None) -> None:
options.warn_list = [normalize_tag(tag) for tag in options.warn_list]

options.configured = True
# 6 chars of entropy should be enough
cache_key = hashlib.sha256(
os.path.abspath(options.project_dir).encode()
).hexdigest()[:6]
options.cache_dir = "%s/ansible-lint/%s" % (
os.getenv("XDG_CACHE_HOME", os.path.expanduser("~/.cache")),
cache_key,
)


def report_outcome(
Expand Down Expand Up @@ -258,7 +267,7 @@ def main(argv: Optional[List[str]] = None) -> int:
@contextmanager
def _previous_revision() -> Iterator[None]:
"""Create or update a temporary workdir containing the previous revision."""
worktree_dir = ".cache/old-rev"
worktree_dir = f"{options.cache_dir}/old-rev"
revision = subprocess.run(
["git", "rev-parse", "HEAD^1"],
check=True,
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@


options = Namespace(
cache_dir=".cache",
colored=True,
configured=False,
cwd=".",
Expand Down
24 changes: 12 additions & 12 deletions src/ansiblelint/prerun.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def install_requirements(requirement: str) -> None:
"role",
"install",
"--roles-path",
f"{options.project_dir}/.cache/roles",
f"{options.cache_dir}/roles",
"-vr",
f"{requirement}",
]
Expand All @@ -136,7 +136,7 @@ def install_requirements(requirement: str) -> None:
"collection",
"install",
"-p",
f"{options.project_dir}/.cache/collections",
f"{options.cache_dir}/collections",
"-vr",
f"{requirement}",
]
Expand Down Expand Up @@ -245,7 +245,7 @@ def _install_galaxy_role() -> None:
fqrn = f"{role_namespace}{role_name}"
else:
fqrn = pathlib.Path(".").absolute().name
p = pathlib.Path(f"{options.project_dir}/.cache/roles")
p = pathlib.Path(f"{options.cache_dir}/roles")
p.mkdir(parents=True, exist_ok=True)
link_path = p / fqrn
# despite documentation stating that is_file() reports true for symlinks,
Expand All @@ -265,10 +265,10 @@ def _prepare_ansible_paths() -> None:

for path_list, path in (
(library_paths, "plugins/modules"),
(library_paths, f"{options.project_dir}/.cache/modules"),
(collection_list, f"{options.project_dir}/.cache/collections"),
(library_paths, f"{options.cache_dir}/modules"),
(collection_list, f"{options.cache_dir}/collections"),
(roles_path, "roles"),
(roles_path, f"{options.project_dir}/.cache/roles"),
(roles_path, f"{options.cache_dir}/roles"),
):
if path not in path_list and os.path.exists(path):
path_list.append(path)
Expand All @@ -283,14 +283,14 @@ def _make_module_stub(module_name: str) -> None:
if re.match(r"^(\w+|\w+\.\w+\.[\.\w]+)$", module_name):
parts = module_name.split(".")
if len(parts) < 3:
path = f"{options.project_dir}/.cache/modules"
module_file = f"{options.project_dir}/.cache/modules/{module_name}.py"
path = f"{options.cache_dir}/modules"
module_file = f"{options.cache_dir}/modules/{module_name}.py"
namespace = None
collection = None
else:
namespace = parts[0]
collection = parts[1]
path = f"{ options.project_dir }/.cache/collections/ansible_collections/{ namespace }/{ collection }/plugins/modules/{ '/'.join(parts[2:-1]) }"
path = f"{ options.cache_dir }/collections/ansible_collections/{ namespace }/{ collection }/plugins/modules/{ '/'.join(parts[2:-1]) }"
module_file = f"{path}/{parts[-1]}.py"
os.makedirs(path, exist_ok=True)
_write_module_stub(
Expand Down Expand Up @@ -336,9 +336,9 @@ def _perform_mockings() -> None:
for role_name in options.mock_roles:
if re.match(r"\w+\.\w+\.\w+$", role_name):
namespace, collection, role_dir = role_name.split(".")
path = f".cache/collections/ansible_collections/{ namespace }/{ collection }/roles/{ role_dir }/"
path = f"{options.cache_dir}/collections/ansible_collections/{ namespace }/{ collection }/roles/{ role_dir }/"
else:
path = f".cache/roles/{role_name}"
path = f"{options.cache_dir}/roles/{role_name}"
os.makedirs(path, exist_ok=True)

if options.mock_modules:
Expand All @@ -357,7 +357,7 @@ def _perform_mockings() -> None:
if not namespace or not collection:
return
p = pathlib.Path(
f"{options.project_dir}/.cache/collections/ansible_collections/{ namespace }"
f"{options.cache_dir}/collections/ansible_collections/{ namespace }"
)
p.mkdir(parents=True, exist_ok=True)
link_path = p / collection
Expand Down

0 comments on commit 85c02f4

Please sign in to comment.