Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove share directory #1467

Merged
merged 6 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ repos:
- id: pylint
args:
- docs/
- share/
- src/
- tests/
additional_dependencies:
Expand Down
9 changes: 9 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"console": "integratedTerminal"
},
// Configuration for pure debugging (use args attribute accordingly)
{
"name": "Debug subcommand: collections",
"type": "python",
"request": "launch",
"module": "ansible_navigator",
"args": ["collections"],
"cwd": "${workspaceFolder}/src",
"justMyCode": false
},
{
"name": "Debug subcommand: exec",
"type": "python",
Expand Down
35 changes: 0 additions & 35 deletions setup.py

This file was deleted.

5 changes: 0 additions & 5 deletions share/ansible_navigator/utils/__init__.py

This file was deleted.

29 changes: 20 additions & 9 deletions src/ansible_navigator/actions/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def notify_none(self):
msgs = ["humph. no collections were found in the following paths:"]
paths = []
for path in self._collection_scanned_paths:
if path.startswith(self._args.internals.share_directory):
if path.startswith(self._args.internals.cache_path):
continue
if self._args.execution_environment:
if path.startswith(self._adjacent_collection_dir):
Expand Down Expand Up @@ -363,6 +363,7 @@ def _build_collection_content(self):
def _run_runner(self) -> None:
# pylint: disable=too-many-branches
# pylint: disable=too-many-statements
# pylint: disable=too-many-locals
"""Use the runner subsystem to catalog collections."""
if isinstance(self._args.set_environment_variable, dict):
set_environment_variable = deepcopy(self._args.set_environment_variable)
Expand Down Expand Up @@ -417,10 +418,10 @@ def _run_runner(self) -> None:
kwargs["host_cwd"] = playbook_dir

self._adjacent_collection_dir = os.path.join(playbook_dir, "collections")
share_directory = self._args.internals.share_directory
cache_path = self._args.internals.cache_path

pass_through_arg = [
f"{share_directory}/utils/catalog_collections.py",
f"{cache_path}/catalog_collections.py",
"-a",
self._adjacent_collection_dir,
"-c",
Expand All @@ -439,8 +440,8 @@ def _run_runner(self) -> None:
)

container_volume_mounts = [
# share utils directory which has introspection script
f"{share_directory}/utils:{share_directory}/utils",
# cache directory which has introspection script
f"{cache_path}:{cache_path}",
# KVS library used by both Navigator and the introspection script
f"{utils_lib}:/opt/ansible_navigator_utils",
]
Expand All @@ -449,13 +450,23 @@ def _run_runner(self) -> None:
f"{self._adjacent_collection_dir}:{self._adjacent_collection_dir}:z",
)

mount_doc_cache = True
# Determine if the doc_cache is relative to the cache directory
if path_is_relative_to(
child=Path(self._args.collection_doc_cache_path), parent=(cache_path)
):
mount_doc_cache = False

