From 00a22059fe66574167d496b65825380d477a1e9d Mon Sep 17 00:00:00 2001 From: Bernard Ogden Date: Fri, 13 Nov 2015 16:25:09 +0000 Subject: yaml-to-json: permit arbitrary replacements 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 Signed-off-by: Bernie Ogden --- yaml-to-json.py | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/yaml-to-json.py b/yaml-to-json.py index 95a0c5c..bd3e0ac 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,23 @@ 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 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=(',', ': ')) -- cgit v1.2.3