mirror of
https://github.com/thegeeklab/ansible-later.git
synced 2024-11-25 22:30:42 +00:00
fix wrong line numbers in normalized yaml
This commit is contained in:
parent
51c5f43e5c
commit
961b965f96
@ -12,23 +12,21 @@ def check_braces_spaces(candidate, settings):
|
|||||||
yamllines, errors = get_normalized_yaml(candidate, settings)
|
yamllines, errors = get_normalized_yaml(candidate, settings)
|
||||||
description = "no suitable numbers of spaces (required: 1)"
|
description = "no suitable numbers of spaces (required: 1)"
|
||||||
|
|
||||||
lineno = 1
|
|
||||||
matches = []
|
matches = []
|
||||||
braces = re.compile("{{(.*?)}}")
|
braces = re.compile("{{(.*?)}}")
|
||||||
|
|
||||||
if not errors:
|
if not errors:
|
||||||
for line in yamllines:
|
for i, line in yamllines:
|
||||||
lineno += 1
|
|
||||||
match = braces.findall(line)
|
match = braces.findall(line)
|
||||||
if match:
|
if match:
|
||||||
for item in match:
|
for item in match:
|
||||||
matches.append((item, lineno))
|
matches.append((i, item))
|
||||||
|
|
||||||
for item, lineno in matches:
|
for i, line in matches:
|
||||||
leading, trailing = count_spaces(item)
|
leading, trailing = count_spaces(line)
|
||||||
|
|
||||||
if not leading == 1 or not trailing == 1:
|
if not leading == 1 or not trailing == 1:
|
||||||
errors.append(Error(lineno, description))
|
errors.append(Error(i, description))
|
||||||
return Result(candidate.path, errors)
|
return Result(candidate.path, errors)
|
||||||
|
|
||||||
|
|
||||||
@ -168,14 +166,12 @@ def check_empty_string_compare(candidate, settings):
|
|||||||
description = 'use `when: var` rather than `when: var != ""` (or ' \
|
description = 'use `when: var` rather than `when: var != ""` (or ' \
|
||||||
'conversely `when: not var` rather than `when: var == ""`)'
|
'conversely `when: not var` rather than `when: var == ""`)'
|
||||||
|
|
||||||
lineno = 0
|
|
||||||
empty_string_compare = re.compile("[=!]= ?[\"'][\"']")
|
empty_string_compare = re.compile("[=!]= ?[\"'][\"']")
|
||||||
|
|
||||||
if not errors:
|
if not errors:
|
||||||
for line in yamllines:
|
for i, line in yamllines:
|
||||||
lineno += 1
|
|
||||||
if empty_string_compare.findall(line):
|
if empty_string_compare.findall(line):
|
||||||
errors.append(Error(lineno, description))
|
errors.append(Error(i, description))
|
||||||
|
|
||||||
return Result(candidate.path, errors)
|
return Result(candidate.path, errors)
|
||||||
|
|
||||||
@ -185,14 +181,12 @@ def check_compare_to_literal_bool(candidate, settings):
|
|||||||
description = "use `when: var` rather than `when: var == True` " \
|
description = "use `when: var` rather than `when: var == True` " \
|
||||||
"(or conversely `when: not var`)"
|
"(or conversely `when: not var`)"
|
||||||
|
|
||||||
lineno = 0
|
|
||||||
literal_bool_compare = re.compile("[=!]= ?(True|true|False|false)")
|
literal_bool_compare = re.compile("[=!]= ?(True|true|False|false)")
|
||||||
|
|
||||||
if not errors:
|
if not errors:
|
||||||
for line in yamllines:
|
for i, line in yamllines:
|
||||||
lineno += 1
|
|
||||||
if literal_bool_compare.findall(line):
|
if literal_bool_compare.findall(line):
|
||||||
errors.append(Error(lineno, description))
|
errors.append(Error(i, description))
|
||||||
|
|
||||||
return Result(candidate.path, errors)
|
return Result(candidate.path, errors)
|
||||||
|
|
||||||
@ -213,15 +207,13 @@ def check_delegate_to_localhost(candidate, settings):
|
|||||||
|
|
||||||
def check_uppercase_literal_bool(candidate, settings):
|
def check_uppercase_literal_bool(candidate, settings):
|
||||||
yamllines, errors = get_normalized_yaml(candidate, settings)
|
yamllines, errors = get_normalized_yaml(candidate, settings)
|
||||||
description = "literal bools should be written as 'True|False' instead of 'true|false'"
|
description = "literal bools should be written as 'True/False' or 'yes/no'"
|
||||||
|
|
||||||
lineno = 1
|
uppercase_bool = re.compile(r"([=!]=|:)\s*(true|false|TRUE|FALSE|Yes|No|YES|NO)\s*$")
|
||||||
uppercase_bool = re.compile(r"([=!]=|:)\s*(true|false)")
|
|
||||||
|
|
||||||
if not errors:
|
if not errors:
|
||||||
for line in yamllines:
|
for i, line in yamllines:
|
||||||
lineno += 1
|
|
||||||
if uppercase_bool.findall(line):
|
if uppercase_bool.findall(line):
|
||||||
errors.append(Error(lineno, description))
|
errors.append(Error(i, description))
|
||||||
|
|
||||||
return Result(candidate.path, errors)
|
return Result(candidate.path, errors)
|
||||||
|
@ -15,18 +15,16 @@ def check_line_between_tasks(candidate, settings):
|
|||||||
description = "missing task separation (required: 1 empty line)"
|
description = "missing task separation (required: 1 empty line)"
|
||||||
|
|
||||||
task_regex = re.compile(r"-\sname:.*")
|
task_regex = re.compile(r"-\sname:.*")
|
||||||
lineno = 0
|
|
||||||
prevline = "#file_start_marker"
|
prevline = "#file_start_marker"
|
||||||
|
|
||||||
allowed_prevline = ["---", "tasks:", "pre_tasks:", "post_tasks:", "block:"]
|
allowed_prevline = ["---", "tasks:", "pre_tasks:", "post_tasks:", "block:"]
|
||||||
|
|
||||||
if not errors:
|
if not errors:
|
||||||
for line in lines:
|
for i, line in lines:
|
||||||
lineno += 1
|
|
||||||
match = task_regex.search(line)
|
match = task_regex.search(line)
|
||||||
if match and prevline:
|
if match and prevline:
|
||||||
if not any(item in prevline for item in allowed_prevline):
|
if not any(item in prevline for item in allowed_prevline):
|
||||||
errors.append(Error(lineno, description))
|
errors.append(Error(i, description))
|
||||||
prevline = line.strip()
|
prevline = line.strip()
|
||||||
|
|
||||||
return Result(candidate.path, errors)
|
return Result(candidate.path, errors)
|
||||||
|
@ -486,25 +486,25 @@ def parse_yaml_linenumbers(data, filename):
|
|||||||
|
|
||||||
|
|
||||||
def normalized_yaml(file, options):
|
def normalized_yaml(file, options):
|
||||||
# lines = []
|
lines = []
|
||||||
removes = []
|
removes = []
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with codecs.open(file, mode='rb', encoding='utf-8') as f:
|
with codecs.open(file, mode='rb', encoding='utf-8') as f:
|
||||||
lines = enumerate(f.readlines())
|
lines = list(enumerate(f.readlines(), start=1))
|
||||||
print(lines)
|
|
||||||
for i, line in lines:
|
for i, line in lines:
|
||||||
if line.strip().startswith("#"):
|
if line.strip().startswith("#"):
|
||||||
removes.append(line)
|
removes.append((i, line))
|
||||||
# remove document starter also
|
# remove document starter also
|
||||||
if options.get("remove_markers") and line.strip() == "---":
|
if options.get("remove_markers") and line.strip() == "---":
|
||||||
removes.append(line)
|
removes.append((i, line))
|
||||||
# remove empty lines
|
# remove empty lines
|
||||||
if options.get("remove_empty") and not line.strip():
|
if options.get("remove_empty") and not line.strip():
|
||||||
removes.append(line)
|
removes.append((i, line))
|
||||||
|
|
||||||
# for line in removes:
|
for line in removes:
|
||||||
# lines.remove(line)
|
lines.remove(line)
|
||||||
except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e:
|
except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e:
|
||||||
raise LaterError("syntax error", e)
|
raise LaterError("syntax error", e)
|
||||||
return lines
|
return lines
|
||||||
|
Loading…
Reference in New Issue
Block a user