diff options
author | Hans Feldt <hans.feldt@ericsson.com> | 2015-05-19 10:32:34 +0200 |
---|---|---|
committer | Hans Feldt <hans.feldt@ericsson.com> | 2015-05-19 10:35:43 +0200 |
commit | bc5339353682d737e5d5d948642028fab3bd186a (patch) | |
tree | 45d24bf997248906305b182619e8a79067c800a2 | |
parent | fca312912a5b0f3f9815db22affbd2b5244fc7c4 (diff) |
add package common with module utils
The utils module supports finding subclasses of a class. This
is used to find Runner and Scenarios types. utils.py originates from
rally but is stripped down and slightly modified (some dependencies
removed).
Change-Id: Ibc196698e1212899b0836988d0345696b00c5caf
JIRA: -
Signed-off-by: Hans Feldt <hans.feldt@ericsson.com>
-rw-r--r-- | yardstick/common/__init__.py | 0 | ||||
-rw-r--r-- | yardstick/common/utils.py | 62 |
2 files changed, 62 insertions, 0 deletions
diff --git a/yardstick/common/__init__.py b/yardstick/common/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/yardstick/common/__init__.py diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py new file mode 100644 index 000000000..4de0fcfd5 --- /dev/null +++ b/yardstick/common/utils.py @@ -0,0 +1,62 @@ +# Copyright 2013: Mirantis Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +# yardstick comment: this is a modified copy of rally/rally/common/utils.py + +import os +import sys +from oslo_utils import importutils + +import yardstick + + +def itersubclasses(cls, _seen=None): + """Generator over all subclasses of a given class in depth first order.""" + + if not isinstance(cls, type): + raise TypeError("itersubclasses must be called with " + "new-style classes, not %.100r" % cls) + _seen = _seen or set() + try: + subs = cls.__subclasses__() + except TypeError: # fails only when cls is type + subs = cls.__subclasses__(cls) + for sub in subs: + if sub not in _seen: + _seen.add(sub) + yield sub + for sub in itersubclasses(sub, _seen): + yield sub + + +def try_append_module(name, modules): + if name not in modules: + modules[name] = importutils.import_module(name) + + +def import_modules_from_package(package): + """Import modules from package and append into sys.modules + + :param: package - Full package name. For example: rally.deploy.engines + """ + path = [os.path.dirname(yardstick.__file__), ".."] + package.split(".") + path = os.path.join(*path) + for root, dirs, files in os.walk(path): + for filename in files: + if filename.startswith("__") or not filename.endswith(".py"): + 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) |