From 3ba6b89bf3fdfcadf95565215a3d3380c112fcf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Billeter?= Date: Tue, 4 Jul 2017 08:54:41 +0200 Subject: _yaml.py: Dump sanitized mappings as unordered mappings This eliminates the incorrect !!omap tags for unordered mappings. --- buildstream/_yaml.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/buildstream/_yaml.py b/buildstream/_yaml.py index 537f4cacd..93fae4903 100644 --- a/buildstream/_yaml.py +++ b/buildstream/_yaml.py @@ -23,6 +23,7 @@ import copy from enum import Enum from ruamel import yaml +from ruamel.yaml.representer import SafeRepresenter, RoundTripRepresenter from . import ImplError, LoadError, LoadErrorReason @@ -538,6 +539,17 @@ def composite(target, source, policy=CompositePolicy.OVERWRITE, typesafe=False): e.actual_type.__name__)) from e +# SanitizedDict is an OrderedDict that is dumped as unordered mapping. +# This provides deterministic output for unordered mappings. +# +class SanitizedDict(collections.OrderedDict): + pass + + +RoundTripRepresenter.add_representer(SanitizedDict, + SafeRepresenter.represent_dict) + + # node_sanitize() # # Returnes an alphabetically ordered recursive copy @@ -549,7 +561,7 @@ def node_sanitize(node): if isinstance(node, collections.Mapping): - result = collections.OrderedDict() + result = SanitizedDict() for key in sorted(node): if key == PROVENANCE_KEY: -- cgit v1.2.1