Fixed import utils code
Added exception paththrough in case module cannot be loaded. Change-Id: Ia429c25d550e11f95a6b7f62636e239eeb4022b8
This commit is contained in:
parent
9b2d8cc883
commit
4898aea328
@ -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)
|
||||
|
0
tests/fixtures/__init__.py
vendored
Normal file
0
tests/fixtures/__init__.py
vendored
Normal file
0
tests/fixtures/import/__init__.py
vendored
Normal file
0
tests/fixtures/import/__init__.py
vendored
Normal file
5
tests/fixtures/import/broken.py
vendored
Normal file
5
tests/fixtures/import/broken.py
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
'''This module is broken and cannot be imported.
|
||||
'''
|
||||
|
||||
|
||||
import missing.module.fromnowhere # noqa
|
0
tests/fixtures/import/package/__init__.py
vendored
Normal file
0
tests/fixtures/import/package/__init__.py
vendored
Normal file
6
tests/fixtures/import/package/a.py
vendored
Normal file
6
tests/fixtures/import/package/a.py
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
'''Usual module.
|
||||
'''
|
||||
|
||||
|
||||
class Bazz:
|
||||
pass
|
7
tests/fixtures/import/package/b.py
vendored
Normal file
7
tests/fixtures/import/package/b.py
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
'''Normal module that can be imported sucessfuly.
|
||||
'''
|
||||
|
||||
|
||||
class Bar:
|
||||
def __init__(self, foo):
|
||||
self._foo = foo
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user