diff options
author | Ross Brattain <ross.b.brattain@intel.com> | 2017-08-10 03:56:50 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2017-08-10 03:56:50 +0000 |
commit | 2ad96094b99e007f5ef46aced3b69a9ced558da3 (patch) | |
tree | adccfc44324e9feb05e15aaf03a2972277dba13c /yardstick | |
parent | daa256f05a1f42f66a84ad96321bc609aeb1f75b (diff) | |
parent | 97004997c00dac2e0dcfeef303af4701e78bb909 (diff) |
Merge "constants: cache YAML config values"
Diffstat (limited to 'yardstick')
-rw-r--r-- | yardstick/common/constants.py | 26 | ||||
-rw-r--r-- | yardstick/common/utils.py | 14 |
2 files changed, 24 insertions, 16 deletions
diff --git a/yardstick/common/constants.py b/yardstick/common/constants.py index 8e8114fbb..822d3b4fa 100644 --- a/yardstick/common/constants.py +++ b/yardstick/common/constants.py @@ -8,15 +8,36 @@ ############################################################################## from __future__ import absolute_import import os +from functools import reduce -from yardstick.common.utils import get_param +import pkg_resources +from yardstick.common.utils import parse_yaml dirname = os.path.dirname abspath = os.path.abspath join = os.path.join sep = os.path.sep +CONF = {} + + +def get_param(key, default=''): + + # we have to defer this to runtime so that we can mock os.environ.get in unittests + conf_file = os.environ.get('CONF_FILE', '/etc/yardstick/yardstick.yaml') + + # don't re-parse yaml for each lookup + if not CONF: + CONF.update(parse_yaml(conf_file)) + try: + return reduce(lambda a, b: a[b], key.split('.'), CONF) + except KeyError: + if not default: + raise + return default + + try: SERVER_IP = get_param('api.server_ip') except KeyError: @@ -41,7 +62,8 @@ CONF_DIR = get_param('dir.conf', '/etc/yardstick') REPOS_DIR = get_param('dir.repos', '/home/opnfv/repos/yardstick') RELENG_DIR = get_param('dir.releng', '/home/opnfv/repos/releng') LOG_DIR = get_param('dir.log', '/tmp/yardstick/') -YARDSTICK_ROOT_PATH = dirname(dirname(dirname(abspath(__file__)))) + sep +YARDSTICK_ROOT_PATH = dirname( + dirname(abspath(pkg_resources.resource_filename(__name__, "")))) + sep CONF_SAMPLE_DIR = join(REPOS_DIR, 'etc/yardstick/') ANSIBLE_DIR = join(REPOS_DIR, 'ansible') SAMPLE_CASE_DIR = join(REPOS_DIR, 'samples') diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py index 969bb4b38..729bc1db9 100644 --- a/yardstick/common/utils.py +++ b/yardstick/common/utils.py @@ -27,7 +27,6 @@ import collections import socket import random import ipaddress -from functools import reduce from contextlib import closing import yaml @@ -107,19 +106,6 @@ def parse_yaml(file_path): return value -def get_param(key, default=''): - - conf_file = os.environ.get('CONF_FILE', '/etc/yardstick/yardstick.yaml') - - conf = parse_yaml(conf_file) - try: - return reduce(lambda a, b: a[b], key.split('.'), conf) - except KeyError: - if not default: - raise - return default - - def makedirs(d): try: os.makedirs(d) |