From daae8c77c277967a5c04bb1e0b537c8470b7cdcd Mon Sep 17 00:00:00 2001 From: Joe Guo Date: Wed, 29 Jan 2020 17:41:25 +1300 Subject: [PATCH 1/3] zaza/utilities/deployment_env.py: simplify is_valid_env_key str.startswith can accept a tuple, no need to use for loop. Signed-off-by: Joe Guo --- zaza/utilities/deployment_env.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/zaza/utilities/deployment_env.py b/zaza/utilities/deployment_env.py index b5347fa2a..5a0ab3dba 100644 --- a/zaza/utilities/deployment_env.py +++ b/zaza/utilities/deployment_env.py @@ -32,14 +32,14 @@ MODEL_SETTINGS_SECTION = 'model_settings' MODEL_CONSTRAINTS_SECTION = 'model_constraints' -VALID_ENVIRONMENT_KEY_PREFIXES = [ +VALID_ENVIRONMENT_KEY_PREFIXES = ( 'OS_', 'TEST_', 'MOJO_', 'JUJU_', 'CHARM_', 'MODEL_', -] +) MODEL_DEFAULTS = { # Model defaults from charm-test-infra @@ -116,12 +116,7 @@ def is_valid_env_key(key): :returns: Whether key is a valid environment variable name :rtype: bool """ - valid = False - for _k in VALID_ENVIRONMENT_KEY_PREFIXES: - if key.startswith(_k): - valid = True - break - return valid + return key.startswith(VALID_ENVIRONMENT_KEY_PREFIXES) def find_setup_file(): From 1df4ac74632fa5c044fd6490952b54d2bf8563b9 Mon Sep 17 00:00:00 2001 From: Joe Guo Date: Wed, 29 Jan 2020 17:55:09 +1300 Subject: [PATCH 2/3] zaza/utilities/deployment_env.py: simplify find_setup_file simplify the logic of setup file path Signed-off-by: Joe Guo --- zaza/utilities/deployment_env.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/zaza/utilities/deployment_env.py b/zaza/utilities/deployment_env.py index 5a0ab3dba..35b826a29 100644 --- a/zaza/utilities/deployment_env.py +++ b/zaza/utilities/deployment_env.py @@ -18,9 +18,11 @@ import os import functools import yaml +from os.path import expandvars ZAZA_SETUP_FILE_LOCATIONS = [ - '{home}/.zaza.yaml'] + expandvars('$HOME/.zaza.yaml'), +] SECRETS = 'secrets' RUNTIME_CONFIG = 'runtime_config' @@ -125,11 +127,9 @@ def find_setup_file(): :returns: Location of zaza config file or None if not found. :rtype: str or None """ - ctxt = { - 'home': os.environ.get('HOME')} for setup_file in ZAZA_SETUP_FILE_LOCATIONS: - if os.path.isfile(setup_file.format(**ctxt)): - return setup_file.format(**ctxt) + if os.path.isfile(setup_file): + return setup_file def get_setup_file_contents(): From 07dc850f7e5b845fdade58eb3ae8ad5270b7ed21 Mon Sep 17 00:00:00 2001 From: Joe Guo Date: Wed, 29 Jan 2020 21:13:25 +1300 Subject: [PATCH 3/3] zaza/utilities/deployment_env.py: simplify get_setup_file_contents can return safe_load result directly, no need to update the result to another temp dict. Signed-off-by: Joe Guo --- zaza/utilities/deployment_env.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/zaza/utilities/deployment_env.py b/zaza/utilities/deployment_env.py index 35b826a29..b5697ea98 100644 --- a/zaza/utilities/deployment_env.py +++ b/zaza/utilities/deployment_env.py @@ -140,16 +140,13 @@ def get_setup_file_contents(): :rtype: dict """ setup_file = find_setup_file() - setup_file_data = {} if setup_file: with open(setup_file, 'r') as stream: try: - _file_data = yaml.safe_load(stream) - if _file_data: - setup_file_data.update(_file_data) + return yaml.safe_load(stream) or {} except yaml.YAMLError: logging.warn("Unable to load data from {}".format(setup_file)) - return setup_file_data + return {} def get_setup_file_section(section_name):