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

[2.10 backport] misc collection metadata fixes #70428

Merged
merged 1 commit into from Jul 17, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/collection_meta_use_libyaml.yml
@@ -0,0 +1,2 @@
bugfixes:
- collection metadata - ensure collection loader uses libyaml/CSafeLoader to parse collection metadata if available
26 changes: 21 additions & 5 deletions lib/ansible/utils/collection_loader/_collection_meta.py
Expand Up @@ -4,14 +4,30 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from yaml import safe_load
from yaml import load
try:
from yaml import CSafeLoader as SafeLoader
except ImportError:
from yaml import SafeLoader

try:
from collections.abc import Mapping # pylint: disable=ansible-bad-import-from
except ImportError:
from collections import Mapping # pylint: disable=ansible-bad-import-from


def _meta_yml_to_dict(yaml_string_data, content_id):
routing_dict = safe_load(yaml_string_data)
"""
Converts string YAML dictionary to a Python dictionary. This function may be monkeypatched to another implementation
by some tools (eg the import sanity test).
:param yaml_string_data: a bytes-ish YAML dictionary
:param content_id: a unique ID representing the content to allow other implementations to cache the output
:return: a Python dictionary representing the YAML dictionary content
"""
# NB: content_id is passed in, but not used by this implementation
routing_dict = load(yaml_string_data, Loader=SafeLoader)
if not routing_dict:
routing_dict = {}
# TODO: change this to Mapping abc?
if not isinstance(routing_dict, dict):
raise ValueError('collection metadata must be a dictionary')
if not isinstance(routing_dict, Mapping):
raise ValueError('collection metadata must be an instance of Python Mapping')
return routing_dict