reactor lin number handling for yaml files

This commit is contained in:
Robert Kaussow 2019-01-25 15:08:37 +01:00
parent 0c88cfb7c2
commit 51c5f43e5c
3 changed files with 41 additions and 13 deletions

View File

@ -21,6 +21,7 @@ from ansiblelater.rules.ansiblefiles import check_shell_instead_command
from ansiblelater.rules.ansiblefiles import check_command_has_changes
from ansiblelater.rules.ansiblefiles import check_empty_string_compare
from ansiblelater.rules.ansiblefiles import check_compare_to_literal_bool
from ansiblelater.rules.ansiblefiles import check_uppercase_literal_bool
tasks_should_be_separated = Standard(dict(
@ -195,6 +196,14 @@ use_yaml_rather_than_key_value = Standard(dict(
types=["playbook", "task", "handler"]
))
literal_bool_should_start_with_uppercase = Standard(dict(
id="LINT0008",
name="Literal bools should start with a capital letter",
check=check_uppercase_literal_bool,
version="0.1",
types=["playbook", "task", "handler"]
))
ansible_min_version = '2.1'
ansible_review_min_version = '0.1.0'
@ -215,6 +224,7 @@ standards = [
commands_should_be_idempotent,
dont_compare_to_empty_string,
dont_compare_to_literal_bool,
literal_bool_should_start_with_uppercase,
# Lint
files_should_not_contain_unnecessarily_empty_lines,
files_should_be_indented,

View File

@ -209,3 +209,19 @@ def check_delegate_to_localhost(candidate, settings):
errors.append(Error(task["__line__"], description))
return Result(candidate.path, errors)
def check_uppercase_literal_bool(candidate, settings):
yamllines, errors = get_normalized_yaml(candidate, settings)
description = "literal bools should be written as 'True|False' instead of 'true|false'"
lineno = 1
uppercase_bool = re.compile(r"([=!]=|:)\s*(true|false)")
if not errors:
for line in yamllines:
lineno += 1
if uppercase_bool.findall(line):
errors.append(Error(lineno, description))
return Result(candidate.path, errors)

View File

@ -486,23 +486,25 @@ def parse_yaml_linenumbers(data, filename):
def normalized_yaml(file, options):
lines = []
# lines = []
removes = []
try:
with codecs.open(file, mode='rb', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
if line.strip().startswith("#"):
removes.append(line)
# remove document starter also
if options.get("remove_markers") and line.strip() == "---":
removes.append(line)
# remove empty lines
if options.get("remove_empty") and not line.strip():
removes.append(line)
for line in removes:
lines.remove(line)
lines = enumerate(f.readlines())
print(lines)
for i, line in lines:
if line.strip().startswith("#"):
removes.append(line)
# remove document starter also
if options.get("remove_markers") and line.strip() == "---":
removes.append(line)
# remove empty lines
if options.get("remove_empty") and not line.strip():
removes.append(line)
# for line in removes:
# lines.remove(line)
except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e:
raise LaterError("syntax error", e)
return lines