1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Some misc cleanup and removal of unused imports.

This commit is contained in:
Michael DeHaan 2013-10-19 13:50:14 -04:00
parent 21d4400c96
commit 8d3db80370

View file

@ -21,16 +21,17 @@ import codecs
import jinja2 import jinja2
from jinja2.runtime import StrictUndefined from jinja2.runtime import StrictUndefined
from jinja2.exceptions import TemplateSyntaxError from jinja2.exceptions import TemplateSyntaxError
import yaml
import json
from ansible import errors from ansible import errors
import ansible.constants as C import ansible.constants as C
import time import time
import subprocess
import datetime import datetime
import pwd import pwd
import ast import ast
FILTER_PLUGINS = None
_LISTRE = re.compile(r"(\w+)\[(\d+)\]")
JINJA2_OVERRIDE='#jinja2:'
class Globals(object): class Globals(object):
FILTERS = None FILTERS = None
@ -55,27 +56,15 @@ def _get_filters():
def _get_extensions(): def _get_extensions():
''' return jinja2 extensions to load ''' ''' return jinja2 extensions to load '''
# if some extensions are set via jinja_extensions in ansible.cfg, we try
''' # to load them with the jinja environment
if some extensions are set via jinja_extensions in ansible.cfg, we try
to load them with the jinja environment
'''
jinja_exts = [] jinja_exts = []
if C.DEFAULT_JINJA2_EXTENSIONS: if C.DEFAULT_JINJA2_EXTENSIONS:
''' # Let's make sure the configuration directive doesn't contain spaces
Let's make sure the configuration directive doesn't contain spaces # and split extensions in an array
and split extensions in an array
'''
jinja_exts = C.DEFAULT_JINJA2_EXTENSIONS.replace(" ", "").split(',') jinja_exts = C.DEFAULT_JINJA2_EXTENSIONS.replace(" ", "").split(',')
return jinja_exts return jinja_exts
# TODO: refactor this file
FILTER_PLUGINS = None
_LISTRE = re.compile(r"(\w+)\[(\d+)\]")
JINJA2_OVERRIDE='#jinja2:'
def lookup(name, *args, **kwargs): def lookup(name, *args, **kwargs):
from ansible import utils from ansible import utils
instance = utils.plugins.lookup_loader.get(name.lower(), basedir=kwargs.get('basedir',None)) instance = utils.plugins.lookup_loader.get(name.lower(), basedir=kwargs.get('basedir',None))
@ -336,9 +325,7 @@ def template(basedir, input_value, vars, lookup_fatal=True, depth=-1, expand_lis
fail_on_undefined=fail_on_undefined, fail_on_undefined=fail_on_undefined,
filter_fatal=filter_fatal, filter_fatal=filter_fatal,
) )
if last_time != result: if last_time == result:
changed = True
else:
changed = False changed = False
last_time = result last_time = result
depth = depth + 1 depth = depth + 1
@ -378,10 +365,11 @@ def _template(basedir, varname, vars, lookup_fatal=True, depth=0, expand_lists=T
elif isinstance(varname, (list, tuple)): elif isinstance(varname, (list, tuple)):
return [ template(basedir, v, vars, lookup_fatal, depth, expand_lists, fail_on_undefined=fail_on_undefined) for v in varname] return [ template(basedir, v, vars, lookup_fatal, depth, expand_lists, fail_on_undefined=fail_on_undefined) for v in varname]
elif isinstance(varname, dict): elif isinstance(varname, dict):
d = {} return dict([
for (k, v) in varname.iteritems(): (k, template(
d[k] = template(basedir, v, vars, lookup_fatal, depth, expand_lists, fail_on_undefined=fail_on_undefined) basedir, v, vars, lookup_fatal, depth, expand_lists, fail_on_undefined=fail_on_undefined)
return d ) for (k,v) in varname.iteritems()
])
else: else:
return varname return varname
@ -432,12 +420,15 @@ def template_from_file(basedir, path, vars):
template_uid = pwd.getpwuid(os.stat(realpath).st_uid).pw_name template_uid = pwd.getpwuid(os.stat(realpath).st_uid).pw_name
except: except:
template_uid = os.stat(realpath).st_uid template_uid = os.stat(realpath).st_uid
vars['template_host'] = os.uname()[1]
vars['template_path'] = realpath vars.update(dict(
vars['template_mtime'] = datetime.datetime.fromtimestamp(os.path.getmtime(realpath)) template_host = os.uname()[1],
vars['template_uid'] = template_uid template_path = realpath,
vars['template_fullpath'] = os.path.abspath(realpath) template_mtime = datetime.datetime.fromtimestamp(os.path.getmtime(realpath)),
vars['template_run_date'] = datetime.datetime.now() template_uid = template_uid,
template_fullpath = os.path.abspath(realpath),
template_run_date = datetime.datetime.now(),
))
managed_default = C.DEFAULT_MANAGED_STR managed_default = C.DEFAULT_MANAGED_STR
managed_str = managed_default.format( managed_str = managed_default.format(
@ -450,6 +441,8 @@ def template_from_file(basedir, path, vars):
time.localtime(os.path.getmtime(realpath)) time.localtime(os.path.getmtime(realpath))
) )
# this double template pass is here to detect errors while we still have context
# actual recursion is handled by the mainline template function further down
try: try:
t = environment.from_string(data) t = environment.from_string(data)
res = t.render(vars) res = t.render(vars)
@ -464,7 +457,7 @@ def template_from_file(basedir, path, vars):
if data.endswith('\n') and not res.endswith('\n'): if data.endswith('\n') and not res.endswith('\n'):
res = res + '\n' res = res + '\n'
return template(basedir, res, vars) return template(basedir, res, vars)
def template_from_string(basedir, data, vars, fail_on_undefined=False): def template_from_string(basedir, data, vars, fail_on_undefined=False):
''' run a string through the (Jinja2) templating engine ''' ''' run a string through the (Jinja2) templating engine '''
@ -501,9 +494,8 @@ def template_from_string(basedir, data, vars, fail_on_undefined=False):
# TODO: may need some way of using lookup plugins here seeing we aren't calling # TODO: may need some way of using lookup plugins here seeing we aren't calling
# the legacy engine, lookup() as a function, perhaps? # the legacy engine, lookup() as a function, perhaps?
data = data.decode('utf-8')
try: try:
t = environment.from_string(data) t = environment.from_string(data.decode('utf-8'))
except Exception, e: except Exception, e:
if 'recursion' in str(e): if 'recursion' in str(e):
raise errors.AnsibleError("recursive loop detected in template string: %s" % data) raise errors.AnsibleError("recursive loop detected in template string: %s" % data)