improved logging of errors around module loading/searching

This commit is contained in:
Scott Moser 2014-09-03 14:46:20 -04:00
commit 4fa812c447
6 changed files with 30 additions and 34 deletions

View File

@ -30,6 +30,7 @@
- FreeBsd: fix initscripts and add working config file [Harm Weites]
- Datasource: fix broken logic to provide hostname if datasource does not
provide one
- Improved and less verbose logging.
0.7.5:
- open 0.7.5
- Add a debug log message around import failures

View File

@ -847,12 +847,10 @@ def extract_default(users, default_name=None, default_config=None):
def fetch(name):
locs = importer.find_module(name,
['', __name__],
['Distro'])
locs, looked_locs = importer.find_module(name, ['', __name__], ['Distro'])
if not locs:
raise ImportError("No distribution found for distro %s"
% (name))
raise ImportError("No distribution found for distro %s (searched %s)"
% (name, looked_locs))
mod = importer.import_module(locs[0])
cls = getattr(mod, 'Distro')
return cls

View File

@ -22,10 +22,6 @@
import sys
from cloudinit import log as logging
LOG = logging.getLogger(__name__)
def import_module(module_name):
__import__(module_name)
@ -33,25 +29,24 @@ def import_module(module_name):
def find_module(base_name, search_paths, required_attrs=None):
found_places = []
if not required_attrs:
required_attrs = []
# NOTE(harlowja): translate the search paths to include the base name.
real_paths = []
lookup_paths = []
for path in search_paths:
real_path = []
if path:
real_path.extend(path.split("."))
real_path.append(base_name)
full_path = '.'.join(real_path)
real_paths.append(full_path)
for full_path in real_paths:
lookup_paths.append(full_path)
found_paths = []
for full_path in lookup_paths:
mod = None
try:
mod = import_module(full_path)
except ImportError as e:
LOG.debug("Failed at attempted import of '%s' due to: %s",
full_path, e)
except ImportError:
pass
if not mod:
continue
found_attrs = 0
@ -59,5 +54,5 @@ def find_module(base_name, search_paths, required_attrs=None):
if hasattr(mod, attr):
found_attrs += 1
if found_attrs == len(required_attrs):
found_places.append(full_path)
return found_places
found_paths.append(full_path)
return (found_paths, lookup_paths)

View File

@ -143,12 +143,14 @@ def construct(parsed_mergers):
for (m_name, m_ops) in parsed_mergers:
if not m_name.startswith(MERGER_PREFIX):
m_name = MERGER_PREFIX + str(m_name)
merger_locs = importer.find_module(m_name,
[__name__],
[MERGER_ATTR])
merger_locs, looked_locs = importer.find_module(m_name,
[__name__],
[MERGER_ATTR])
if not merger_locs:
msg = ("Could not find merger module named '%s' "
"with attribute '%s'") % (m_name, MERGER_ATTR)
"with attribute '%s' (searched %s)") % (m_name,
MERGER_ATTR,
looked_locs)
raise ImportError(msg)
else:
mod = importer.import_module(merger_locs[0])

View File

@ -272,9 +272,9 @@ def list_sources(cfg_list, depends, pkg_list):
for ds_name in cfg_list:
if not ds_name.startswith(DS_PREFIX):
ds_name = '%s%s' % (DS_PREFIX, ds_name)
m_locs = importer.find_module(ds_name,
pkg_list,
['get_datasource_list'])
m_locs, _looked_locs = importer.find_module(ds_name,
pkg_list,
['get_datasource_list'])
for m_loc in m_locs:
mod = importer.import_module(m_loc)
lister = getattr(mod, "get_datasource_list")

View File

@ -386,12 +386,12 @@ class Init(object):
potential_handlers = util.find_modules(path)
for (fname, mod_name) in potential_handlers.iteritems():
try:
mod_locs = importer.find_module(mod_name, [''],
['list_types',
'handle_part'])
mod_locs, looked_locs = importer.find_module(
mod_name, [''], ['list_types', 'handle_part'])
if not mod_locs:
LOG.warn(("Could not find a valid user-data handler"
" named %s in file %s"), mod_name, fname)
LOG.warn("Could not find a valid user-data handler"
" named %s in file %s (searched %s)",
mod_name, fname, looked_locs)
continue
mod = importer.import_module(mod_locs[0])
mod = handlers.fixup_handler(mod)
@ -621,11 +621,11 @@ class Modules(object):
" has an unknown frequency %s"), raw_name, freq)
# Reset it so when ran it will get set to a known value
freq = None
mod_locs = importer.find_module(mod_name,
['', type_utils.obj_name(config)],
['handle'])
mod_locs, looked_locs = importer.find_module(
mod_name, ['', type_utils.obj_name(config)], ['handle'])
if not mod_locs:
LOG.warn("Could not find module named %s", mod_name)
LOG.warn("Could not find module named %s (searched %s)",
mod_name, looked_locs)
continue
mod = config.fixup_module(importer.import_module(mod_locs[0]))
mostly_mods.append([mod, raw_name, freq, run_args])