Added script to validate jinja templates

* tox -e validate-jinja
* Added to ci-jobs

Change-Id: I799ec9e586a96800ee592522a0c1af7bd591ca67
This commit is contained in:
Chandan Kumar
2017-03-04 20:10:58 +05:30
parent 5d4035799e
commit 1554cd0b03
3 changed files with 67 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
#!/usr/bin/python
# coding: utf-8 -*-
#
# (c) 2017, Chandan Kumar <chkumar@redhat.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
DOCUMENTATION = '''
---
python script to read and parse jinja2 templates
---
'''
from jinja2 import Environment
from jinja2 import exceptions
import os
# Jinja Environment
env = Environment()
def get_jinja_files(dir_path):
"""Get all the .j2 and .jinja2 files"""
files_path = []
for root, subdir, files in os.walk(dir_path):
for file in files:
if file.endswith('.j2') or file.endswith('.jinja2'):
files_path.append(os.path.join(root, file))
return files_path
def validate_jinja_templates(file_path):
"""Validate jinja templates file"""
try:
with open(file_path) as fobj:
env.parse(fobj.read())
except exceptions.TemplateSyntaxError as e:
print('%s has template error: %s' % (file_path, e))
raise(e)
if __name__ == "__main__":
os.chdir("..")
root_path = os.getcwd()
jinja_files = get_jinja_files(root_path)
for file_path in jinja_files:
validate_jinja_templates(file_path)
print('Validating: %s' % file_path)

View File

@@ -5,3 +5,4 @@ ansible-lint
sphinx!=1.3b1,<1.4,>=1.2.1 # BSD
oslosphinx>=4.7.0 # Apache-2.0
reno>=1.8.0 # Apache-2.0
jinja2

View File

@@ -64,6 +64,7 @@ commands =
{[testenv:bashate]commands}
{[testenv:pep8]commands}
{[testenv:ansible-lint]commands}
{[testenv:validate-jinja]commands}
[testenv:venv]
commands = {posargs}
@@ -79,3 +80,8 @@ commands = {posargs}
show-source = True
ignore = E123,E125,E402,E501,F403,H303,H301,F405
builtins = _
[testenv:validate-jinja]
basepython=python2
commands =
python ci-scripts/validate_jinja2.py