summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilosz Wasilewski <milosz.wasilewski@linaro.org>2017-01-06 14:34:50 +0000
committerMilosz Wasilewski <milosz.wasilewski@linaro.org>2017-01-06 14:36:43 +0000
commit175a5268a52ccea0851201bd256325e022e020fe (patch)
tree072dd12954b75c42c51f70eeea937959202a943e
parent52c10d39e763d08ff920b96aee032045aa058096 (diff)
downloadlci-build-tools-175a5268a52ccea0851201bd256325e022e020fe.tar.gz
Refactored yaml-to-json script
Variable substitution is also needed for YAML files. This part was separated to a library. For backward compatibility the call of yaml-to-json remains the same. New script prepare-template.py was added to cover the used case of variable substitution in any file Change-Id: I2cd28c851d14e13b07c3fb4f966e2587abf70448 Signed-off-by: Milosz Wasilewski <milosz.wasilewski@linaro.org>
-rwxr-xr-xprepare-template.py15
-rwxr-xr-xyaml-to-json.py15
-rw-r--r--yamljsonlib/__init__.py17
3 files changed, 34 insertions, 13 deletions
diff --git a/prepare-template.py b/prepare-template.py
new file mode 100755
index 0000000..aa27d38
--- /dev/null
+++ b/prepare-template.py
@@ -0,0 +1,15 @@
+#!/usr/bin/python
+
+import sys
+from yamljsonlib import template_replace
+
+
+def main():
+ with open(sys.argv[1]) as f:
+ template = string.Template(f.read())
+ lava_template = template_replace(template)
+ print lava_template
+
+
+if __name__ == '__main__':
+ main()
diff --git a/yaml-to-json.py b/yaml-to-json.py
index 4da2eae..c087d81 100755
--- a/yaml-to-json.py
+++ b/yaml-to-json.py
@@ -2,29 +2,18 @@
import itertools
import json
-import os
import string
import sys
import yaml
+from yamljsonlib import template_replace
# 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)
+ lava_template = template_replace(template)
# FIXME: use ordered dictionaries - see http://pyyaml.org/ticket/29
config = json.dumps(yaml.safe_load(lava_template), indent=2, separators=(',', ': '))
diff --git a/yamljsonlib/__init__.py b/yamljsonlib/__init__.py
new file mode 100644
index 0000000..4b12525
--- /dev/null
+++ b/yamljsonlib/__init__.py
@@ -0,0 +1,17 @@
+import os
+import string
+
+
+def template_replace(template):
+ 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)
+ return lava_template