X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=yardstick%2Fcommon%2Futils.py;h=92bb7b7d35c2e87af61f1020af831020df2f87b7;hb=c1a4f5028f88e7e97e3ae2cc3f8aae6bf67c07b2;hp=3c5895f1e1c9cea089fc35367f974ad7febeb1cd;hpb=501175fbb095a771f5f1b9fb80dcf729192214d2;p=yardstick.git diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py index 3c5895f1e..92bb7b7d3 100644 --- a/yardstick/common/utils.py +++ b/yardstick/common/utils.py @@ -23,9 +23,16 @@ import logging import os import subprocess import sys +import collections +import socket +import random from functools import reduce +from contextlib import closing import yaml +import six +from flask import jsonify +from six.moves import configparser from oslo_utils import importutils from oslo_serialization import jsonutils @@ -80,29 +87,36 @@ def import_modules_from_package(package): continue new_package = ".".join(root.split(os.sep)).split("....")[1] module_name = "%s.%s" % (new_package, filename[:-3]) - try_append_module(module_name, sys.modules) + try: + try_append_module(module_name, sys.modules) + except ImportError: + logger.exception("unable to import %s", module_name) -def get_para_from_yaml(file_path, args): - - def func(a, b): - if a is None: - return None - return a.get(b) - - if os.path.exists(file_path): +def parse_yaml(file_path): + try: with open(file_path) as f: value = yaml.safe_load(f) - value = reduce(func, args.split('.'), value) + except IOError: + return {} + except OSError as e: + if e.errno != errno.EEXIST: + raise + else: + return value - if value is None: - print('parameter not found') - return None - return value - else: - print('file not exist') - return None +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): @@ -113,6 +127,14 @@ def makedirs(d): raise +def remove_file(path): + try: + os.remove(path) + except OSError as e: + if e.errno != errno.ENOENT: + raise + + def execute_command(cmd): exec_msg = "Executing command: '%s'" % cmd logger.debug(exec_msg) @@ -133,7 +155,9 @@ def source_env(env_file): def read_json_from_file(path): with open(path, 'r') as f: - return jsonutils.load(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'): @@ -144,3 +168,109 @@ def write_json_to_file(path, data, mode='w'): 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()}) + + return config + + +def get_port_mac(sshclient, port): + cmd = "ifconfig |grep HWaddr |grep %s |awk '{print $5}' " % port + status, stdout, stderr = sshclient.execute(cmd) + + if status: + raise RuntimeError(stderr) + return stdout.rstrip() + + +def get_port_ip(sshclient, port): + cmd = "ifconfig %s |grep 'inet addr' |awk '{print $2}' " \ + "|cut -d ':' -f2 " % port + status, stdout, stderr = sshclient.execute(cmd) + + if status: + raise RuntimeError(stderr) + return stdout.rstrip() + + +def flatten_dict_key(data): + next_data = {} + + # use list, because iterable is too generic + if not any(isinstance(v, (collections.Mapping, list)) + for v in data.values()): + return data + + for k, v in six.iteritems(data): + if isinstance(v, collections.Mapping): + for n_k, n_v in six.iteritems(v): + next_data["%s.%s" % (k, n_k)] = n_v + # use list because iterable is too generic + elif isinstance(v, list): + for index, item in enumerate(v): + next_data["%s%d" % (k, index)] = item + else: + next_data[k] = v + + return flatten_dict_key(next_data) + + +def translate_to_str(obj): + if isinstance(obj, collections.Mapping): + return {str(k): translate_to_str(v) for k, v in obj.items()} + elif isinstance(obj, list): + return [translate_to_str(ele) for ele in obj] + elif isinstance(obj, six.text_type): + return str(obj) + return obj + + +def result_handler(status, data): + result = { + 'status': status, + 'result': data + } + return jsonify(result) + + +def change_obj_to_dict(obj): + dic = {} + for k, v in vars(obj).items(): + try: + vars(v) + except TypeError: + dic.update({k: v}) + return dic + + +def set_dict_value(dic, keys, value): + return_dic = dic + + for key in keys.split('.'): + + return_dic.setdefault(key, {}) + if key == keys.split('.')[-1]: + return_dic[key] = value + else: + return_dic = return_dic[key] + return dic + + +def get_free_port(ip): + with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: + while True: + port = random.randint(5000, 10000) + if s.connect_ex((ip, port)) != 0: + return port