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

Merge pull request #78 from mgwilliams/master

Catch jinja errors in template module
This commit is contained in:
Michael DeHaan 2012-03-27 17:00:49 -07:00
commit 85a0709d8f

View file

@ -26,6 +26,8 @@ try:
except ImportError: except ImportError:
import simplejson as json import simplejson as json
environment = jinja2.Environment()
# =========================================== # ===========================================
# convert arguments of form a=b c=d # convert arguments of form a=b c=d
# to a dictionary # to a dictionary
@ -57,6 +59,7 @@ if not os.path.exists(metadata):
sys.exit(1) sys.exit(1)
# raise an error if we can't parse the template metadata # raise an error if we can't parse the template metadata
#data = {}
try: try:
f = open(metadata) f = open(metadata)
data = json.loads(f.read()) data = json.loads(f.read())
@ -90,16 +93,20 @@ md5sum = None
if os.path.exists(dest): if os.path.exists(dest):
md5sum = os.popen("md5sum %s" % dest).read().split()[0] md5sum = os.popen("md5sum %s" % dest).read().split()[0]
# call Jinja2 here and save the new template file try:
template = jinja2.Template(source) # call Jinja2 here and save the new template file
data_out = template.render(data) template = environment.from_string(source)
data_out = template.render(data)
except jinja2.TemplateError as e:
print json.dumps({
"failed": True,
"msg" : e.message
})
sys.exit(1)
f = open(dest, "w+") f = open(dest, "w+")
f.write(data_out) f.write(data_out)
f.close() f.close()
# TODO: catch templating errors and do not clobber the file on the
# other end unless things were successful
# record m5sum and return success and whether things have changed # record m5sum and return success and whether things have changed
md5sum2 = os.popen("md5sum %s" % dest).read().split()[0] md5sum2 = os.popen("md5sum %s" % dest).read().split()[0]