X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=yardstick%2Fcommon%2Futils.py;h=d2be8000ebf7333d637ebe166a22318d1e003e40;hb=8fe8ab2cc3b0814291cec3579aff267e042f4d6b;hp=3ecb0ae2018b553d925ddd519ed287c5aea2c8d7;hpb=beeeec75ea196f14659d9836d26dc0654df5fd4d;p=yardstick.git diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py index 3ecb0ae20..d2be8000e 100644 --- a/yardstick/common/utils.py +++ b/yardstick/common/utils.py @@ -15,17 +15,20 @@ # yardstick comment: this is a modified copy of rally/rally/common/utils.py -import os -import sys -import yaml +from __future__ import absolute_import +from __future__ import print_function + import errno -import subprocess import logging +import os +import subprocess +import sys +from functools import reduce +import yaml +from six.moves import configparser from oslo_utils import importutils -from keystoneauth1 import identity -from keystoneauth1 import session -from neutronclient.v2_0 import client +from oslo_serialization import jsonutils import yardstick @@ -94,12 +97,12 @@ def get_para_from_yaml(file_path, args): value = reduce(func, args.split('.'), value) if value is None: - print 'parameter not found' + print('parameter not found') return None return value else: - print 'file not exist' + print('file not exist') return None @@ -129,15 +132,34 @@ def source_env(env_file): return env -def get_openstack_session(): - auth = identity.Password(auth_url=os.environ.get('OS_AUTH_URL'), - username=os.environ.get('OS_USERNAME'), - password=os.environ.get('OS_PASSWORD'), - tenant_name=os.environ.get('OS_TENANT_NAME')) - return session.Session(auth=auth) +def read_json_from_file(path): + with open(path, 'r') as f: + j = f.read() + # don't use jsonutils.load() it conflicts with already decoded input + return jsonutils.loads(j) + + +def write_json_to_file(path, data, mode='w'): + with open(path, mode) as f: + jsonutils.dump(data, f) + + +def write_file(path, data, mode='w'): + with open(path, mode) as f: + f.write(data) + + +def parse_ini_file(path): + parser = configparser.ConfigParser() + parser.read(path) + + try: + default = {k: v for k, v in parser.items('DEFAULT')} + except configparser.NoSectionError: + default = {} + config = dict(DEFAULT=default, + **{s: {k: v for k, v in parser.items( + s)} for s in parser.sections()}) -def get_neutron_client(): - sess = get_openstack_session() - neutron_client = client.Client(session=sess) - return neutron_client + return config