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
20 from oslo_utils import importutils
25 # Decorator for cli-args
26 def cliargs(*args, **kwargs):
28 func.__dict__.setdefault('arguments', []).insert(0, (args, kwargs))
33 def itersubclasses(cls, _seen=None):
34 """Generator over all subclasses of a given class in depth first order."""
36 if not isinstance(cls, type):
37 raise TypeError("itersubclasses must be called with "
38 "new-style classes, not %.100r" % cls)
39 _seen = _seen or set()
41 subs = cls.__subclasses__()
42 except TypeError: # fails only when cls is type
43 subs = cls.__subclasses__(cls)
48 for sub in itersubclasses(sub, _seen):
52 def try_append_module(name, modules):
53 if name not in modules:
54 modules[name] = importutils.import_module(name)
57 def import_modules_from_package(package):
58 """Import modules from package and append into sys.modules
60 :param: package - Full package name. For example: rally.deploy.engines
62 path = [os.path.dirname(yardstick.__file__), ".."] + package.split(".")
63 path = os.path.join(*path)
64 for root, dirs, files in os.walk(path):
65 for filename in files:
66 if filename.startswith("__") or not filename.endswith(".py"):
68 new_package = ".".join(root.split(os.sep)).split("....")[1]
69 module_name = "%s.%s" % (new_package, filename[:-3])
70 try_append_module(module_name, sys.modules)