Merge "Change PTL informatin in INFO"
[bottlenecks.git] / utils / dispatcher / func.py
1 # Copyright 2013: Mirantis Inc.
2 # All Rights Reserved.
3 #
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
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
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
14 #    under the License.
15
16 # bottlenecks comment: this is a modified copy of rally/rally/common/utils.py
17
18 import os
19 import sys
20 from oslo_utils import importutils
21
22 import utils
23
24
25 # Decorator for cli-args
26 def cliargs(*args, **kwargs):
27     def _decorator(func):
28         func.__dict__.setdefault('arguments', []).insert(0, (args, kwargs))
29         return func
30     return _decorator
31
32
33 def itersubclasses(cls, _seen=None):
34     """Generator over all subclasses of a given class in depth first order."""
35
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()
40     try:
41         subs = cls.__subclasses__()
42     except TypeError:   # fails only when cls is type
43         subs = cls.__subclasses__(cls)
44     for sub in subs:
45         if sub not in _seen:
46             _seen.add(sub)
47             yield sub
48             for sub in itersubclasses(sub, _seen):
49                 yield sub
50
51
52 def try_append_module(name, modules):
53     if name not in modules:
54         modules[name] = importutils.import_module(name)
55
56
57 def import_modules_from_package(package):
58     """Import modules from package and append into sys.modules
59
60     :param: package - Full package name. For example: rally.deploy.engines
61     """
62     path = [os.path.dirname(utils.__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"):
67                 continue
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)