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): def fetch(name):
locs = importer.find_module(name, locs, looked_locs = importer.find_module(name, ['', __name__], ['Distro'])
['', __name__],
['Distro'])
if not locs: if not locs:
raise ImportError("No distribution found for distro %s" raise ImportError("No distribution found for distro %s (searched %s)"
% (name)) % (name, looked_locs))
mod = importer.import_module(locs[0]) mod = importer.import_module(locs[0])
cls = getattr(mod, 'Distro') cls = getattr(mod, 'Distro')
return cls return cls

View File

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

View File

@ -143,12 +143,14 @@ def construct(parsed_mergers):
for (m_name, m_ops) in parsed_mergers: for (m_name, m_ops) in parsed_mergers:
if not m_name.startswith(MERGER_PREFIX): if not m_name.startswith(MERGER_PREFIX):
m_name = MERGER_PREFIX + str(m_name) m_name = MERGER_PREFIX + str(m_name)
merger_locs = importer.find_module(m_name, merger_locs, looked_locs = importer.find_module(m_name,
[__name__], [__name__],
[MERGER_ATTR]) [MERGER_ATTR])
if not merger_locs: if not merger_locs:
msg = ("Could not find merger module named '%s' " 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) raise ImportError(msg)
else: else:
mod = importer.import_module(merger_locs[0]) 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: for ds_name in cfg_list:
if not ds_name.startswith(DS_PREFIX): if not ds_name.startswith(DS_PREFIX):
ds_name = '%s%s' % (DS_PREFIX, ds_name) 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, pkg_list,
['get_datasource_list']) ['get_datasource_list'])
for m_loc in m_locs: for m_loc in m_locs:
mod = importer.import_module(m_loc) mod = importer.import_module(m_loc)
lister = getattr(mod, "get_datasource_list") lister = getattr(mod, "get_datasource_list")

View File

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