create_env_files.py 1.08 KB
Newer Older
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
import configparser
import logging
from pathlib import Path

logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s : %(levelname)-8s %(message)s')
logger = logging.getLogger(__name__)

# Open config file detailing the paths / URLs to the channels for different
# machines.
config_file_name = 'locations.ini'
config = configparser.ConfigParser()
config.read(config_file_name)

# For each section (indicating the location or kind of network environment),
# read all env templates, expand the channel placeholders, and write them to
# the respective yml-files.
for section_name in config.sections():
    for input_file_name in Path('_env_templates').glob('*.yml'):
        env_dir = Path(section_name)
        env_dir.mkdir(exist_ok=True)
        output_file_name = env_dir / input_file_name.name
        with input_file_name.open() as input_file, \
                output_file_name.open(mode='w') as output_file:
            logger.info('Creating {}'.format(output_file.name))
            template = input_file.read()
            output_file.write(template.format(**config[section_name]))