scripts: compliance: add support for YAMLLint

Add a YAMLLint compliance check that uses the yamllint package to report
linting error on YAML files.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
This commit is contained in:
Fabio Baltieri 2022-12-30 15:34:10 +00:00 committed by Carles Cufí
parent 888607d550
commit 9d3681c10c
4 changed files with 50 additions and 1 deletions

View file

@ -27,7 +27,7 @@ jobs:
run: | run: |
pip3 install setuptools pip3 install setuptools
pip3 install wheel pip3 install wheel
pip3 install python-magic lxml junitparser gitlint pylint pykwalify pip3 install python-magic lxml junitparser gitlint pylint pykwalify yamllint
pip3 install west pip3 install west
- name: west setup - name: west setup

16
.yamllint Normal file
View file

@ -0,0 +1,16 @@
# SPDX-License-Identifier: Apache-2.0
extends: default
rules:
line-length:
max: 100
comments:
min-spaces-from-content: 1
indentation:
spaces: 2
indent-sequences: consistent
document-start:
present: false
truthy:
check-keys: false

View file

@ -17,6 +17,8 @@ import tempfile
import traceback import traceback
import shlex import shlex
from yamllint import config, linter
from junitparser import TestCase, TestSuite, JUnitXml, Skipped, Error, Failure from junitparser import TestCase, TestSuite, JUnitXml, Skipped, Error, Failure
import magic import magic
@ -991,6 +993,36 @@ class MaintainersFormat(ComplianceTest):
self.failure(f"Error parsing {file}: {ex}") self.failure(f"Error parsing {file}: {ex}")
class YAMLLint(ComplianceTest):
"""
YAMLLint
"""
name = "YAMLLint"
doc = "Check YAML files with YAMLLint."
path_hint = "<git-top>"
def run(self):
config_file = os.path.join(ZEPHYR_BASE, ".yamllint")
for file in get_files(filter="d"):
if Path(file).suffix not in ['.yaml', '.yml']:
continue
yaml_config = config.YamlLintConfig(file=config_file)
if file.startswith(".github/"):
# Tweak few rules for workflow files.
yaml_config.rules["line-length"] = False
yaml_config.rules["truthy"]["allowed-values"].extend(['on', 'off'])
elif file == ".codecov.yml":
yaml_config.rules["truthy"]["allowed-values"].extend(['yes', 'no'])
with open(file, 'r') as fp:
for p in linter.run(fp, yaml_config):
self.fmtd_failure('warning', f'YAMLLint ({p.rule})', file,
p.line, col=p.column, desc=p.desc)
def init_logs(cli_arg): def init_logs(cli_arg):
# Initializes logging # Initializes logging

View file

@ -6,3 +6,4 @@ python-magic-bin; sys_platform == "win32"
lxml lxml
junitparser>=2 junitparser>=2
pylint pylint
yamllint