summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernard Ogden <bernie.ogden@linaro.org>2015-11-13 16:25:09 +0000
committerBernard Ogden <bernie.ogden@linaro.org>2015-11-13 16:25:09 +0000
commit5732fee6ebbdeab200d29d64284ce5bff422ac48 (patch)
tree7214355cf488d9442498a7dce8569b07a096734a
parent3d23206c4afe0213d8f05b669a8213e7b26903cc (diff)
Permit arbitrary replacementsarbitrary
All placeholders in input are replaced with values of equivalently named environment variables. If environment variable is unset, replace with empty string. Note that this is a behaviour change: previously, we would replace with the string 'None' if the environment variable was unset. Change-Id: I87e88d64a0146e305db9d4d4146d6b9607c2a5ef
-rwxr-xr-xyaml-to-json.py42
1 files changed, 15 insertions, 27 deletions
diff --git a/yaml-to-json.py b/yaml-to-json.py
index 95a0c5c..3f60e2d 100755
--- a/yaml-to-json.py
+++ b/yaml-to-json.py
@@ -1,5 +1,6 @@
#!/usr/bin/python
+import itertools
import json
import os
import string
@@ -7,37 +8,24 @@ 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())
- lava_template = template.safe_substitute(
- BOOT_URL=os.environ.get('BOOT_URL'),
- BUILD_NUMBER=os.environ.get('BUILD_NUMBER'),
- BUILD_URL=os.environ.get('BUILD_URL'),
- BUNDLE_STREAM_NAME=os.environ.get('BUNDLE_STREAM_NAME'),
- DATA_URL=os.environ.get('DATA_URL'),
- DEVICE_TARGET=os.environ.get('DEVICE_TARGET'),
- DEVICE_TYPE=os.environ.get('DEVICE_TYPE'),
- DTB=os.environ.get('DTB'),
- DTB_URL=os.environ.get('DTB_URL'),
- GIT_BRANCH=os.environ.get('GIT_BRANCH'),
- GIT_COMMIT=os.environ.get('GIT_COMMIT'),
- GIT_URL=os.environ.get('GIT_URL'),
- HWPACK_BUILD_URL=os.environ.get('HWPACK_BUILD_URL'),
- IMAGE_URL=os.environ.get('IMAGE_URL'),
- KERNEL=os.environ.get('KERNEL'),
- KERNEL_URL=os.environ.get('KERNEL_URL'),
- LAVA_SERVER=os.environ.get('LAVA_SERVER'),
- ROOTFS_BUILD_NUMBER=os.environ.get('ROOTFS_BUILD_NUMBER'),
- ROOTFS_BUILD_URL=os.environ.get('ROOTFS_BUILD_URL'),
- STARTUP_NSH=os.environ.get('STARTUP_NSH'),
- SYSTEM_URL=os.environ.get('SYSTEM_URL'),
- defconfig=os.environ.get('defconfig'),
- hwpack_type=os.environ.get('hwpack_type'),
- kernel_config=os.environ.get('kernel_config'),
- rootfs_type=os.environ.get('rootfs_type'),
- )
+ placeholders = map(lambda match: match.group('named', 'braced'),
+ string.Template.pattern.finditer(template.template))
+ placeholders = filter(None, itertools.chain(*placeholders))
+
+ #Pretend that missing environment variables are set to empty string.
+ #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=(',', ': '))