Remove/adjust the verbose 'failed at attempted import of' log

Instead of using this log (which really isn't a failure) we should
instead of just return the looked up locations and then if there really
is an error the caller can handle the usage of the looked up locations
as they choose fit.
This commit is contained in:
Joshua Harlow 2014-09-02 13:31:18 -07:00
parent b2d20dd5de
commit 34a50a1b82
5 changed files with 29 additions and 34 deletions

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,
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,7 +272,7 @@ 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,
m_locs, _looked_locs = importer.find_module(ds_name,
pkg_list,
['get_datasource_list'])
for m_loc in m_locs:

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])