Skip to content

Commit

Permalink
Make cache directory configurable
Browse files Browse the repository at this point in the history
Fixes: #1566
  • Loading branch information
ssbarnea committed May 21, 2021
1 parent 127d248 commit f27dffe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/ansiblelint/__main__.py
Expand Up @@ -258,7 +258,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
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
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 f27dffe

Please sign in to comment.