From 19a2fc00dccabf85c656561124ea0503645ff5de Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sun, 15 Jul 2012 08:46:58 -0400 Subject: [PATCH] Clean up imports in utils, add nicer recursion depth limit check. --- lib/ansible/utils.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index 50765a430b..a53092a981 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -25,7 +25,9 @@ import codecs import jinja2 import yaml import optparse -from operator import methodcaller +import operator +from ansible import errors +import ansible.constants as C try: import json @@ -37,8 +39,6 @@ try: except ImportError: from md5 import md5 as _md5 -from ansible import errors -import ansible.constants as C ############################################################### # UTILITY FUNCTIONS FOR COMMAND LINE TOOLS @@ -278,7 +278,11 @@ def template(text, vars, setup_cache=None): ''' run a text buffer through the templating engine until it no longer changes ''' prev_text = '' + depth = 0 while prev_text != text: + depth = depth + 1 + if (depth > 20): + raise errors.AnsibleError("template recursion depth exceeded") prev_text = text text = _template(text, vars, setup_cache) return text @@ -343,7 +347,7 @@ def md5(filename): class SortedOptParser(optparse.OptionParser): '''Optparser which sorts the options by opt before outputting --help''' def format_help(self, formatter=None): - self.option_list.sort(key=methodcaller('get_opt_string')) + self.option_list.sort(key=operator.methodcaller('get_opt_string')) return optparse.OptionParser.format_help(self, formatter=None) def base_parser(constants=C, usage="", output_opts=False, runas_opts=False, async_opts=False, connect_opts=False):