diff options
author | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2018-08-31 13:25:51 +0300 |
---|---|---|
committer | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2018-09-06 14:44:12 +0300 |
commit | ff075f6ee795a590b244d70a90cc312ba1f2d83d (patch) | |
tree | a617790bdbfdeef960665ba0242e1f0c93e5301a /meta-phosphor/recipes-phosphor/settings/phosphor-settings-manager/merge_settings.py | |
parent | 3e4da38c127bb7e7641adc2fc41f4c33744cb918 (diff) | |
download | openbmc-ff075f6ee795a590b244d70a90cc312ba1f2d83d.tar.xz |
meta-phosphor: Move layer content from common/
Adopt a more conventional directory hierarchy. meta-phosphor is still
a _long_ way from suitable for hosting on yoctoproject.org but things
like this don't help.
(From meta-phosphor rev: 471cfcefa74b8c7ceb704cb670e6d915cf27c63b)
Change-Id: I3f106b2f6cdc6cec734be28a6090800546f362eb
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
Diffstat (limited to 'meta-phosphor/recipes-phosphor/settings/phosphor-settings-manager/merge_settings.py')
-rwxr-xr-x | meta-phosphor/recipes-phosphor/settings/phosphor-settings-manager/merge_settings.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/meta-phosphor/recipes-phosphor/settings/phosphor-settings-manager/merge_settings.py b/meta-phosphor/recipes-phosphor/settings/phosphor-settings-manager/merge_settings.py new file mode 100755 index 000000000..01f5e3552 --- /dev/null +++ b/meta-phosphor/recipes-phosphor/settings/phosphor-settings-manager/merge_settings.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +"""Loads a "target" YAML file and overwrites its values with values from +"override" YAML files. + +Override files are processed in the order given. + +Usage: + merge_settings.py <target yaml> [override yamls] +""" +import sys +import yaml +import copy + +def dict_merge(target, source): + """Deep merge for dicts. + + Works like dict.update() that recursively updates any dict values present in + both parameters. + + Args: + target (dict): Values to be overwritten by corresponding values from + `source`. + source (dict): Overriding values. Not changed by call. + + Returns: + `target` with values overwritten from those in `source` at any and all + levels of nested dicts. + """ + if not isinstance(source, dict): + return source + for k, v in source.iteritems(): + if k in target and isinstance(target[k], dict): + dict_merge(target[k], v) + else: + target[k] = copy.deepcopy(v) + return target + +if len(sys.argv) < 2: + sys.exit('Argument required: target yaml') + +if len(sys.argv) == 2: + # No overrides to handle + sys.exit() + +target_filename = sys.argv[1] +with open(target_filename) as target_file: + data = yaml.safe_load(target_file) + print('Loaded target YAML file ' + target_filename) + +for override_filename in sys.argv[2:]: + with open(override_filename) as override_file: + override = yaml.safe_load(override_file) + dict_merge(data, override) + print('Merged override YAML file ' + override_filename) + +with open(target_filename, 'w') as target_file: + yaml.dump(data, target_file) + print('Wrote merged target YAML file ' + target_filename) |