Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
Kolla Ansible
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Very Demiurge Very Mindful
Kolla Ansible
Commits
a00fec8c
Commit
a00fec8c
authored
4 years ago
by
Michal Nasiadka
Committed by
Radosław Piliszek
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
CI: Initial jinja2 templates syntax checks
Change-Id: If42b7324b58851a4f0beb3a80c70ca2c9dd729af
parent
4bf52895
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
tests/j2lint.py
+79
-0
79 additions, 0 deletions
tests/j2lint.py
tox.ini
+6
-0
6 additions, 0 deletions
tox.ini
with
85 additions
and
0 deletions
tests/j2lint.py
0 → 100755
+
79
−
0
View file @
a00fec8c
#!/usr/bin/env python3
# Copyright 2020 StackHPC Ltd.
# 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.
"""
Original license:
@author Gerard van Helden <drm@melp.nl>
@license DBAD, see <http://www.dbad-license.org/>
@url https://github.com/drm/jinja2-lint
Simple j2 linter, useful for checking jinja2 template syntax
Adapted for OpenStack Kolla/Kolla-Ansible purposes
"""
from
ansible.plugins.filter.core
import
to_json
from
functools
import
reduce
from
jinja2
import
BaseLoader
from
jinja2
import
Environment
from
jinja2
import
exceptions
from
jinja2
import
TemplateNotFound
from
kolla_ansible
import
kolla_address
from
kolla_ansible
import
put_address_in_context
import
os.path
class
AbsolutePathLoader
(
BaseLoader
):
def
get_source
(
self
,
environment
,
template
):
if
not
os
.
path
.
exists
(
template
):
raise
TemplateNotFound
(
template
)
mtime
=
os
.
path
.
getmtime
(
template
)
with
open
(
template
)
as
file
:
source
=
file
.
read
()
return
source
,
template
,
lambda
:
mtime
==
os
.
path
.
getmtime
(
template
)
def
check
(
template
,
out
,
err
,
env
=
Environment
(
loader
=
AbsolutePathLoader
(),
autoescape
=
True
)):
try
:
env
.
filters
[
'
basename
'
]
=
os
.
path
.
basename
env
.
filters
[
'
bool
'
]
=
bool
env
.
filters
[
'
hash
'
]
=
hash
env
.
filters
[
'
to_json
'
]
=
to_json
env
.
filters
[
'
kolla_address
'
]
=
kolla_address
env
.
filters
[
'
put_address_in_context
'
]
=
put_address_in_context
env
.
get_template
(
template
)
out
.
write
(
"
%s: Syntax OK
\n
"
%
template
)
return
0
except
TemplateNotFound
:
err
.
write
(
"
%s: File not found
\n
"
%
template
)
return
2
except
exceptions
.
TemplateSyntaxError
as
ex
:
err
.
write
(
"
%s: Syntax check failed: %s in %s at %d
\n
"
%
(
template
,
ex
.
message
,
ex
.
filename
,
ex
.
lineno
))
return
1
def
main
(
**
kwargs
):
import
sys
try
:
sys
.
exit
(
reduce
(
lambda
r
,
fn
:
r
+
check
(
fn
,
sys
.
stdout
,
sys
.
stderr
,
**
kwargs
),
sys
.
argv
[
1
:],
0
))
except
IndexError
:
sys
.
stdout
.
write
(
"
Usage: j2lint.py filename [filename ...]
\n
"
)
if
__name__
==
"
__main__
"
:
main
()
This diff is collapsed.
Click to expand it.
tox.ini
+
6
−
0
View file @
a00fec8c
...
@@ -87,6 +87,7 @@ commands =
...
@@ -87,6 +87,7 @@ commands =
{
[testenv:doc8]
commands}
{
[testenv:doc8]
commands}
{
[testenv:bandit]
commands}
{
[testenv:bandit]
commands}
{
[testenv:bashate]
commands}
{
[testenv:bashate]
commands}
{
[testenv:j2lint]
commands}
{
[testenv:yamllint]
commands}
{
[testenv:yamllint]
commands}
{
[testenv:ansible-lint]
commands}
{
[testenv:ansible-lint]
commands}
...
@@ -121,6 +122,11 @@ commands =
...
@@ -121,6 +122,11 @@ commands =
deps
=
{[testenv:linters]deps}
deps
=
{[testenv:linters]deps}
commands
=
bandit --skip B303 -r ansible kolla_ansible tests tools
commands
=
bandit --skip B303 -r ansible kolla_ansible tests tools
[testenv:j2lint]
deps
=
{[testenv:linters]deps}
commands
=
find
{toxinidir}
-type
f
-name
"*.j2"
-not
-path
"*/.tox/*"
-exec
{toxinidir}/tests/j2lint.py
\{\}
+
[testenv:ansible-lint]
[testenv:ansible-lint]
# Lint only code in ansible/* - ignore tests/ and roles/ used by CI
# Lint only code in ansible/* - ignore tests/ and roles/ used by CI
setenv
=
{[testenv:linters]setenv}
setenv
=
{[testenv:linters]setenv}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment