diff options
author | Ross Brattain <ross.b.brattain@intel.com> | 2018-03-01 15:11:03 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2018-03-01 15:11:03 +0000 |
commit | 9a71438273cefd57c652b6049c697bdf137088c8 (patch) | |
tree | 7ef9fe6f5e357eee8b77cd5c08df64bea915937c /yardstick/common/utils.py | |
parent | 14a0f0b13e37cf0a65603915b7c75b86fd4a4383 (diff) | |
parent | d08a8d477fd7b9fb88855b12ee53eafa07e79afa (diff) |
Merge "Import "traffic_profile" modules only once"
Diffstat (limited to 'yardstick/common/utils.py')
-rw-r--r-- | yardstick/common/utils.py | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py index a77a4cad6..3f2d546fc 100644 --- a/yardstick/common/utils.py +++ b/yardstick/common/utils.py @@ -66,7 +66,7 @@ def itersubclasses(cls, _seen=None): yield sub -def import_modules_from_package(package): +def import_modules_from_package(package, raise_exception=False): """Import modules given a package name :param: package - Full package name. For example: rally.deploy.engines @@ -87,7 +87,9 @@ def import_modules_from_package(package): for module_name in missing_modules: try: importlib.import_module(module_name) - except (ImportError, SyntaxError): + except (ImportError, SyntaxError) as exc: + if raise_exception: + raise exc logger.exception('Unable to import module %s', module_name) @@ -410,3 +412,31 @@ def read_meminfo(ssh_client): output[match[0]] = match[1] return output + + +def find_relative_file(path, task_path): + """ + Find file in one of places: in abs of path or relative to a directory path, + in this order. + + :param path: + :param task_path: + :return str: full path to file + """ + # fixme: create schema to validate all fields have been provided + for lookup in [os.path.abspath(path), os.path.join(task_path, path)]: + try: + with open(lookup): + return lookup + except IOError: + pass + raise IOError(errno.ENOENT, 'Unable to find {} file'.format(path)) + + +def open_relative_file(path, task_path): + try: + return open(path) + except IOError as e: + if e.errno == errno.ENOENT: + return open(os.path.join(task_path, path)) + raise |