diff options
author | Ross Brattain <ross.b.brattain@intel.com> | 2017-06-21 19:25:22 -0700 |
---|---|---|
committer | Ross Brattain <ross.b.brattain@intel.com> | 2017-08-08 21:24:27 -0700 |
commit | 97004997c00dac2e0dcfeef303af4701e78bb909 (patch) | |
tree | ad6b962b8ec15db6296ea8c636e5f23f2b1342a1 /yardstick/common/constants.py | |
parent | 5ce3b6f8c8b3217091e51a6041455738603d90b8 (diff) |
constants: cache YAML config values
don't reparse the yardstick.yaml file each
time we lookup an option.
Since it is global data, just cache it in a global
var
Use pkg_resources.resource_filename
to lookup the path of yardstick tests, intsead of using __file__
pkg_resources is slightly more proper than __file__
at least for packages
Change-Id: I05d9748390a37bd45c53013fc084d23069ab7c51
Signed-off-by: Ross Brattain <ross.b.brattain@intel.com>
Diffstat (limited to 'yardstick/common/constants.py')
-rw-r--r-- | yardstick/common/constants.py | 26 |
1 files changed, 24 insertions, 2 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') |