diff options
Diffstat (limited to 'lib/ansible/galaxy/collection/concrete_artifact_manager.py')
-rw-r--r-- | lib/ansible/galaxy/collection/concrete_artifact_manager.py | 111 |
1 files changed, 51 insertions, 60 deletions
diff --git a/lib/ansible/galaxy/collection/concrete_artifact_manager.py b/lib/ansible/galaxy/collection/concrete_artifact_manager.py index 67d8e43f..d251127d 100644 --- a/lib/ansible/galaxy/collection/concrete_artifact_manager.py +++ b/lib/ansible/galaxy/collection/concrete_artifact_manager.py @@ -21,7 +21,7 @@ from tempfile import mkdtemp if t.TYPE_CHECKING: from ansible.galaxy.dependency_resolution.dataclasses import ( - Candidate, Requirement, + Candidate, Collection, Requirement, ) from ansible.galaxy.token import GalaxyToken @@ -30,13 +30,11 @@ from ansible.galaxy import get_collections_galaxy_meta_info from ansible.galaxy.api import should_retry_error from ansible.galaxy.dependency_resolution.dataclasses import _GALAXY_YAML from ansible.galaxy.user_agent import user_agent -from ansible.module_utils._text import to_bytes, to_native, to_text +from ansible.module_utils.common.text.converters import to_bytes, to_native, to_text from ansible.module_utils.api import retry_with_delays_and_condition from ansible.module_utils.api import generate_jittered_backoff from ansible.module_utils.common.process import get_bin_path -from ansible.module_utils.common._collections_compat import MutableMapping from ansible.module_utils.common.yaml import yaml_load -from ansible.module_utils.six import raise_from from ansible.module_utils.urls import open_url from ansible.utils.display import Display from ansible.utils.sentinel import Sentinel @@ -141,13 +139,10 @@ class ConcreteArtifactsManager: try: url, sha256_hash, token = self._galaxy_collection_cache[collection] except KeyError as key_err: - raise_from( - RuntimeError( - 'The is no known source for {coll!s}'. - format(coll=collection), - ), - key_err, - ) + raise RuntimeError( + 'There is no known source for {coll!s}'. + format(coll=collection), + ) from key_err display.vvvv( "Fetching a collection tarball for '{collection!s}' from " @@ -195,7 +190,7 @@ class ConcreteArtifactsManager: return b_artifact_path def get_artifact_path(self, collection): - # type: (t.Union[Candidate, Requirement]) -> bytes + # type: (Collection) -> bytes """Given a concrete collection pointer, return a cached path. If it's not yet on disk, this method downloads the artifact first. @@ -230,17 +225,14 @@ class ConcreteArtifactsManager: timeout=self.timeout ) except Exception as err: - raise_from( - AnsibleError( - 'Failed to download collection tar ' - "from '{coll_src!s}': {download_err!s}". - format( - coll_src=to_native(collection.src), - download_err=to_native(err), - ), + raise AnsibleError( + 'Failed to download collection tar ' + "from '{coll_src!s}': {download_err!s}". + format( + coll_src=to_native(collection.src), + download_err=to_native(err), ), - err, - ) + ) from err elif collection.is_scm: b_artifact_path = _extract_collection_from_git( collection.src, @@ -259,16 +251,22 @@ class ConcreteArtifactsManager: self._artifact_cache[collection.src] = b_artifact_path return b_artifact_path + def get_artifact_path_from_unknown(self, collection): + # type: (Candidate) -> bytes + if collection.is_concrete_artifact: + return self.get_artifact_path(collection) + return self.get_galaxy_artifact_path(collection) + def _get_direct_collection_namespace(self, collection): # type: (Candidate) -> t.Optional[str] return self.get_direct_collection_meta(collection)['namespace'] # type: ignore[return-value] def _get_direct_collection_name(self, collection): - # type: (Candidate) -> t.Optional[str] + # type: (Collection) -> t.Optional[str] return self.get_direct_collection_meta(collection)['name'] # type: ignore[return-value] def get_direct_collection_fqcn(self, collection): - # type: (Candidate) -> t.Optional[str] + # type: (Collection) -> t.Optional[str] """Extract FQCN from the given on-disk collection artifact. If the collection is virtual, ``None`` is returned instead @@ -284,7 +282,7 @@ class ConcreteArtifactsManager: )) def get_direct_collection_version(self, collection): - # type: (t.Union[Candidate, Requirement]) -> str + # type: (Collection) -> str """Extract version from the given on-disk collection artifact.""" return self.get_direct_collection_meta(collection)['version'] # type: ignore[return-value] @@ -297,7 +295,7 @@ class ConcreteArtifactsManager: return collection_dependencies # type: ignore[return-value] def get_direct_collection_meta(self, collection): - # type: (t.Union[Candidate, Requirement]) -> dict[str, t.Union[str, dict[str, str], list[str], None, t.Type[Sentinel]]] + # type: (Collection) -> dict[str, t.Union[str, dict[str, str], list[str], None, t.Type[Sentinel]]] """Extract meta from the given on-disk collection artifact.""" try: # FIXME: use unique collection identifier as a cache key? return self._artifact_meta_cache[collection.src] @@ -311,13 +309,10 @@ class ConcreteArtifactsManager: try: collection_meta = _get_meta_from_dir(b_artifact_path, self.require_build_metadata) except LookupError as lookup_err: - raise_from( - AnsibleError( - 'Failed to find the collection dir deps: {err!s}'. - format(err=to_native(lookup_err)), - ), - lookup_err, - ) + raise AnsibleError( + 'Failed to find the collection dir deps: {err!s}'. + format(err=to_native(lookup_err)), + ) from lookup_err elif collection.is_scm: collection_meta = { 'name': None, @@ -439,29 +434,23 @@ def _extract_collection_from_git(repo_url, coll_ver, b_path): try: subprocess.check_call(git_clone_cmd) except subprocess.CalledProcessError as proc_err: - raise_from( - AnsibleError( # should probably be LookupError - 'Failed to clone a Git repository from `{repo_url!s}`.'. - format(repo_url=to_native(git_url)), - ), - proc_err, - ) + raise AnsibleError( # should probably be LookupError + 'Failed to clone a Git repository from `{repo_url!s}`.'. + format(repo_url=to_native(git_url)), + ) from proc_err git_switch_cmd = git_executable, 'checkout', to_text(version) try: subprocess.check_call(git_switch_cmd, cwd=b_checkout_path) except subprocess.CalledProcessError as proc_err: - raise_from( - AnsibleError( # should probably be LookupError - 'Failed to switch a cloned Git repo `{repo_url!s}` ' - 'to the requested revision `{commitish!s}`.'. - format( - commitish=to_native(version), - repo_url=to_native(git_url), - ), + raise AnsibleError( # should probably be LookupError + 'Failed to switch a cloned Git repo `{repo_url!s}` ' + 'to the requested revision `{commitish!s}`.'. + format( + commitish=to_native(version), + repo_url=to_native(git_url), ), - proc_err, - ) + ) from proc_err return ( os.path.join(b_checkout_path, to_bytes(fragment)) @@ -637,17 +626,14 @@ def _get_meta_from_src_dir( try: manifest = yaml_load(manifest_file_obj) except yaml.error.YAMLError as yaml_err: - raise_from( - AnsibleError( - "Failed to parse the galaxy.yml at '{path!s}' with " - 'the following error:\n{err_txt!s}'. - format( - path=to_native(galaxy_yml), - err_txt=to_native(yaml_err), - ), + raise AnsibleError( + "Failed to parse the galaxy.yml at '{path!s}' with " + 'the following error:\n{err_txt!s}'. + format( + path=to_native(galaxy_yml), + err_txt=to_native(yaml_err), ), - yaml_err, - ) + ) from yaml_err if not isinstance(manifest, dict): if require_build_metadata: @@ -716,6 +702,11 @@ def _get_meta_from_installed_dir( def _get_meta_from_tar( b_path, # type: bytes ): # type: (...) -> dict[str, t.Union[str, list[str], dict[str, str], None, t.Type[Sentinel]]] + if not os.path.exists(b_path): + raise AnsibleError( + f"Unable to find collection artifact file at '{to_native(b_path)}'." + ) + if not tarfile.is_tarfile(b_path): raise AnsibleError( "Collection artifact at '{path!s}' is not a valid tar file.". |