Support dates in metadata in import sanity test.

No changelog entry since this feature and the associated bug have not been included in a release.
pull/69921/head
Matt Clay 5 years ago
parent 4882471bc9
commit f733517e6e

@ -11,6 +11,7 @@ def main():
""" """
import ansible import ansible
import contextlib import contextlib
import datetime
import json import json
import os import os
import re import re
@ -42,13 +43,31 @@ def main():
if collection_full_name: if collection_full_name:
# allow importing code from collections when testing a collection # allow importing code from collections when testing a collection
from ansible.module_utils.common.text.converters import to_bytes, to_text, to_native from ansible.module_utils.common.text.converters import to_bytes, to_text, to_native, text_type
from ansible.utils.collection_loader._collection_finder import _AnsibleCollectionFinder from ansible.utils.collection_loader._collection_finder import _AnsibleCollectionFinder
from ansible.utils.collection_loader import _collection_finder from ansible.utils.collection_loader import _collection_finder
yaml_to_json_path = os.path.join(os.path.dirname(__file__), 'yaml_to_json.py') yaml_to_json_path = os.path.join(os.path.dirname(__file__), 'yaml_to_json.py')
yaml_to_dict_cache = {} yaml_to_dict_cache = {}
# unique ISO date marker matching the one present in yaml_to_json.py
iso_date_marker = 'isodate:f23983df-f3df-453c-9904-bcd08af468cc:'
iso_date_re = re.compile('^%s([0-9]{4})-([0-9]{2})-([0-9]{2})$' % iso_date_marker)
def parse_value(value):
"""Custom value parser for JSON deserialization that recognizes our internal ISO date format."""
if isinstance(value, text_type):
match = iso_date_re.search(value)
if match:
value = datetime.date(int(match.group(1)), int(match.group(2)), int(match.group(3)))
return value
def object_hook(data):
"""Object hook for custom ISO date deserialization from JSON."""
return dict((key, parse_value(value)) for key, value in data.items())
def yaml_to_dict(yaml, content_id): def yaml_to_dict(yaml, content_id):
""" """
Return a Python dict version of the provided YAML. Return a Python dict version of the provided YAML.
@ -65,7 +84,7 @@ def main():
if proc.returncode != 0: if proc.returncode != 0:
raise Exception('command %s failed with return code %d: %s' % ([to_native(c) for c in cmd], proc.returncode, to_native(stderr_bytes))) raise Exception('command %s failed with return code %d: %s' % ([to_native(c) for c in cmd], proc.returncode, to_native(stderr_bytes)))
data = yaml_to_dict_cache[content_id] = json.loads(to_text(stdout_bytes)) data = yaml_to_dict_cache[content_id] = json.loads(to_text(stdout_bytes), object_hook=object_hook)
return data return data
except Exception as ex: except Exception as ex:

@ -2,6 +2,7 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import datetime
import json import json
import sys import sys
@ -12,5 +13,15 @@ try:
except ImportError: except ImportError:
from yaml import SafeLoader from yaml import SafeLoader
# unique ISO date marker matching the one present in importer.py
ISO_DATE_MARKER = 'isodate:f23983df-f3df-453c-9904-bcd08af468cc:'
json.dump(load(sys.stdin, Loader=SafeLoader), sys.stdout)
def default(value):
if isinstance(value, datetime.date):
return '%s%s' % (ISO_DATE_MARKER, value.isoformat())
raise TypeError('cannot serialize type: %s' % type(value))
json.dump(load(sys.stdin, Loader=SafeLoader), sys.stdout, default=default)

Loading…
Cancel
Save