generated from dabble-of-devops-bioanalyze/terraform-example-module
-
Notifications
You must be signed in to change notification settings - Fork 3
/
merge_yamls.py
executable file
·72 lines (57 loc) · 2.26 KB
/
merge_yamls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
import os
import glob
import hiyapyco
import argparse
from pprint import pprint
import sys
import yaml
try:
from collections import OrderedDict
except ImportError:
# try importing the backported replacement
# requires a: `pip-2.6 install ordereddict`
from ordereddict import OrderedDict
import yaml.loader
import yaml.dumper
import yaml.representer
from hiyapyco.odyldo import ODYD
import six
import datetime
import types
class ODYD(yaml.SafeDumper):
"""Ordered Dict Yaml Dumper"""
def __init__(self, *args, **kwargs):
yaml.SafeDumper.__init__(self, *args, **kwargs)
yaml.representer.SafeRepresenter.add_representer(str, type(self).repr_str)
yaml.representer.SafeRepresenter.add_representer(OrderedDict, type(self)._odyrepr)
def rstrip_multilines(self, data):
out = []
for line in data.splitlines():
out.append(line.rstrip())
return '\n'.join(out)
def _odyrepr(self, data):
"""see: yaml.representer.represent_mapping"""
return self.represent_mapping('tag:yaml.org,2002:map', data.items())
def repr_str(self, data):
if '\n' in data:
return self.represent_scalar(u'tag:yaml.org,2002:str', self.rstrip_multilines(data), style='|')
elif len(data) > 80:
return self.represent_scalar(u'tag:yaml.org,2002:str', data, style='|')
else:
return self.represent_scalar(u'tag:yaml.org,2002:str', data, style=None)
def safe_dump(data, stream=None, **kwds):
"""implementation of safe dumper using Ordered Dict Yaml Dumper"""
return yaml.dump(data, stream=stream, Dumper=ODYD, **kwds)
def merge_yamls(files, output):
conf = hiyapyco.load(*files, method=hiyapyco.METHOD_MERGE, interpolate=True, failonmissingfiles=True)
# print(safe_dump(conf))
with open(output, 'w') as file:
safe_dump(conf, file)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Merge some yamls')
parser.add_argument('--yaml-files', metavar='N', type=str, nargs='+',
help='YAML Files to merge', required=True)
parser.add_argument('--output', help='Path to Destination file', required=True)
args = parser.parse_args()
merge_yamls(args.yaml_files, args.output)