-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml-to-json.py
executable file
·36 lines (28 loc) · 1.27 KB
/
yaml-to-json.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
#!/usr/bin/python
# (C) 2016 Fathi Boudra <[email protected]>
import itertools
import json
import os
import string
import sys
import yaml
# Find all placeholders in the input and replace them with the values of
# equivalently-named environment variables.
def main():
with open(sys.argv[1]) as f:
template = string.Template(f.read())
placeholders = map(lambda match: match.group('named', 'braced'),
string.Template.pattern.finditer(template.template))
placeholders = set(filter(None, itertools.chain(*placeholders)))
# Pretend that missing environment variables are set to empty strings.
# It would be better to throw an error on missing environment variables,
# but this doesn't play nicely with Jenkins. When Jenkins adds parameters to
# a triggered job via a properties file, any parameters explicitly set to
# the empty string will be unset in the environment of the downstream job.
substitutions = {x: os.environ.get(x) or '' for x in placeholders}
lava_template = template.safe_substitute(substitutions)
# FIXME: use ordered dictionaries - see http://pyyaml.org/ticket/29
config = json.dumps(yaml.safe_load(lava_template), indent=2, separators=(',', ': '))
print config
if __name__ == '__main__':
main()