From 4898aea328287fae159bb77d29c6f4f0ba974d4c Mon Sep 17 00:00:00 2001 From: Alexei Kornienko Date: Tue, 24 Sep 2013 12:54:25 +0300 Subject: [PATCH] Fixed import utils code Added exception paththrough in case module cannot be loaded. Change-Id: Ia429c25d550e11f95a6b7f62636e239eeb4022b8 --- rally/utils.py | 10 +++------- tests/fixtures/__init__.py | 0 tests/fixtures/import/__init__.py | 0 tests/fixtures/import/broken.py | 5 +++++ tests/fixtures/import/package/__init__.py | 0 tests/fixtures/import/package/a.py | 6 ++++++ tests/fixtures/import/package/b.py | 7 +++++++ tests/test_utils.py | 12 ++++++++++++ 8 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/__init__.py create mode 100644 tests/fixtures/import/__init__.py create mode 100644 tests/fixtures/import/broken.py create mode 100644 tests/fixtures/import/package/__init__.py create mode 100644 tests/fixtures/import/package/a.py create mode 100644 tests/fixtures/import/package/b.py diff --git a/rally/utils.py b/rally/utils.py index e4a6f8bbbf..9ff7eb0543 100644 --- a/rally/utils.py +++ b/rally/utils.py @@ -68,9 +68,7 @@ def itersubclasses(cls, _seen=None): def try_append_module(name, modules): if name not in modules: - module = importutils.try_import(name) - if module is not None: - modules[name] = module + modules[name] = importutils.import_module(name) def import_modules_from_package(package): @@ -81,9 +79,7 @@ def import_modules_from_package(package): path = [os.path.dirname(__file__), '..'] + package.split('.') path = os.path.join(*path) for filename in os.listdir(path): - module_name = '%s.%s' % (package, filename) - if filename.endswith('.py') and not filename.startswith('__'): - module_name = module_name[:-3] - elif os.path.isfile(os.path.join(path, filename)): + if filename.startswith('__') or not filename.endswith('.py'): continue + module_name = '%s.%s' % (package, filename[:-3]) try_append_module(module_name, sys.modules) diff --git a/tests/fixtures/__init__.py b/tests/fixtures/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fixtures/import/__init__.py b/tests/fixtures/import/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fixtures/import/broken.py b/tests/fixtures/import/broken.py new file mode 100644 index 0000000000..34a04c145c --- /dev/null +++ b/tests/fixtures/import/broken.py @@ -0,0 +1,5 @@ +'''This module is broken and cannot be imported. +''' + + +import missing.module.fromnowhere # noqa diff --git a/tests/fixtures/import/package/__init__.py b/tests/fixtures/import/package/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fixtures/import/package/a.py b/tests/fixtures/import/package/a.py new file mode 100644 index 0000000000..284079e499 --- /dev/null +++ b/tests/fixtures/import/package/a.py @@ -0,0 +1,6 @@ +'''Usual module. +''' + + +class Bazz: + pass diff --git a/tests/fixtures/import/package/b.py b/tests/fixtures/import/package/b.py new file mode 100644 index 0000000000..bc7a5eab93 --- /dev/null +++ b/tests/fixtures/import/package/b.py @@ -0,0 +1,7 @@ +'''Normal module that can be imported sucessfuly. +''' + + +class Bar: + def __init__(self, foo): + self._foo = foo diff --git a/tests/test_utils.py b/tests/test_utils.py index 5a629a2931..15a7369e4a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -71,3 +71,15 @@ class ImportModulesTestCase(test.NoDBTestCase): modules = {} utils.try_append_module('rally.version', modules) self.assertTrue('rally.version' in modules) + + def test_try_append_broken_module(self): + modules = {} + self.assertRaises(ImportError, + utils.try_append_module, + 'tests.fixtures.import.broken', + modules) + + def test_import_modules_from_package(self): + utils.import_modules_from_package('tests.fixtures.import.package') + self.assertTrue('tests.fixtures.import.package.a' in sys.modules) + self.assertTrue('tests.fixtures.import.package.b' in sys.modules)