1 # Copyright 2013: Mirantis Inc.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
16 # yardstick comment: this is a modified copy of rally/rally/common/utils.py
18 from __future__ import absolute_import
19 from __future__ import print_function
26 from functools import reduce
29 from six.moves import configparser
30 from oslo_utils import importutils
31 from oslo_serialization import jsonutils
35 logger = logging.getLogger(__name__)
36 logger.setLevel(logging.DEBUG)
39 # Decorator for cli-args
40 def cliargs(*args, **kwargs):
42 func.__dict__.setdefault('arguments', []).insert(0, (args, kwargs))
47 def itersubclasses(cls, _seen=None):
48 """Generator over all subclasses of a given class in depth first order."""
50 if not isinstance(cls, type):
51 raise TypeError("itersubclasses must be called with "
52 "new-style classes, not %.100r" % cls)
53 _seen = _seen or set()
55 subs = cls.__subclasses__()
56 except TypeError: # fails only when cls is type
57 subs = cls.__subclasses__(cls)
62 for sub in itersubclasses(sub, _seen):
66 def try_append_module(name, modules):
67 if name not in modules:
68 modules[name] = importutils.import_module(name)
71 def import_modules_from_package(package):
72 """Import modules from package and append into sys.modules
74 :param: package - Full package name. For example: rally.deploy.engines
76 path = [os.path.dirname(yardstick.__file__), ".."] + package.split(".")
77 path = os.path.join(*path)
78 for root, dirs, files in os.walk(path):
79 for filename in files:
80 if filename.startswith("__") or not filename.endswith(".py"):
82 new_package = ".".join(root.split(os.sep)).split("....")[1]
83 module_name = "%s.%s" % (new_package, filename[:-3])
84 try_append_module(module_name, sys.modules)
87 def get_para_from_yaml(file_path, args):
94 if os.path.exists(file_path):
95 with open(file_path) as f:
96 value = yaml.safe_load(f)
97 value = reduce(func, args.split('.'), value)
100 print('parameter not found')
105 print('file not exist')
113 if e.errno != errno.EEXIST:
117 def execute_command(cmd):
118 exec_msg = "Executing command: '%s'" % cmd
119 logger.debug(exec_msg)
121 output = subprocess.check_output(cmd.split()).split(os.linesep)
126 def source_env(env_file):
127 p = subprocess.Popen(". %s; env" % env_file, stdout=subprocess.PIPE,
129 output = p.communicate()[0]
130 env = dict((line.split('=', 1) for line in output.splitlines()))
131 os.environ.update(env)
135 def read_json_from_file(path):
136 with open(path, 'r') as f:
138 # don't use jsonutils.load() it conflicts with already decoded input
139 return jsonutils.loads(j)
142 def write_json_to_file(path, data, mode='w'):
143 with open(path, mode) as f:
144 jsonutils.dump(data, f)
147 def write_file(path, data, mode='w'):
148 with open(path, mode) as f:
152 def parse_ini_file(path):
153 parser = configparser.ConfigParser()
157 default = {k: v for k, v in parser.items('DEFAULT')}
158 except configparser.NoSectionError:
161 config = dict(DEFAULT=default,
162 **{s: {k: v for k, v in parser.items(
163 s)} for s in parser.sections()})