From 5a5b7ff561ce097ede8fd8462cde63b9de2a8d00 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 15 Jul 2015 19:47:59 -0400 Subject: [PATCH] fixed first_available_found for template, refactored into common function added deprecation warning fixed display.deprecated to make version optional (code already assumed this) turned warning + 'deprecated' in plugin loader into actual call to deprecated() --- lib/ansible/plugins/__init__.py | 3 +-- lib/ansible/plugins/action/__init__.py | 24 ++++++++++++++++++++++++ lib/ansible/plugins/action/copy.py | 16 ++-------------- lib/ansible/plugins/action/template.py | 19 ++----------------- lib/ansible/utils/display.py | 2 +- 5 files changed, 30 insertions(+), 34 deletions(-) diff --git a/lib/ansible/plugins/__init__.py b/lib/ansible/plugins/__init__.py index d40a4f5f810..c71da6b7d66 100644 --- a/lib/ansible/plugins/__init__.py +++ b/lib/ansible/plugins/__init__.py @@ -250,8 +250,7 @@ class PluginLoader: if alias_name in self._plugin_path_cache: if not os.path.islink(self._plugin_path_cache[alias_name]): d = Display() - d.warning('%s has been deprecated, which means ' - 'it is kept for backwards compatibility ' + d.deprecated('%s is kept for backwards compatibility ' 'but usage is discouraged. The module ' 'documentation details page may explain ' 'more about this rationale.' % diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py index 49038b29c91..5ef52a44f01 100644 --- a/lib/ansible/plugins/action/__init__.py +++ b/lib/ansible/plugins/action/__init__.py @@ -448,3 +448,27 @@ class ActionBase: rc = 0 return dict(rc=rc, stdout=out, stderr=err) + + def _get_first_available_file(self, faf, of=None, searchdir='files'): + + self._connection._display.deprecated("first_available_file, use with_first_found or lookup('first_found',...) instead") + for fn in faf: + fn_orig = fn + fnt = self._templar.template(fn) + if self._task._role is not None: + lead = self._task._role._role_path + else: + lead = fnt + fnd = self._loader.path_dwim_relative(lead, searchdir, fnt) + + if not os.path.exists(fnd) and of is not None: + if self._task._role is not None: + lead = self._task._role._role_path + else: + lead = of + fnd = self._loader.path_dwim_relative(lead, searchdir, of) + + if os.path.exists(fnd): + return fnd + + return None diff --git a/lib/ansible/plugins/action/copy.py b/lib/ansible/plugins/action/copy.py index 7f11dfda2f3..b9798101504 100644 --- a/lib/ansible/plugins/action/copy.py +++ b/lib/ansible/plugins/action/copy.py @@ -74,20 +74,8 @@ class ActionModule(ActionBase): # if we have first_available_file in our vars # look up the files and use the first one we find as src elif faf: - #FIXME: issue deprecation warning for first_available_file, use with_first_found or lookup('first_found',...) instead - found = False - for fn in faf: - fn_orig = fn - fnt = self._templar.template(fn) - fnd = self._loader.path_dwim_relative(self._task._role._role_path, 'files', fnt) - of = task_vars.get('_original_file', None) - if not os.path.exists(fnd) and of is not None: - fnd = self._loader.path_dwim_relative(of, 'files', of) - if os.path.exists(fnd): - source = fnd - found = True - break - if not found: + source = self._get_first_available_file(faf, task_vars.get('_original_file', None)) + if source is None: return dict(failed=True, msg="could not find src in first_available_file list") else: if self._task._role is not None: diff --git a/lib/ansible/plugins/action/template.py b/lib/ansible/plugins/action/template.py index c13dc32b8a7..09523967504 100644 --- a/lib/ansible/plugins/action/template.py +++ b/lib/ansible/plugins/action/template.py @@ -64,23 +64,8 @@ class ActionModule(ActionBase): tmp = self._make_tmp_path() if faf: - #FIXME: issue deprecation warning for first_available_file, use with_first_found or lookup('first_found',...) instead - found = False - for fn in faf: - fn_orig = fn - fnt = self._templar.template(fn) - fnd = self._loader.path_dwim(self._task._role_._role_path, 'templates', fnt) - - if not os.path.exists(fnd): - of = task_vars.get('_original_file', None) - if of is not None: - fnd = self._loader.path_dwim(self._task._role_._role_path, 'templates', of) - - if os.path.exists(fnd): - source = fnd - found = True - break - if not found: + source = self._get_first_available_file(faf, task_vars.get('_original_file', None, 'templates')) + if source is None: return dict(failed=True, msg="could not find src in first_available_file list") else: if self._task._role is not None: diff --git a/lib/ansible/utils/display.py b/lib/ansible/utils/display.py index a9a4f8bb50a..ede2b29b805 100644 --- a/lib/ansible/utils/display.py +++ b/lib/ansible/utils/display.py @@ -111,7 +111,7 @@ class Display: else: self.display("<%s> %s" % (host, msg), color='blue', screen_only=True) - def deprecated(self, msg, version, removed=False): + def deprecated(self, msg, version=None, removed=False): ''' used to print out a deprecation message.''' if not removed and not C.DEPRECATION_WARNINGS: