From ff6a30cc43a8645d432b76cb6670fbf44e7ac798 Mon Sep 17 00:00:00 2001 From: Mark Horn Date: Fri, 8 Dec 2017 16:56:43 -0800 Subject: [PATCH] Enable Environment Variables in Configuration File This code change enables the use of Linux Environment variables in the configuration file. This is very useful for paths to avoid having to hard-code paths. Added error checking on the environment variables. For proper use, it will also require a similar patch to the mixer-tools project. Signed-off-by: Mark Horn --- bundle-chroot-builder.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/bundle-chroot-builder.py b/bundle-chroot-builder.py index 6158b76..83d7db1 100755 --- a/bundle-chroot-builder.py +++ b/bundle-chroot-builder.py @@ -25,6 +25,7 @@ import argparse import configparser import os import os.path +import io import platform import re import shutil @@ -54,9 +55,20 @@ def get_config(args): if args.config: buildconf = args.config - config = configparser.ConfigParser() print("Reading from %s" % buildconf) - config.read(buildconf) + cfg_txt = "" + # Check that the environment variables in the config file are valid + pattern = re.compile("\$\{?(\w+)\}?") + for i, line in enumerate(open(buildconf, 'r')): + for match in re.finditer(pattern, line): + if not match.group(1) in os.environ: + print("ERROR:\nbuilder.conf contains an undefined environment variable: %s on line %s\n" + % (i+1, match.group(1))) + exit(1) + cfg_txt += os.path.expandvars(line) + + config = configparser.ConfigParser() + config.readfp(io.StringIO(cfg_txt)) return config