diff options
author | Matt Davis <nitzmahone@users.noreply.github.com> | 2020-07-17 12:42:20 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-17 12:42:20 -0700 |
commit | 865ba1953be460d76332f6845b40ea1030c40938 (patch) | |
tree | abd4678a156fbf82da1149be6272f9ed73e70689 | |
parent | 212d2024f4d36c0ada0fdc75cda833729c62d387 (diff) | |
download | ansible-865ba1953be460d76332f6845b40ea1030c40938.tar.gz |
misc collection metadata fixes (#70403) (#70428)
* misc collection metadata fixes
* parse collection meta with libyaml if available
* require only Mapping for validation
* add explanatory text for _meta_yml_to_dict
* ignore custom pylint rule
* this code shouldn't import a bunch of stuff from ansible, since it's run under the import sanity test
(cherry picked from commit b9e38e8b55efdb6cf7debac2b9a495c20c73241c)
-rw-r--r-- | changelogs/fragments/collection_meta_use_libyaml.yml | 2 | ||||
-rw-r--r-- | lib/ansible/utils/collection_loader/_collection_meta.py | 26 |
2 files changed, 23 insertions, 5 deletions
diff --git a/changelogs/fragments/collection_meta_use_libyaml.yml b/changelogs/fragments/collection_meta_use_libyaml.yml new file mode 100644 index 0000000000..90db2bcc07 --- /dev/null +++ b/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 diff --git a/lib/ansible/utils/collection_loader/_collection_meta.py b/lib/ansible/utils/collection_loader/_collection_meta.py index 979a921cab..b306b810a2 100644 --- a/lib/ansible/utils/collection_loader/_collection_meta.py +++ b/lib/ansible/utils/collection_loader/_collection_meta.py @@ -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 |