# The playbook directory will be mounted as host_cwd, so don't duplicate
if not path_is_relative_to(
child=Path(self._collection_cache_path),
parent=(Path(playbook_dir)),
if path_is_relative_to(
child=Path(self._args.collection_doc_cache_path), parent=(Path(playbook_dir))
):
mount_doc_cache = False

if mount_doc_cache:
container_volume_mounts.append(
f"{self._collection_cache_path}:{self._collection_cache_path}:z",
f"{self._args.collection_doc_cache_path}:"
f"{self._args.collection_doc_cache_path}:z",
)

for volume_mount in container_volume_mounts:
Expand Down
6 changes: 3 additions & 3 deletions src/ansible_navigator/actions/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,12 +549,12 @@ def _run_runner(self, image_name: str) -> tuple[str, str, int]:
:param image_name: The full image name
:returns: Output, errors and the return code
"""
share_directory = self._args.internals.share_directory
container_volume_mounts = [f"{share_directory}/utils:{share_directory}/utils"]
cache_path = self._args.internals.cache_path
container_volume_mounts = [f"{cache_path}:{cache_path}"]
python_exec_path = "python3"

kwargs = {
"cmdline": [f"{share_directory}/utils/image_introspect.py"],
"cmdline": [f"{cache_path}/image_introspect.py"],
"container_engine": self._args.container_engine,
"container_volume_mounts": container_volume_mounts,
"execution_environment_image": image_name,
Expand Down
24 changes: 24 additions & 0 deletions src/ansible_navigator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Navigator entry point."""
from __future__ import annotations

import filecmp
import logging
import os
import signal
Expand All @@ -12,6 +13,7 @@
from importlib.metadata import version
from importlib.util import find_spec
from pathlib import Path
from shutil import copyfile

from .action_defs import ActionReturn
from .action_defs import RunInteractiveReturn
Expand All @@ -31,6 +33,8 @@
from .utils.definitions import ExitPrefix
from .utils.definitions import LogMessage
from .utils.functions import clear_screen
from .utils.functions import generate_cache_path
from .utils.packaged_data import path_to_file


__version__: Constants | str
Expand All @@ -46,6 +50,25 @@
logger = logging.getLogger(PKG_NAME)


def cache_scripts() -> None:
"""Cache the scripts used to introspect the container image."""
scripts = ("catalog_collections.py", "image_introspect.py")
for script in scripts:
src = path_to_file(filename=script)
cache_path = generate_cache_path(app_name=APP_NAME)
cache_path.mkdir(parents=True, exist_ok=True)
dst = cache_path / script
message = f"No update required for {src} to {dst}"
try:
if not filecmp.cmp(src, dst):
copyfile(src, dst)
message = f"Updated {src} to {dst} (outdated)"
except FileNotFoundError:
copyfile(src, dst)
message = f"Copied {src} to {dst} (missing)"
logger.log(level=logging.DEBUG, msg=message)


def log_dependencies() -> list[LogMessage]:
"""Retrieve installed packages and log as debug.

Expand Down Expand Up @@ -167,6 +190,7 @@ def main():

if args.execution_environment:
pull_image(args)
cache_scripts()

run_return = run(args)
run_message = f"{run_return.message}\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

from dataclasses import dataclass
from dataclasses import field
from pathlib import Path

from ..utils.definitions import ExitMessage
from ..utils.definitions import LogMessage
from ..utils.functions import abs_user_path
from ..utils.functions import get_share_directory
from ..utils.functions import generate_cache_path
from ..utils.functions import oxfordcomma
from ..utils.key_value_store import KeyValueStore
from ..utils.packaged_data import ImageEntry
Expand Down Expand Up @@ -65,28 +66,17 @@ def generate_editor_command() -> str:
return command


def generate_cache_path():
def _generate_doc_cache_path() -> str:
"""Generate a path for the collection cache.

:returns: Collection cache path
"""
file_name = "collection_doc_cache.db"
cache_home = os.environ.get("XDG_CACHE_HOME", f"{os.path.expanduser('~')}/.cache")
cache_path = os.path.join(cache_home, APP_NAME.replace("_", "-"), file_name)
cache_dir = generate_cache_path(APP_NAME.replace("_", "-"))
cache_path = cache_dir / file_name
message = f"Default collection_doc_cache_path set to: {cache_path}"
initialization_messages.append(LogMessage(level=logging.DEBUG, message=message))
return cache_path


def generate_share_directory():
"""Generate a share director.

:returns: Share directory path
"""
messages, exit_messages, share_directory = get_share_directory(APP_NAME)
initialization_messages.extend(messages)
initialization_exit_messages.extend(exit_messages)
return share_directory
return str(cache_path)


@dataclass
Expand All @@ -95,14 +85,14 @@ class Internals:

ansible_configuration: AnsibleConfiguration = field(default_factory=AnsibleConfiguration)
action_packages: tuple[str] = ("ansible_navigator.actions",)
cache_path: Path = generate_cache_path(APP_NAME.replace("_", "-"))
collection_doc_cache: C | KeyValueStore = C.NOT_SET
initializing: bool = False
"""This is an initial run (app starting for the first time)."""
initialization_exit_messages = initialization_exit_messages
initialization_messages = initialization_messages
settings_file_path: str | None = None
settings_source: C = C.NOT_SET
share_directory: str = generate_share_directory()


navigator_subcommands = [
Expand Down Expand Up @@ -276,7 +266,7 @@ class Internals:
cli_parameters=CliParameters(short="--cdcp"),
short_description="The path to collection doc cache",
value=SettingsEntryValue(
default=generate_cache_path(),
default=_generate_doc_cache_path(),
schema_default="~/.cache/ansible-navigator/collection_doc_cache.db",
),
version_added="v1.0",
Expand Down
3 changes: 2 additions & 1 deletion src/ansible_navigator/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,11 @@ def _basics(self) -> dict[str, JSONTypes]:
"application_name": self._args.application_name,
"application_version": str(self._args.application_version),
"action_packages": list(self._args.internals.action_packages),
"cache_path": str(self._args.internals.cache_path),
"collection_doc_cache": str(self._args.internals.collection_doc_cache),
"original_command": shlex_join(sys.argv),
"settings_file_path": str(self._args.internals.settings_file_path),
"settings_source": str(self._args.internals.settings_source),
"share_directory": self._args.internals.share_directory,
}

@diagnostic_runner
Expand Down
Loading