diff options
| author | Jeffrey Zhang <zhang.lei.fly@gmail.com> | 2016-10-20 02:21:35 +0800 |
|---|---|---|
| committer | Jeffrey Zhang <zhang.lei.fly@gmail.com> | 2016-11-05 01:16:12 +0800 |
| commit | 2392d7e38cc5937921c2ecaf5af2b21f3cb32c40 (patch) | |
| tree | 00db61c89b5da3826f897aaef415a0c9e7b848dd /tools/validate-all-file.py | |
| parent | f25f1b7b6d2fe9e77886f1fd2a191266877fa2b6 (diff) | |
| download | kolla-2392d7e38cc5937921c2ecaf5af2b21f3cb32c40.tar.gz kolla-2392d7e38cc5937921c2ecaf5af2b21f3cb32c40.zip | |
Add file validator for *.json.j2 file
All json file in Kolla are json.j2 file, it shold be render as jinja
template, then validate by using json.
Change-Id: Ibabbe435116fd255d68347e00407156db76fedfe
Closes-Bug: #1634447
Diffstat (limited to 'tools/validate-all-file.py')
| -rwxr-xr-x | tools/validate-all-file.py | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/tools/validate-all-file.py b/tools/validate-all-file.py index 601f2e432..e44641291 100755 --- a/tools/validate-all-file.py +++ b/tools/validate-all-file.py @@ -12,18 +12,26 @@ # See the License for the specific language governing permissions and # limitations under the License. +import collections import fnmatch +import json import logging import os import re import sys +import jinja2 + PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) NEWLINE_EOF_INCLUDE_PATTERNS = ['*.j2', '*.yml', '*.py', '*.sh'] NEWLINE_EOF_EXCLUDE_PATTERNS = ['.tox', '.testrepository', '.git'] +# Render json file by using jinja2 template is OK +JSON_J2_INCLUDE_PATTERNS = ['*.json.j2', '*.json'] +JSON_J2_EXCLUDE_PATTERNS = ['.tox', '.testrepository', '.git'] + logging.basicConfig() LOG = logging.getLogger(__name__) @@ -52,9 +60,54 @@ def check_newline_eof(): return return_code -def main(): - return sum([check_newline_eof()]) +def check_json_j2(): + includes = r'|'.join([fnmatch.translate(x) + for x in JSON_J2_INCLUDE_PATTERNS]) + excludes = r'|'.join([fnmatch.translate(x) + for x in JSON_J2_EXCLUDE_PATTERNS]) + return_code = 0 + + def bool_filter(value): + return True + # Mock ansible hostvars variable, which is a nested dict + def hostvars(): + return collections.defaultdict(hostvars) + + def validate_json_j2(root, filename): + env = jinja2.Environment( # nosec: not used to render HTML + loader=jinja2.FileSystemLoader(root)) + env.filters['bool'] = bool_filter + template = env.get_template(filename) + # Mock ansible variables. + context = { + 'hostvars': hostvars(), + 'cluster_interface': 'cluster_interface', + 'storage_interface': 'storage_interface', + 'inventory_hostname': 'hostname' + } + data = template.render(**context) + json.loads(data) + for root, dirs, files in os.walk(PROJECT_ROOT): + dirs[:] = [d for d in dirs if not re.match(excludes, d)] + for filename in files: + if not re.match(excludes, filename) and \ + re.match(includes, filename): + fullpath = os.path.join(root, filename) + try: + validate_json_j2(root, filename) + except (ValueError, jinja2.exceptions.TemplateError): + return_code = 1 + LOG.exception('%s file error', fullpath) + return return_code + + +def main(): + checks = ( + check_newline_eof, + check_json_j2 + ) + return sum([check() for check in checks]) if __name__ == "__main__": sys.exit(main()) |
